edu.isi.gamebots.client
Class GamebotsClient
- public class GamebotsClient
- implements GamebotsConstants
- This class is a JavaBean client for the Gamebots server. It encapsulates the lowest level network connection and provides an EventListener interface GamebotsClient.Listener to handle incoming server messages.
- Author:
- Andrew n marshall
BUFFER_LENGTH
protected static final int BUFFER_LENGTH
- Length of the socket read buffer. Used by #parseMessage.
log
private DebugLog log
- The client's logging mechanism.
logOutput
private boolean logOutput
- Toggles whether sent messages are logged. Set by (@link #setLogOutput}. Used by #sendMessage.
serverAddress
private InetAddress serverAddress
- Gamebots server's internet address. Must be initialized via #setServerAddress.
serverPort
private int serverPort
- Gamebots server's port number. Must be initialized via #setServerPort.
connectionLock
protected final Object connectionLock
- Synchronization lock for manipulating the connection.
socket
protected Socket socket
- The network connection socket. It remains null when not connected.
inputThread
protected Thread inputThread
- Thread used to handle incoming messages from the server.
outputLock
protected final Object outputLock
- Synchronization lock for manipulating the outgoing connection.
output
protected PrintWriter output
- The outgoing wruter . It remains null when not connected.
listeners
protected List listeners
- List of registered GamebotsClient.Listeners.
lastMessageTime
protected long lastMessageTime
- Time of the last received message.
pingTimer
protected Timer pingTimer
- A timer used to detect dropped connections.
pingTask
protected PingTask pingTask
- The current PingTask on the #pingTimer detect dropped connections.
setLog
public void setLog(DebugLog log)
- Sets the DebugLog used to log events. If never set, events will be logged to PrintWriterLog#DEFAULT.
getLogOutput
public boolean getLogOutput()
- Returns:
- true if outgoing messages are being logged.
setLogOutput
public void setLogOutput(boolean logOutput)
- Sets whether outgoing messages are logged.
- See Also:
- #setLog
getLastMessageReceivedTimeStamp
public long getLastMessageReceivedTimeStamp()
- Gets the timestamp of the last message received from the server. If the client is not connected or no message has been received, it returns -1.
addListener
public void addListener(Listener listener)
- Registers a GamebotsClient.Listener with this client. Does nothing if the listener is already registered.
removeListener
public void removeListener(Listener listener)
- Unregisters listener with this client. Does nothing if the listener is not currently registered.
getServerAddress
public InetAddress getServerAddress()
- Returns:
- the internet address of the Gamebots server. If unset, it returns null.
setServerAddress
public void setServerAddress(InetAddress serverAddress)
- Sets the internet address of the Gamebots server. If the client is already connected, the connection is broken before setting the server.
- Throws:
IllegalArgumentException
- if serverAddress is null.
getServerPort
public int getServerPort()
- Returns:
- the network port of the Gamebots server. Defaults to GamebotsConstants#DEFAULT_BOT_PORT.
setServerPort
public void setServerPort(int port)
- Sets the network port of the Gamebots server. If the client is already connected, the connection is broken before setting the server.
- Throws:
IllegalStateException
- is client is currently connected.
IllegalArgumentException
- if port <= 0.
isConnected
public boolean isConnected()
- Returns:
- whether the client is currently connected.
connect
public void connect()
- Initiates a connection to the server. If the client is already connected, it does nothing.
- Throws:
IOException
- if socket cannot be created.
IllegalStateException
- if the server address is not set.
sendMessage
public boolean sendMessage(String msg)
- Sends a string message to the server
sendMessage
public boolean sendMessage(String type,
Properties properties)
- Sends a message to the server with the given type and properties. May
- Throws:
IllegalArgumentException
- if type is null or empty.
- See Also:
- #setLogOutput
disconnect
public void disconnect()
- Disconnects the client from the server. If the client is not connected, it does nothing. Any errors thrown by socket.close() are caught and logged.
handleSocket
protected void handleSocket(Socket socket)
- Manages incoming network stream, and sends incoming Messages to registered Listeners.
parseMessage
protected Message parseMessage(Reader in)
- Builds Message objects out of the PushbackReader in.
- Throws:
IOException
- caused by in.read(...)
fireReceivedSocketError
protected void fireReceivedSocketError(Throwable error)
- Passes given network error to all registered Listeners.