|
|
@ -54,7 +54,7 @@ import java.util.List; |
|
|
|
import java.util.logging.Level; |
|
|
|
import java.util.logging.Level; |
|
|
|
import java.util.logging.Logger; |
|
|
|
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 Logger log = Logger.getLogger(Client.class.getName()); |
|
|
|
|
|
|
|
|
|
|
|
protected static int clientIDCounter = 0; |
|
|
|
protected static int clientIDCounter = 0; |
|
|
@ -79,6 +79,7 @@ public class Client extends ServiceManager implements MessageListener, Connectio |
|
|
|
|
|
|
|
|
|
|
|
protected boolean isConnector; |
|
|
|
protected boolean isConnector; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected ClientObserver listener = new ClientObserver(); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Constructs this client. |
|
|
|
* Constructs this client. |
|
|
@ -211,10 +212,10 @@ public class Client extends ServiceManager implements MessageListener, Connectio |
|
|
|
|
|
|
|
|
|
|
|
private void registerInternalListeners() { |
|
|
|
private void registerInternalListeners() { |
|
|
|
if (tcp != null) { |
|
|
|
if (tcp != null) { |
|
|
|
tcp.addConnectionListener(this); |
|
|
|
tcp.addConnectionListener(listener); |
|
|
|
tcp.socketChannel.keyFor(tcp.selector).attach(this); |
|
|
|
tcp.socketChannel.keyFor(tcp.selector).attach(this); |
|
|
|
} |
|
|
|
} |
|
|
|
addMessageListener(this, DisconnectMessage.class); |
|
|
|
addMessageListener(listener, DisconnectMessage.class); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -517,69 +518,72 @@ public class Client extends ServiceManager implements MessageListener, Connectio |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void messageReceived(Message message) { |
|
|
|
/** |
|
|
|
try { |
|
|
|
* {@inheritDoc} |
|
|
|
disconnectInternal((DisconnectMessage)message); |
|
|
|
*/ |
|
|
|
} catch (IOException e) { |
|
|
|
public boolean equals(Object obj) { |
|
|
|
log.log(Level.WARNING, "[{0}][???] Could not disconnect.", label); |
|
|
|
if (obj == null || !(obj instanceof Client || obj instanceof Integer)) { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} else if (obj instanceof Client){ |
|
|
|
|
|
|
|
return ((Client)obj).getClientID() == getClientID(); |
|
|
|
|
|
|
|
} else if (obj instanceof Integer) { |
|
|
|
|
|
|
|
return ((Integer)obj).intValue() == getClientID(); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void messageSent(Message message) { |
|
|
|
protected class ClientObserver implements MessageListener, ConnectionListener { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void objectReceived(Object object) { |
|
|
|
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 objectSent(Object object) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
public void objectReceived(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) { |
|
|
|
public void objectSent(Object object) { |
|
|
|
if (thread != null) { |
|
|
|
|
|
|
|
// We can disconnect now.
|
|
|
|
|
|
|
|
thread.setKeepAlive(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GC it.
|
|
|
|
|
|
|
|
thread = null; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
public void clientConnected(Client client) { |
|
|
|
* {@inheritDoc} |
|
|
|
// We are a client. This means that we succeeded in connecting to the server.
|
|
|
|
*/ |
|
|
|
if (!isConnected) return; |
|
|
|
public boolean equals(Object obj) { |
|
|
|
long time = System.currentTimeMillis(); |
|
|
|
if (obj == null || !(obj instanceof Client || obj instanceof Integer)) { |
|
|
|
playerID = time; |
|
|
|
return false; |
|
|
|
ClientRegistrationMessage message = new ClientRegistrationMessage(); |
|
|
|
} else if (obj instanceof Client){ |
|
|
|
message.setId(time); |
|
|
|
return ((Client)obj).getClientID() == getClientID(); |
|
|
|
try { |
|
|
|
} else if (obj instanceof Integer) { |
|
|
|
message.setReliable(false); |
|
|
|
return ((Integer)obj).intValue() == getClientID(); |
|
|
|
send(message); |
|
|
|
} else { |
|
|
|
message.setReliable(true); |
|
|
|
return false; |
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|