Class AbstractTCPBabelChannel

java.lang.Object
pt.unl.fct.di.novasys.babel2.channels.tcp_tls.AbstractTCPBabelChannel
All Implemented Interfaces:
BabelChannel, IPBabelChannel
Direct Known Subclasses:
TCPBabelChannel, TLSBabelChannel

public abstract class AbstractTCPBabelChannel extends Object implements IPBabelChannel
Babel 2 channel implementation that transports messages over persistent TCP connections.

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:

  1. A call to connect(BabelNode) resolves peer address components and initiates the handshake protocol (TCPHandshakeHandler).
  2. Once the handshake completes on both sides, addConnection is called and a ConnectionUpEvent is delivered to the Babel runtime.
  3. If two nodes connect to each other simultaneously, the duplicate connection is resolved using a tie-breaking Comparator (configured via TCPBabelChannelBuilder.setComparator(Comparator), defaulting to PublicKeyBabelNodeComparator).
  4. When a socket closes, removeConnection delivers a ConnectionDownEvent.

Authentication Modes: The channel supports different authentication configurations via AbstractTCPBabelChannel.TCPBabelChannelAuthMode:

Additionally, cryptographic handshake identity verification can be enabled via 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.

  • Field Details

    • EXPECTED_PEER

      public static final io.netty.util.AttributeKey<BabelNode> EXPECTED_PEER
      Netty channel attribute key storing the expected remote peer node identity for outbound connections.
    • HANDSHAKE_CODEC_NAME

      public static final String HANDSHAKE_CODEC_NAME
      Name of the handshake codec handler in the Netty pipeline.
      See Also:
    • HANDSHAKE_HANDLER_NAME

      public static final String 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_DELAY
      Default delay before delivering a connection up event to allow state settling.
      See Also:
    • DEFAULT_CONNECTION_UP_DELAY_UNIT

      public static final TimeUnit DEFAULT_CONNECTION_UP_DELAY_UNIT
      Default time unit for DEFAULT_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 an AbstractTCPBabelChannel.
      Parameters:
      runtime - the Babel runtime
      store - the node store for address and component resolution
      babelNodeComparator - comparator used for tie-breaking concurrent connections
      numberOfConnections - the initial capacity for concurrent peer connections
      permittedNodes - the set of permitted nodes, or null if all nodes are permitted
      connectionUpDelay - delay before delivering ConnectionUpEvent
      connectionUpDelayUnit - time unit for connectionUpDelay
  • Method Details

    • getPermittedNodes

      public @Nullable Set<BabelNode> getPermittedNodes()
      Returns the set of permitted nodes for this channel, or null if all nodes are permitted.
      Returns:
      the set of permitted nodes, or null
    • 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 to
      workerGroup - the Netty event loop group for channel I/O
      numberOfMessages - outbound message queue buffer capacity per connection
      verifyIdentity - whether cryptographic identity verification is enabled
      authMode - 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 Netty ServerSocketChannel for accepting incoming connections.
      Parameters:
      runtime - the Babel runtime
      store - the node store for address and component resolution
      address - the local address to bind to
      workerGroup - the Netty event loop group for channel I/O
      numberOfMessages - outbound message queue buffer capacity per connection
      verifyIdentity - whether cryptographic identity verification is enabled
      authMode - the authentication mode configuring accepted message types
      permittedNodes - the set of permitted nodes, or null if 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 client Bootstrap for initiating outbound connections.
      Parameters:
      runtime - the Babel runtime
      store - the node store for address and component resolution
      workerGroup - the Netty event loop group for channel I/O
      numberOfMessages - outbound message queue buffer capacity per connection
      verifyIdentity - whether cryptographic identity verification is enabled
      authMode - the authentication mode configuring accepted message types
      permittedNodes - the set of permitted nodes, or null if all nodes are permitted
      Returns:
      the configured client Bootstrap
    • connect

      public void connect(BabelNode peer)
      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 ConnectionUpEvent is immediately emitted. Otherwise, peer address components are resolved through the NodeStore and an asynchronous TCP connection is established.

      Specified by:
      connect in interface BabelChannel
      Parameters:
      peer - the remote node to connect to
    • sendEphemeral

      public void sendEphemeral(BabelNode peer, BabelMessage message)
      Sends a message to peer without 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:
      sendEphemeral in interface BabelChannel
      Parameters:
      peer - the target node
      message - the message to deliver
      Throws:
      UnsupportedOperationException - ephemeral messaging is not supported over TCP
    • send

      public void send(BabelNode peer, BabelMessage message)
      Sends a BabelMessage to 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:
      send in interface BabelChannel
      Parameters:
      peer - the target recipient node
      message - the message to transmit
    • disconnect

      public void disconnect(BabelNode peer)
      Disconnects the active TCP connection to the specified peer.
      Specified by:
      disconnect in interface BabelChannel
      Parameters:
      peer - the peer node to disconnect from
    • isConnected

      public boolean isConnected(BabelNode peer)
      Checks whether an active TCP connection to the specified peer exists.
      Specified by:
      isConnected in interface BabelChannel
      Parameters:
      peer - the peer node to query
      Returns:
      true if connected and active, false otherwise
    • shutdown

      public void shutdown()
      Shuts down the TCP server channel and terminates all active peer connections.
      Specified by:
      shutdown in interface BabelChannel
    • getBindingAddress

      public InetSocketAddress getBindingAddress()
      Returns the local socket address to which the server socket channel is bound.
      Specified by:
      getBindingAddress in interface IPBabelChannel
      Returns:
      the local binding InetSocketAddress