public class EventQueue
extends java.lang.Object
An EventQueue listens for incoming Connection
s and
allows the user to get a sequence of Event
s via the
pop
method.
The sequence of events is guaranteed to have the following properties:
Event.SERVER_START_EVENT
will always be first (unless the
server failed to start).ConnectEvent
will occur per connection.DisconnectEvent
will occur per connection.
There will always be a DisconnectEvent
for every connection,
even if the event queue is stopped while the connection is active.
InputEvent
s will occur per connection.
InputEvent
s will always come after the connection's
ConnectEvent
and before the connection's {DisconnectEvent}.Event.SERVER_STOP_EVENT
will always be last (unless the
server failed to start).ExceptionEvent
s can occur at any time. If the server fails to
start, an ExceptionEvent
will explain why.The EventQueue does not begin listening for connections until
the start
method is called. When the stop
method is called, all active connections will be closed and any new
connections will be refused. The pop
method will continue to
return events until all the requirements above are met.
Calls to pop
before the queue is started or after the
queue has returned the Event.SERVER_STOP_EVENT
will return null.
An EventQueue can only be started once.
The EventQueue creates new Connection
objects for each
new socket that connects. If you want to extend Connection
, override
the protected createNewConnection
method.
Modifier and Type | Field and Description |
---|---|
int |
port
The port on which the event queue will listen for connections.
|
Constructor and Description |
---|
EventQueue(int port)
Creates a new EventQueue which, when started, will listen on
the given port for new
Connection s and begin filling with
Event s. |
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. |
Event |
pop()
Removes the first
Event from the queue and returns it. |
void |
start()
When this method is called, the EventQueue begins listening on
the specified port for new connections and begins filling with events.
|
void |
stop()
When this method is called, the EventQueue will stop listening
for new connections and close all currently active connections.
|
public final int port
public EventQueue(int port)
Connection
s and begin filling with
Event
s.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 final void start()
public final Event pop()
Event
from the queue and returns it. If the
queue is empty, this method will block until an event is added.Event
in the queuepublic final void stop()
pop
method will continue to return events after this
method is called until the queue is empty.