SVCS - English Documentation
Loading...
Searching...
No Matches
EventBus.hxx
Go to the documentation of this file.
1
17#pragma once
18
19#include "Event.hxx"
20#include "IObserver.hxx"
21#include "ISubject.hxx"
22#include <algorithm>
23#include <vector>
24#include <memory>
25#include <mutex>
26
42namespace svcs::services {
43
53namespace ConsoleColor {
54 const std::string RESET = "\033[0m";
55 const std::string BLACK = "\033[30m";
56 const std::string RED = "\033[31m";
57 const std::string GREEN = "\033[32m";
58 const std::string YELLOW = "\033[33m";
59 const std::string BLUE = "\033[34m";
60 const std::string MAGENTA = "\033[35m";
61 const std::string CYAN = "\033[36m";
62 const std::string WHITE = "\033[37m";
63 const std::string BRIGHT_RED = "\033[91m";
64 const std::string BRIGHT_GREEN = "\033[92m";
65 const std::string BRIGHT_YELLOW = "\033[93m";
66 const std::string BRIGHT_BLUE = "\033[94m";
67 const std::string BRIGHT_MAGENTA = "\033[95m";
68 const std::string BRIGHT_CYAN = "\033[96m";
69 const std::string BRIGHT_WHITE = "\033[97m";
71 const std::string BOLD = "\033[1m";
72 const std::string DIM = "\033[2m";
73 const std::string ITALIC = "\033[3m";
74 const std::string UNDERLINE = "\033[4m";
75}
76
94class EventBus : public ISubject, public std::enable_shared_from_this<EventBus> {
95private:
105 std::vector<std::weak_ptr<IObserver>> observers_;
106
114 mutable std::mutex observers_mutex_;
115
127 static std::string getEventColor(Event::Type type) {
128 switch (type) {
130 return ConsoleColor::BRIGHT_GREEN;
132 return ConsoleColor::BRIGHT_GREEN;
134 return ConsoleColor::BRIGHT_RED;
136 return ConsoleColor::BRIGHT_YELLOW;
138 return ConsoleColor::BRIGHT_CYAN;
140 return ConsoleColor::BRIGHT_BLUE;
142 return ConsoleColor::GREEN;
144 return ConsoleColor::BRIGHT_MAGENTA;
146 return ConsoleColor::BRIGHT_GREEN;
148 return ConsoleColor::BRIGHT_RED;
150 return ConsoleColor::BRIGHT_CYAN;
152 return ConsoleColor::BRIGHT_BLUE;
154 return ConsoleColor::BRIGHT_YELLOW;
156 return ConsoleColor::BRIGHT_GREEN;
158 return ConsoleColor::BRIGHT_MAGENTA;
159 default:
160 return ConsoleColor::WHITE;
161 }
162 }
163
175 static std::string getEventIcon(Event::Type type) {
176 switch (type) {
178 return "✅";
180 return "💾";
182 return "❌";
184 return "⚠️ ";
186 return "ℹ️ ";
188 return "🐛";
190 return "💡";
192 return "🚀";
194 return "✅";
196 return "💥";
198 return "📤";
200 return "📥";
202 return "📦";
204 return "🔗";
206 return "🤝";
207 default:
208 return "";
209 }
210 }
211
212public:
214 * @english
215 * @brief Default virtual destructor.
216 *
217 * @russian
218 * @brief Виртуальный деструктор по умолчанию.
219 */
220 ~EventBus() override = default;
221
232
233 void attach(std::shared_ptr<IObserver> observer) override {
234 std::lock_guard<std::mutex> lock(observers_mutex_);
235 observers_.push_back(observer);
236 }
237
249 void detach(std::shared_ptr<IObserver> observer_to_remove) override {
250 std::lock_guard<std::mutex> lock(observers_mutex_);
251
252 auto it = std::remove_if(observers_.begin(), observers_.end(),
253 [&](const std::weak_ptr<IObserver>& weak_obs) {
254 auto shared_obs = weak_obs.lock();
255 return shared_obs && (shared_obs == observer_to_remove);
256 }
257 );
258 observers_.erase(it, observers_.end());
259 }
260
274 void notify(const Event& event) const override {
275 std::string color = getEventColor(event.type);
276 std::string icon = getEventIcon(event.type);
277 std::string sourceColor = ConsoleColor::DIM + ConsoleColor::BLACK;
278
279 // Format message with colors
280 std::string formattedMessage = color + icon + " " + event.details +
281 sourceColor + " [" + event.source_name + "]" +
282 ConsoleColor::RESET;
283
284 // Create event copy with formatted message
285 Event coloredEvent = event;
286 coloredEvent.details = formattedMessage;
287
288 for (auto& observer : observers_) {
289 observer.lock()->update(coloredEvent);
290 }
291 }
292};
293
294}
Defines the event structure used for the Observer pattern notifications.
Defines the interface for the Observer pattern component.
Defines the interface for the Subject (Publisher) component of the Observer pattern.
A thread-safe implementation of ISubject for centralized event distribution.
Definition EventBus.hxx:70
void attach(std::shared_ptr< IObserver > observer) override
Registers a new Observer.
Definition EventBus.hxx:184
void notify(const Event &event) const override
Sends an event to all active Observers.
Definition EventBus.hxx:214
~EventBus() override=default
Default virtual destructor.
void detach(std::shared_ptr< IObserver > observer_to_remove) override
Deregisters a specific Observer.
Definition EventBus.hxx:195
The Subject (Publisher) Interface (Abstract Base Class).
Definition ISubject.hxx:38
Defines ANSI color codes for console output formatting.
Service layer components and infrastructure services.
Type
Types of events that can be published.
Definition Event.hxx:35
@ NETWORK_SEND
Data has been sent over network.
Definition Event.hxx:142
@ PROTOCOL_SUCCESS
Protocol operation completed successfully.
Definition Event.hxx:130
@ DEBUG_MESSAGE
Debug message for development purposes.
Definition Event.hxx:106
@ OBJECT_TRANSFER
Object transfer in progress.
Definition Event.hxx:154
@ PROTOCOL_ERROR
Protocol operation encountered an error.
Definition Event.hxx:136
@ PROTOCOL_START
Protocol operation has started.
Definition Event.hxx:124
@ REFERENCE_UPDATE
Reference (branch/tag) has been updated.
Definition Event.hxx:160
@ GENERAL_INFO
General informational message.
Definition Event.hxx:94
@ ERROR_MESSAGE
Error message for user notification.
Definition Event.hxx:112
@ SAVE_SUCCESS
Save (commit) operation completed successfully.
Definition Event.hxx:64
@ WARNING_MESSAGE
Warning message for potential issues.
Definition Event.hxx:118
@ NEGOTIATION_PHASE
Protocol negotiation phase in progress.
Definition Event.hxx:166
@ REPOSITORY_INIT_SUCCESS
Repository initialization completed successfully.
Definition Event.hxx:52
@ HELP_MESSAGE
Help message for user assistance.
Definition Event.hxx:100
@ NETWORK_RECEIVE
Data has been received over network.
Definition Event.hxx:148
std::string details
Detailed description or payload (e.g., object hash).
Definition Event.hxx:179