Class Babel
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
ProtocolEventinstances to registeredBabelProtocolHolders 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 Summary
Modifier and TypeMethodDescriptionstatic BabelBootstrapbuilder()Entry to configure and start the runtime with default configs fromBabelBootstrap.static BabelBootstrapbuilder(int numberOfProtocols, int numberOfExtensions, int numberOfChannels, int numberOfEvents, int numberOfMessages) Entry to configure and start the runtime with given configs.voiddelayedNetworkEvent(BabelChannel channel, NetworkEvent event, long delay, TimeUnit unit) Schedules aNetworkEventoriginating from the givenBabelChannelto be delivered to all interested registered protocols after a specified delay.voiddeliverEvent(ProtocolEvent event) Dispatches aProtocolEventto all registered protocol holders that have expressed interest in that event type.voiddeliverEvent(ProtocolEvent event, Class<? extends BabelProtocol> protocolClass) Distributes aProtocolEventto all protocols that are instances ofprotocolClassregistered with an interest in the event.voiddeliverInMessage(MessageInEvent event) Dispatches an inbound message event to all protocol holders that registered a handler for the contained message type.voiddeliverNetworkEvent(BabelChannel channel, NetworkEvent event) Delivers a network event that happened in the givenBabelChannelvoidDispatches a failed outbound message event to all protocol holders that registered an@UponMessageOutFailedhandler for the message type.voidDispatches a successful outbound message event to all protocol holders that registered an@UponMessageOutSuccesshandler for the message type.getKeys()Returns the private and public keys associated with this Babel instanceReturns the node identity of this Babel instance.init()Starts the runtime by delivering aStartEventto each registered protocol and scheduling the initial queue drain.setUpTimer(ProtocolEvent event, long delay, long period, TimeUnit unit) Registers a periodic timer that delivers an event repeatedly.setUpTimer(ProtocolEvent event, long delay, TimeUnit unit) Registers a single-shot timer that delivers an event to the runtime after a specified delay.voidshutdown()Shuts down the runtime by stopping the executor and closing all channels.
-
Method Details
-
deliverEvent
Dispatches aProtocolEventto all registered protocol holders that have expressed interest in that event type.- Parameters:
event- the event to dispatch
-
deliverInMessage
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 receivedBabelMessageand the sendingBabelNode
-
deliverOutSuccessMessage
Dispatches a successful outbound message event to all protocol holders that registered an@UponMessageOutSuccesshandler for the message type.- Parameters:
event- the event confirming that a message was successfully sent
-
deliverOutFailedMessage
Dispatches a failed outbound message event to all protocol holders that registered an@UponMessageOutFailedhandler for the message type.- Parameters:
event- the event reporting that a message could not be delivered
-
deliverNetworkEvent
Delivers a network event that happened in the givenBabelChannel- Parameters:
channel- the channel that created the eventevent- the event itself
-
deliverEvent
Distributes aProtocolEventto all protocols that are instances ofprotocolClassregistered 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
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
ScheduledFuturerepresenting this timer.
-
setUpTimer
Registers a periodic timer that delivers an event repeatedly.The first event is triggered after
delay, and subsequent events follow at the specifiedperiod.- 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
ScheduledFuturerepresenting this timer.
-
delayedNetworkEvent
public void delayedNetworkEvent(BabelChannel channel, NetworkEvent event, long delay, TimeUnit unit) Schedules aNetworkEventoriginating from the givenBabelChannelto 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
ConnectionUpEventnotifications, allowing simultaneous connection resolution and handshakes to settle before protocols attempt to send messages.- Parameters:
channel- the channel that originated the network eventevent- the network event to be delivereddelay- the duration to wait before delivering the eventunit- the time unit of the delay parameter
-
getMyself
Returns the node identity of this Babel instance.- Returns:
- the node identity
-
getKeys
-
init
Starts the runtime by delivering aStartEventto 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
Babelinstance 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
Entry to configure and start the runtime with default configs fromBabelBootstrap.- Returns:
- a new
BabelBootstrapinstance. - 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 protocolsnumberOfExtensions- number of expected extensionsnumberOfChannels- number of expected channelsnumberOfEvents- number of eventsnumberOfMessages- number of messages- Returns:
- a new
BabelBootstrapinstance. - See Also:
-