ocsf.server
Class ObservableServer

java.lang.Object
  |
  +--java.util.Observable
        |
        +--ocsf.server.ObservableServer
Direct Known Subclasses:
ObservableOriginatorServer

public class ObservableServer
extends java.util.Observable

This class acts as a subclass of AbstractServer and is also an Observable class. This means that when a message is received, all observers are notified. In this implementation, there are two options for notifications. Under the default anonymous notification mode, the observers receive only the message, information about the client that sends this message is therefore lost. In the other option, an array of 2 elements is sent: the first one is the message and the second one is a ConnectionToClient object describing the client.


Field Summary
private  AdaptableServer service
          The service used to simulate multiple class inheritance.
 
Fields inherited from class java.util.Observable
changed, obs
 
Constructor Summary
ObservableServer(int port)
          Constructs a new server.
 
Method Summary
protected  void clientConnected(ConnectionToClient client)
          Hook method called each time a new client connection is accepted.
protected  void clientDisconnected(ConnectionToClient client)
          Hook method called each time a client disconnects.
protected  void clientException(ConnectionToClient client, java.lang.Throwable exception)
          Hook method called each time an exception is raised in a client thread.
 void close()
          Closes the server's connections with all clients.
 java.lang.Thread[] getClientConnections()
          Returns an array of containing the existing client connections.
 int getNumberOfClients()
           
 int getPort()
           
protected  void handleMessageFromClient(java.lang.Object message, ConnectionToClient client)
          This method is used to handle messages coming from the client.
 boolean isListening()
           
 void listen()
          Begins the thread that waits for new clients
protected  void listeningException(java.lang.Throwable exception)
          This method is called when the server stops accepting connections because an exception has been raised.
 void sendToAllClients(java.lang.Object msg)
          Sends a message to every client connected to the server.
protected  void serverClosed()
          This method is called when the server is closed.
protected  void serverPaused()
          This method is called when the server briefly stop accepting connections.
protected  void serverStarted()
          This method is called when the server starts listening for connections.
protected  void serverStopped()
          This method is called when the server stops accepting connections for any reason.
 void setBacklog(int backlog)
          Sets the maximum number of waiting connections accepted by the operating system.
 void setPort(int port)
          Sets the port number for the next connection.
 void setTimeout(int timeout)
          Sets the timeout time when accepting connection.
 void stopListening()
          Causes the server to stop accepting new connections.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

service

private AdaptableServer service
The service used to simulate multiple class inheritance.
Constructor Detail

ObservableServer

public ObservableServer(int port)
Constructs a new server.
Parameters:
port - the port on which to listen.
Method Detail

listen

public final void listen()
                  throws java.io.IOException
Begins the thread that waits for new clients

stopListening

public final void stopListening()
Causes the server to stop accepting new connections.

close

public final void close()
                 throws java.io.IOException
Closes the server's connections with all clients.

sendToAllClients

public void sendToAllClients(java.lang.Object msg)
Sends a message to every client connected to the server. This is merely a utility; a subclass may want to do some checks before actually sending messages to all clients. This method can be overriden, but if so it should still perform the general function of sending to all clients, perhaps after some kind of filtering is done.
Parameters:
msg - Object: The message to be sent

isListening

public final boolean isListening()

getClientConnections

public final java.lang.Thread[] getClientConnections()
Returns an array of containing the existing client connections. This can be used by concrete subclasses to implement messages that do something with each connection (e.g. kill it, send a message to it etc.)
Returns:
an array of Thread containing ConnectionToClient instances.

getNumberOfClients

public final int getNumberOfClients()
Returns:
the number of clients currently connected.

getPort

public final int getPort()
Returns:
the port number.

setPort

public final void setPort(int port)
Sets the port number for the next connection. Only has effect if the server is not currently listening.
Parameters:
port - the port number.

setTimeout

public final void setTimeout(int timeout)
Sets the timeout time when accepting connection. The default is half a second. The server must be stopped and restarted for the timeout change be in effect.
Parameters:
timeout - the timeout time in ms.

setBacklog

public final void setBacklog(int backlog)
Sets the maximum number of waiting connections accepted by the operating system. The default is 20. The server must be closed and restart for the backlog change be in effect.
Parameters:
backlog - the maximum number of connections.

clientConnected

protected void clientConnected(ConnectionToClient client)
Hook method called each time a new client connection is accepted. The default implementation does nothing. This method may be overridden by subclasses.
Parameters:
client - the connection connected to the client.

clientDisconnected

protected void clientDisconnected(ConnectionToClient client)
Hook method called each time a client disconnects. The default implementation does nothing. The method may be overridden by subclasses.
Parameters:
client - the connection with the client.

clientException

protected void clientException(ConnectionToClient client,
                               java.lang.Throwable exception)
Hook method called each time an exception is raised in a client thread. The default implementation simply closes the client connection, ignoring any exception. The method may be overridden by subclasses.
Parameters:
client - the client that raised the exception.
exception - the exception raised.

listeningException

protected void listeningException(java.lang.Throwable exception)
This method is called when the server stops accepting connections because an exception has been raised. The default implementation simply calls stopListening. This method may be overriden by subclasses.
Parameters:
exception - the exception raised.

serverPaused

protected void serverPaused()
This method is called when the server briefly stop accepting connections. When this method ends, server will restart listening. The default implementation does nothing. This method may be overriden by subclasses.

serverStopped

protected void serverStopped()
This method is called when the server stops accepting connections for any reason. The default implementation does nothing. This method may be overriden by subclasses.

serverClosed

protected void serverClosed()
This method is called when the server is closed. The default implementation does nothing. This method may be overriden by subclasses.

serverStarted

protected void serverStarted()
This method is called when the server starts listening for connections. The default implementation does nothing. The method may be overridden by subclasses.

handleMessageFromClient

protected void handleMessageFromClient(java.lang.Object message,
                                       ConnectionToClient client)
This method is used to handle messages coming from the client. Observers are notfied by receiveing the transmitted message. Note that, in this implementation, the information concerning the client that sent the message is lost. It can be overriden, but is still expected to call notifyObservers().
Parameters:
message - The message received from the client.
client - The connection to the client.