|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: INNER | 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
Field Summary | |
private int |
backlog
The maximum queue length; i.e. |
private java.lang.ThreadGroup |
clientThreadGroup
The thread group associated with client threads. |
private java.lang.Thread |
connectionListener
The connection listener thread. |
private int |
port
The port number |
private boolean |
readyToStop
Indicates if the listening thread is ready to stop. |
private java.net.ServerSocket |
serverSocket
The server socket: listens for clients who want to connect. |
private int |
timeout
The server timeout while for accepting connections. |
Constructor Summary | |
AbstractServer(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 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,
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. |
(package private) void |
receiveMessageFromClient(java.lang.Object msg,
ConnectionToClient client)
Receives a command sent from the client to the server. |
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 |
|
Field Detail |
private java.net.ServerSocket serverSocket
private java.lang.Thread connectionListener
private int port
private int timeout
private int backlog
private java.lang.ThreadGroup clientThreadGroup
ConnectionToClient
.private boolean readyToStop
Constructor Detail |
public AbstractServer(int port)
port
- the port number on which to listen.Method Detail |
public final void listen() throws java.io.IOException
public final void stopListening()
public final void close() throws java.io.IOException
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()
protected void clientConnected(ConnectionToClient client)
client
- the connection connected to the client.protected void clientDisconnected(ConnectionToClient client)
client
- the connection with the client.protected void clientException(ConnectionToClient client, java.lang.Throwable exception)
client
- the client that raised the exception.Throwable
- the exception thrown.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, ConnectionToClient client)
msg
- the message sent.client
- the connection connected to the client that
sent the message.final void receiveMessageFromClient(java.lang.Object msg, ConnectionToClient client)
ConnectionToClient
instances that are watching for messages coming from the server
This method is synchronized to ensure that whatever effects it has
do not conflict with work being done by other threads. The method
simply calls the handleMessageFromClient
slot method.msg
- the message sent.client
- the connection connected to the client that
sent the message.
|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |