vooga.engine.networking.server
Class ClientHandler

java.lang.Object
  extended by java.lang.Thread
      extended by vooga.engine.networking.server.Handler
          extended by vooga.engine.networking.server.ClientHandler
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
TicTacToeHandler

public abstract class ClientHandler
extends Handler

Abstract class that lays some of the groundwork for communicating between clients over the network. There is one instance of a ClientHandler subclass for each time the game is run and a static list of the current running ClientHandler subclasses. ClientHandler extends the Handler class so it also extends Thread and has a socket to send and receive chats through as well as a session ID to group ClientHandlers that are part of the same game session. It also knows the number of players it needs for the game to start, and in its run method waits to start the game until that number is fulfilled. Classes that extend ClientHandler must extend the firstRun() method which is where the programmer puts anything that needs to be sent before the game is begun. Subclasses of ClientHandler will also subclass run (while calling super.run() as the first line of the method) in order to add whatever game specific code they need to send messages to the other players in their game session.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected static java.util.List<ClientHandler> handlers
           
protected  int numberOfPlayers
           
 
Fields inherited from class vooga.engine.networking.server.Handler
sessionID, socket
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ClientHandler(VoogaDaemon daemon, GameSocket socket, int numberOfPlayers, int gameNumber)
          Static method to return the XML document with the list of games that can be run on the networking Vooga servers.
 
Method Summary
protected static void broadcastToAll(java.lang.String message, ClientHandler sender)
          Broadcast (send) a message to everyone in the sender's game session.
protected static void broadcastToOthers(java.lang.String chat, ClientHandler sender)
          Broadcast (send) a message to everyone in the sender's game session but the sender.
abstract  void firstRun()
          Abstract method that is called by run() right after the correct number of players are found and before the game has to start.
 ClientHandler getFirstPlayer(int gameNumber)
           
 int getPlayers(int gameNumber)
           
 void run()
          Each time a ClientHandler is started, add itself to the list of handlers, send out "wait" to all the players until the correct number of players is met, and then call firstRun() which is where the user will put any code that needs to be run right after the correct number of players are found.
 
Methods inherited from class vooga.engine.networking.server.Handler
getSessionID, getSocket
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

numberOfPlayers

protected int numberOfPlayers

handlers

protected static java.util.List<ClientHandler> handlers
Constructor Detail

ClientHandler

public ClientHandler(VoogaDaemon daemon,
                     GameSocket socket,
                     int numberOfPlayers,
                     int gameNumber)
Static method to return the XML document with the list of games that can be run on the networking Vooga servers.

Parameters:
daemon - the VoogaDaemon that initialized this object; used to increment the numberOfGames when the correct amount of players is met
socket - the socket that clients will be communicating through
numberOfPlayers - the number of players necessary to play the game
gameNumber - this individual instance's number in relation to all the run games on the server, used to group clients in a common game session
Method Detail

run

public void run()
Each time a ClientHandler is started, add itself to the list of handlers, send out "wait" to all the players until the correct number of players is met, and then call firstRun() which is where the user will put any code that needs to be run right after the correct number of players are found. Subclasses of ClientHandler will almost always override run(), and when they do they must call super.run() before doing anything else in order to take advantage of waiting for the correct number of players and the call to firstRun.

Specified by:
run in interface java.lang.Runnable
Specified by:
run in class Handler

firstRun

public abstract void firstRun()
Abstract method that is called by run() right after the correct number of players are found and before the game has to start. Here is the place to put any code that needs to be run before the game begins (i.e. messages to say whose turn it is for turn games like TicTacToe, etc.)


broadcastToAll

protected static void broadcastToAll(java.lang.String message,
                                     ClientHandler sender)
Broadcast (send) a message to everyone in the sender's game session.

Parameters:
message - the message to send
sender - the handler who is sending the message

broadcastToOthers

protected static void broadcastToOthers(java.lang.String chat,
                                        ClientHandler sender)
Broadcast (send) a message to everyone in the sender's game session but the sender.

Parameters:
message - the message to send
sender - the handler who is sending the method

getPlayers

public int getPlayers(int gameNumber)
Parameters:
gameNumber - the id of the game session
Returns:
the number of players in a given game session.

getFirstPlayer

public ClientHandler getFirstPlayer(int gameNumber)
Parameters:
gameNumber - the id of the game session
Returns:
the first player in a game session.