public abstract class MessageRouter
extends java.lang.Object
implements java.lang.Runnable
Modifier and Type | Field and Description |
---|---|
protected java.util.concurrent.ConcurrentHashMap<java.nio.channels.SelectionKey,java.lang.Integer> |
changeInterest |
static int |
DEFAULT_READ_BUFFER_SIZE
The default read buffer size is 8 MB.
|
static int |
DEFAULT_WRITE_QUEUE_SIZE
The default write queue allows 100 items to be inserted before it
starts blocking.
|
protected static java.util.logging.Logger |
logger |
protected boolean |
online
Flag used to determine whether the Selector thread should run
|
static int |
PREFIX_SZ
The size (in bytes) of the message prefix used in the system.
|
static java.lang.String |
READ_BUFFER_PROPERTY
System property that overrides the read buffer size.
|
protected int |
readBufferSize |
protected java.nio.channels.Selector |
selector |
static java.lang.String |
WRITE_QUEUE_PROPERTY
System property that overrides the write queue maximum size.
|
protected int |
writeQueueSize |
Constructor and Description |
---|
MessageRouter() |
MessageRouter(int readBufferSize,
int maxWriteQueueSize) |
Modifier and Type | Method and Description |
---|---|
protected void |
accept(java.nio.channels.SelectionKey key)
Accepts new connections.
|
void |
addListener(MessageListener listener)
Adds a message listener (consumer) to this MessageRouter.
|
protected void |
connect(java.nio.channels.SelectionKey key)
Finishes setting up a connection on a SocketChannel.
|
protected void |
disconnect(java.nio.channels.SelectionKey key)
Handle termination of connections.
|
protected void |
dispatchConnect(NetworkDestination endpoint)
Informs all listening consumers that a connection to a remote endpoint
has been made.
|
protected void |
dispatchDisconnect(NetworkDestination endpoint)
Informs all listening consumers that a connection to a remote endpoint
has been terminated.
|
protected void |
dispatchMessage(MendelMessage message)
Dispatches a message to all listening consumers.
|
protected static java.lang.String |
getClientString(java.nio.channels.SocketChannel channel)
Determines a connection's hostname and port, then concatenates the two
values, separated by a colon (:).
|
protected static NetworkDestination |
getDestination(java.nio.channels.SocketChannel channel)
Determines a connection's endpoint information (hostname and port) and
encapsulates them in a
NetworkDestination . |
boolean |
isOnline()
Determines whether or not this MessageRouter is online.
|
protected void |
processIncomingMessage(java.nio.channels.SelectionKey key)
Process data received from a client SocketChannel.
|
protected void |
processSelectionKeys()
Performs a select operation, and then processes the resulting
SelectionKey set based on interest ops.
|
protected void |
read(java.nio.channels.SelectionKey key)
Read data from a SocketChannel.
|
protected static boolean |
readPrefix(java.nio.ByteBuffer buffer,
mendel.network.TransmissionTracker transmission)
Read the payload size prefix from a channel.
|
void |
run()
As long as the MessageRouter is online, monitor connection operations
through the Selector instance.
|
Transmission |
sendMessage(java.nio.channels.SelectionKey key,
MendelMessage message)
Adds a message to the pending write queue for a particular SelectionKey
and submits a change request for its interest set.
|
protected void |
updateInterestOps()
Updates interest sets for any SelectionKey instances that require
changes.
|
protected static java.nio.ByteBuffer |
wrapWithPrefix(MendelMessage message)
Wraps a given message in a
ByteBuffer , including the payload size
prefix. |
protected static final java.util.logging.Logger logger
public static final int PREFIX_SZ
public static final int DEFAULT_READ_BUFFER_SIZE
public static final int DEFAULT_WRITE_QUEUE_SIZE
public static final java.lang.String READ_BUFFER_PROPERTY
public static final java.lang.String WRITE_QUEUE_PROPERTY
protected boolean online
protected java.nio.channels.Selector selector
protected int readBufferSize
protected int writeQueueSize
protected java.util.concurrent.ConcurrentHashMap<java.nio.channels.SelectionKey,java.lang.Integer> changeInterest
public MessageRouter()
public MessageRouter(int readBufferSize, int maxWriteQueueSize)
public void run()
run
in interface java.lang.Runnable
protected void updateInterestOps()
protected void processSelectionKeys() throws java.io.IOException
java.io.IOException
protected void accept(java.nio.channels.SelectionKey key) throws java.io.IOException
key
- The SelectionKey for the connecting client.java.io.IOException
protected void connect(java.nio.channels.SelectionKey key)
key
- SelectionKey for the SocketChannel.protected void read(java.nio.channels.SelectionKey key)
key
- SelectionKey for the SocketChannel.protected void processIncomingMessage(java.nio.channels.SelectionKey key)
key
- SelectionKey for the client.protected static boolean readPrefix(java.nio.ByteBuffer buffer, mendel.network.TransmissionTracker transmission)
protected static java.nio.ByteBuffer wrapWithPrefix(MendelMessage message)
ByteBuffer
, including the payload size
prefix. Data produced by this method will be subsequently read by the
readPrefix() method.public Transmission sendMessage(java.nio.channels.SelectionKey key, MendelMessage message) throws java.io.IOException
key
- SelectionKey for the channel.message
- MendelMessage to publish on the channel.Transmission
instance representing the send operation.java.io.IOException
protected void disconnect(java.nio.channels.SelectionKey key)
key
- The SelectionKey of the SocketChannel that has disconnected.public void addListener(MessageListener listener)
listener
- MessageListener
that will consume messages
published by this MessageRouter.protected void dispatchMessage(MendelMessage message)
message
- MendelMessage
to dispatch.protected void dispatchConnect(NetworkDestination endpoint)
protected void dispatchDisconnect(NetworkDestination endpoint)
public boolean isOnline()
protected static java.lang.String getClientString(java.nio.channels.SocketChannel channel)
channel
- Channel to get client information about.protected static NetworkDestination getDestination(java.nio.channels.SocketChannel channel)
NetworkDestination
.channel
- The SocketChannel of the network endpoint.