Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
event_bus_interface.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
19#include <any>
20#include <atomic>
21#include <chrono>
22#include <concepts>
23#include <functional>
24#include <memory>
25#include <mutex>
26#include <string>
27#include <type_traits>
28#include <typeindex>
29#include <unordered_map>
30#include <vector>
33
34#if KCENON_HAS_COMMON_SYSTEM
35#include <kcenon/common/concepts/event.h>
36#endif
37
38namespace kcenon { namespace monitoring {
39
40namespace concepts {
41
51template <typename T>
52concept EventType = std::is_class_v<T> && std::is_copy_constructible_v<T>;
53
61template <typename H, typename E>
62concept EventHandler =
63 std::invocable<H, const E&> && std::is_void_v<std::invoke_result_t<H, const E&>>;
64
72template <typename F, typename E>
73concept EventFilter =
74 std::invocable<F, const E&> && std::convertible_to<std::invoke_result_t<F, const E&>, bool>;
75
76} // namespace concepts
77
107public:
108 virtual ~event_base() = default;
109
114 virtual std::string get_type_name() const = 0;
115
120 std::chrono::steady_clock::time_point get_timestamp() const {
121 return timestamp_;
122 }
123
128 uint64_t get_id() const {
129 return id_;
130 }
131
132protected:
134 : timestamp_(std::chrono::steady_clock::now()),
135 id_(generate_id()) {}
136
137private:
138 static uint64_t generate_id() {
139 static std::atomic<uint64_t> counter{0};
140 return counter.fetch_add(1, std::memory_order_relaxed);
141 }
142
143 std::chrono::steady_clock::time_point timestamp_;
144 uint64_t id_;
145};
146
151enum class event_priority {
152 low,
153 normal,
154 high,
156};
157
162template<typename EventType>
164public:
165 using handler_func = std::function<void(const EventType&)>;
166
169
170 void operator()(const EventType& event) const {
171 if (handler_) {
172 handler_(event);
173 }
174 }
175
177 uint64_t get_id() const { return id_; }
178
179private:
180 static uint64_t generate_id() {
181 static std::atomic<uint64_t> counter{0};
182 return counter.fetch_add(1, std::memory_order_relaxed);
183 }
184
187 uint64_t id_;
188};
189
195public:
196 subscription_token(std::type_index event_type, uint64_t handler_id)
197 : event_type_(event_type), handler_id_(handler_id) {}
198
199 std::type_index get_event_type() const { return event_type_; }
200 uint64_t get_handler_id() const { return handler_id_; }
201
202private:
203 std::type_index event_type_;
204 uint64_t handler_id_;
205};
206
252public:
253 virtual ~interface_event_bus() = default;
254
265 template <concepts::EventType E>
266 common::VoidResult publish_event(const E& event) {
267 return publish_event_impl(std::type_index(typeid(E)), std::make_any<E>(event));
268 }
269
281 template <concepts::EventType E>
282 common::Result<subscription_token> subscribe_event(std::function<void(const E&)> handler,
284 auto wrapped_handler = event_handler<E>(handler, priority);
286 std::type_index(typeid(E)),
287 [wrapped_handler](const std::any& event) { wrapped_handler(std::any_cast<E>(event)); },
288 wrapped_handler.get_id(), priority);
289 }
290
302 template <concepts::EventType E, concepts::EventHandler<E> H>
303 common::Result<subscription_token> subscribe_event(H&& handler,
305 return subscribe_event(std::function<void(const E&)>(std::forward<H>(handler)), priority);
306 }
307
313 virtual common::VoidResult unsubscribe_event(const subscription_token& token) = 0;
314
320 template <concepts::EventType E>
321 common::VoidResult clear_subscriptions() {
322 return clear_subscriptions_impl(std::type_index(typeid(E)));
323 }
324
330 template <concepts::EventType E>
331 size_t get_subscriber_count() const {
332 return get_subscriber_count_impl(std::type_index(typeid(E)));
333 }
334
339 virtual bool is_active() const = 0;
340
345 virtual common::VoidResult start() = 0;
346
351 virtual common::VoidResult stop() = 0;
352
357 virtual size_t get_pending_event_count() const = 0;
358
363 virtual common::VoidResult process_pending_events() = 0;
364
365protected:
366 // Implementation methods to be overridden by concrete classes
367 virtual common::VoidResult publish_event_impl(
368 std::type_index event_type,
369 std::any event) = 0;
370
371 virtual common::Result<subscription_token> subscribe_event_impl(
372 std::type_index event_type,
373 std::function<void(const std::any&)> handler,
374 uint64_t handler_id,
375 event_priority priority) = 0;
376
377 virtual common::VoidResult clear_subscriptions_impl(
378 std::type_index event_type) = 0;
379
381 std::type_index event_type) const = 0;
382};
383
420public:
421 virtual ~interface_event_publisher() = default;
422
428 virtual common::VoidResult set_event_bus(std::shared_ptr<interface_event_bus> bus) = 0;
429
434 virtual std::shared_ptr<interface_event_bus> get_event_bus() const = 0;
435};
436
486public:
487 virtual ~interface_event_subscriber() = default;
488
494 virtual common::VoidResult subscribe_to_events(std::shared_ptr<interface_event_bus> bus) = 0;
495
500 virtual common::VoidResult unsubscribe_from_events() = 0;
501
506 virtual std::vector<subscription_token> get_subscriptions() const = 0;
507};
508
509} } // namespace kcenon::monitoring
Priority levels for event processing.
Base class for all events in the system.
std::chrono::steady_clock::time_point timestamp_
virtual std::string get_type_name() const =0
Get the event type name.
uint64_t get_id() const
Get unique event ID.
virtual ~event_base()=default
std::chrono::steady_clock::time_point get_timestamp() const
Get timestamp when event was created.
Type-safe event handler wrapper.
std::function< void(const EventType &)> handler_func
void operator()(const EventType &event) const
event_handler(handler_func handler, event_priority priority=event_priority::normal)
Pure virtual interface for event bus implementation.
size_t get_subscriber_count() const
Get the number of subscribers for an event type.
virtual common::Result< subscription_token > subscribe_event_impl(std::type_index event_type, std::function< void(const std::any &)> handler, uint64_t handler_id, event_priority priority)=0
virtual bool is_active() const =0
Check if event bus is active.
virtual common::VoidResult unsubscribe_event(const subscription_token &token)=0
Unsubscribe from events using subscription token.
virtual common::VoidResult start()=0
Start the event bus.
virtual common::VoidResult publish_event_impl(std::type_index event_type, std::any event)=0
virtual common::VoidResult process_pending_events()=0
Process all pending events synchronously.
common::VoidResult publish_event(const E &event)
Publish an event to all subscribers.
virtual size_t get_subscriber_count_impl(std::type_index event_type) const =0
common::Result< subscription_token > subscribe_event(H &&handler, event_priority priority=event_priority::normal)
Subscribe to events with a callable handler.
common::VoidResult clear_subscriptions()
Clear all subscriptions for a specific event type.
virtual common::VoidResult clear_subscriptions_impl(std::type_index event_type)=0
virtual size_t get_pending_event_count() const =0
Get pending event count.
common::Result< subscription_token > subscribe_event(std::function< void(const E &)> handler, event_priority priority=event_priority::normal)
Subscribe to events of a specific type.
virtual common::VoidResult stop()=0
Stop the event bus.
Interface for components that publish events.
virtual std::shared_ptr< interface_event_bus > get_event_bus() const =0
Get the current event bus.
virtual common::VoidResult set_event_bus(std::shared_ptr< interface_event_bus > bus)=0
Set the event bus for publishing.
Interface for components that subscribe to events.
virtual common::VoidResult subscribe_to_events(std::shared_ptr< interface_event_bus > bus)=0
Subscribe to required events.
virtual common::VoidResult unsubscribe_from_events()=0
Unsubscribe from all events.
virtual std::vector< subscription_token > get_subscriptions() const =0
Get subscription tokens.
Token for managing event subscriptions.
subscription_token(std::type_index event_type, uint64_t handler_id)
A callable that filters events based on criteria.
A callable that can handle events of a specific type.
A type that can be used as an event in the event bus.
Unified feature flags header for monitoring_system.
@ counter
Monotonically increasing counter.
Result pattern type definitions for monitoring system.