Event System

[中文]

API Reference

Public header: #include "brookesia/service_manager/event/definition.hpp"

Header File

Event Dispatcher

Public header: #include "brookesia/service_manager/event/dispatcher.hpp"

Header File

Classes

class EventDispatcher

Dispatches RPC event notifications to local subscription callbacks.

Public Types

using NotifyCallback = std::function<void(const EventItemMap&)>

Callback invoked when a subscribed event notification arrives.

Public Functions

bool subscribe(const std::string &subscription_id, NotifyCallback callback)

Register a callback for a subscription identifier.

Parameters
  • subscription_id[in] Subscription id returned by the RPC server.

  • callback[in] Callback to invoke when matching notifications arrive.

Returns

true on success, false if the subscription id is already registered.

void unsubscribe(const std::string &subscription_id)

Remove a previously registered subscription callback.

Parameters

subscription_id[in] Subscription id to remove.

void on_notify(const std::vector<std::string> &subscription_ids, const EventItemMap &event_items)

Dispatch an incoming notification to all matching local callbacks.

Parameters
  • subscription_ids[in] Subscription ids targeted by the notification.

  • event_items[in] Event payload associated with the notification.

Event Registry

Public header: #include "brookesia/service_manager/event/registry.hpp"

Header File

Classes

class EventRegistry

Registry of event schemas and active RPC subscriptions for one service.

Public Types

using Subscriptions = std::unordered_set<std::string>

Set of subscription identifiers attached to an event.

using Signal = boost::signals2::signal<void(const std::string &event_name, const EventItemMap &event_items)>

Signal type used for local in-process event listeners.

using SignalConnection = boost::signals2::scoped_connection

RAII connection handle returned by Signal::connect().

using SignalSlot = Signal::slot_type

Slot type accepted by subscribe_event().

Public Functions

bool add(EventSchema event_schema)

Register an event schema.

Parameters

event_schema[in] Schema to add.

Returns

true on success, false if an event with the same name already exists.

void remove(const std::string &event_name)

Remove a registered event schema.

Parameters

event_name[in] Name of the event to remove.

void remove_all()

Remove all registered event schemas.

bool validate_items(const std::string &event_name, const EventItemMap &event_items)

Validate an event payload against a registered schema.

Parameters
  • event_name[in] Event name to validate against.

  • event_items[in] Event payload to validate.

Returns

true if the payload conforms to the schema, false otherwise.

bool on_rpc_subscribe(const std::string &event_name, std::string &subscription_id, std::string &error_message)

Create a new RPC subscription for the given event.

Parameters
  • event_name[in] Name of the event to subscribe to.

  • subscription_id[out] Generated subscription id on success.

  • error_message[out] Validation or registration failure details.

Returns

true if the subscription was created, false otherwise.

void on_rpc_unsubscribe_by_name(const std::string &event_name)

Remove all RPC subscriptions associated with an event name.

Parameters

event_name[in] Name of the event whose subscriptions should be removed.

void on_rpc_unsubscribe_by_subscriptions(const Subscriptions &subscriptions)

Remove a set of RPC subscriptions from every registered event.

Parameters

subscriptions[in] Subscription ids to remove.

inline const EventSchema *get_schema(const std::string &event_name)

Look up the schema for a registered event.

Parameters

event_name[in] Event name to query.

Returns

const EventSchema* Pointer to the schema, or nullptr if not found.

std::vector<EventSchema> get_schemas() const

Get a snapshot of all registered event schemas.

Returns

std::vector<EventSchema> Copy of the registered schemas.

boost::json::array get_schemas_json()

Export all registered event schemas as JSON.

Returns

boost::json::array JSON representation of every schema.

inline bool has(const std::string &event_name)

Check whether an event schema exists.

Parameters

event_name[in] Event name to check.

Returns

true if the event is registered.

inline size_t get_count()

Get the number of registered event schemas.

Returns

size_t Count of registered events.

Subscriptions get_subscriptions(const std::string &event_name)

Get all RPC subscription ids associated with an event.

Parameters

event_name[in] Event name to query.

Returns

Subscriptions Copy of the registered subscription ids.

Signal *get_signal(const std::string &event_name)

Get the in-process signal associated with an event.

Parameters

event_name[in] Event name to query.

Returns

Signal* Pointer to the signal, or nullptr if the event does not exist.