pictionary.pictionaryserver
Class PictionaryServer.ClientConnection

java.lang.Object
  extended by pictionary.PictionaryPlayer
      extended by pictionary.pictionaryserver.PictionaryServer.ClientConnection
Enclosing class:
PictionaryServer

private class PictionaryServer.ClientConnection
extends PictionaryPlayer

Handles communication with one client; there is one ClientConnection per connected client. Also extends PictionaryPlayer so it can be used with PictionaryServer.pictionaryGame. Has one thread for receiving messages from the client and one for sending messages.


Nested Class Summary
private  class PictionaryServer.ClientConnection.ReceiveThread
          Receives messages from the client and adds them to the server's message queue.
private  class PictionaryServer.ClientConnection.SendThread
          Initially handles setup, then takes care of sending messages to the client.
 
Field Summary
private  java.lang.String clientID
          The clients unique identifier, a String containing it's name.
private  boolean closed
          Is set to true if the client should be disconnected gracefully.
private  java.net.Socket connection
          The Socket used for getting the in and output streams.
private  java.io.ObjectInputStream in
          The stream used for reading messages from the client.
private  java.util.concurrent.BlockingQueue<PictionaryServer.ClientConnectionMessage> incomingMessages
          Received messages are put in this collection, this is a reference to PictionaryServer.incomingMessages.
private  java.io.ObjectOutputStream out
          The stream used for sending messages to the client.
private  java.util.concurrent.LinkedBlockingQueue<Message> outgoingMessages
          A collection of messages waiting to be sent to the client.
private  java.lang.Thread receiveThread
          Takes care of receiving and forwarding messages from the client to the server.
private  java.lang.Thread sendThread
          The PictionaryServer.ClientConnection.SendThread instance.
 
Fields inherited from class pictionary.PictionaryPlayer
DRAWING, SPECTATING, WAITING_FOR_TURN
 
Constructor Summary
PictionaryServer.ClientConnection(java.util.concurrent.BlockingQueue<PictionaryServer.ClientConnectionMessage> receivedMessageQueue, java.net.Socket connection)
          The constructor.
 
Method Summary
private  void close()
          Closes this connection and terminates the send and receive threads.
private  void closedWithError(java.lang.String message)
          Sends the message to PictionaryServer.connectionToClientClosedWithError(ClientConnection, String) and calls close().
 void correctGuess(java.lang.String guess)
          Informs the player that he made a correct guess.
 void correctGuessBroadcast(java.lang.String clientID, java.lang.String guess)
          Informs the player that a player made a correct guess.
 void gameStarted()
          Informs the player that a new game of Pictionary has started.
 java.lang.String getClientID()
          Get method for clientID.
 void playerStartedDrawing(java.lang.String clientID)
          Informs the player that a player started drawing.
 void playerStoppedDrawing(java.lang.String clientID)
          Informs the player that a player stopped drawing.
 void reportScores(java.util.List<java.lang.String> players, java.util.List<java.lang.Integer> scores)
          Reports the players' scores.
 void send(Message message)
          Adds a message to outgoingMessages.
 void startDraw(java.lang.String wordToDraw)
          Informs the player that it is her turn to start drawing a word.
 void startGuessing()
          Informs the player that it can start guessing on the word being drawn.
 void stopDraw()
          Informs the player that its draw turn is over.
 void stopGuessing()
          Informs the player that the guessing turn is over.
 void tellWord(java.lang.String word)
          Tells the player the last word.
 void wrongGuess(java.lang.String clientID, java.lang.String guess)
          Informs the player that someone made an incorrect guess.
 
Methods inherited from class pictionary.PictionaryPlayer
equals, getScore, getStatus, setScore, setStatus
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

clientID

private java.lang.String clientID
The clients unique identifier, a String containing it's name.


incomingMessages

private java.util.concurrent.BlockingQueue<PictionaryServer.ClientConnectionMessage> incomingMessages
Received messages are put in this collection, this is a reference to PictionaryServer.incomingMessages.


outgoingMessages

private java.util.concurrent.LinkedBlockingQueue<Message> outgoingMessages
A collection of messages waiting to be sent to the client.


connection

private java.net.Socket connection
The Socket used for getting the in and output streams.


in

private java.io.ObjectInputStream in
The stream used for reading messages from the client.


out

private java.io.ObjectOutputStream out
The stream used for sending messages to the client.


closed

