pictionary.pictionaryclient
Class PictionaryClient.ServerConnection

java.lang.Object
  extended by pictionary.pictionaryclient.PictionaryClient.ServerConnection
Enclosing class:
PictionaryClient

private class PictionaryClient.ServerConnection
extends java.lang.Object

ServerConnection handles communication with the PictionaryServer.
It uses two threads for sending and receiving messages; PictionaryClient.ServerConnection.SendThread and PictionaryClient.ServerConnection.ReceiveThread.


Nested Class Summary
private  class PictionaryClient.ServerConnection.ReceiveThread
          Responsible for reading Messages from in and pass them along to handleMessage(pictionary.message.Message).
private  class PictionaryClient.ServerConnection.SendThread
          Responsible for taking Messages from outgoingMessages and sending them to the server using out.
 
Field Summary
private  boolean closed
          When the connection is closing, closed is set to true.
private  java.lang.String id
          The clients ID.
private  java.io.ObjectInputStream in
          The stream used for reading messages from the server.
private  java.io.ObjectOutputStream out
          The stream used for sending messages to the server.
private  java.util.concurrent.LinkedBlockingQueue<Message> outgoingMessages
          Messages waiting to be sent to the server by sendThread.
private  PictionaryClient.ServerConnection.ReceiveThread receiveThread
          The thread that received messages from the server.
private  PictionaryClient.ServerConnection.SendThread sendThread
          The thread that sends messages to the server.
private  java.net.Socket socket
          The socket connected to the server.
 
Constructor Summary
PictionaryClient.ServerConnection(java.lang.String host, int port)
          The constructor.
 
Method Summary
private  void close()
          Closes the connection.
private  void closedByError(java.lang.String error)
          Called when an error occurs, probably in receiveThread or sendThread.
private  void handleMessage(Message message)
          This method is called when a Message has been received in PictionaryClient.ServerConnection.ReceiveThread.
private  void send(Message message)
          Simply adds a message to outgoingMessages.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

id

private final java.lang.String id
The clients ID.


socket

private final java.net.Socket socket
The socket connected to the server.


in

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


out

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


sendThread

private final PictionaryClient.ServerConnection.SendThread sendThread
The thread that sends messages to the server.


receiveThread

private final PictionaryClient.ServerConnection.ReceiveThread receiveThread
The thread that received messages from the server.


outgoingMessages

private final java.util.concurrent.LinkedBlockingQueue<Message> outgoingMessages
Messages waiting to be sent to the server by sendThread.


closed

private volatile boolean closed
When the connection is closing, closed is set to true.

Constructor Detail

PictionaryClient.ServerConnection

PictionaryClient.ServerConnection(java.lang.String host,
                                  int port)
                            throws java.io.IOException
The constructor.

Parameters:
host - The server's hostname.
port - The server's port.
Throws:
java.io.IOException - if the socket could not be created and connected to the server.
Method Detail

close

private void close()
Closes the connection.


send

private void send(Message message)
Simply adds a message to outgoingMessages.

Parameters:
message - The Message to be sent to the server.

closedByError

private void closedByError(java.lang.String error)
Called when an error occurs, probably in receiveThread or sendThread.
Also passes the information along to PictionaryClient.callback's IPictionaryClientCallback.closedByError(String).

Parameters:
error - A string describing the error.

handleMessage

private void handleMessage(Message message)
This method is called when a Message has been received in PictionaryClient.ServerConnection.ReceiveThread.
It checks the message's Message.messageType and takes appropriate action, for example calling PictionaryClient.callback's IPictionaryClientCallback.connectedToServer() method when a message of the type Message.MessageType.ServerAcceptedConnection is received.

Parameters:
message - The Message that was received from the server.