Class TCPNetwork
- All Implemented Interfaces:
Network
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.
-
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 TypeFieldDescriptionstatic final long
Default connect timeout for outgoing connections (20000L milliseconds).Fields inherited from class org.dellroad.stuff.net.ChannelNetwork
connectionMap, DEFAULT_MAX_CONNECTIONS, DEFAULT_MAX_IDLE_TIME, DEFAULT_MAX_INPUT_QUEUE_SIZE, DEFAULT_MAX_MESSAGE_SIZE, DEFAULT_MAX_OUTPUT_QUEUE_SIZE, DEFAULT_MIN_DIRECT_BUFFER_SIZE, log
Fields inherited from class org.dellroad.stuff.net.SelectorSupport
closureTrackables, DEFAULT_HOUSEKEEPING_INTERVAL, provider
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
configureServerSocketChannel
(ServerSocketChannel serverSocketChannel) Configure theServerSocketChannel
to be used by this instance.protected void
configureSocketChannel
(SocketChannel socketChannel) Configure a newSocketChannel
to be used by this instance.protected TCPConnection
createConnection
(String peer) Create a new connection to the specified peer.long
Get the outgoing connection timeout.Get theInetSocketAddress
to which this instance is bound or will bind.protected String
identifyPeer
(InetSocketAddress remote) Identify the peer behind an incoming connection.protected String
normalizePeerName
(String peer) Normalize the given peer.static String
parseAddressPart
(String address) Parse out the address part of an address that has an optional colon plus TCP port number suffix.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.protected void
Perform housekeeping.void
setConnectTimeout
(long connectTimeout) void
setListenAddress
(InetSocketAddress address) Configure theInetSocketAddress
to which this instance should bind.void
start
(Network.Handler handler) Start this instance.void
stop()
Stop this instance.toString()
Methods inherited from class org.dellroad.stuff.net.ChannelNetwork
getMaxConnections, getMaxIdleTime, getMaxInputQueueSize, getMaxMessageSize, getMaxOutputQueueSize, getMinDirectBufferSize, getServiceThreadName, send, serviceCleanup, setMaxConnections, setMaxIdleTime, setMaxInputQueueSize, setMaxMessageSize, setMaxOutputQueueSize, setMinDirectBufferSize, setServiceThreadName
Methods inherited from class org.dellroad.stuff.net.SelectorSupport
createSelectionKey, createSelectionKey, dbg, dbg, dbgOps, isRunning, isServiceThread, selectFor, setHousekeepingInterval, start, wakeup
-
Field Details
-
DEFAULT_CONNECT_TIMEOUT
public static final long DEFAULT_CONNECT_TIMEOUTDefault 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
- ifport
is invalid
-
-
Method Details
-
getListenAddress
Get theInetSocketAddress
to which this instance is bound or will bind.- Returns:
- listen address, possibly null for default behavior
-
setListenAddress
Configure theInetSocketAddress
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
- ifaddress
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
Description copied from interface:Network
Start this instance.- Specified by:
start
in interfaceNetwork
- Overrides:
start
in classChannelNetwork
- 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 classChannelNetwork
-
parseAddressPart
Parse out the address part of an address that has an optional colon plus TCP port number suffix.- Parameters:
address
- address of the formipaddr
oripaddr:port
- Returns:
- the IP address part
-
parsePortPart
Parse out the port part of an address that has an optional colon plus TCP port number suffix.- Parameters:
address
- address of the formipaddr
oripaddr:port
defaultPort
- default port if none specified inaddress
- Returns:
- the port part, or
defaultPort
if there is no explicit port
-
configureServerSocketChannel
Configure theServerSocketChannel
to be used by this instance. This method is invoked bystart(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
Configure a newSocketChannel
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
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
-
normalizePeerName
Normalize the given peer.The implementation in
TCPNetwork
convertspeer
to lower case and appends the (default) port if not specified in the string.- Overrides:
normalizePeerName
in classChannelNetwork
- Parameters:
peer
- remote peer- Returns:
- normalized form for
peer
- Throws:
IllegalArgumentException
-inheritDoc
-
createConnection
Description copied from class:ChannelNetwork
Create a new connection to the specified peer.- Specified by:
createConnection
in classChannelNetwork
- 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 classChannelNetwork
-