Переглянути джерело

Beefing up the client server tests a little to add
some listeners and to better report what's going on to the
console. This is in prep for making a combined test.

Paul Speed 9 роки тому
батько
коміт
9e80d8a7aa

+ 41 - 6
jme3-examples/src/main/java/jme3test/network/TestChatClient.java

@@ -32,6 +32,7 @@
 package jme3test.network;
 
 import com.jme3.network.Client;
+import com.jme3.network.ClientStateListener;
 import com.jme3.network.Message;
 import com.jme3.network.MessageListener;
 import com.jme3.network.Network;
@@ -51,11 +52,11 @@ import jme3test.network.TestChatServer.ChatMessage;
  */
 public class TestChatClient extends JFrame {
 
-    private Client client;
-    private JEditorPane chatLog;
-    private StringBuilder chatMessages = new StringBuilder();
-    private JTextField nameField;
-    private JTextField messageField;
+    private final Client client;
+    private final JEditorPane chatLog;
+    private final StringBuilder chatMessages = new StringBuilder();
+    private final JTextField nameField;
+    private final JTextField messageField;
 
     public TestChatClient(String host) throws IOException {
         super("jME3 Test Chat Client - to:" + host);
@@ -90,9 +91,17 @@ public class TestChatClient extends JFrame {
         client = Network.connectToServer(TestChatServer.NAME, TestChatServer.VERSION,
                 host, TestChatServer.PORT, TestChatServer.UDP_PORT);
         client.addMessageListener(new ChatHandler(), ChatMessage.class);
+        client.addClientStateListener(new ChatClientStateListener());
         client.start();
     }
 
+    @Override
+    public void dispose() {
+        System.out.println("Chat window closing.");
+        super.dispose();
+        client.close();
+    }
+
     public static String getString(Component owner, String title, String message, String initialValue) {
         return (String) JOptionPane.showInputDialog(owner, message, title, JOptionPane.PLAIN_MESSAGE,
                 null, null, initialValue);
@@ -108,12 +117,23 @@ public class TestChatClient extends JFrame {
             return;
         }
 
+        // Register a shutdown hook to get a message on the console when the
+        // app actually finishes
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+                @Override
+                public void run() {
+                    System.out.println("Chat client is terminating.");
+                }
+            });
+
+
         TestChatClient test = new TestChatClient(s);
         test.setVisible(true);
     }
 
     private class ChatHandler implements MessageListener<Client> {
 
+        @Override
         public void messageReceived(Client source, Message m) {
             ChatMessage chat = (ChatMessage) m;
 
@@ -134,15 +154,30 @@ public class TestChatClient extends JFrame {
         }
     }
 
+    private class ChatClientStateListener implements ClientStateListener {
+
+        @Override
+        public void clientConnected(Client c) {
+            System.out.println("clientConnected()");
+        }
+
+        @Override
+        public void clientDisconnected(Client c, DisconnectInfo info) {
+            System.out.println("clientDisconnected()");
+        }
+        
+    }
+    
     private class SendAction extends AbstractAction {
 
-        private boolean reliable;
+        private final boolean reliable;
 
         public SendAction(boolean reliable) {
             super(reliable ? "TCP" : "UDP");
             this.reliable = reliable;
         }
 
+        @Override
         public void actionPerformed(ActionEvent evt) {
             String name = nameField.getText();
             String message = messageField.getText();

+ 41 - 9
jme3-examples/src/main/java/jme3test/network/TestChatServer.java

@@ -34,6 +34,7 @@ package jme3test.network;
 import com.jme3.network.*;
 import com.jme3.network.serializing.Serializable;
 import com.jme3.network.serializing.Serializer;
+import java.io.IOException;
 
 /**
  *  A simple test chat server.  When SM implements a set
@@ -51,22 +52,37 @@ public class TestChatServer {
     public static final int PORT = 5110;
     public static final int UDP_PORT = 5110;
 
-    public static void initializeClasses() {
-        // Doing it here means that the client code only needs to
-        // call our initialize. 
-        Serializer.registerClass(ChatMessage.class);
-    }
-
-    public static void main(String... args) throws Exception {
+    private Server server;
+    
+    public TestChatServer() throws IOException {
         initializeClasses();
 
         // Use this to test the client/server name version check
-        Server server = Network.createServer(NAME, VERSION, PORT, UDP_PORT);
-        server.start();
+        this.server = Network.createServer(NAME, VERSION, PORT, UDP_PORT);
 
         ChatHandler handler = new ChatHandler();
         server.addMessageListener(handler, ChatMessage.class);
+        
+        server.addConnectionListener(new ChatConnectionListener());
+    }
+
+    public void start() {
+        server.start();
+    }
 
+    public static void initializeClasses() {
+        // Doing it here means that the client code only needs to
+        // call our initialize. 
+        Serializer.registerClass(ChatMessage.class);
+    }
+
+    public static void main(String... args) throws Exception {
+    
+        TestChatServer chatServer = new TestChatServer();
+        chatServer.start();
+ 
+        System.out.println("Waiting for connections on port:" + PORT);
+                
         // Keep running basically forever
         synchronized (NAME) {
             NAME.wait();
@@ -78,6 +94,7 @@ public class TestChatServer {
         public ChatHandler() {
         }
 
+        @Override
         public void messageReceived(HostedConnection source, Message m) {
             if (m instanceof ChatMessage) {
                 // Keep track of the name just in case we 
@@ -96,6 +113,20 @@ public class TestChatServer {
         }
     }
 
+    private static class ChatConnectionListener implements ConnectionListener {
+
+        @Override
+        public void connectionAdded( Server server, HostedConnection conn ) {
+            System.out.println("connectionAdded(" + conn + ")");
+        }
+
+        @Override
+        public void connectionRemoved(Server server, HostedConnection conn) {
+            System.out.println("connectionRemoved(" + conn + ")");
+        }
+        
+    }
+    
     @Serializable
     public static class ChatMessage extends AbstractMessage {
 
@@ -126,6 +157,7 @@ public class TestChatServer {
             return message;
         }
 
+        @Override
         public String toString() {
             return name + ":" + message;
         }