package server;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import org.apache.log4j.Logger;
import commoms.CommandIF;
import commoms.CommandImpl;
import commoms.ProtocolConstants;
/**
* This is the real server.
* @author prateek jain
*
*/
public class Server {
private static final Logger log = Logger.getLogger("chat-server");
// Tracer tracer1;
private Hashtable outputStreams = new Hashtable();
private Hashtable<String, Socket> peers = new Hashtable<String, Socket>();
private ServerSocket ss;
//TODO try for volatile
static List<String> clientNames = new ArrayList<String>();
static boolean updateList = false;
public static List<String> getClientNames() {
return clientNames;
}
public Server(int port) throws IOException {
listen(port);
}
private void listen(int port) throws IOException {
ss = new ServerSocket(port);
log.debug("Listening on port" + ss);
while (true) {
Socket s = ss.accept();
log.debug("connection from " + s);
ObjectOutputStream data = new ObjectOutputStream(s
.getOutputStream());
outputStreams.put(s, data);
new ServerThread(this, s);
}// end while
}// end listen
Enumeration getoutputStreams() {
return outputStreams.elements();
}
void sendToAll(CommandIF command) {
synchronized (outputStreams) {
for (Enumeration e = getoutputStreams(); e.hasMoreElements();) {
// ... get the output stream ...
ObjectOutputStream dout = (ObjectOutputStream) e.nextElement();
// ... and send the message
try {
dout.writeObject(command);
} catch (IOException ie) {
System.out.println(ie);
}
}
}
}
void removeConnection(String senderName, Socket s) {
synchronized (outputStreams) {
// Tell the world
log.error("Removing connection to " + senderName);
CommandIF comm = updateRemovedPeerList(senderName, s);
outputStreams.remove(s);
// Make sure it's closed
try {
s.close();
} catch (IOException ie) {
log.error("Error closing " + senderName);
ie.printStackTrace();
}
// send all peers notification.
sendToAll(comm);
}
}
// Main routine
// Usage: java Server <port>
static public void main(String args[]) throws Exception {
// Get the port # from the command line
int port = Integer.parseInt(args[0]);
// Create a Server object, which will automatically begin
// accepting connections.
new Server(port);
}
void updatePeerList(CommandIF com, Socket store) {
String userId = "";
try {
userId = com.getMsgFrom();
} catch (NullPointerException npe) {
//don't do anything
}
if (!peers.containsKey(userId)) {
peers.put(userId, store);
// send message to all.
CommandIF comm = makeCommandOfPeerList();
comm.setMessage(userId + " joined in!");
sendToAll(comm);
}// if
}
// while removing
CommandIF updateRemovedPeerList(String userName, Socket store) {
peers.remove(userName);
CommandIF comm = makeCommandOfPeerList();
comm.setMessage(" " + userName + " gone away!");
return comm;
}
CommandIF makeCommandOfPeerList() {
CommandImpl impl = new CommandImpl();
impl.setPeerListChanged(true);
impl.setPrivateMessage(false);
impl.setCommand(ProtocolConstants.UPDATE_PEER_LIST);
impl.setNewPeerList(Arrays.asList(peers.keySet().toArray()));
return impl;
}
}// end class