com.asdf.server
Class ClientConnection

java.lang.Object
  extended by com.asdf.server.ClientConnection

public class ClientConnection
extends java.lang.Object

Contains functionality required to manage single channel with multiple recipients and senders. This class is accessed from multiple threads. Send is accessed from Game instances and don't need synchronization as it doesn't modify class's data. Server's single thread is the only one that modifies data so synchronization isn't needed.


Nested Class Summary
static class ClientConnection.State
          Possible states for data receiving.
 
Field Summary
 boolean disposing
           
 java.nio.ByteBuffer msgBuffer
           
 int size
           
 java.nio.ByteBuffer sizeBuffer
           
 
Constructor Summary
ClientConnection(Server server, java.nio.channels.SelectionKey key, java.net.InetSocketAddress address)
          Constructs ClientConnection.
 
Method Summary
 Account getAccount()
          Getter for account
 java.lang.String getAddress()
          Getter for client ip string
 java.lang.String getAddressString()
          Getter for client remote address string
 java.nio.channels.SelectionKey getKey()
          Getter method for connection channel key.
 java.net.InetAddress getRawAddress()
          Getter for client ip address
 ClientConnection.State getReceivingState()
          Getter for receivingState
 Server getServer()
          Getter for server
 void login(Account acc)
          Mark connection authenticated.
 void logout()
          Mark connection unauthenticated.
 void msgReceived()
          MsgReceived Called when message has been received.
 void send(byte[] data)
          Sends data to recipients.
 void send(Message msg)
          Sends message with it's size to client.
 void sizeReceived()
          SizeReceived Is called when size has been read from the socket.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sizeBuffer

public java.nio.ByteBuffer sizeBuffer

msgBuffer

public java.nio.ByteBuffer msgBuffer

size

public int size

disposing

public boolean disposing
Constructor Detail

ClientConnection

public ClientConnection(Server server,
                        java.nio.channels.SelectionKey key,
                        java.net.InetSocketAddress address)
Constructs ClientConnection. Sets receivingState to default State.WAITINGSIZE.

Parameters:
server -
Method Detail

getServer

public Server getServer()
Getter for server

Returns:
Server instance

getAccount

public Account getAccount()
Getter for account

Returns:
Client account

getReceivingState

public ClientConnection.State getReceivingState()
Getter for receivingState

Returns:
Receiving state

getKey

public java.nio.channels.SelectionKey getKey()
Getter method for connection channel key.


getAddressString

public java.lang.String getAddressString()
Getter for client remote address string


getAddress

public java.lang.String getAddress()
Getter for client ip string


getRawAddress

public java.net.InetAddress getRawAddress()
Getter for client ip address

Returns:
IP address in byte array in network byte order

sizeReceived

public void sizeReceived()
                  throws java.io.IOException
SizeReceived Is called when size has been read from the socket. Function initializes message buffer to be large enough for to be received message. Function allocates new buffer if previous one doesn't exist, is small enough or too large (more than WASTING_SPACE_RATIO times as large as the current message).

Throws:
java.io.IOException

msgReceived

public void msgReceived()
                 throws java.io.IOException
MsgReceived Called when message has been received.

Throws:
java.io.IOException

send

public void send(Message msg)
Sends message with it's size to client.

Parameters:
msg - Message to send
Throws:
java.io.IOException

send

public void send(byte[] data)
          throws java.io.IOException
Sends data to recipients.

Parameters:
data -
Throws:
java.io.IOException

login

public void login(Account acc)
Mark connection authenticated.

Parameters:
acc - Valid account

logout

public void logout()
Mark connection unauthenticated.