Class SocketEventBus

java.lang.Object
com.framed.core.remote.SocketEventBus
All Implemented Interfaces:
EventBus

public class SocketEventBus extends Object implements EventBus
A distributed EventBus implementation that uses a Transport for remote communication.

This class allows local and remote event dispatching across multiple peers connected via TCP or UDP. It supports point-to-point and publish-subscribe semantics, combining local handler execution with remote message forwarding.

Features:

  • Integrates with any Transport implementation (e.g., TCP or UDP).
  • Maintains a dynamic set of remote peers for message propagation.
  • Supports local handler registration and synchronous or asynchronous local dispatch.
  • Graceful shutdown via shutdown().

Usage Example:


 Transport tcpTransport = new TCPTransport(8080);
 SocketEventBus eventBus = new SocketEventBus(tcpTransport, DispatchMode.PER_HANDLER);

 eventBus.register("sensor.data", msg -> System.out.println("Local handler received: " + msg));

 eventBus.addPeer(new Peer("remote-host", 8081));

 eventBus.publish("sensor.data", "Temperature: 22°C");

 // Later:
 eventBus.shutdown();
 
Threading Model: Remote dispatch is delegated to the underlying Transport implementation. Note: Ensure shutdown() is called to release resources and stop the transport.
  • Constructor Details

    • SocketEventBus

      public SocketEventBus(Transport transport, DispatchMode dispatchMode)
      Creates a new SocketEventBus using the specified transport and dispatch mode.
      Parameters:
      transport - the transport implementation (e.g., TCPTransport or UDPTransport)
      dispatchMode - determines how local handlers are executed:
  • Method Details

    • addPeer

      public void addPeer(Peer peer)
      Adds a remote peer to the event bus.

      Messages sent or published will also be forwarded to this peer.

      Parameters:
      peer - the remote peer to add
    • removePeer

      public void removePeer(Peer peer)
      Removes a remote peer from the event bus.
      Parameters:
      peer - the remote peer to remove
    • register

      public void register(String address, Consumer<Object> handler)
      Registers a local handler for the specified address.

      The handler will also be registered with the underlying transport for remote messages.

      Specified by:
      register in interface EventBus
      Parameters:
      address - the logical address/topic to listen on
      handler - the handler to process incoming payloads
    • send

      public void send(String address, Object message)
      Sends a point-to-point message to all registered peers and dispatches locally.

      Uses send semantics: only the first handler on the remote side will process the message.

      Specified by:
      send in interface EventBus
      Parameters:
      address - the logical address/topic
      message - the payload to send
    • publish

      public void publish(String address, Object message)
      Publishes a message to all registered peers and dispatches locally.

      Uses publish semantics: all handlers on the remote side will process the message.

      Specified by:
      publish in interface EventBus
      Parameters:
      address - the logical address/topic
      message - the payload to publish
    • shutdown

      public void shutdown()
      Shuts down the event bus and releases resources.

      Stops the underlying transport, alle executors, and clears local handlers and peer list.

      Specified by:
      shutdown in interface EventBus