Class TCPNetwork

All Implemented Interfaces:
Network

public class TCPNetwork extends ChannelNetwork
Network implementation based on TCP connections.

Remote peers have the String form IP-Address[:port]. If the port is omitted, the default port provided to the constructor is assumed.

Individual TCP connections are created and maintained automatically as needed.

Note: if each peer's IP address is unique, consider overriding identifyPeer() to remove the port. This will prevent the creation of duplicate connections between peers.

  • Field Details

    • DEFAULT_CONNECT_TIMEOUT

      public static final long DEFAULT_CONNECT_TIMEOUT
      Default connect timeout for outgoing connections (20000L milliseconds).
      See Also:
  • Constructor Details

    • TCPNetwork

      public TCPNetwork(int defaultPort)
      Constructor.
      Parameters:
      defaultPort - default TCP port when no port is explicitly proviced
      Throws:
      IllegalArgumentException - if port is invalid
  • Method Details

    • getListenAddress

      public InetSocketAddress getListenAddress()
      Get the InetSocketAddress to which this instance is bound or will bind.
      Returns:
      listen address, possibly null for default behavior
    • setListenAddress

      public void setListenAddress(InetSocketAddress address)
      Configure the InetSocketAddress to which this instance should bind.

      If this instance is already started, invoking this method will have no effect until it is stopped and restarted.

      By default, instances listen on all interfaces on the defaul port configured in the constructor.

      Parameters:
      address - listen address, or null to listen on all interfaces on the default port provided to the constructor
      Throws:
      IllegalArgumentException - if address is null
    • getConnectTimeout

      public long getConnectTimeout()
      Get the outgoing connection timeout. Default is 20000Lms.
      Returns:
      outgoing connection timeout in milliseconds
    • setConnectTimeout

      public void setConnectTimeout(long connectTimeout)
    • start

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

      public static String parseAddressPart(String address)
      Parse out the address part of an address that has an optional colon plus TCP port number suffix.
      Parameters:
      address - address of the form ipaddr or ipaddr:port
      Returns:
      the IP address part
    • parsePortPart

      public static int parsePortPart(String address, int defaultPort)
      Parse out the port part of an address that has an optional colon plus TCP port number suffix.
      Parameters:
      address - address of the form ipaddr or ipaddr:port
      defaultPort - default port if none specified in address
      Returns:
      the port part, or defaultPort if there is no explicit port
    • configureServerSocketChannel

      protected void configureServerSocketChannel(ServerSocketChannel serverSocketChannel)
      Configure the ServerSocketChannel to be used by this instance. This method is invoked by start(org.dellroad.stuff.net.Network.Handler).

      The implementation in TCPNetwork does nothing. Subclasses may override to configure socket options, etc.

      Parameters:
      serverSocketChannel - channel to configure
    • configureSocketChannel

      protected void configureSocketChannel(SocketChannel socketChannel)
      Configure a new SocketChannel to be used by this instance. This method is invoked when new connections are created.

      The implementation in TCPNetwork does nothing. Subclasses may override to configure socket options, etc.

      Parameters:
      socketChannel - channel to configure
    • identifyPeer

      protected String identifyPeer(InetSocketAddress remote)
      Identify the peer behind an incoming connection.

      The implementation in TCPNetwork returns the the remote IP address, followed by a colon and the remote port if that port differs from the default.

      Note that the default behavior does not identify a peer simply by its IP address. Since TCP connections typically originate from randomly chosen ports, every connection from the same host will appear as a new peer. As a result, two hosts configured to talk to each other will end up creating two TCP connections, one in each direction.

      To resolve that problem, and identify a peer by its IP address alone, override this method and just return remote.getHostString().

      Parameters:
      remote - remote address of incoming connection
      Returns:
      associated peer name, or null if peer is not recognized (socket will be closed)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • normalizePeerName

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

      The implementation in TCPNetwork converts peer to lower case and appends the (default) port if not specified in the string.

      Overrides:
      normalizePeerName in class ChannelNetwork
      Parameters:
      peer - remote peer
      Returns:
      normalized form for peer
      Throws:
      IllegalArgumentException - inheritDoc
    • createConnection

      protected TCPConnection createConnection(String peer) throws IOException
      Description copied from class: ChannelNetwork
      Create a new connection to the specified peer.
      Specified by:
      createConnection in class ChannelNetwork
      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 ChannelNetwork