Class Server

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

public class Server
extends java.lang.Thread

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


Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Server(Game owner, int port)
          Server constructor
 
Method Summary
 void addUser(java.lang.String userName, int robotNum)
          Creates an add user command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.
 int getMaxCon()
          Returns the maximum number of connections allowed.
 int getPort()
          Returns the number of the port to listen on.
 int[] getThreadNums()
          Returns the thread numbers of each ServerThread in the clients vector.
 void run()
          Initializes Server and waits for connections.
 void sendChangeLevelRequest(java.lang.String level, int robotNum)
          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 calls the sendToAll() method to send the command to all connected clients.
 void sendDisconnect(int robot)
          Creates a disconnect command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.
 void sendMap(java.lang.String level)
          Sends the current Map to all clients.
 void sendMove(int robot, Direction dir)
          Creates a move command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.
 void sendRestartMap()
          Creates a restartMap command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.
 void sendRestartRequest(int robotNum)
          Creates a restart request command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.
protected  void sendToAll(java.lang.String command)
          Sends a command String to all connected clients.
 void sendUndo(int robot)
          Creates an undo command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.
 void setMaxCon(int max)
          Sets the maximum number of connections allowed.
 void setPort(int port)
          Sets the port to listen on.
 void stopListening()
          Stops the Server from listening for connections.
 void stopServer()
          Stops the server and sends a disconnect message to each client.
 
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

Server

public Server(Game owner,
              int port)

Server constructor

Pre-condition: none

Post-condition: Server class created; the internal owner variable is set to passed value.

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

run

public void run()

Initializes Server and waits for connections.

Pre-condition: none

Post-condition: server is waiting for connections

Overrides:
run in class java.lang.Thread

sendMove

public void sendMove(int robot,
                     Direction dir)

Creates a move 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 move command String is created and sent to the sendToAll() method.

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 calls the sendToAll() method to send the command to all connected clients.

Pre-condition: none

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

Parameters:
robot - the robot number of the robot moving

sendDisconnect

public void sendDisconnect(int robot)

Creates a disconnect 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 disconnect command String is created and sent to the sendToAll() method.

Parameters:
robot - the robot number of the robot moving

sendChat

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

Creates a chat 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 chat command String is created and sent to the sendToAll() method.

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

sendRestartMap

public void sendRestartMap()

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(int robotNum)

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.


sendChangeLevelRequest

public void sendChangeLevelRequest(java.lang.String level,
                                   int robotNum)

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.


sendToAll

protected void sendToAll(java.lang.String command)

Sends a command String to all connected clients.

Pre-condition: the command parameter is a properly formatted String object.

Post-condition: the command parameter is sent to each ServerThread to be sent to each client.

Parameters:
command - a formatted command String to be sent to each client connected

sendMap

public void sendMap(java.lang.String level)

Sends the current Map to all clients.

Pre-condition: server has a map loaded in the board[][] in Map class

Post-condition: all clients' Map classes are updated.


addUser

public void addUser(java.lang.String userName,
                    int robotNum)

Creates an add user command String for the robot passed and calls the sendToAll() method to send the command to all connected clients.

Pre-condition: none

Post-condition: an add user command String is created and sent to the sendToAll() method.


stopServer

public void stopServer()

Stops the server and sends a disconnect message to each client.

Pre-condition: existing socket connection to a server

Post-condition: disconnect command sent to all clients; server stoped listening for connections


setPort

public void setPort(int port)

Sets the port to listen on.

Parameters:
port - the port to listen on

getPort

public int getPort()

Returns the number of the port to listen on.

Returns:
port - the port used to listen for connections

setMaxCon

public void setMaxCon(int max)

Sets the maximum number of connections allowed.

Parameters:
max - the maximum number of connections allowed

getMaxCon

public int getMaxCon()

Returns the maximum number of connections allowed.

Returns:
maxCon - the maximum number of connections to listen for

getThreadNums

public int[] getThreadNums()

Returns the thread numbers of each ServerThread in the clients vector.

Returns:
the thread numbers of each ServerThread

stopListening

public void stopListening()
Stops the Server from listening for connections.
preconditions: the server is listening for connections.
postconditions: the server has stopped listening for connections.