Class AbstractTCPBabelChannel
- All Implemented Interfaces:
BabelChannel, IPBabelChannel
- Direct Known Subclasses:
TCPBabelChannel, TLSBabelChannel
The channel maintains one logical TCPConnection per remote peer. Each
connection is backed by a Netty SocketChannel
and
buffers outbound messages while the socket is not yet writable.
Connection lifecycle:
- A call to
connect(BabelNode)resolves peer address components and initiates the handshake protocol (TCPHandshakeHandler). - Once the handshake completes on both sides,
addConnectionis called and aConnectionUpEventis delivered to the Babel runtime. - If two nodes connect to each other simultaneously, the duplicate
connection is resolved
using a tie-breaking
Comparator(configured viaTCPBabelChannelBuilder.setComparator(Comparator), defaulting toPublicKeyBabelNodeComparator). - When a socket closes,
removeConnectiondelivers aConnectionDownEvent.
Authentication Modes:
The channel supports different authentication configurations via
AbstractTCPBabelChannel.TCPBabelChannelAuthMode:
AbstractTCPBabelChannel.TCPBabelChannelAuthMode.NO_AUTH: standard unsigned messages only.AbstractTCPBabelChannel.TCPBabelChannelAuthMode.MIXED_AUTH: both unsigned and signed messages.AbstractTCPBabelChannel.TCPBabelChannelAuthMode.FULL_AUTH: signed messages only.
TCPBabelChannelBuilder.verifyIdentity().
Permitted Nodes Filtering:
Channels can optionally restrict connections to an explicit set of permitted peers
(via TCPBabelChannelBuilder.setPermittedNodes(Set) or
TLSBabelChannelBuilder.setPermittedNodes(Set)).
Handshake attempts from nodes not included in this set are rejected.
Self-connections (connecting to the local node's own address) are short-circuited in memory without involving the network: messages are immediately delivered back to the runtime.
The underlying I/O transport (epoll / kqueue / NIO) is selected automatically
by
BabelIpUtils based on OS
availability.
Concrete channels provide a builder() method (e.g.
TCPBabelChannel.builder())
to obtain a builder and configure the channel before passing it to the Babel
runtime.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumModes used to describe which types of signed messages are allowed on the channel. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longDefault delay before delivering a connection up event to allow state settling.static final TimeUnitDefault time unit forDEFAULT_CONNECTION_UP_DELAY.static final io.netty.util.AttributeKey<BabelNode> Netty channel attribute key storing the expected remote peer node identity for outbound connections.static final StringName of the handshake codec handler in the Netty pipeline.static final StringName of the handshake handler in the Netty pipeline. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractTCPBabelChannel(Babel runtime, NodeStore store, Comparator<? extends BabelNode> babelNodeComparator, int numberOfConnections, @Nullable Set<? extends BabelNode> permittedNodes, long connectionUpDelay, TimeUnit connectionUpDelayUnit) Initializes the common state of anAbstractTCPBabelChannel. -
Method Summary
Modifier and TypeMethodDescriptionvoidInitiates a TCP connection to the specified peer node.protected abstract io.netty.bootstrap.BootstrapcreateClientBootstrap(Babel runtime, NodeStore store, io.netty.channel.EventLoopGroup workerGroup, int numberOfMessages, boolean verifyIdentity, AbstractTCPBabelChannel.TCPBabelChannelAuthMode authMode, @Nullable Set<BabelNode> permittedNodes) Creates and configures the Netty clientBootstrapfor initiating outbound connections.protected abstract io.netty.channel.socket.ServerSocketChannelcreateServerSocketChannel(Babel runtime, NodeStore store, InetSocketAddress address, io.netty.channel.EventLoopGroup workerGroup, int numberOfMessages, boolean verifyIdentity, AbstractTCPBabelChannel.TCPBabelChannelAuthMode authMode, @Nullable Set<BabelNode> permittedNodes) Creates and binds the NettyServerSocketChannelfor accepting incoming connections.voiddisconnect(BabelNode peer) Disconnects the active TCP connection to the specified peer.Returns the local socket address to which the server socket channel is bound.Returns the set of permitted nodes for this channel, ornullif all nodes are permitted.protected voidinit(InetSocketAddress address, io.netty.channel.EventLoopGroup workerGroup, int numberOfMessages, boolean verifyIdentity, AbstractTCPBabelChannel.TCPBabelChannelAuthMode authMode) Initializes the server channel and client bootstrap by invoking the subclass-defined channel and bootstrap factory methods using the configured permitted nodes.booleanisConnected(BabelNode peer) Checks whether an active TCP connection to the specified peer exists.voidsend(BabelNode peer, BabelMessage message) Sends aBabelMessageto the specified peer over the active TCP connection.voidsendEphemeral(BabelNode peer, BabelMessage message) Sends a message topeerwithout establishing a persistent connection.voidshutdown()Shuts down the TCP server channel and terminates all active peer connections.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface BabelChannel
getInnerModifier and TypeMethodDescriptiondefault BabelChannelgetInner()Gets this channel or returns the inner channel if this instance is a wrapper.
-
Field Details
-
EXPECTED_PEER
Netty channel attribute key storing the expected remote peer node identity for outbound connections. -
HANDSHAKE_CODEC_NAME
Name of the handshake codec handler in the Netty pipeline.- See Also:
-
HANDSHAKE_HANDLER_NAME
Name of the handshake handler in the Netty pipeline.- See Also:
-
DEFAULT_CONNECTION_UP_DELAY
public static final long DEFAULT_CONNECTION_UP_DELAYDefault delay before delivering a connection up event to allow state settling.- See Also:
-
DEFAULT_CONNECTION_UP_DELAY_UNIT
Default time unit forDEFAULT_CONNECTION_UP_DELAY.
-
-
Constructor Details
-
AbstractTCPBabelChannel
protected AbstractTCPBabelChannel(Babel runtime, NodeStore store, Comparator<? extends BabelNode> babelNodeComparator, int numberOfConnections, @Nullable Set<? extends BabelNode> permittedNodes, long connectionUpDelay, TimeUnit connectionUpDelayUnit) Initializes the common state of anAbstractTCPBabelChannel.- Parameters:
runtime- the Babel runtimestore- the node store for address and component resolutionbabelNodeComparator- comparator used for tie-breaking concurrent connectionsnumberOfConnections- the initial capacity for concurrent peer connectionspermittedNodes- the set of permitted nodes, ornullif all nodes are permittedconnectionUpDelay- delay before delivering ConnectionUpEventconnectionUpDelayUnit- time unit for connectionUpDelay
-
-
Method Details
-
getPermittedNodes
-
init
protected void init(InetSocketAddress address, io.netty.channel.EventLoopGroup workerGroup, int numberOfMessages, boolean verifyIdentity, AbstractTCPBabelChannel.TCPBabelChannelAuthMode authMode) Initializes the server channel and client bootstrap by invoking the subclass-defined channel and bootstrap factory methods using the configured permitted nodes.- Parameters:
address- the local address to bind toworkerGroup- the Netty event loop group for channel I/OnumberOfMessages- outbound message queue buffer capacity per connectionverifyIdentity- whether cryptographic identity verification is enabledauthMode- the authentication mode configuring accepted message types
-
createServerSocketChannel
protected abstract io.netty.channel.socket.ServerSocketChannel createServerSocketChannel(Babel runtime, NodeStore store, InetSocketAddress address, io.netty.channel.EventLoopGroup workerGroup, int numberOfMessages, boolean verifyIdentity, AbstractTCPBabelChannel.TCPBabelChannelAuthMode authMode, @Nullable Set<BabelNode> permittedNodes) Creates and binds the NettyServerSocketChannelfor accepting incoming connections.- Parameters:
runtime- the Babel runtimestore- the node store for address and component resolutionaddress- the local address to bind toworkerGroup- the Netty event loop group for channel I/OnumberOfMessages- outbound message queue buffer capacity per connectionverifyIdentity- whether cryptographic identity verification is enabledauthMode- the authentication mode configuring accepted message typespermittedNodes- the set of permitted nodes, ornullif all nodes are permitted- Returns:
- the bound and initialized
ServerSocketChannel
-
createClientBootstrap
protected abstract io.netty.bootstrap.Bootstrap createClientBootstrap(Babel runtime, NodeStore store, io.netty.channel.EventLoopGroup workerGroup, int numberOfMessages, boolean verifyIdentity, AbstractTCPBabelChannel.TCPBabelChannelAuthMode authMode, @Nullable Set<BabelNode> permittedNodes) Creates and configures the Netty clientBootstrapfor initiating outbound connections.- Parameters:
runtime- the Babel runtimestore- the node store for address and component resolutionworkerGroup- the Netty event loop group for channel I/OnumberOfMessages- outbound message queue buffer capacity per connectionverifyIdentity- whether cryptographic identity verification is enabledauthMode- the authentication mode configuring accepted message typespermittedNodes- the set of permitted nodes, ornullif all nodes are permitted- Returns:
- the configured client
Bootstrap
-
connect
Initiates a TCP connection to the specified peer node.If the target peer is the local node, the connection is short-circuited in memory and a
ConnectionUpEventis immediately emitted. Otherwise, peer address components are resolved through theNodeStoreand an asynchronous TCP connection is established.- Specified by:
connectin interfaceBabelChannel- Parameters:
peer- the remote node to connect to
-
sendEphemeral
Sends a message topeerwithout establishing a persistent connection. The channel implementation may use a transient transport internally, but none of the connection lifecycle events (connectionUp/connectionDown) are triggered.- Specified by:
sendEphemeralin interfaceBabelChannel- Parameters:
peer- the target nodemessage- the message to deliver- Throws:
UnsupportedOperationException- ephemeral messaging is not supported over TCP
-
send
Sends aBabelMessageto the specified peer over the active TCP connection.If connecting to the local node, the message is delivered in-memory. Otherwise, the message is queued and written to the peer's socket channel.
- Specified by:
sendin interfaceBabelChannel- Parameters:
peer- the target recipient nodemessage- the message to transmit
-
disconnect
Disconnects the active TCP connection to the specified peer.- Specified by:
disconnectin interfaceBabelChannel- Parameters:
peer- the peer node to disconnect from
-
isConnected
Checks whether an active TCP connection to the specified peer exists.- Specified by:
isConnectedin interfaceBabelChannel- Parameters:
peer- the peer node to query- Returns:
trueif connected and active,falseotherwise
-
shutdown
public void shutdown()Shuts down the TCP server channel and terminates all active peer connections.- Specified by:
shutdownin interfaceBabelChannel
-
getBindingAddress
Returns the local socket address to which the server socket channel is bound.- Specified by:
getBindingAddressin interfaceIPBabelChannel- Returns:
- the local binding
InetSocketAddress
-