Class Babel

java.lang.Object
pt.unl.fct.di.novasys.babel2.core.Babel

public final class Babel extends Object
The Babel class serves as the central orchestration engine for the Babel 2 runtime. Its primary responsibility is to manage the lifecycle and execution of decentralized protocols in a non-blocking, asynchronous manner.

It facilitates:

  • Event Dispatching: A Publish/Subscribe mechanism that routes ProtocolEvent instances to registered BabelProtocolHolders based on defined interests.
  • Scheduling: Fair execution of protocol logic via a cooperative multi-tasking approach, utilizing a shared ScheduledExecutorService.
  • Timer Management: Precision scheduling of one-off and periodic events, essential for timeout-heavy distributed algorithms (e.g., heartbeats, retransmissions).

This implementation supports the Babel 2 objective of high-performance event processing across heterogeneous environments by minimizing synchronization overhead during event delivery.

See Also:
  • Method Details

    • deliverEvent

      public void deliverEvent(ProtocolEvent event)
      Dispatches a ProtocolEvent to all registered protocol holders that have expressed interest in that event type.
      Parameters:
      event - the event to dispatch
    • deliverInMessage

      public void deliverInMessage(MessageInEvent event)
      Dispatches an inbound message event to all protocol holders that registered a handler for the contained message type.
      Parameters:
      event - the inbound message event wrapping the received BabelMessage and the sending BabelNode
    • deliverOutSuccessMessage

      public void deliverOutSuccessMessage(MessageOutSuccessEvent event)
      Dispatches a successful outbound message event to all protocol holders that registered an @UponMessageOutSuccess handler for the message type.
      Parameters:
      event - the event confirming that a message was successfully sent
    • deliverOutFailedMessage

      public void deliverOutFailedMessage(MessageOutFailedEvent event)
      Dispatches a failed outbound message event to all protocol holders that registered an @UponMessageOutFailed handler for the message type.
      Parameters:
      event - the event reporting that a message could not be delivered
    • deliverNetworkEvent

      public void deliverNetworkEvent(BabelChannel channel, NetworkEvent event)
      Delivers a network event that happened in the given BabelChannel
      Parameters:
      channel - the channel that created the event
      event - the event itself
    • deliverEvent

      public void deliverEvent(ProtocolEvent event, Class<? extends BabelProtocol> protocolClass)
      Distributes a ProtocolEvent to all protocols that are instances of protocolClass registered with an interest in the event.

      Upon delivery, each target protocol is immediately scheduled for a queue drain.

      Parameters:
      event - the event to be propagated through the protocol stack.
      protocolClass - the class of the protocols eligible to receive the event.
    • setUpTimer

      public ScheduledFuture<?> setUpTimer(ProtocolEvent event, long delay, TimeUnit unit)
      Registers a single-shot timer that delivers an event to the runtime after a specified delay.
      Parameters:
      event - the event to be triggered.
      delay - duration to wait before triggering.
      unit - the time unit of the delay parameter.
      Returns:
      a ScheduledFuture representing this timer.
    • setUpTimer

      public ScheduledFuture<?> setUpTimer(ProtocolEvent event, long delay, long period, TimeUnit unit)
      Registers a periodic timer that delivers an event repeatedly.

      The first event is triggered after delay, and subsequent events follow at the specified period.

      Parameters:
      event - the event to be triggered periodically.
      delay - initial delay before the first execution.
      period - the interval between successive executions.
      unit - the time unit for the delay and period parameters.
      Returns:
      a ScheduledFuture representing this timer.
    • delayedNetworkEvent

      public void delayedNetworkEvent(BabelChannel channel, NetworkEvent event, long delay, TimeUnit unit)
      Schedules a NetworkEvent originating from the given BabelChannel to be delivered to all interested registered protocols after a specified delay.

      This is typically used by channels (such as TCP, TLS, or QUIC) to delay ConnectionUpEvent notifications, allowing simultaneous connection resolution and handshakes to settle before protocols attempt to send messages.

      Parameters:
      channel - the channel that originated the network event
      event - the network event to be delivered
      delay - the duration to wait before delivering the event
      unit - the time unit of the delay parameter
    • getMyself

      public BabelNode getMyself()
      Returns the node identity of this Babel instance.
      Returns:
      the node identity
    • getKeys

      public KeyPair getKeys()
      Returns the private and public keys associated with this Babel instance
      Returns:
      the KeyPair object containing the keys.
    • init

      public Babel init()
      Starts the runtime by delivering a StartEvent to each registered protocol and scheduling the initial queue drain.

      This method is idempotent: calling it more than once has no effect after the first invocation.

      Returns:
      this Babel instance for method chaining
    • shutdown

      public void shutdown()
      Shuts down the runtime by stopping the executor and closing all channels.

      After this call no further events will be dispatched. In-flight event processing may be interrupted depending on the executor's shutdown policy.

    • builder

      public static BabelBootstrap builder()
      Entry to configure and start the runtime with default configs from BabelBootstrap.
      Returns:
      a new BabelBootstrap instance.
      See Also:
    • builder

      public static BabelBootstrap builder(int numberOfProtocols, int numberOfExtensions, int numberOfChannels, int numberOfEvents, int numberOfMessages)
      Entry to configure and start the runtime with given configs. This should be preferred to the one using the default configs for maximum performance.
      Parameters:
      numberOfProtocols - number of expected protocols
      numberOfExtensions - number of expected extensions
      numberOfChannels - number of expected channels
      numberOfEvents - number of events
      numberOfMessages - number of messages
      Returns:
      a new BabelBootstrap instance.
      See Also: