Back to Site
Loading...
Searching...
No Matches
event_bus.h File Reference

A tiny typed publish/subscribe hub for decoupled module notifications. More...

#include <functional>
#include <mutex>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
Include dependency graph for event_bus.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  librats::EventBus
 

Namespaces

namespace  librats
 

Detailed Description

A tiny typed publish/subscribe hub for decoupled module notifications.

The "something happened" half of inter-module communication: a publisher emits an event value and every subscriber registered for that event type is invoked. The publisher neither knows nor names its subscribers — which is exactly what keeps modules from acquiring direct references to one another (the road back to a god-class). For the "do X / give me Y" half — a targeted call with a return value — use ServiceRegistry instead.

An event type is any value type; subscription and dispatch are keyed by it:

struct NetworkChanged { std::vector<std::string> addresses; };
bus.on<NetworkChanged>([](const NetworkChanged& e){ ... });   // subscribe
bus.emit(NetworkChanged{addrs});                              // publish → all handlers

Threading: handlers run synchronously on the thread that calls emit(). The bus itself is thread-safe (on/emit may be called concurrently), but a handler must not block that thread for long — if emit() is driven from a latency-sensitive thread, offload slow work. emit() snapshots the handler list under the lock and invokes it unlocked, so a handler may itself emit() or on() without deadlocking.

Convention: subscribe during a subsystem's attach() (single-threaded, before any reactor runs), the same "configure before start" rule the rest of the node uses.

Definition in file event_bus.h.