edu.isi.gamebots.client
Class GamebotsClient

java.lang.Object
  |
  +--edu.isi.gamebots.client.GamebotsClient
All Implemented Interfaces:
GamebotsConstants

public class GamebotsClient
extends Object
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

Nested Class Summary
static class GamebotsClient.Adapter
          Simple implementation of a GamebotsClient.Listener.
static interface GamebotsClient.Listener
          Interface for listening to a GamebotsClient.
protected  class GamebotsClient.PingTask
           
 
Nested classes inherited from class edu.isi.gamebots.client.GamebotsConstants
GamebotsConstants.TeamColorMap
 
Field Summary
protected static int BUFFER_LENGTH
          Length of the socket read buffer.
protected  Object connectionLock
          Synchronization lock for manipulating the connection.
protected  Thread inputThread
          Thread used to handle incoming messages from the server.
protected  long lastMessageTime
          Time of the last received message.
protected  List listeners
          List of registered GamebotsClient.Listeners.
protected static int MAX_WAIT
           
protected  PrintWriter output
          The outgoing wruter .
protected  Object outputLock
          Synchronization lock for manipulating the outgoing connection.
protected  GamebotsClient.PingTask pingTask
          The current PingTask on the pingTimer detect dropped connections.
protected  Timer pingTimer
          A timer used to detect dropped connections.
protected  Socket socket
          The network connection socket.
 
Fields inherited from interface edu.isi.gamebots.client.GamebotsConstants
ACTOR_CLASS, ACTOR_ID, ACTOR_REACHABLE, ARG_ALT, ARG_AMOUNT, ARG_GLOBAL, ARG_TARGET, ARG_TEXT, ARG_WALK, BEG, BUMP, CHANGEWEAPON, CHECKREACH, DAMAGE, DEATH_DAMAGE_TYPE, DEATH_KILLER, DEFAULT_BOT_PORT, DEFAULT_VIZ_PORT, DIE, DOM, DOMPOINTS, END, FALSE, FINISHED, GAMESTATE, GETPATH, INFO, INIT, INV, ITEM, ITEM_ID, JUMP, KILL, LOCATION, MESSAGE, MOV, NAV, NODE_DOMINATION, NODE_INVENTORY, NODE_MOVER, NODE_NAVIGATION, OWNER, PATH, PING, PLAYER, PLAYER_AMMO, PLAYER_ARMOR, PLAYER_HEALTH, PLAYER_NAME, PLAYER_RADIUS, PLAYER_TEAM, PLAYER_WEAPON, PONG, ROTATE, ROTATION, RUNTO, SEE, SELF, SETWALK, SHOOT, STOP, STOP_SHOOT, STRAFE, TEAM_ANY, TEAM_BLUE, TEAM_GOLD, TEAM_GREEN, TEAM_NONE, TEAM_RED, TRUE, TURNTO, VELOCITY, WALL, WEAPON_1, WEAPON_2
 
Constructor Summary
GamebotsClient()
           
 
Method Summary
 void addListener(GamebotsClient.Listener listener)
          Registers a GamebotsClient.Listener with this client.
 void connect()
          Initiates a connection to the server.
 void disconnect()
          Disconnects the client from the server.
protected  void fireConnected()
           
protected  void fireDisconnected()
           
protected  void fireReceivedSocketError(Throwable error)
          Passes given network error to all registered GamebotsClient.Listeners.
 long getLastMessageReceivedTimeStamp()
          Gets the timestamp of the last message received from the server.
 boolean getLogOutput()
           
 InetAddress getServerAddress()
           
 int getServerPort()
           
protected  void handleSocket(Socket socket)
          Manages incoming network stream, and sends incoming Messages to registered GamebotsClient.Listeners.
 boolean isConnected()
           
protected  Message parseMessage(Reader in)
          Builds Message objects out of the PushbackReader in.
 void ping()
           
 void removeListener(GamebotsClient.Listener listener)
          Unregisters listener with this client.
 boolean sendMessage(String msg)
          Sends a string message to the server
 boolean sendMessage(String type, Properties properties)
          Sends a message to the server with the given type and properties.
 void setLog(us.ca.la.anm.util.io.DebugLog log)
          Sets the DebugLog used to log events.
 void setLogOutput(boolean logOutput)
          Sets whether outgoing messages are logged.
 void setServerAddress(InetAddress serverAddress)
          Sets the internet address of the Gamebots server.
 void setServerPort(int port)
          Sets the network port of the Gamebots server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUFFER_LENGTH

protected static final int BUFFER_LENGTH
Length of the socket read buffer. Used by parseMessage(java.io.Reader).

See Also:
Constant Field Values

MAX_WAIT

protected static final int MAX_WAIT
See Also:
Constant Field Values

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 GamebotsClient.PingTask pingTask
The current PingTask on the pingTimer detect dropped connections.

Constructor Detail

GamebotsClient

public GamebotsClient()
Method Detail

setLog

public void setLog(us.ca.la.anm.util.io.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(us.ca.la.anm.util.io.DebugLog)

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(GamebotsClient.Listener listener)
Registers a GamebotsClient.Listener with this client. Does nothing if the listener is already registered.


removeListener

public void removeListener(GamebotsClient.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()
             throws IOException
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(boolean)

ping

public void ping()

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 GamebotsClient.Listeners.


parseMessage

protected Message parseMessage(Reader in)
                        throws IOException
Builds Message objects out of the PushbackReader in.

Throws:
IOException - caused by in.read(...)

fireConnected

protected void fireConnected()

fireReceivedSocketError

protected void fireReceivedSocketError(Throwable error)
Passes given network error to all registered GamebotsClient.Listeners.


fireDisconnected

protected void fireDisconnected()