public abstract class MultiSocketServer
extends java.lang.Object
A convenient abstract class which maintains its own EventQueue
and calls the relevant onX() method each time a new event is
returned from the queue.
The six event methods are as follows:
onStart
is called when the server starts.onConnect
is called each time a new
connection is made to the server.onInput
is called each time a
connection receives input.onDisconnect
is called each time a
connections disconnects from the server.onException
is called any time an
uncaught exception is thrown from one of these six methods or if an
IOException
is thrown during the life cycle of a
Connection
.onStop
is called once all other events have taken
place and the server has stopped.The order and frequency that these methods are called in reflects the
guaranteed properties of the sequence of events returned by
EventQueue
.
No two onX() methods will ever be called at the same time. The server waits until the currently executing onX() returns before calling the next onX() method. This means you do not have to worry about synchronized data structures (unless they will be used by other threads).
Note that the server will create a new thread to
pop
events from its queue and call the various
onX() methods. In other words, the onX() methods will
not be called from the same thread which calls start
.
Therefore, it is highly recommended that if you define any new public
methods that you make them synchronized. The server's event dispatching
thread will always synchronize on this object before calling one of the
onX() methods.
If you want to extend Connection
, you can override the protected
createNewConnection
method to return
subclasses of Connection
. This method will be used instead of
EventQueue.createNewConnection
.
Modifier and Type | Field and Description |
---|---|
int |
port
The port on which this server will listen for connections.
|
Constructor and Description |
---|
MultiSocketServer(int port)
Creates a new server that will listen on the specified port.
|
Modifier and Type | Method and Description |
---|---|
protected Connection |
createNewConnection(java.net.Socket socket)
This method creates a new
Connection object for each socket that
connects. |
protected abstract void |
onConnect(Connection connection)
Called when an
ConnectEvent occurs. |
protected abstract void |
onDisconnect(Connection connection)
Called when an
DisconnectEvent occurs. |
protected abstract void |
onException(java.lang.Exception exception)
Called when an
ExceptionEvent occurs. |
protected abstract void |
onInput(Connection connection,
java.lang.String input)
Called when an
InputEvent occurs. |
protected abstract void |
onStart()
Called when the
Event.SERVER_START_EVENT occurs. |
protected abstract void |
onStop()
Called when the
Event.SERVER_STOP_EVENT occurs. |
void |
start()
When this method is called the server will begin listening for
connections on the specified port and calling the appropriate
onX() methods.
|
void |
stop()
When this method is called the server stops accepting new connections
and closes all active connections.
|
public MultiSocketServer(int port)
port
- the port to listen onprotected Connection createNewConnection(java.net.Socket socket) throws java.io.IOException
Connection
object for each socket that
connects. If you want to extend Connection
, override this method
to return objects which are a subclass of Connection
.socket
- the newly connected socketConnection
object for that socketjava.io.IOException
- if an I/O error occurs while creating the Connection
public void start()
protected abstract void onStart()
Event.SERVER_START_EVENT
occurs.protected abstract void onConnect(Connection connection)
ConnectEvent
occurs.connection
- the new connectionprotected abstract void onInput(Connection connection, java.lang.String input)
InputEvent
occurs.connection
- the connection from which the input cameinput
- the input from the connectionprotected abstract void onDisconnect(Connection connection)
DisconnectEvent
occurs.connection
- the connection which has disconnectedprotected abstract void onException(java.lang.Exception exception)
ExceptionEvent
occurs.exception
- the exception which occuredprotected abstract void onStop()
Event.SERVER_STOP_EVENT
occurs.public void stop()
EventQueue
is empty.