private volatile boolean closed
Is set to true if the client should be disconnected gracefully.


sendThread

private java.lang.Thread sendThread
The PictionaryServer.ClientConnection.SendThread instance.


receiveThread

private volatile java.lang.Thread receiveThread
Takes care of receiving and forwarding messages from the client to the server.

Constructor Detail

PictionaryServer.ClientConnection

public PictionaryServer.ClientConnection(java.util.concurrent.BlockingQueue<PictionaryServer.ClientConnectionMessage> receivedMessageQueue,
                                         java.net.Socket connection)
The constructor.

Parameters:
receivedMessageQueue - The queue where received messages will be put.
connection - The Socket that will be used to get streams from.
Method Detail

getClientID

public java.lang.String getClientID()
Get method for clientID.

Specified by:
getClientID in class PictionaryPlayer
Returns:
The player's ID.

close

private void close()
Closes this connection and terminates the send and receive threads.


send

public void send(Message message)
Adds a message to outgoingMessages.

Parameters:
message - The message to be sent.

closedWithError

private void closedWithError(java.lang.String message)
Sends the message to PictionaryServer.connectionToClientClosedWithError(ClientConnection, String) and calls close().

Parameters:
message - The message to be printed.

stopDraw

public void stopDraw()
Description copied from class: PictionaryPlayer
Informs the player that its draw turn is over.

Specified by:
stopDraw in class PictionaryPlayer

playerStoppedDrawing

public void playerStoppedDrawing(java.lang.String clientID)
Description copied from class: PictionaryPlayer
Informs the player that a player stopped drawing.

Specified by:
playerStoppedDrawing in class PictionaryPlayer
Parameters:
clientID - The name of the player that stopped drawing.

startDraw

public void startDraw(java.lang.String wordToDraw)
Description copied from class: PictionaryPlayer
Informs the player that it is her turn to start drawing a word.

Specified by:
startDraw in class PictionaryPlayer
Parameters:
wordToDraw - The word that the player should attempt to draw.

playerStartedDrawing

public void playerStartedDrawing(java.lang.String clientID)
Description copied from class: PictionaryPlayer
Informs the player that a player started drawing.

Specified by:
playerStartedDrawing in class PictionaryPlayer
Parameters:
clientID - The name of the player that started drawing.

gameStarted

public void gameStarted()
Description copied from class: PictionaryPlayer
Informs the player that a new game of Pictionary has started.

Specified by:
gameStarted in class PictionaryPlayer

correctGuess

public void correctGuess(java.lang.String guess)
Description copied from class: PictionaryPlayer
Informs the player that he made a correct guess.

Specified by:
correctGuess in class PictionaryPlayer
Parameters:
guess - The guess that was correct.

correctGuessBroadcast

public void correctGuessBroadcast(java.lang.String clientID,
                                  java.lang.String guess)
Description copied from class: PictionaryPlayer
Informs the player that a player made a correct guess.

Specified by:
correctGuessBroadcast in class PictionaryPlayer
Parameters:
clientID - The player that made the guess.
guess - The correct word.

startGuessing

public void startGuessing()
Description copied from class: PictionaryPlayer
Informs the player that it can start guessing on the word being drawn.

Specified by:
startGuessing in class PictionaryPlayer

stopGuessing

public void stopGuessing()
Description copied from class: PictionaryPlayer
Informs the player that the guessing turn is over.

Specified by:
stopGuessing in class PictionaryPlayer

wrongGuess

public void wrongGuess(java.lang.String clientID,
                       java.lang.String guess)
Description copied from class: PictionaryPlayer
Informs the player that someone made an incorrect guess.

Specified by:
wrongGuess in class PictionaryPlayer
Parameters:
clientID - The player that made the guess.
guess - The incorrect guess.

reportScores

public void reportScores(java.util.List<java.lang.String> players,
                         java.util.List<java.lang.Integer> scores)
Description copied from class: PictionaryPlayer
Reports the players' scores.
A score at a specific index in scores corresponds to the player at the same index in players.

Specified by:
reportScores in class PictionaryPlayer
Parameters:
players - A collection of players.
scores - A collection of scores.

tellWord

public void tellWord(java.lang.String word)
Description copied from class: PictionaryPlayer
Tells the player the last word. This method is used when no one makes a correct guess.

Specified by:
tellWord in class PictionaryPlayer
Parameters:
word - The word that was being drawn.