pictionary.pictionaryclient
Class PictionaryClient

java.lang.Object
  extended by pictionary.pictionaryclient.PictionaryClient

public final class PictionaryClient
extends java.lang.Object

Implementation of a basic Pictionary Client which handles communication with a PictionaryServer.
Uses the private class PictionaryClient.ServerConnection for the actual communication with the server.
PictionaryClient's job is to handle communication with the server and just forward information to an IPictionaryClientCallback.

Author:
Kristoffer Nordkvist

Nested Class Summary
private  class PictionaryClient.ServerConnection
          ServerConnection handles communication with the PictionaryServer.
 
Field Summary
private  IPictionaryClientCallback callback
          The IPictionaryClientCallback, in this case PictionaryClientGUI.
 java.util.List<java.lang.String> connectedClients
          A collection containing the connected client's unique names.
private  PictionaryClient.ServerConnection connection
          Represents the network connection to the server.
private  java.lang.String serverHostName
          The server's host name.
private  int serverPort
          The server's port.
 
Constructor Summary
PictionaryClient(java.lang.String serverHostName, int serverPort)
          The constructor.
 
Method Summary
 void broadcastChatMessage(java.lang.String message)
          Creates and sends a Message of the type Message.MessageType.ClientMessageBroadcast using PictionaryClient.ServerConnection.send(Message).
 void broadcastDraw(java.awt.Point p)
          Creates and sends a Message of the type Message.MessageType.DrawData using PictionaryClient.ServerConnection.send(Message).
 void connect(IPictionaryClientCallback callback)
          Creates a new PictionaryClient.ServerConnection using serverHostName and serverPort.
 void disconnect()
          Sends a message to the server, informing it that we are about to disconnect from it.
 java.lang.String getID()
          Gets this client unique ID, used by the server to identify it.
 java.lang.String getServerInfo()
          Gets information about the server's hostname and port, this method is used by PictionaryClientGUI when setting the client's window title.
 void guess(java.lang.String guess)
          Creates and sends a Message of the type Message.MessageType.Guess using PictionaryClient.ServerConnection.send(Message).
 void joinGame()
          Sends a message to the server, informing it that we want to join a game of Pictionary.
private  void send(Message message)
          Sends a Message to the server using PictionaryClient.ServerConnection.send(Message).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

connectedClients

public java.util.List<java.lang.String> connectedClients
A collection containing the connected client's unique names.


connection

private PictionaryClient.ServerConnection connection
Represents the network connection to the server.


serverHostName

private java.lang.String serverHostName
The server's host name.


serverPort

private int serverPort
The server's port.


callback

private IPictionaryClientCallback callback
The IPictionaryClientCallback, in this case PictionaryClientGUI.

Constructor Detail

PictionaryClient

public PictionaryClient(java.lang.String serverHostName,
                        int serverPort)
The constructor.

Parameters:
serverHostName - The server's host name, this is used when connecting to the server.
serverPort - The server's port, this is used when connecting to the server.
Method Detail

connect

public void connect(IPictionaryClientCallback callback)
             throws java.io.IOException
Creates a new PictionaryClient.ServerConnection using serverHostName and serverPort.

Parameters:
callback - The IPictionaryClientCallback which will be notified of events.
Throws:
java.io.IOException - If the connection to the server could not be established.

joinGame

public void joinGame()
Sends a message to the server, informing it that we want to join a game of Pictionary.


disconnect

public void disconnect()
Sends a message to the server, informing it that we are about to disconnect from it.


send

private void send(Message message)
           throws java.lang.IllegalStateException
Sends a Message to the server using PictionaryClient.ServerConnection.send(Message).

Parameters:
message - The message to be sent to the server.
Throws:
java.lang.IllegalStateException - If connection's boolean PictionaryClient.ServerConnection.closed equals true.

broadcastChatMessage

public void broadcastChatMessage(java.lang.String message)
Creates and sends a Message of the type Message.MessageType.ClientMessageBroadcast using PictionaryClient.ServerConnection.send(Message).

Parameters:
message - The Message to be sent.

guess

public void guess(java.lang.String guess)
Creates and sends a Message of the type Message.MessageType.Guess using PictionaryClient.ServerConnection.send(Message).

Parameters:
guess - The word that was guessed by the player.

broadcastDraw

public void broadcastDraw(java.awt.Point p)
Creates and sends a Message of the type Message.MessageType.DrawData using PictionaryClient.ServerConnection.send(Message).

Parameters:
p - The Point to send to the server.

getID

public final java.lang.String getID()
Gets this client unique ID, used by the server to identify it.

Returns:
PictionaryClient.ServerConnection.id.

getServerInfo

public final java.lang.String getServerInfo()
Gets information about the server's hostname and port, this method is used by PictionaryClientGUI when setting the client's window title.

Returns:
A String in the format of "Connected to [hostname] at port [port]".