|
@@ -53,6 +53,7 @@ public class TestChatServer {
|
|
public static final int UDP_PORT = 5110;
|
|
public static final int UDP_PORT = 5110;
|
|
|
|
|
|
private Server server;
|
|
private Server server;
|
|
|
|
+ private boolean isRunning;
|
|
|
|
|
|
public TestChatServer() throws IOException {
|
|
public TestChatServer() throws IOException {
|
|
initializeClasses();
|
|
initializeClasses();
|
|
@@ -66,10 +67,49 @@ public class TestChatServer {
|
|
server.addConnectionListener(new ChatConnectionListener());
|
|
server.addConnectionListener(new ChatConnectionListener());
|
|
}
|
|
}
|
|
|
|
|
|
- public void start() {
|
|
|
|
|
|
+ public synchronized void start() {
|
|
|
|
+ if( isRunning ) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
server.start();
|
|
server.start();
|
|
|
|
+ isRunning = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public synchronized void close() {
|
|
|
|
+ if( !isRunning ) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Gracefully let any connections know that the server is
|
|
|
|
+ // going down. Without this, their connections will simply
|
|
|
|
+ // error out.
|
|
|
|
+ for( HostedConnection conn : server.getConnections() ) {
|
|
|
|
+ conn.close("Server is shutting down.");
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ Thread.sleep(1000); // wait a couple beats to let the messages go out
|
|
|
|
+ } catch( InterruptedException e ) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ server.close();
|
|
|
|
+ isRunning = false;
|
|
|
|
+ notifyAll();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ protected void runCommand( HostedConnection conn, String user, String command ) {
|
|
|
|
+ if( "/shutdown".equals(command) ) {
|
|
|
|
+ server.broadcast(new ChatMessage("server", "Server is shutting down."));
|
|
|
|
+ close();
|
|
|
|
+ } else if( "/help".equals(command) ) {
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ sb.append("Chat commands:\n");
|
|
|
|
+ sb.append("/help - prints this message.\n");
|
|
|
|
+ sb.append("/shutdown - shuts down the server.");
|
|
|
|
+ server.broadcast(new ChatMessage("server", sb.toString()));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
public static void initializeClasses() {
|
|
public static void initializeClasses() {
|
|
// Doing it here means that the client code only needs to
|
|
// Doing it here means that the client code only needs to
|
|
// call our initialize.
|
|
// call our initialize.
|
|
@@ -84,12 +124,14 @@ public class TestChatServer {
|
|
System.out.println("Waiting for connections on port:" + PORT);
|
|
System.out.println("Waiting for connections on port:" + PORT);
|
|
|
|
|
|
// Keep running basically forever
|
|
// Keep running basically forever
|
|
- synchronized (NAME) {
|
|
|
|
- NAME.wait();
|
|
|
|
|
|
+ while( chatServer.isRunning ) {
|
|
|
|
+ synchronized (chatServer) {
|
|
|
|
+ chatServer.wait();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private static class ChatHandler implements MessageListener<HostedConnection> {
|
|
|
|
|
|
+ private class ChatHandler implements MessageListener<HostedConnection> {
|
|
|
|
|
|
public ChatHandler() {
|
|
public ChatHandler() {
|
|
}
|
|
}
|
|
@@ -100,20 +142,27 @@ public class TestChatServer {
|
|
// Keep track of the name just in case we
|
|
// Keep track of the name just in case we
|
|
// want to know it for some other reason later and it's
|
|
// want to know it for some other reason later and it's
|
|
// a good example of session data
|
|
// a good example of session data
|
|
- source.setAttribute("name", ((ChatMessage) m).getName());
|
|
|
|
|
|
+ ChatMessage cm = (ChatMessage)m;
|
|
|
|
+ source.setAttribute("name", cm.getName());
|
|
|
|
+
|
|
|
|
+ // Check for a / command
|
|
|
|
+ if( cm.message.startsWith("/") ) {
|
|
|
|
+ runCommand(source, cm.name, cm.message);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
System.out.println("Broadcasting:" + m + " reliable:" + m.isReliable());
|
|
System.out.println("Broadcasting:" + m + " reliable:" + m.isReliable());
|
|
|
|
|
|
// Just rebroadcast... the reliable flag will stay the
|
|
// Just rebroadcast... the reliable flag will stay the
|
|
// same so if it came in on UDP it will go out on that too
|
|
// same so if it came in on UDP it will go out on that too
|
|
- source.getServer().broadcast(m);
|
|
|
|
|
|
+ source.getServer().broadcast(cm);
|
|
} else {
|
|
} else {
|
|
System.err.println("Received odd message:" + m);
|
|
System.err.println("Received odd message:" + m);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private static class ChatConnectionListener implements ConnectionListener {
|
|
|
|
|
|
+ private class ChatConnectionListener implements ConnectionListener {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void connectionAdded( Server server, HostedConnection conn ) {
|
|
public void connectionAdded( Server server, HostedConnection conn ) {
|