|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--ocsf.server.AbstractServer
The AbstractServer
class maintains a thread that waits
for connection attempts from clients. When a connection attempt occurs
it creates a new ConnectionToClient
instance which
runs as a thread. When a client is thus connected to the
server, the two programs can then exchange Object
instances.
Method handleMessageFromClient
must be defined by
a concrete subclass. Several other hook methods may also be
overriden.
Several public service methods are provided to applications that use this framework, and several hook methods are also available
Project Name: OCSF (Object Client-Server Framework)
ConnectionToClient
Constructor Summary | |
AbstractServer(int port)
Constructs a new server. |
Method Summary | |
protected void |
clientConnected(ocsf.server.ConnectionToClient client)
Hook method called each time a new client connection is accepted. |
protected void |
clientDisconnected(ocsf.server.ConnectionToClient client)
Hook method called each time a client disconnects. |
protected void |
clientException(ocsf.server.ConnectionToClient client,
java.lang.Throwable exception)
Hook method called each time an exception is thrown in a ConnectionToClient thread. |
void |
close()
Closes the server socket and the connections with all clients. |
java.lang.Thread[] |
getClientConnections()
Returns an array containing the existing client connections. |
int |
getNumberOfClients()
Counts the number of clients currently connected. |
int |
getPort()
Returns the port number. |
protected abstract void |
handleMessageFromClient(java.lang.Object msg,
ocsf.server.ConnectionToClient client)
Handles a command sent from one client to the server. |
boolean |
isListening()
Returns true if the server is ready to accept new clients. |
void |
listen()
Begins the thread that waits for new clients. |
protected void |
listeningException(java.lang.Throwable exception)
Hook method called when the server stops accepting connections because an exception has been raised. |
void |
run()
Runs the listening thread that allows clients to connect. |
void |
sendToAllClients(java.lang.Object msg)
Sends a message to every client connected to the server. |
protected void |
serverClosed()
Hook method called when the server is clased. |
protected void |
serverStarted()
Hook method called when the server starts listening for connections. |
protected void |
serverStopped()
Hook method called when the server stops accepting connections. |
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 connections. |
void |
stopListening()
Causes the server to stop accepting new connections. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public AbstractServer(int port)
port
- the port number on which to listen.Method Detail |
public final void listen() throws java.io.IOException
java.io.IOException
- if an I/O error occurs
when creating the server socket.public final void stopListening()
public final void close() throws java.io.IOException
java.io.IOException
- if an I/O error occurs while
closing the server socket.public void sendToAllClients(java.lang.Object msg)
msg
- Object The message to be sentpublic final boolean isListening()
public final java.lang.Thread[] getClientConnections()
Thread
containing
ConnectionToClient
instances.public final int getNumberOfClients()
public final int getPort()
public final void setPort(int port)
port
- the port number.public final void setTimeout(int timeout)
timeout
- the timeout time in ms.public final void setBacklog(int backlog)
backlog
- the maximum number of connections.public final void run()
run
in interface java.lang.Runnable
protected void clientConnected(ocsf.server.ConnectionToClient client)
client
- the connection connected to the client.protected void clientDisconnected(ocsf.server.ConnectionToClient client)
client
- the connection with the client.protected void clientException(ocsf.server.ConnectionToClient client, java.lang.Throwable exception)
client
- the client that raised the exception.protected void listeningException(java.lang.Throwable exception)
exception
- the exception raised.protected void serverStarted()
protected void serverStopped()
protected void serverClosed()
protected abstract void handleMessageFromClient(java.lang.Object msg, ocsf.server.ConnectionToClient client)
msg
- the message sent.client
- the connection connected to the client that
sent the message.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |