Class ChannelNetwork
- All Implemented Interfaces:
Network
- Direct Known Subclasses:
TCPNetwork
Network implementations based on SelectableChannels.
Uses Java NIO.-
Nested Class Summary
Nested classes/interfaces inherited from class org.dellroad.stuff.net.SelectorSupport
SelectorSupport.IOHandlerNested classes/interfaces inherited from interface org.dellroad.stuff.net.Network
Network.Handler -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final HashMap<String,ChannelConnection> static final intDefault maximum number of simultaneous connections (1000).static final longDefault idle connection timeout (30000L milliseconds).static final longDefault maximum allowed size of a connection's incoming queue before we start dropping messages (134217728L bytes).static final intDefault maximum allowed size of an incoming message (33554432 bytes).static final longDefault maximum allowed size of a connection's outgoing queue before we start dropping messages (67108864L bytes).static final intDefault minimum buffer size to use a direct buffer.protected final LoggerFields inherited from class org.dellroad.stuff.net.SelectorSupport
closureTrackables, DEFAULT_HOUSEKEEPING_INTERVAL, provider -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract ChannelConnectioncreateConnection(String peer) Create a new connection to the specified peer.intGet the maximum number of allowed connections.longGet the maximum idle time for connections before automatically closing them down.longGet the maximum allowed size of the queue for incoming messages.intGet the maximum allowed length for incoming messages.longGet the maximum allowed size of the queue for outgoing messages.intGet the minimum incoming message size for which we should allocate a directByteBuffer.Get the name of the service thread.protected StringnormalizePeerName(String peer) Normalize the given peer.booleansend(String peer, ByteBuffer msg) Send (or enqueue for sending) a message to a remote peer.protected voidPerform shutdown cleanups.protected voidPerform housekeeping.voidsetMaxConnections(int maxConnections) voidsetMaxIdleTime(long maxIdleTime) voidsetMaxInputQueueSize(long maxInputQueueSize) voidsetMaxMessageSize(int maxMessageSize) voidsetMaxOutputQueueSize(long maxOutputQueueSize) voidsetMinDirectBufferSize(int minDirectBufferSize) Set the minimum incoming message size for which we should allocate a directByteBuffer.voidsetServiceThreadName(String serviceThreadName) voidstart(Network.Handler handler) Start this instance.voidstop()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.Handlerhas 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
ByteBufferminimum 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:NetworkStart this instance.- Specified by:
startin interfaceNetwork- Parameters:
handler- handler for notifications- Throws:
IOException- if an error occurs
-
stop
public void stop()Description copied from class:SelectorSupportStop this instance.Does nothing if already stopped.
- Specified by:
stopin interfaceNetwork- Overrides:
stopin classSelectorSupport
-
send
Description copied from interface:NetworkSend (or enqueue for sending) a message to a remote peer.If this method returns true, then
Handler.outputQueueEmpty()is guaranteed to be invoked with parameterpeerat some later point. -
normalizePeerName
Normalize the given peer.The implementation in
ChannelNetworkjust 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- ifpeeris an invalid peer nameIllegalArgumentException- ifpeeris 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:SelectorSupportPerform 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
SelectorSupportdoes nothing.- Overrides:
serviceHousekeepingin classSelectorSupport
-
serviceCleanup
protected void serviceCleanup()Description copied from class:SelectorSupportPerform 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
SelectorSupportdoes nothing.- Overrides:
serviceCleanupin classSelectorSupport
-