com.eriksilkensen.net
Class Client

java.lang.Object
  extended by com.eriksilkensen.net.Client
All Implemented Interfaces:
Observable
Direct Known Subclasses:
SudokuClient

public class Client
extends java.lang.Object
implements Observable

An Observable Client that can be used to connect to Servers.

Observers that register themselves and pushed a reference to this Observable, along with an Object argument. This allows Observers to either accept the push or use a pull to take advantage of the Observable interface.

Specific jobs or functionality for a Client should be implemented by extending this Client.

Note: To remove a Client from the Server send out a quit message on closing.
This must be done with:

writer.println( Server.QUIT_MESSAGE + name )

Since:
1.0.2
Version:
03/03/07
Author:
Erik Silkensen
See Also:
Server, Observer, Observable

Field Summary
protected  java.lang.Object arg
           
protected  boolean changed
           
protected  java.util.List<Observer> observers
           
 
Constructor Summary
Client()
          Default constructor that initializes the List of Observers, clears changed.
Client(java.lang.String host, int port)
          Constructs a Client with the specified host and port configuration.
 
Method Summary
 void clearChanged()
          Clears this Client of being "changed"
 void clearConnected()
          Clears this Client of being connected
 void clearHost()
          Clears this Client from being a host
 void clearInit()
          Clears this Client from being initialized
 java.lang.String getHost()
           
protected  IncomingReader getIncomingReader(java.lang.String name)
          Returns a new instance of an IncomingReader that will handle all of the incoming messages.
 java.lang.String getName()
           
 int getPort()
           
protected  java.io.BufferedReader getReader()
          Returns this Client's reader.
 java.lang.String getRealName()
           
 boolean hasInit()
          Returns whether or not this Client has been initialized
 void init(java.lang.String name)
          Initializes this Client by setting up networking, and starting the Reader Thread.
 boolean isConnected()
          Returns whether or not this Client is connected
 boolean isHost()
          Returns whether or not this Client is a host
 void notifyObservers()
          Notifiies Observers.
 void quit()
          Disconnects this Client from the Server by sending out a special 'quit' message telling the Server to remove it from its List of Clients.
 void registerObserver(Observer obs)
          Registers an Observer with the List of observers
 void removeObserver(Observer obs)
          Removes an Observer from the List of observers
 void sendMessage(java.lang.String message)
          Sends a message.
 void setArg(java.lang.Object arg)
          Sets the optional Object arg that gets pushed to Observers when they are notified of a change
 void setChanged()
          Sets this Client to be "changed"
 void setConnected()
          Sets this Client to be connected
 void setHost()
          Sets this Client to be a host
 void setHost(java.lang.String host)
          Sets this Client's host configuration
 void setPort(int port)
          Sets this Client's port configuration
protected  void setUpNetworking()
          Sets up the networking by creating a Socket and attempting to connect to the Server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

changed

protected boolean changed

arg

protected java.lang.Object arg

observers

protected java.util.List<Observer> observers
Constructor Detail

Client

public Client()

Default constructor that initializes the List of Observers, clears changed.

The default host and port are set to 127.0.0.1 and 5000, respectively. Before the Client can be used it must run its init() method. This must be done manually by whichever class wants to use it.


Client

public Client(java.lang.String host,
              int port)
Constructs a Client with the specified host and port configuration.

Method Detail

removeObserver

public void removeObserver(Observer obs)
Removes an Observer from the List of observers

Specified by:
removeObserver in interface Observable
Parameters:
obs - the Observer to remove

registerObserver

public void registerObserver(Observer obs)
Registers an Observer with the List of observers

Specified by:
registerObserver in interface Observable
Parameters:
obs - the Observer to register

notifyObservers

public void notifyObservers()
Notifiies Observers. By default it pushes every Observer a reference to this Observable and a null arg. This is only done if changed is set, and clear is changed once it has been completed.

Specified by:
notifyObservers in interface Observable

setChanged

public void setChanged()
Sets this Client to be "changed"


clearChanged

public void clearChanged()
Clears this Client of being "changed"


setArg

public void setArg(java.lang.Object arg)
Sets the optional Object arg that gets pushed to Observers when they are notified of a change

Parameters:
arg - the arg

init

public void init(java.lang.String name)
          throws NotConnectedException
Initializes this Client by setting up networking, and starting the Reader Thread.

Throws:
NotConnectedException

getIncomingReader

protected IncomingReader getIncomingReader(java.lang.String name)
                                    throws NotConnectedException
Returns a new instance of an IncomingReader that will handle all of the incoming messages. Subclasses will want to override this in order to supply for their own IncomingReaders that have their own specific functionality.

Throws:
NotConnectedException

hasInit

public boolean hasInit()
Returns whether or not this Client has been initialized


clearInit

public void clearInit()
Clears this Client from being initialized


isConnected

public boolean isConnected()
Returns whether or not this Client is connected


setConnected

public void setConnected()
Sets this Client to be connected


clearConnected

public void clearConnected()
Clears this Client of being connected


isHost

public boolean isHost()
Returns whether or not this Client is a host


setHost

public void setHost()
Sets this Client to be a host


clearHost

public void clearHost()
Clears this Client from being a host


setHost

public void setHost(java.lang.String host)
Sets this Client's host configuration


setPort

public void setPort(int port)
Sets this Client's port configuration


setUpNetworking

protected void setUpNetworking()
                        throws NotConnectedException
Sets up the networking by creating a Socket and attempting to connect to the Server. Creates the name and reader.

Throws:
NotConnectedException

sendMessage

public void sendMessage(java.lang.String message)
Sends a message.

Parameters:
message - the message to send

quit

public void quit()
Disconnects this Client from the Server by sending out a special 'quit' message telling the Server to remove it from its List of Clients.


getReader

protected java.io.BufferedReader getReader()
                                    throws NotConnectedException
Returns this Client's reader. Protected so that only the IncomingReader/Server in this package and their subclasses have access to it. It will throw a NotConnectedException if the reader is null.

Throws:
NotConnectedException

getName

public java.lang.String getName()

getRealName

public java.lang.String getRealName()

getHost

public java.lang.String getHost()

getPort

public int getPort()