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.
cleanup_build_scripts
Paul Speed 9 years ago
parent dc0bcb5d13
commit 9e80d8a7aa
  1. 47
      jme3-examples/src/main/java/jme3test/network/TestChatClient.java
  2. 44
      jme3-examples/src/main/java/jme3test/network/TestChatServer.java

@ -32,6 +32,7 @@
package jme3test.network; package jme3test.network;
import com.jme3.network.Client; import com.jme3.network.Client;
import com.jme3.network.ClientStateListener;
import com.jme3.network.Message; import com.jme3.network.Message;
import com.jme3.network.MessageListener; import com.jme3.network.MessageListener;
import com.jme3.network.Network; import com.jme3.network.Network;
@ -51,11 +52,11 @@ import jme3test.network.TestChatServer.ChatMessage;
*/ */
public class TestChatClient extends JFrame { public class TestChatClient extends JFrame {
private Client client; private final Client client;
private JEditorPane chatLog; private final JEditorPane chatLog;
private StringBuilder chatMessages = new StringBuilder(); private final StringBuilder chatMessages = new StringBuilder();
private JTextField nameField; private final JTextField nameField;
private JTextField messageField; private final JTextField messageField;
public TestChatClient(String host) throws IOException { public TestChatClient(String host) throws IOException {
super("jME3 Test Chat Client - to:" + host); super("jME3 Test Chat Client - to:" + host);
@ -90,9 +91,17 @@ public class TestChatClient extends JFrame {
client = Network.connectToServer(TestChatServer.NAME, TestChatServer.VERSION, client = Network.connectToServer(TestChatServer.NAME, TestChatServer.VERSION,
host, TestChatServer.PORT, TestChatServer.UDP_PORT); host, TestChatServer.PORT, TestChatServer.UDP_PORT);
client.addMessageListener(new ChatHandler(), ChatMessage.class); client.addMessageListener(new ChatHandler(), ChatMessage.class);
client.addClientStateListener(new ChatClientStateListener());
client.start(); 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) { public static String getString(Component owner, String title, String message, String initialValue) {
return (String) JOptionPane.showInputDialog(owner, message, title, JOptionPane.PLAIN_MESSAGE, return (String) JOptionPane.showInputDialog(owner, message, title, JOptionPane.PLAIN_MESSAGE,
null, null, initialValue); null, null, initialValue);
@ -108,12 +117,23 @@ public class TestChatClient extends JFrame {
return; 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); TestChatClient test = new TestChatClient(s);
test.setVisible(true); test.setVisible(true);
} }
private class ChatHandler implements MessageListener<Client> { private class ChatHandler implements MessageListener<Client> {
@Override
public void messageReceived(Client source, Message m) { public void messageReceived(Client source, Message m) {
ChatMessage chat = (ChatMessage) 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 class SendAction extends AbstractAction {
private boolean reliable; private final boolean reliable;
public SendAction(boolean reliable) { public SendAction(boolean reliable) {
super(reliable ? "TCP" : "UDP"); super(reliable ? "TCP" : "UDP");
this.reliable = reliable; this.reliable = reliable;
} }
@Override
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
String name = nameField.getText(); String name = nameField.getText();
String message = messageField.getText(); String message = messageField.getText();

@ -34,6 +34,7 @@ package jme3test.network;
import com.jme3.network.*; import com.jme3.network.*;
import com.jme3.network.serializing.Serializable; import com.jme3.network.serializing.Serializable;
import com.jme3.network.serializing.Serializer; import com.jme3.network.serializing.Serializer;
import java.io.IOException;
/** /**
* A simple test chat server. When SM implements a set * A simple test chat server. When SM implements a set
@ -51,6 +52,24 @@ public class TestChatServer {
public static final int PORT = 5110; public static final int PORT = 5110;
public static final int UDP_PORT = 5110; public static final int UDP_PORT = 5110;
private Server server;
public TestChatServer() throws IOException {
initializeClasses();
// Use this to test the client/server name version check
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() { 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.
@ -58,14 +77,11 @@ public class TestChatServer {
} }
public static void main(String... args) throws Exception { public static void main(String... args) throws Exception {
initializeClasses();
// Use this to test the client/server name version check TestChatServer chatServer = new TestChatServer();
Server server = Network.createServer(NAME, VERSION, PORT, UDP_PORT); chatServer.start();
server.start();
ChatHandler handler = new ChatHandler(); System.out.println("Waiting for connections on port:" + PORT);
server.addMessageListener(handler, ChatMessage.class);
// Keep running basically forever // Keep running basically forever
synchronized (NAME) { synchronized (NAME) {
@ -78,6 +94,7 @@ public class TestChatServer {
public ChatHandler() { public ChatHandler() {
} }
@Override
public void messageReceived(HostedConnection source, Message m) { public void messageReceived(HostedConnection source, Message m) {
if (m instanceof ChatMessage) { if (m instanceof ChatMessage) {
// Keep track of the name just in case we // 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 @Serializable
public static class ChatMessage extends AbstractMessage { public static class ChatMessage extends AbstractMessage {
@ -126,6 +157,7 @@ public class TestChatServer {
return message; return message;
} }
@Override
public String toString() { public String toString() {
return name + ":" + message; return name + ":" + message;
} }

Loading…
Cancel
Save