Line data Source code
1 : /*
2 : ___________________________________________
3 : | _ ___ _ |
4 : | | | |__ \ | | |
5 : | | |__ ) |__ _ __ _ ___ _ __ | |_ |
6 : | | '_ \ / // _` |/ _` |/ _ \ '_ \| __| | HTTP/2 AGENT FOR MOCK TESTING
7 : | | | | |/ /| (_| | (_| | __/ | | | |_ | Version 0.0.z
8 : | |_| |_|____\__,_|\__, |\___|_| |_|\__| | https://github.com/testillano/h2agent
9 : | __/ | |
10 : | |___/ |
11 : |___________________________________________|
12 :
13 : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
14 : SPDX-License-Identifier: MIT
15 : Copyright (c) 2021 Eduardo Ramos
16 :
17 : Permission is hereby granted, free of charge, to any person obtaining a copy
18 : of this software and associated documentation files (the "Software"), to deal
19 : in the Software without restriction, including without limitation the rights
20 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 : copies of the Software, and to permit persons to whom the Software is
22 : furnished to do so, subject to the following conditions:
23 :
24 : The above copyright notice and this permission notice shall be included in all
25 : copies or substantial portions of the Software.
26 :
27 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 : SOFTWARE.
34 : */
35 :
36 : #pragma once
37 :
38 : #include <string>
39 : #include <unordered_set>
40 : #include <unordered_map>
41 : #include <functional>
42 : #include <mutex>
43 : #include <atomic>
44 :
45 : #include <nlohmann/json.hpp>
46 :
47 : namespace h2agent
48 : {
49 : namespace model
50 : {
51 :
52 : /**
53 : * Manages Server-Sent Events (SSE) connections for vault event streaming.
54 : *
55 : * Each connection watches a set of vault keys. When a vault mutation occurs,
56 : * matching connections receive the event via their write callback.
57 : */
58 : class SseManager
59 : {
60 : public:
61 : using write_cb_t = std::function<void(const std::string &)>;
62 :
63 5 : SseManager() = default;
64 5 : ~SseManager() = default;
65 :
66 : /**
67 : * Registers an SSE connection.
68 : *
69 : * @param keys Set of vault keys this connection watches (empty = all keys)
70 : * @param writeCb Callback to write SSE-formatted data to the stream
71 : * @return Connection ID for later removal
72 : */
73 : uint64_t addConnection(std::unordered_set<std::string> keys, write_cb_t writeCb);
74 :
75 : /**
76 : * Removes an SSE connection.
77 : */
78 : void removeConnection(uint64_t id);
79 :
80 : /**
81 : * Notifies all matching connections of a vault mutation.
82 : * Called by Vault on every write.
83 : */
84 : void notify(const std::string &key, const nlohmann::json &value);
85 :
86 : /** Returns number of active SSE connections. */
87 : size_t activeConnections() const;
88 :
89 : private:
90 : struct Connection {
91 : std::unordered_set<std::string> keys; // empty = watch all
92 : write_cb_t writeCb;
93 : };
94 :
95 : mutable std::mutex mutex_;
96 : std::unordered_map<uint64_t, Connection> connections_;
97 : std::atomic<uint64_t> next_id_{1};
98 : };
99 :
100 : }
101 : }
|