Package com.yahoo.io
Class Listener
- java.lang.Object
-
- java.lang.Thread
-
- com.yahoo.io.Listener
-
- All Implemented Interfaces:
java.lang.Runnable
public class Listener extends java.lang.Thread
A basic Reactor implementation using NIO.- Author:
- Bob Travis, Bjorn Borud
-
-
Constructor Summary
Constructors Constructor Description Listener(java.lang.String name)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Connection
addNewConnection(Connection newConn)
This method is used by the Acceptor to hand off newly accepted connections to the Listener.void
addSelectLoopPostHook(SelectLoopHook hook)
Add pre-select loop hook.void
addSelectLoopPreHook(SelectLoopHook hook)
Add pre-select loop hook.void
listen(ConnectionFactory factory, int port)
Add a listening port and create an Acceptor thread which accepts new connections on this port.void
listenNoAcceptor(ConnectionFactory factory, int port)
Add a listening port without creating a separate acceptor thread.Listener
modifyInterestOps(Connection connection, int op, boolean set)
This is the preferred way of modifying interest ops, giving a Connection rather than a SelectionKey as input.Listener
modifyInterestOps(java.nio.channels.SelectionKey key, int op, boolean set)
Enqueue change to interest set of SelectionKey.Listener
modifyInterestOpsBatch(Connection connection, int op, boolean set)
Batch version of modifyInterestOps().Listener
modifyInterestOpsBatch(java.nio.channels.SelectionKey key, int op, boolean set)
Does the same as modifyInterestOps(), but does not call wakeup on the selector.Listener
modifyInterestOpsDone()
Signal that a batch update of SelectionKey is done and the selector should be awoken.void
registerConnection(Connection connection)
Register a connection that was set up outside the listener.void
run()
Thread entry pointvoid
setFatalErrorHandler(FatalErrorHandler f)
Register a handler for fatal errors.void
shutdown()
Perform clean shutdown of Listener.-
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
-
-
-
Method Detail
-
setFatalErrorHandler
public void setFatalErrorHandler(FatalErrorHandler f)
Register a handler for fatal errors.- Parameters:
f
- The FatalErrorHandler instance to be registered
-
addSelectLoopPreHook
public void addSelectLoopPreHook(SelectLoopHook hook)
Add pre-select loop hook. Not threadsafe so please do this during initial setup before you start the listener.
-
addSelectLoopPostHook
public void addSelectLoopPostHook(SelectLoopHook hook)
Add pre-select loop hook. Not threadsafe so please do this during initial setup before you start the listener.
-
listen
public void listen(ConnectionFactory factory, int port) throws java.io.IOException
Add a listening port and create an Acceptor thread which accepts new connections on this port.- Parameters:
factory
- The connection factory for new connections on this portport
- The port we are going to listen to.- Throws:
java.io.IOException
-
listenNoAcceptor
public void listenNoAcceptor(ConnectionFactory factory, int port) throws java.io.IOException
Add a listening port without creating a separate acceptor thread.- Parameters:
factory
- The connection factory for new connections on this portport
- The port we are going to listen to.- Throws:
java.io.IOException
-
modifyInterestOps
public Listener modifyInterestOps(Connection connection, int op, boolean set)
This is the preferred way of modifying interest ops, giving a Connection rather than a SelectionKey as input. This way the we can look it up and ensure the correct SelectionKey is always used.- Returns:
- Returns a
this
reference for chaining
-
modifyInterestOpsBatch
public Listener modifyInterestOpsBatch(Connection connection, int op, boolean set)
Batch version of modifyInterestOps().- Returns:
- Returns a
this
reference for chaining
-
modifyInterestOps
public Listener modifyInterestOps(java.nio.channels.SelectionKey key, int op, boolean set)
Enqueue change to interest set of SelectionKey. This is a workaround for an NIO design error that makes it impossible to update interest sets for a SelectionKey while a select is in progress -- and sometimes you actually want to do this from other threads, which will then block. Hence, we make it possible to enqueue requests for SelectionKey modification in the thread where select runs.- Returns:
- Returns a
this
reference for chaining
-
modifyInterestOpsBatch
public Listener modifyInterestOpsBatch(java.nio.channels.SelectionKey key, int op, boolean set)
Does the same as modifyInterestOps(), but does not call wakeup on the selector. Allows adding more modifications before we wake up the selector.- Returns:
- Returns a
this
reference for chaining
-
modifyInterestOpsDone
public Listener modifyInterestOpsDone()
Signal that a batch update of SelectionKey is done and the selector should be awoken. Also see modifyInterestOps().- Returns:
- Returns a
this
reference for chaining
-
run
public void run()
Thread entry point- Specified by:
run
in interfacejava.lang.Runnable
- Overrides:
run
in classjava.lang.Thread
-
addNewConnection
public Connection addNewConnection(Connection newConn)
This method is used by the Acceptor to hand off newly accepted connections to the Listener. Note that this is run in the context of the Acceptor thread, so doing things here versus doing them in the acceptNewConnections(), which runs in the context of the Listener thread, is a tradeoff that may need to be re-evaluated
-
registerConnection
public void registerConnection(Connection connection)
Register a connection that was set up outside the listener. Typically what we do when we actively reach out and connect somewhere.
-
shutdown
public void shutdown()
Perform clean shutdown of Listener. TODO: implement
-
-