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";
94class EventBus :
public ISubject,
public std::enable_shared_from_this<EventBus> {
105 std::vector<std::weak_ptr<IObserver>> observers_;
114 mutable std::mutex observers_mutex_;
127 static std::string getEventColor(
Event::Type 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;
160 return ConsoleColor::WHITE;
175 static std::string getEventIcon(
Event::Type type) {
233 void attach(std::shared_ptr<IObserver> observer)
override {
234 std::lock_guard<std::mutex> lock(observers_mutex_);
235 observers_.push_back(observer);
249 void detach(std::shared_ptr<IObserver> observer_to_remove)
override {
250 std::lock_guard<std::mutex> lock(observers_mutex_);
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);
258 observers_.erase(it, observers_.end());
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;
280 std::string formattedMessage = color + icon +
" " +
event.details +
281 sourceColor +
" [" +
event.source_name +
"]" +
285 Event coloredEvent = event;
286 coloredEvent.
details = formattedMessage;
288 for (
auto& observer : observers_) {
289 observer.lock()->update(coloredEvent);
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