diff --git a/engine/src/networking/com/jme3/network/connection/Client.java b/engine/src/networking/com/jme3/network/connection/Client.java index 2e83a356a..6023df7b4 100644 --- a/engine/src/networking/com/jme3/network/connection/Client.java +++ b/engine/src/networking/com/jme3/network/connection/Client.java @@ -54,7 +54,7 @@ import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; -public class Client extends ServiceManager implements MessageListener, ConnectionListener { +public class Client extends ServiceManager { protected Logger log = Logger.getLogger(Client.class.getName()); protected static int clientIDCounter = 0; @@ -79,6 +79,7 @@ public class Client extends ServiceManager implements MessageListener, Connectio protected boolean isConnector; + protected ClientObserver listener = new ClientObserver(); /** * Constructs this client. @@ -211,10 +212,10 @@ public class Client extends ServiceManager implements MessageListener, Connectio private void registerInternalListeners() { if (tcp != null) { - tcp.addConnectionListener(this); + tcp.addConnectionListener(listener); tcp.socketChannel.keyFor(tcp.selector).attach(this); } - addMessageListener(this, DisconnectMessage.class); + addMessageListener(listener, DisconnectMessage.class); } /** @@ -516,57 +517,6 @@ public class Client extends ServiceManager implements MessageListener, Connectio if (udp != null) udp.removeMessageListener(c, listener); } } - - public void messageReceived(Message message) { - try { - disconnectInternal((DisconnectMessage)message); - } catch (IOException e) { - log.log(Level.WARNING, "[{0}][???] Could not disconnect.", label); - } - } - - public void messageSent(Message message) { - - } - - public void objectReceived(Object object) { - - } - - public void objectSent(Object object) { - - } - - public void clientConnected(Client client) { - // We are a client. This means that we succeeded in connecting to the server. - if (!isConnected) return; - long time = System.currentTimeMillis(); - playerID = time; - ClientRegistrationMessage message = new ClientRegistrationMessage(); - message.setId(time); - try { - message.setReliable(false); - send(message); - message.setReliable(true); - send(message); - } catch (Exception e) { - e.printStackTrace(); - log.log(Level.SEVERE, "[{0}][???] Could not sent client registration message. Disconnecting.", label); - try { - disconnect(DisconnectMessage.ERROR); - } catch (IOException ie) {} - } - } - - public void clientDisconnected(Client client) { - if (thread != null) { - // We can disconnect now. - thread.setKeepAlive(false); - - // GC it. - thread = null; - } - } /** * {@inheritDoc} @@ -583,4 +533,58 @@ public class Client extends ServiceManager implements MessageListener, Connectio } } + protected class ClientObserver implements MessageListener, ConnectionListener { + + public void messageReceived(Message message) { + try { + disconnectInternal((DisconnectMessage)message); + } catch (IOException e) { + log.log(Level.WARNING, "[{0}][???] Could not disconnect.", label); + } + } + + public void messageSent(Message message) { + + } + + public void objectReceived(Object object) { + + } + + public void objectSent(Object object) { + + } + + public void clientConnected(Client client) { + // We are a client. This means that we succeeded in connecting to the server. + if (!isConnected) return; + long time = System.currentTimeMillis(); + playerID = time; + ClientRegistrationMessage message = new ClientRegistrationMessage(); + message.setId(time); + try { + message.setReliable(false); + send(message); + message.setReliable(true); + send(message); + } catch (Exception e) { + e.printStackTrace(); + log.log(Level.SEVERE, "[{0}][???] Could not sent client registration message. Disconnecting.", label); + try { + disconnect(DisconnectMessage.ERROR); + } catch (IOException ie) {} + } + } + + public void clientDisconnected(Client client) { + if (thread != null) { + // We can disconnect now. + thread.setKeepAlive(false); + + // GC it. + thread = null; + } + } + } + } diff --git a/engine/src/networking/com/jme3/network/connection/Server.java b/engine/src/networking/com/jme3/network/connection/Server.java index 51d286f84..3cd0c6a0e 100644 --- a/engine/src/networking/com/jme3/network/connection/Server.java +++ b/engine/src/networking/com/jme3/network/connection/Server.java @@ -56,7 +56,7 @@ import java.util.logging.Logger; * * @author Lars Wesselius */ -public class Server extends ServiceManager implements MessageListener { +public class Server extends ServiceManager { protected Logger log = Logger.getLogger(Server.class.getName()); protected static int serverIDCounter = 0; @@ -75,6 +75,8 @@ public class Server extends ServiceManager implements MessageListener { protected ClientManager clientManager = new ClientManager(); + protected MessageListener listener = new ServerMessageObserver(); + /** * Default constructor. Sets the label to * Server#[serverID] @@ -171,12 +173,12 @@ public class Server extends ServiceManager implements MessageListener { private void registerInternalListeners() { if (tcp != null) { - tcp.addMessageListener(DisconnectMessage.class, this); + tcp.addMessageListener(DisconnectMessage.class, listener); tcp.addMessageListener(ClientRegistrationMessage.class, clientManager); tcp.addConnectionListener(clientManager); } if (udp != null) { - udp.addMessageListener(DisconnectMessage.class, this); + udp.addMessageListener(DisconnectMessage.class, listener); udp.addMessageListener(ClientRegistrationMessage.class, clientManager); udp.addConnectionListener(clientManager); } @@ -423,31 +425,34 @@ public class Server extends ServiceManager implements MessageListener { } } - public void messageReceived(Message message) { - // Right now, this is definitely a DisconnectMessage. - DisconnectMessage dcMessage = (DisconnectMessage)message; - Client client = dcMessage.getClient(); - - if (clientManager.isClientConnected(client)) { - log.log(Level.INFO, "[{0}][???] Client {1} disconnected ({2}: {3}).", new Object[]{ - label, - client, - dcMessage.getType(), - (dcMessage.getReason() != null) ? dcMessage.getReason() : "No description" - }); + protected class ServerMessageObserver implements MessageListener { + + public void messageReceived(Message message) { + // Right now, this is definitely a DisconnectMessage. + DisconnectMessage dcMessage = (DisconnectMessage)message; + Client client = dcMessage.getClient(); + + if (clientManager.isClientConnected(client)) { + log.log(Level.INFO, "[{0}][???] Client {1} disconnected ({2}: {3}).", new Object[]{ + label, + client, + dcMessage.getType(), + (dcMessage.getReason() != null) ? dcMessage.getReason() : "No description" + }); + } + dcMessage.getConnection().addToDisconnectionQueue(client); + } + + public void messageSent(Message message) { + } - dcMessage.getConnection().addToDisconnectionQueue(client); - } - - public void messageSent(Message message) { - - } - - public void objectReceived(Object object) { - - } - public void objectSent(Object object) { + public void objectReceived(Object object) { + + } - } + public void objectSent(Object object) { + + } + } }