|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2025 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef EVENT_H |
| 8 | +#define EVENT_H |
| 9 | + |
| 10 | +#include <string> |
| 11 | +#include <unordered_map> |
| 12 | +#include <mutex> |
| 13 | +#include "concurrency.h" |
| 14 | +#include "logger.h" |
| 15 | +#include <unordered_map> |
| 16 | +#include <any> |
| 17 | + |
| 18 | +namespace mesh::event { |
| 19 | + |
| 20 | +enum class Type { |
| 21 | + empty_event, |
| 22 | + conn_unlink_requested, |
| 23 | +}; |
| 24 | + |
| 25 | +typedef void (* Handler)(const Type& type); |
| 26 | + |
| 27 | +class Event { |
| 28 | +public: |
| 29 | + std::string consumer_id; |
| 30 | + Type type; |
| 31 | + std::unordered_map<std::string, std::any> params; |
| 32 | +}; |
| 33 | + |
| 34 | +/** |
| 35 | + * EventBroker |
| 36 | + * |
| 37 | + * Subsystem for passing events from producers to consumers. |
| 38 | + * Enables software component decoupling. |
| 39 | + */ |
| 40 | +class EventBroker { |
| 41 | +public: |
| 42 | + EventBroker() {} |
| 43 | + |
| 44 | + thread::Channel<Event> * subscribe(const std::string& consumer_id, int queue_sz = 1) { |
| 45 | + auto ch = new(std::nothrow) thread::Channel<Event>(queue_sz); |
| 46 | + if (ch) { |
| 47 | + std::unique_lock lk(mx); |
| 48 | + channels[ch] = consumer_id; |
| 49 | + } |
| 50 | + return ch; |
| 51 | + } |
| 52 | + |
| 53 | + bool unsubscribe(thread::Channel<Event> *ch) { |
| 54 | + std::unique_lock lk(mx); |
| 55 | + auto ret = channels.erase(ch) > 0; |
| 56 | + delete ch; |
| 57 | + return ret; |
| 58 | + } |
| 59 | + |
| 60 | + bool send(context::Context& ctx, const std::string& consumer_id, const Type type, |
| 61 | + const std::unordered_map<std::string, std::any>& params = {}) { |
| 62 | + Event evt = { |
| 63 | + .consumer_id = consumer_id, |
| 64 | + .type = type, |
| 65 | + .params = params, |
| 66 | + }; |
| 67 | + return events.send(ctx, evt); |
| 68 | + } |
| 69 | + |
| 70 | + void run(context::Context& ctx) { |
| 71 | + for (;;) { |
| 72 | + auto v = events.receive(ctx); |
| 73 | + if (ctx.cancelled()) |
| 74 | + return; |
| 75 | + |
| 76 | + if (!v.has_value()) |
| 77 | + continue; |
| 78 | + |
| 79 | + auto evt = v.value(); |
| 80 | + for (const auto& kv : channels) { |
| 81 | + if (kv.second == evt.consumer_id) { |
| 82 | + auto tctx = context::WithTimeout(ctx, std::chrono::milliseconds(3000)); |
| 83 | + if (ctx.cancelled()) |
| 84 | + return; |
| 85 | + |
| 86 | + if (tctx.cancelled()) { |
| 87 | + log::error("Event sending timeout")("type", (int)evt.type) |
| 88 | + ("consumer_id", evt.consumer_id); |
| 89 | + } else if (!kv.first->send(tctx, evt)) { |
| 90 | + log::error("Event sending failed")("type", (int)evt.type) |
| 91 | + ("consumer_id", evt.consumer_id); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | +private: |
| 99 | + std::unordered_map<thread::Channel<Event> *, std::string> channels; |
| 100 | + std::mutex mx; |
| 101 | + thread::Channel<Event> events = thread::Channel<Event>(100); |
| 102 | +}; |
| 103 | + |
| 104 | +extern EventBroker broker; |
| 105 | + |
| 106 | +} // namespace mesh::event |
| 107 | + |
| 108 | +#endif // EVENT_H |
0 commit comments