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.IOHandlerNested classes/interfaces inherited from interface org.dellroad.stuff.net.Network
Network.Handler -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longDefault 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, logFields inherited from class org.dellroad.stuff.net.SelectorSupport
closureTrackables, DEFAULT_HOUSEKEEPING_INTERVAL, provider -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidconfigureServerSocketChannel(ServerSocketChannel serverSocketChannel) Configure theServerSocketChannelto be used by this instance.protected voidconfigureSocketChannel(SocketChannel socketChannel) Configure a newSocketChannelto be used by this instance.protected TCPConnectioncreateConnection(String peer) Create a new connection to the specified peer.longGet the outgoing connection timeout.Get theInetSocketAddressto which this instance is bound or will bind.protected StringidentifyPeer(InetSocketAddress remote) Identify the peer behind an incoming connection.protected StringnormalizePeerName(String peer) Normalize the given peer.static StringparseAddressPart(String address) Parse out the address part of an address that has an optional colon plus TCP port number suffix.static intparsePortPart(String address, int defaultPort) Parse out the port part of an address that has an optional colon plus TCP port number suffix.protected voidPerform housekeeping.voidsetConnectTimeout(long connectTimeout) voidsetListenAddress(InetSocketAddress address) Configure theInetSocketAddressto which this instance should bind.voidstart(Network.Handler handler) Start this instance.voidstop()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, setServiceThreadNameMethods 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- ifportis invalid
-
-
Method Details
-
getListenAddress
Get theInetSocketAddressto which this instance is bound or will bind.- Returns:
- listen address, possibly null for default behavior
-
setListenAddress
Configure theInetSocketAddressto 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- ifaddressis 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:NetworkStart this instance.- Specified by:
startin interfaceNetwork- Overrides:
startin classChannelNetwork- 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 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 formipaddroripaddr: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 formipaddroripaddr:portdefaultPort- default port if none specified inaddress- Returns:
- the port part, or
defaultPortif there is no explicit port
-
configureServerSocketChannel
Configure theServerSocketChannelto be used by this instance. This method is invoked bystart(org.dellroad.stuff.net.Network.Handler).The implementation in
TCPNetworkdoes nothing. Subclasses may override to configure socket options, etc.- Parameters:
serverSocketChannel- channel to configure
-
configureSocketChannel
Configure a newSocketChannelto be used by this instance. This method is invoked when new connections are created.The implementation in
TCPNetworkdoes 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
TCPNetworkreturns 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
TCPNetworkconvertspeerto lower case and appends the (default) port if not specified in the string.- Overrides:
normalizePeerNamein classChannelNetwork- Parameters:
peer- remote peer- Returns:
- normalized form for
peer - Throws:
IllegalArgumentException-inheritDoc
-
createConnection
Description copied from class:ChannelNetworkCreate a new connection to the specified peer.- Specified by:
createConnectionin 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: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 classChannelNetwork
-