Class Client

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--Client
All Implemented Interfaces:
java.lang.Runnable

public class Client
extends java.lang.Thread

The Client class is a communications interface for an individual user who wants to join a multiplayer game of Sokoban.


Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Client(Game owner, java.lang.String ip, int port)
          Client constructor
 
Method Summary
 void disconnect()
          Sends a disconnect message to the server.
 java.lang.String getIP()
          Returns the ip address to connect to.
 int getPort()
          Returns the port to connect to.
 int getRobotNumber()
          Returns the robot number of this client.
 boolean isConnected()
          Returns true if a connection to a server exists and false if a connection to a server doesn't exist.
 void postCommand(java.lang.String command)
          Sends the command to the server.
 void processCommand(java.lang.String command)
          Checks the command received from the server and determines what to do with the information
 void run()
          Connects to and receives input from a server.
 void sendChangeLevelAccept(boolean accepted, java.lang.String level)
          Creates a restartMap command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.
 void sendChangeLevelRequest(java.lang.String level)
          Creates a restart request command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.
 void sendChat(java.lang.String user, java.lang.String message)
          Creates a chat command String for the robot passed and sends the command to to the server.
 void sendMove(int robot, Direction dir)
          Creates a move command String for the robot passed and sends the command to the server.
 void sendRestartAccept(boolean accepted)
          Creates a restartMap command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.
 void sendRestartRequest()
          Creates a restart request command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.
 void sendUndo(int robot)
          Creates an undo command String for the robot passed and sends the command to to the server.
 void setIP(java.lang.String ip)
          Sets the ip address to connect to.
 void setOwner(Game owner)
          Sets the owner attribute.
 void setPort(int port)
          Sets the port to connect to.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Client

public Client(Game owner,
              java.lang.String ip,
              int port)

Client constructor

Pre-condition: none

Post-condition: Client class created; connectIP set to value of ip parameter; port set to value of port parameter

Parameters:
ip - the IP address of the computer to connect to
port - the port to connect to
Method Detail

run

public void run()

Connects to and receives input from a server.

Pre-condition: none

Post-condition: client thread is running

Overrides:
run in class java.lang.Thread

processCommand

public void processCommand(java.lang.String command)

Checks the command received from the server and determines what to do with the information

Pre-condition: command contains the input sent from server

Post-condition: the input has been processed and the Game is updated

Parameters:
command - the String recieved from the server to be processed

sendMove

public void sendMove(int robot,
                     Direction dir)

Creates a move command String for the robot passed and sends the command to the server.

Pre-condition: none

Post-condition: a move command String is created and sent to the server.

Parameters:
robot - the robot number of the robot moving
dir - the direction the robot is moving

sendUndo

public void sendUndo(int robot)

Creates an undo command String for the robot passed and sends the command to to the server.

Pre-condition: none

Post-condition: an undo command String is created and sent to the server.

Parameters:
robot - the robot number of the robot moving
direction - the direction the robot is moving

sendChat

public void sendChat(java.lang.String user,
                     java.lang.String message)

Creates a chat command String for the robot passed and sends the command to to the server.

Pre-condition: none

Post-condition: a chat command String is created and sent to the server.

Parameters:
user - the user name of the user who sent the message
message - the message sent by the user

sendChangeLevelAccept

public void sendChangeLevelAccept(boolean accepted,
                                  java.lang.String level)

Creates a restartMap command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.

Pre-condition: none

Post-condition: a restart command String is created and sent to the sendToAll() method.


sendChangeLevelRequest

public void sendChangeLevelRequest(java.lang.String level)

Creates a restart request command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.

Pre-condition: none

Post-condition: a restart command String is created and sent to the sendToAll() method.


sendRestartAccept

public void sendRestartAccept(boolean accepted)

Creates a restartMap command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.

Pre-condition: none

Post-condition: a restart command String is created and sent to the sendToAll() method.


sendRestartRequest

public void sendRestartRequest()

Creates a restart request command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.

Pre-condition: none

Post-condition: a restart command String is created and sent to the sendToAll() method.


disconnect

public void disconnect()

Sends a disconnect message to the server.

Pre-condition: existing socket connection to a server

Post-condition: disconnect command sent to the server; socket is closed


postCommand

public void postCommand(java.lang.String command)

Sends the command to the server.

Pre-condition: existing socket connection to a server

Post-condition: command parameter is sent to the server

Parameters:
command - information to be sent to the server

setOwner

public void setOwner(Game owner)

Sets the owner attribute.

Parameters:
owner - provides a link back to the game.

setPort

public void setPort(int port)

Sets the port to connect to.

Parameters:
port - the port to connect to

getPort

public int getPort()

Returns the port to connect to.

Returns:
port - the port used on the by the host computer to connect to

setIP

public void setIP(java.lang.String ip)

Sets the ip address to connect to.

Parameters:
ip - the IP address of the computer to connect to

getIP

public java.lang.String getIP()

Returns the ip address to connect to.

Returns:
connectIP - the ip address of the host computer to connect to

getRobotNumber

public int getRobotNumber()

Returns the robot number of this client.

Returns:
robotNum - the robot number of this client

isConnected

public boolean isConnected()

Returns true if a connection to a server exists and false if a connection to a server doesn't exist.

Returns:
connected - true if connected to the server, false if not