public class ReactorImpl extends java.lang.Object implements Reactor, Extendable
Reactor.Factory
Modifier and Type | Field and Description |
---|---|
protected static java.lang.String |
CONNECTION_PEER_ADDRESS_KEY |
static ExtendableAccessor<Event,Handler> |
ROOT |
Modifier | Constructor and Description |
---|---|
|
ReactorImpl() |
protected |
ReactorImpl(IO io) |
protected |
ReactorImpl(IO io,
ReactorOptions options) |
|
ReactorImpl(ReactorOptions options) |
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)
Creates a new out-bound connection.
|
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 connection)
Get the address used by the connection
|
Handler |
getGlobalHandler() |
Handler |
getHandler() |
IO |
getIO() |
ReactorOptions |
getOptions()
Gets the reactor options.
|
protected Selector |
getSelector() |
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. |
SelectableImpl |
selectable(ReactorChild child) |
void |
setConnectionHost(Connection connection,
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.
|
protected void |
setSelector(Selector selector) |
void |
setTimeout(long timeout)
The value the reactor will use for
Selector.select(long) that is called as part of Reactor.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
Reactor.process() method. |
void |
yield()
Yields, causing the next call to
Reactor.process() to return
successfully - without processing any events. |
public static final ExtendableAccessor<Event,Handler> ROOT
protected static final java.lang.String CONNECTION_PEER_ADDRESS_KEY
protected ReactorImpl(IO io) throws java.io.IOException
java.io.IOException
protected ReactorImpl(IO io, ReactorOptions options) throws java.io.IOException
java.io.IOException
public ReactorImpl() throws java.io.IOException
java.io.IOException
public ReactorImpl(ReactorOptions options) throws java.io.IOException
java.io.IOException
public long mark()
Reactor
public long now()
now
in interface Reactor
Reactor.mark()
was called.public void free()
Reactor
public Record attachments()
attachments
in interface Extendable
attachments
in interface Reactor
Record
that can be used to associate
other objects (attachments) with this instance of the
Reactor class.public ReactorOptions getOptions()
Reactor
getOptions
in interface Reactor
public long getTimeout()
getTimeout
in interface Reactor
Reactor.setTimeout(long)
or
0 if no previous value has been set.public void setTimeout(long timeout)
Reactor
Selector.select(long)
that is called as part of Reactor.process()
.setTimeout
in interface Reactor
timeout
- a timeout value in milliseconds, to associate with this instance of
the reactor. This can be retrieved using the
Reactor.getTimeout()
methodpublic Handler getGlobalHandler()
getGlobalHandler
in interface Reactor
getGlobalHandler().add(yourHandler);
public void setGlobalHandler(Handler handler)
Reactor
Reactor.getGlobalHandler()
method.setGlobalHandler
in interface Reactor
handler
- the new global handler.public Handler getHandler()
getHandler
in interface Reactor
getHandler().add(yourHandler);
public void setHandler(Handler handler)
Reactor
Reactor.getHandler()
method.setHandler
in interface Reactor
handler
- the new handler for this reactor.public java.util.Set<ReactorChild> children()
children
in interface Reactor
Task
-
created using the Reactor.schedule(int, Handler)
method,
Connection
- created using the
Reactor.connectionToHost(String, int, Handler)
method,
Acceptor
- created using the
Reactor.acceptor(String, int)
method,
Reactor.acceptor(String, int, Handler)
method, or
Selectable
- created using the
Reactor.selectable()
method.public Collector collector()
public Selectable selectable()
Reactor
Selectable
as a child of this reactor.selectable
in interface Reactor
Selectable
.public SelectableImpl selectable(ReactorChild child)
public void update(Selectable selectable)
Reactor
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.public void yield()
Reactor
Reactor.process()
to return
successfully - without processing any events. If multiple calls
can be made to yield
and only the next invocation of
Reactor.process()
will be affected.public boolean quiesced()
public boolean process() throws HandlerException
Reactor
process
in interface Reactor
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
.public void wakeup()
Reactor
Reactor.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.public void start()
Reactor
Reactor.process()
.public void stop() throws HandlerException
Reactor
Reactor.process()
.stop
in interface Reactor
HandlerException
public void run() throws HandlerException
Reactor
start
, run
, and stop
method
calls.
Logically the implementation of this method is:
start(); while(process()) {} stop();
run
in interface Reactor
HandlerException
- if an unchecked exception is thrown by one of
the handlers - it will be re-thrown attached to an instance of
HandlerException
.public Task schedule(int delay, Handler handler)
Reactor
schedule
in interface Reactor
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.protected Selector getSelector()
protected void setSelector(Selector selector)
public Connection connection(Handler handler)
Reactor
connection
in interface Reactor
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
Reactor.setConnectionHost(Connection, String, int)
public Connection connectionToHost(java.lang.String host, int port, Handler handler)
Reactor
This method will cause Reactor to set up a network connection to the host and create a Connection for it.
connectionToHost
in interface Reactor
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.public java.lang.String getConnectionAddress(Connection connection)
Reactor
This may be used to retrieve the remote peer address. Note that the returned address may be in numeric IP format.
getConnectionAddress
in interface Reactor
connection
- the Connectionhost[:port]
public void setConnectionHost(Connection connection, java.lang.String host, int port)
Reactor
This method will set/change the host address used by the Reactor to create an outbound network connection for the given Connection
setConnectionHost
in interface Reactor
connection
- 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.public Acceptor acceptor(java.lang.String host, int port) throws java.io.IOException
Reactor
acceptor(host, port, null);
public Acceptor acceptor(java.lang.String host, int port, Handler handler) throws java.io.IOException
Reactor
acceptor
in interface Reactor
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
public IO getIO()