server.framework.network
Class SocketWrapper

java.lang.Object
  extended by server.framework.network.SocketWrapper

public class SocketWrapper
extends java.lang.Object

A wrapper class that is used to provide common methods for sending and receiving messages independent of whether TCP or UDP is used for the message transmission. Each connected client has a unique SocketWrapper instance, that is identified by its remoteAddress variable.

Author:
Martin Jarrett, Eivind Sorteberg

Constructor Summary
SocketWrapper(java.lang.Object socket)
          Creates a SocketWrapper instance.
 
Method Summary
 void close()
          Closes the connection when this is needed (TCP only)
 boolean equals(SocketWrapper other)
          Checks whether two SocketWrapper objects are equal.
 java.net.SocketAddress getRemoteAddress()
           
 java.lang.Object getSocket()
           
 boolean isConnected()
           
 java.lang.String readMessage(int sleeptime)
          Listens for incoming messages.
 void sendMessage(java.lang.String message)
          Sends a message in the specified format.
 void setMessageSize(int messageSize)
          Only used when running tests
 void setPlayer(AbstractPlayer player)
          Sets the player that "owns" this SocketWrapper.
 void setRemoteAddress(java.net.SocketAddress remoteAddress)
          Sets the address of the associated client.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SocketWrapper

public SocketWrapper(java.lang.Object socket)
Creates a SocketWrapper instance. If the socket parameter is a Socket object, this object will use TCP for its communication. If the socket is a DatagramSocket, it will use UDP.

Parameters:
socket - The socket determining what kind of transport protocol to use.
Method Detail

setRemoteAddress

public void setRemoteAddress(java.net.SocketAddress remoteAddress)
Sets the address of the associated client.

Parameters:
remoteAddress - The address of the associated client.

getRemoteAddress

public java.net.SocketAddress getRemoteAddress()
Returns:
The address of the associated client.

equals

public boolean equals(SocketWrapper other)
Checks whether two SocketWrapper objects are equal. This method is used to determine the sender of received messages.

Parameters:
other - The SocketWrapper that received the message.
Returns:
Whether this object's player was the sender of the message or not.

getSocket

public java.lang.Object getSocket()
Returns:
The socket object.

sendMessage

public void sendMessage(java.lang.String message)
Sends a message in the specified format. The method for sending the message depends on what kind of socket the socket object is. However, since both methods are contained in this message, the differences in implementation is abstracted from the users of this method.

Parameters:
message - The message to send

readMessage

public java.lang.String readMessage(int sleeptime)
Listens for incoming messages. As with the previous method, how this is performed depends on the transport protocol in use. But since both methods are implemented in this method, this is abstracted from the users of this method.

Parameters:
sleeptime - How long the tread sleeps between each time it checks the input stream (TCP only).
Returns:
A received message.

isConnected

public boolean isConnected()
Returns:
Whether the connection is established or not.

close

public void close()
Closes the connection when this is needed (TCP only)


setPlayer

public void setPlayer(AbstractPlayer player)
Sets the player that "owns" this SocketWrapper.

Parameters:
player - The owner of this object.

setMessageSize

public void setMessageSize(int messageSize)
Only used when running tests

Parameters:
messageSize - The expected size of messages to receive