-
Notifications
You must be signed in to change notification settings - Fork 21
Add notification flow and shutdown handling in SDK #393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025 Intel Corporation | ||
| * | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
| #ifndef CLIENT_H | ||
| #define CLIENT_H | ||
|
|
||
| #include <string> | ||
|
|
||
| namespace mesh::client { | ||
|
|
||
| /** | ||
| * Client | ||
| * | ||
| * SDK client implementation. | ||
| */ | ||
| class Client { | ||
|
|
||
| public: | ||
| Client() {} | ||
|
|
||
| std::string id; | ||
| }; | ||
|
|
||
| } // namespace mesh::client | ||
|
|
||
| #endif // CLIENT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025 Intel Corporation | ||
| * | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
| #ifndef CLIENT_REGISTRY_H | ||
| #define CLIENT_REGISTRY_H | ||
|
|
||
| #include "client.h" | ||
| #include <unordered_map> | ||
| #include <mutex> | ||
| #include <shared_mutex> | ||
| #include "uuid.h" | ||
|
|
||
| namespace mesh::client { | ||
|
|
||
| /** | ||
| * Registry | ||
| * | ||
| * Thread-safe registry to store pointers to SDK client instances. | ||
| */ | ||
| class Registry { | ||
| public: | ||
| int add(const std::string& id, Client *client) { | ||
| std::unique_lock lk(mx); | ||
| if (clients.contains(id)) | ||
| return -1; | ||
| clients[id] = client; | ||
| return 0; | ||
| } | ||
|
|
||
| bool remove(const std::string& id) { | ||
| std::unique_lock lk(mx); | ||
| return clients.erase(id) > 0; | ||
| } | ||
|
|
||
| Client * get(const std::string& id) { | ||
| std::shared_lock lk(mx); | ||
| auto it = clients.find(id); | ||
| if (it != clients.end()) { | ||
| return it->second; | ||
| } | ||
| return nullptr; | ||
| } | ||
|
|
||
| private: | ||
| std::unordered_map<std::string, Client *> clients; | ||
| std::shared_mutex mx; | ||
| }; | ||
|
|
||
| extern Registry registry; | ||
|
|
||
| } // namespace mesh::client | ||
|
|
||
| #endif // CLIENT_REGISTRY_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025 Intel Corporation | ||
| * | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
| #ifndef EVENT_H | ||
| #define EVENT_H | ||
|
|
||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <mutex> | ||
| #include "concurrency.h" | ||
| #include "logger.h" | ||
| #include <unordered_map> | ||
| #include <any> | ||
|
|
||
| namespace mesh::event { | ||
|
|
||
| enum class Type { | ||
| empty_event, | ||
| conn_unlink_requested, | ||
| }; | ||
|
|
||
| typedef void (* Handler)(const Type& type); | ||
|
|
||
| class Event { | ||
| public: | ||
| std::string consumer_id; | ||
| Type type; | ||
| std::unordered_map<std::string, std::any> params; | ||
| }; | ||
|
|
||
| /** | ||
| * EventBroker | ||
| * | ||
| * Subsystem for passing events from producers to consumers. | ||
| * Enables software component decoupling. | ||
| */ | ||
| class EventBroker { | ||
| public: | ||
| EventBroker() {} | ||
|
|
||
| thread::Channel<Event> * subscribe(const std::string& consumer_id, int queue_sz = 1) { | ||
| auto ch = new(std::nothrow) thread::Channel<Event>(queue_sz); | ||
| if (ch) { | ||
| std::unique_lock lk(mx); | ||
| channels[ch] = consumer_id; | ||
| } | ||
| return ch; | ||
| } | ||
|
|
||
| bool unsubscribe(thread::Channel<Event> *ch) { | ||
| std::unique_lock lk(mx); | ||
| return channels.erase(ch) > 0; | ||
| } | ||
|
|
||
| bool send(context::Context& ctx, const std::string& consumer_id, const Type type, | ||
| const std::unordered_map<std::string, std::any>& params = {}) { | ||
| Event evt = { | ||
| .consumer_id = consumer_id, | ||
| .type = type, | ||
| .params = params, | ||
| }; | ||
| return events.send(ctx, evt); | ||
| } | ||
|
|
||
| void run(context::Context& ctx) { | ||
| for (;;) { | ||
| auto v = events.receive(ctx); | ||
| if (ctx.cancelled()) | ||
| return; | ||
|
|
||
| if (!v.has_value()) | ||
| continue; | ||
|
|
||
| auto evt = v.value(); | ||
| for (const auto& kv : channels) { | ||
| if (kv.second == evt.consumer_id) { | ||
| auto tctx = context::WithTimeout(ctx, std::chrono::milliseconds(3000)); | ||
| if (ctx.cancelled()) | ||
| return; | ||
|
|
||
| if (tctx.cancelled()) { | ||
| log::error("Event sending timeout")("type", (int)evt.type) | ||
| ("consumer_id", evt.consumer_id); | ||
| } else if (!kv.first->send(tctx, evt)) { | ||
| log::error("Event sending failed")("type", (int)evt.type) | ||
| ("consumer_id", evt.consumer_id); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| std::unordered_map<thread::Channel<Event> *, std::string> channels; | ||
| std::mutex mx; | ||
| thread::Channel<Event> events = thread::Channel<Event>(100); | ||
| }; | ||
|
|
||
| extern EventBroker broker; | ||
|
|
||
| } // namespace mesh::event | ||
|
|
||
| #endif // EVENT_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.