fang
Class Client

java.lang.Object
  extended by fang.Client
All Implemented Interfaces:
Observer

public class Client
extends Object
implements Observer

The Client traps input from the user and sends it to the Server asynchronously. It also receives regular updates from the Server for the mouse and keyboard inputs and the current time, then updates and refreshes the screen.

Author:
Jam Jenkins

Field Summary
 Keyboard[] keyboard
          all of the keyboards of the computers connected
 Object[] message
          used to receive general messages from the Server
 Mouse[] mouse
          all of the mouses of the computers connected
 
Constructor Summary
Client(String machine, String game, String session, int players, GameLoop frameAdvancer)
          sets the domain, game and session name and connects to the server.
 
Method Summary
 void clearInput()
           
 void connect()
          Connects to the server then goes into an infinite loop sending and receiving consistency information from and to the server.
 void disconnect()
          disconnects and terminates sending and receiving informations to and from the server
static String getHostname()
          gets the name of the localhost on the network
 int getID()
          gets the id of the current client.
static String[] getWaitingSessions(String machine, String game)
           
 boolean isConnected()
          determines if this Client has open communications to the Server
 boolean isPaused()
          determines if the game is currently advancing
 void pauseToggle()
          if the game was paused, this method will set it running again.
 void setGameLoop(GameLoop frameAdvancer)
          sets the Client's GameLoop.
 void setLocalKeyboard(Keyboard localKeyboard)
          sets the Client's local Keyboard.
 void setLocalMessage(Object localMessage)
          clients can send a generic message to all of the connected clients via the server.
 void setLocalMouse(Mouse localMouse)
          sets the Client's local Mouse.
 void update(Observable arg0, Object arg1)
          this method is called when there is new information to send to the server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

keyboard

public Keyboard[] keyboard
all of the keyboards of the computers connected


mouse

public Mouse[] mouse
all of the mouses of the computers connected


message

public Object[] message
used to receive general messages from the Server

Constructor Detail

Client

public Client(String machine,
              String game,
              String session,
              int players,
              GameLoop frameAdvancer)
sets the domain, game and session name and connects to the server.

Parameters:
machine - the domain of the server
game - the name of the game
session - the name of the current session
players - the number of players to wait for (only used for the first person starting the session)
frameAdvancer - the model/view to update
Method Detail

getWaitingSessions

public static String[] getWaitingSessions(String machine,
                                          String game)

setLocalMessage

public void setLocalMessage(Object localMessage)
clients can send a generic message to all of the connected clients via the server. The message being sent must be Serializable. If the object being sent is not already Serializable, all that is normally required is that the object implement Serializable.

Parameters:
localMessage - The localMessage to set.

setGameLoop

public void setGameLoop(GameLoop frameAdvancer)
sets the Client's GameLoop. This GameLoop will be updated upon receiving transmissions from the Server.

Parameters:
frameAdvancer - The frameAdvancer to set.

setLocalKeyboard

public void setLocalKeyboard(Keyboard localKeyboard)
sets the Client's local Keyboard. This is the keyboard which will be used for generating messages to the Server about the newly pressed key.

Parameters:
localKeyboard - The localKeyboard to set.

setLocalMouse

public void setLocalMouse(Mouse localMouse)
sets the Client's local Mouse. Ths is the mouse which will be used for generating messages to the Server when the mouse changes position or is clicked.

Parameters:
localMouse - The localMouse to set.

isConnected

public boolean isConnected()
determines if this Client has open communications to the Server

Returns:
true if there are open communications, false otherwise

getHostname

public static String getHostname()
gets the name of the localhost on the network

Returns:
the name of the localhost on the network

clearInput

public void clearInput()

pauseToggle

public void pauseToggle()
if the game was paused, this method will set it running again. If the game was not paused, this method will pause the game.


connect

public void connect()
Connects to the server then goes into an infinite loop sending and receiving consistency information from and to the server. See class comment for further details

See Also:
Runnable.run()

disconnect

public void disconnect()
disconnects and terminates sending and receiving informations to and from the server


isPaused

public boolean isPaused()
determines if the game is currently advancing

Returns:
true if the game has temporarily stopped advancing, false otherwise

getID

public int getID()
gets the id of the current client. This id corresponds to the index of this client in the array of mouse and keyboard information received from the client.

Returns:
the id of the current client, also the same as the index of this client in the playerMouse and playerKeyboard arrays

update

public void update(Observable arg0,
                   Object arg1)
this method is called when there is new information to send to the server. It is called when new keyboard and/or mouse information is received, when the game is paused or resumed, or when a message is sent.

Specified by:
update in interface Observer
Parameters:
arg0 - not used
arg1 - not used
See Also:
Observer.update(java.util.Observable, java.lang.Object)