事件系统
API 参考
公共头文件: #include "brookesia/service_manager/event/definition.hpp"
Header File
事件调度器
公共头文件: #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.
- 参数
subscription_id – [in] Subscription id returned by the RPC server.
callback – [in] Callback to invoke when matching notifications arrive.
- 返回
true on success, false if the subscription id is already registered.
-
void unsubscribe(const std::string &subscription_id)
Remove a previously registered subscription callback.
- 参数
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.
- 参数
subscription_ids – [in] Subscription ids targeted by the notification.
event_items – [in] Event payload associated with the notification.
-
using NotifyCallback = std::function<void(const EventItemMap&)>
事件注册表
公共头文件: #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().
Public Functions
-
bool add(EventSchema event_schema)
Register an event schema.
- 参数
event_schema – [in] Schema to add.
- 返回
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.
- 参数
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.
- 参数
event_name – [in] Event name to validate against.
event_items – [in] Event payload to validate.
- 返回
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.
- 参数
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.
- 返回
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.
- 参数
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.
- 参数
subscriptions – [in] Subscription ids to remove.
-
inline const EventSchema *get_schema(const std::string &event_name)
Look up the schema for a registered event.
- 参数
event_name – [in] Event name to query.
- 返回
const EventSchema* Pointer to the schema, or
nullptrif not found.
-
std::vector<EventSchema> get_schemas() const
Get a snapshot of all registered event schemas.
- 返回
std::vector<EventSchema> Copy of the registered schemas.
-
boost::json::array get_schemas_json()
Export all registered event schemas as JSON.
- 返回
boost::json::array JSON representation of every schema.
-
inline bool has(const std::string &event_name)
Check whether an event schema exists.
- 参数
event_name – [in] Event name to check.
- 返回
true if the event is registered.
-
inline size_t get_count()
Get the number of registered event schemas.
- 返回
size_t Count of registered events.
-
Subscriptions get_subscriptions(const std::string &event_name)
Get all RPC subscription ids associated with an event.
- 参数
event_name – [in] Event name to query.
- 返回
Subscriptions Copy of the registered subscription ids.
-
using Subscriptions = std::unordered_set<std::string>