public interface Reactor
BaseHandler
and only handle the events that it cares to
implement methods for.
This class is not thread safe (with the exception of the wakeup()
method) and should only be used by a single thread at any given time.
Modifier and Type | Interface and Description |
---|---|
static class |
Reactor.Factory |
Modifier and Type | Method and Description |
---|---|
Acceptor |
acceptor(java.lang.String host,
int port)
Creates a new acceptor.
|
Acceptor |
acceptor(java.lang.String host,
int port,
Handler handler)
Creates a new acceptor.
|
Record |
attachments() |
java.util.Set<ReactorChild> |
children() |
Collector |
collector() |
Connection |
connection(Handler handler)
Deprecated.
Use
connectionToHost(String, int, Handler) instead. |
Connection |
connectionToHost(java.lang.String host,
int port,
Handler handler)
Creates a new out-bound connection to the given host and port.
|
void |
free()
Frees any resources (such as sockets and selectors) held by the reactor
or its children.
|
java.lang.String |
getConnectionAddress(Connection c)
Get the address used by the connection
|
Handler |
getGlobalHandler() |
Handler |
getHandler() |
ReactorOptions |
getOptions()
Gets the reactor options.
|
long |
getTimeout() |
long |
mark()
Updates the last time that the reactor's state has changed, potentially
resulting in events being generated.
|
long |
now() |
boolean |
process()
Process any events pending for this reactor.
|
boolean |
quiesced() |
void |
run()
Simplifies the use of the reactor by wrapping the use of
start , run , and stop method
calls. |
Task |
schedule(int delay,
Handler handler)
Schedules execution of a task to take place at some point in the future.
|
Selectable |
selectable()
Creates a new
Selectable as a child of this reactor. |
void |
setConnectionHost(Connection c,
java.lang.String host,
int port)
Set the host address used by the connection
|
void |
setGlobalHandler(Handler handler)
Sets a new global handler.
|
void |
setHandler(Handler handler)
Sets a new handler, that will receive any events not handled by a child
of the reactor.
|
void |
setTimeout(long timeout)
The value the reactor will use for
Selector.select(long) that is called as part of process() . |
void |
start()
Starts the reactor.
|
void |
stop()
Stops the reactor.
|
void |
update(Selectable selectable)
Updates the specified
Selectable either emitting a
Event.Type.SELECTABLE_UPDATED event if the selectable is not terminal,
or Event.Type.SELECTABLE_FINAL if the selectable is terminal and has
not already emitted a Event.Type.SELECTABLE_FINAL event. |
void |
wakeup()
Wakes up the thread (if any) blocked in the
process() method. |
void |
yield()
Yields, causing the next call to
process() to return
successfully - without processing any events. |
long mark()
System.currentTimeMillis()
.long now()
mark()
was called.Record attachments()
Record
that can be used to associate
other objects (attachments) with this instance of the
Reactor class.void setTimeout(long timeout)
Selector.select(long)
that is called as part of process()
.timeout
- a timeout value in milliseconds, to associate with this instance of
the reactor. This can be retrieved using the
getTimeout()
methodlong getTimeout()
setTimeout(long)
or
0 if no previous value has been set.Handler getGlobalHandler()
getGlobalHandler().add(yourHandler);
void setGlobalHandler(Handler handler)
getGlobalHandler()
method.handler
- the new global handler.Handler getHandler()
getHandler().add(yourHandler);
void setHandler(Handler handler)
getHandler()
method.handler
- the new handler for this reactor.java.util.Set<ReactorChild> children()
Task
-
created using the schedule(int, Handler)
method,
Connection
- created using the
connectionToHost(String, int, Handler)
method,
Acceptor
- created using the
acceptor(String, int)
method,
acceptor(String, int, Handler)
method, or
Selectable
- created using the
selectable()
method.Collector collector()
Selectable selectable()
Selectable
as a child of this reactor.Selectable
.void update(Selectable selectable)
Selectable
either emitting a
Event.Type.SELECTABLE_UPDATED
event if the selectable is not terminal,
or Event.Type.SELECTABLE_FINAL
if the selectable is terminal and has
not already emitted a Event.Type.SELECTABLE_FINAL
event.selectable
- void yield()
boolean quiesced()
true
if the reactor is in quiesced state (e.g. has
no events to process). false
is returned otherwise.boolean process() throws HandlerException
true
if the reactor may have more events in the
future. For example: if there are scheduled tasks, or open
selectors. false
is returned if the reactor has
(and will have) no more events to process.HandlerException
- if an unchecked exception is thrown by one of
the handlers - it will be re-thrown attached to an instance of
HandlerException
.void wakeup()
process()
method.
This is the only method of this class that is thread safe, in that it
can be used at the same time as another thread is using the reactor.void start()
process()
.void stop() throws HandlerException
process()
.HandlerException
void run() throws HandlerException
start
, run
, and stop
method
calls.
Logically the implementation of this method is:
start(); while(process()) {} stop();
HandlerException
- if an unchecked exception is thrown by one of
the handlers - it will be re-thrown attached to an instance of
HandlerException
.Task schedule(int delay, Handler handler)
delay
- the number of milliseconds, in the future, to schedule the
task for.handler
- a handler to associate with the task. This is notified
when the deadline for the task is reached.@Deprecated Connection connection(Handler handler)
connectionToHost(String, int, Handler)
instead.handler
- a handler that is notified when events occur for the
connection. Typically the host and port to connect to
would be supplied to the connection object inside the
logic which handles the Event.Type.CONNECTION_INIT
event via
setConnectionHost(Connection, String, int)
Connection connectionToHost(java.lang.String host, int port, Handler handler)
This method will cause Reactor to set up a network connection to the host and create a Connection for it.
host
- the host to connect to (e.g. "localhost")port
- the port used for the connection.handler
- a handler that is notified when events occur for the
connection.void setConnectionHost(Connection c, java.lang.String host, int port)
This method will set/change the host address used by the Reactor to create an outbound network connection for the given Connection
c
- the Connection to assign the address tohost
- the address of the host to connect to (e.g. "localhost")port
- the port to use for the connection.ReactorOptions getOptions()
java.lang.String getConnectionAddress(Connection c)
This may be used to retrieve the remote peer address. Note that the returned address may be in numeric IP format.
c
- the Connectionhost[:port]
Acceptor acceptor(java.lang.String host, int port) throws java.io.IOException
acceptor(host, port, null);
host
- port
- java.io.IOException
Acceptor acceptor(java.lang.String host, int port, Handler handler) throws java.io.IOException
host
- the host name or address of the NIC to listen on.port
- the port number to listen on.handler
- if non-null
this handler is registered with
each new connection accepted by the acceptor.java.io.IOException
void free()