Class ChannelNetwork

java.lang.Object
org.dellroad.stuff.net.SelectorSupport
org.dellroad.stuff.net.ChannelNetwork
All Implemented Interfaces:
Network
Direct Known Subclasses:
TCPNetwork

public abstract class ChannelNetwork extends SelectorSupport implements Network
Support superclass for Network implementations based on SelectableChannels. Uses Java NIO.
  • Field Details

  • 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 the Network.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

      public String 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

      public void setServiceThreadName(String serviceThreadName)
    • getMinDirectBufferSize

      public int getMinDirectBufferSize()
      Get the minimum incoming message size for which we should allocate a direct ByteBuffer.
      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 direct ByteBuffer.
      Parameters:
      minDirectBufferSize - direct buffer size lower limit
      See Also:
    • start

      public void start(Network.Handler handler) throws IOException
      Description copied from interface: Network
      Start this instance.
      Specified by:
      start in interface Network
      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 interface Network
      Overrides:
      stop in class SelectorSupport
    • send

      public boolean send(String peer, ByteBuffer msg)
      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 parameter peer at some later point.

      Specified by:
      send in interface Network
      Parameters:
      peer - message destination
      msg - message to send
      Returns:
      true if message was succesfully enqueued for output; false if message failed to be delivered due to local reasons, such as failure to initiate a new connection or output queue overflow
    • normalizePeerName

      protected String normalizePeerName(String peer)
      Normalize the given peer.

      The implementation in ChannelNetwork just returns peer. 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 - if peer is an invalid peer name
      IllegalArgumentException - if peer is null
    • createConnection

      protected abstract ChannelConnection createConnection(String peer) throws IOException
      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 class SelectorSupport
    • 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 an IllegalStateException.

      The implementation in SelectorSupport does nothing.

      Overrides:
      serviceCleanup in class SelectorSupport