com.asdf.client
Class ConnectionHandler

java.lang.Object
  extended by com.asdf.client.ConnectionHandler
All Implemented Interfaces:
java.lang.Runnable

public class ConnectionHandler
extends java.lang.Object
implements java.lang.Runnable

ConnectionHandler is a class which connects to the server, queues and sends messages to server. ConnectionHandler also receives messages from server and passes them to gameclients and lobbyclient running on client machine.


Nested Class Summary
static class ConnectionHandler.State
           
 
Field Summary
 boolean running
           
 
Constructor Summary
ConnectionHandler(java.lang.String hostAddress, int port)
          Constructor for ConnectionHandler.
 
Method Summary
 void breakConnection()
          Closes connection.
 void registerConnectionObserver(ClientStateObserver observer)
           
 boolean registerMessageHandler(MessageHandler handler)
          Game and lobby clients register their response handlers to ConnectionHandler if they want to receive messages from server.
 void run()
          Starts the ConnectionHandler.
 void runOnce()
          Runs ConnectionHandler once.
 void send(byte[] data)
          Queues data to be send to a server.
 void sendMessage(Message msg)
          Queues message to be send to server.
 void sendRaw(java.nio.ByteBuffer data)
          Queues data to be send to the server as it is.
 boolean unRegisterMessageHandler(MessageHandler handler)
          Removes a registered response handler.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

running

public boolean running
Constructor Detail

ConnectionHandler

public ConnectionHandler(java.lang.String hostAddress,
                         int port)
                  throws java.io.IOException
Constructor for ConnectionHandler.

Parameters:
hostAddress - Servers ip address in dot notation for example 98.162.0.27
port - Servers port for example 8080
Throws:
java.io.IOException - if connection could not be established
Method Detail

registerConnectionObserver

public void registerConnectionObserver(ClientStateObserver observer)
Parameters:
observer -

registerMessageHandler

public boolean registerMessageHandler(MessageHandler handler)
Game and lobby clients register their response handlers to ConnectionHandler if they want to receive messages from server.

Parameters:
handler - Game or lobby clients message handler
Returns:
false if response handler is already registered

unRegisterMessageHandler

public boolean unRegisterMessageHandler(MessageHandler handler)
Removes a registered response handler.

Parameters:
handler - response handler to be unregistered
Returns:
false if response handler could not be found

sendMessage

public void sendMessage(Message msg)
                 throws java.io.IOException,
                        javax.xml.bind.JAXBException
Queues message to be send to server.

Parameters:
msg - message to be send
Throws:
java.io.IOException - If connection to server is lost.
javax.xml.bind.JAXBException - If message could not be translated to bytes. This means that the message did not match our message.xsd schema.

send

public void send(byte[] data)
          throws java.io.IOException
Queues data to be send to a server. Size of the data is sent as binary network byte order before the actual data.

Parameters:
data - data to be written
Throws:
java.io.IOException - If connection to server is lost.

sendRaw

public void sendRaw(java.nio.ByteBuffer data)
             throws java.io.IOException
Queues data to be send to the server as it is.

Parameters:
data - to be written
Throws:
java.io.IOException - If connection to server is lost.
See Also:
to send data with prepending size of the data

run

public void run()
Starts the ConnectionHandler.

Specified by:
run in interface java.lang.Runnable

runOnce

public void runOnce()
Runs ConnectionHandler once.


breakConnection

public void breakConnection()
                     throws java.io.IOException
Closes connection.

Throws:
java.io.IOException - if the connection was already closed