NetworkIO
Class ClientBase

java.lang.Object
  extended by java.lang.Thread
      extended by NetworkIO.ClientBase
All Implemented Interfaces:
java.lang.Runnable

public class ClientBase
extends java.lang.Thread

ClientBase maintains a connection with a server of some kind. It will not allow for any connections to be made to it, nor is it able to connect to more than one server at a time.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected  boolean connected
           
protected  java.net.Socket connection
           
protected  java.util.HashSet connectionListeners
           
protected  java.io.ObjectInputStream input
           
protected  NetworkLogger logger
           
protected  java.util.HashSet networkListeners
           
protected  java.io.ObjectOutputStream output
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ClientBase(java.net.Socket socket)
          Creates a new instance of ClientBase using a pre-defined Socket to create a connection.
ClientBase(java.lang.String host, int port)
          Creates a new instance of ClientBase using a String host and int port to create a socket connection.
 
Method Summary
 void addConnectionListener(ConnectionListener listener)
          Adds a ConnectionListener to Client base in case of a lost connection.
 void addNetworkListener(NetworkListener listener)
          Adds a NetworkListener to ClientBase so that any incomming connection will be sent to it.
protected  java.net.Socket connect(java.lang.String host, int port)
          Creates and returns a socket created from the recieved host and port.
 void disconnect()
          Disconnects from the current connected server.
 java.net.Socket getSocket()
          Returns the current Socket connection.
protected  void getStreams()
          Registers the input and output streams to establish a communication connection.
 void run()
          Starts the client's process of recieving Messages and telling NetworkListeners to process them.
 void send(Message message)
          Sends the Message to the connected server.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, 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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

protected NetworkLogger logger

connection

protected java.net.Socket connection

networkListeners

protected java.util.HashSet networkListeners

connectionListeners

protected java.util.HashSet connectionListeners

input

protected java.io.ObjectInputStream input

output

protected java.io.ObjectOutputStream output

connected

protected boolean connected
Constructor Detail

ClientBase

public ClientBase(java.lang.String host,
                  int port)
           throws java.lang.Exception
Creates a new instance of ClientBase using a String host and int port to create a socket connection.

Parameters:
host - The host name of a computer running a valid server.
port - The port of the computer to connect to.
Throws:
java.lang.Exception - Any exception that occurs while creating a socket and registering a connection.

ClientBase

public ClientBase(java.net.Socket socket)
           throws java.lang.Exception
Creates a new instance of ClientBase using a pre-defined Socket to create a connection.

Parameters:
socket - The connected socket to another computer.
Throws:
java.lang.Exception - Any exception occuring while registering a connection.
Method Detail

connect

protected java.net.Socket connect(java.lang.String host,
                                  int port)
                           throws java.lang.Exception
Creates and returns a socket created from the recieved host and port.

Parameters:
host - The host name of the computer running a valid server.
port - The port of the computer to connect to.
Returns:
Returns the created Socket.
Throws:
java.lang.Exception - Any exception that occured while trying to establish a connection to the serving computer.

getStreams

protected void getStreams()
                   throws java.lang.Exception
Registers the input and output streams to establish a communication connection.

Throws:
java.lang.Exception - Any exception occured while registering input and output streams.

run

public void run()
Starts the client's process of recieving Messages and telling NetworkListeners to process them.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

send

public void send(Message message)
          throws java.lang.Exception
Sends the Message to the connected server.

Parameters:
message - The Message to send.
Throws:
java.lang.Exception - Any exception that occurs while trying to send the message.

getSocket

public java.net.Socket getSocket()
Returns the current Socket connection.

Returns:
The current Socket connection.

disconnect

public void disconnect()
Disconnects from the current connected server.


addNetworkListener

public void addNetworkListener(NetworkListener listener)
Adds a NetworkListener to ClientBase so that any incomming connection will be sent to it.

Parameters:
listener - The NetworkListener to add.

addConnectionListener

public void addConnectionListener(ConnectionListener listener)
Adds a ConnectionListener to Client base in case of a lost connection.

Parameters:
listener - The ConnectionListener to add.