Class ChannelNetwork
- All Implemented Interfaces:
Network
- Direct Known Subclasses:
TCPNetwork
Network
implementations based on SelectableChannel
s.
Uses Java NIO.-
Nested Class Summary
Nested classes/interfaces inherited from class org.dellroad.stuff.net.SelectorSupport
SelectorSupport.IOHandler
Nested classes/interfaces inherited from interface org.dellroad.stuff.net.Network
Network.Handler
-
Field Summary
Modifier and TypeFieldDescriptionprotected final HashMap<String,
ChannelConnection> static final int
Default maximum number of simultaneous connections (1000).static final long
Default idle connection timeout (30000L milliseconds).static final long
Default maximum allowed size of a connection's incoming queue before we start dropping messages (134217728L bytes).static final int
Default maximum allowed size of an incoming message (33554432 bytes).static final long
Default maximum allowed size of a connection's outgoing queue before we start dropping messages (67108864L bytes).static final int
Default minimum buffer size to use a direct buffer.protected final Logger
Fields inherited from class org.dellroad.stuff.net.SelectorSupport
closureTrackables, DEFAULT_HOUSEKEEPING_INTERVAL, provider
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected abstract ChannelConnection
createConnection
(String peer) Create a new connection to the specified peer.int
Get the maximum number of allowed connections.long
Get the maximum idle time for connections before automatically closing them down.long
Get the maximum allowed size of the queue for incoming messages.int
Get the maximum allowed length for incoming messages.long
Get the maximum allowed size of the queue for outgoing messages.int
Get the minimum incoming message size for which we should allocate a directByteBuffer
.Get the name of the service thread.protected String
normalizePeerName
(String peer) Normalize the given peer.boolean
send
(String peer, ByteBuffer msg) Send (or enqueue for sending) a message to a remote peer.protected void
Perform shutdown cleanups.protected void
Perform housekeeping.void
setMaxConnections
(int maxConnections) void
setMaxIdleTime
(long maxIdleTime) void
setMaxInputQueueSize
(long maxInputQueueSize) void
setMaxMessageSize
(int maxMessageSize) void
setMaxOutputQueueSize
(long maxOutputQueueSize) void
setMinDirectBufferSize
(int minDirectBufferSize) Set the minimum incoming message size for which we should allocate a directByteBuffer
.void
setServiceThreadName
(String serviceThreadName) void
start
(Network.Handler handler) Start this instance.void
stop()
Stop this instance.Methods inherited from class org.dellroad.stuff.net.SelectorSupport
createSelectionKey, createSelectionKey, dbg, dbg, dbgOps, isRunning, isServiceThread, selectFor, setHousekeepingInterval, start, wakeup
-
Field Details
-
DEFAULT_MAX_CONNECTIONS
public static final int DEFAULT_MAX_CONNECTIONSDefault maximum number of simultaneous connections (1000).- See Also:
-
DEFAULT_MAX_IDLE_TIME
public static final long DEFAULT_MAX_IDLE_TIMEDefault idle connection timeout (30000L milliseconds).- See Also:
-
DEFAULT_MAX_MESSAGE_SIZE
public static final int DEFAULT_MAX_MESSAGE_SIZEDefault maximum allowed size of an incoming message (33554432 bytes).- See Also:
-
DEFAULT_MAX_OUTPUT_QUEUE_SIZE
public static final long DEFAULT_MAX_OUTPUT_QUEUE_SIZEDefault maximum allowed size of a connection's outgoing queue before we start dropping messages (67108864L bytes).- See Also:
-
DEFAULT_MAX_INPUT_QUEUE_SIZE
public static final long DEFAULT_MAX_INPUT_QUEUE_SIZEDefault maximum allowed size of a connection's incoming queue before we start dropping messages (134217728L bytes).- See Also:
-
DEFAULT_MIN_DIRECT_BUFFER_SIZE
public static final int DEFAULT_MIN_DIRECT_BUFFER_SIZEDefault minimum buffer size to use a direct buffer. -
log
-
connectionMap
-
-
Constructor Details
-
ChannelNetwork
public ChannelNetwork()
-
-
Method Details
-
getMaxConnections
public int getMaxConnections()Get the maximum number of allowed connections. Default is 1000.- Returns:
- max allowed connections
-
setMaxConnections
public void setMaxConnections(int maxConnections) -
getMaxIdleTime
public long getMaxIdleTime()Get the maximum idle time for connections before automatically closing them down. Default is 30000Lms.- Returns:
- max connection idle time in milliseconds
-
setMaxIdleTime
public void setMaxIdleTime(long maxIdleTime) -
getMaxMessageSize
public int getMaxMessageSize()Get the maximum allowed length for incoming messages. Default is 33554432 bytes.- Returns:
- max allowed incoming message length in bytes
-
setMaxMessageSize
public void setMaxMessageSize(int maxMessageSize) -
getMaxOutputQueueSize
public long getMaxOutputQueueSize()Get the maximum allowed size of the queue for outgoing messages. Default is 67108864L bytes.- Returns:
- max allowed outgoing message queue length in bytes
-
setMaxOutputQueueSize
public void setMaxOutputQueueSize(long maxOutputQueueSize) -
getMaxInputQueueSize
public long getMaxInputQueueSize()Get the maximum allowed size of the queue for incoming messages. Default is 134217728L bytes.Messages are considered in the "input queue" if they have been received, but not yet handled by the
Network.Handler
, because theNetwork.Handler
has not finished handling one or more earlier messages.- Returns:
- max allowed incomign message queue length in bytes
-
setMaxInputQueueSize
public void setMaxInputQueueSize(long maxInputQueueSize) -
getServiceThreadName
Get the name of the service thread.By default this is null, meaning no special name is used. In that case the name will look something like
pool-2-thread-1
.- Returns:
- custom service thread name, or null for none
-
setServiceThreadName
-
getMinDirectBufferSize
public int getMinDirectBufferSize()Get the minimum incoming message size for which we should allocate a directByteBuffer
.- Returns:
- direct
ByteBuffer
minimum size - See Also:
-
setMinDirectBufferSize
public void setMinDirectBufferSize(int minDirectBufferSize) Set the minimum incoming message size for which we should allocate a directByteBuffer
.- Parameters:
minDirectBufferSize
- direct buffer size lower limit- See Also:
-
start
Description copied from interface:Network
Start this instance.- Specified by:
start
in interfaceNetwork
- Parameters:
handler
- handler for notifications- Throws:
IOException
- if an error occurs
-
stop
public void stop()Description copied from class:SelectorSupport
Stop this instance.Does nothing if already stopped.
- Specified by:
stop
in interfaceNetwork
- Overrides:
stop
in classSelectorSupport
-
send
Description copied from interface:Network
Send (or enqueue for sending) a message to a remote peer.If this method returns true, then
Handler.outputQueueEmpty()
is guaranteed to be invoked with parameterpeer
at some later point. -
normalizePeerName
Normalize the given peer.The implementation in
ChannelNetwork
just returnspeer
. Subclasses should override this method as needed to ensure that two different strings that refer to the same destination have the same normalized form.- Parameters:
peer
- remote peer- Returns:
- normalized form for
peer
- Throws:
IllegalArgumentException
- ifpeer
is an invalid peer nameIllegalArgumentException
- ifpeer
is null
-
createConnection
Create a new connection to the specified peer.- Parameters:
peer
- remote peer- Returns:
- new connection to
peer
- Throws:
IOException
- if an I/O error occurs
-
serviceHousekeeping
protected void serviceHousekeeping()Description copied from class:SelectorSupport
Perform housekeeping.This method is invoked from the internal service thread; this instance will be locked at that time.
This method is invoked after every I/O service (while still holding this instance's lock), and periodically at least every housekeeping interval, if enabled. If this method is invoked, it is guaranteed that this instance is not being
SelectorSupport.stop()
'ed.Any unchecked exceptions thrown by this method are logged but otherwise ignored. If a fatal error occurs,
SelectorSupport.stop()
may be invoked to initiate a graceful shutdown.Use
SelectorSupport.wakeup()
to trigger an immediate invocation of this method.The implementation in
SelectorSupport
does nothing.- Overrides:
serviceHousekeeping
in classSelectorSupport
-
serviceCleanup
protected void serviceCleanup()Description copied from class:SelectorSupport
Perform shutdown cleanups.This method is invoked at shutdown from the internal service thread; this instance will be locked at that time.
Note: ay invocation from this method to
createSelectionKey()
will result in anIllegalStateException
.The implementation in
SelectorSupport
does nothing.- Overrides:
serviceCleanup
in classSelectorSupport
-