-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_dispatcher.cpp
113 lines (93 loc) · 3.23 KB
/
event_dispatcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "event_dispatcher.h"
#include <sstream>
// EventDataA implementation
EventDataA::EventDataA(int d) : data(d) {}
void EventDataA::extractData() const {
std::cout << "EventDataA: " << data << std::endl;
}
// EventDataB implementation
EventDataB::EventDataB(const std::string& d) : data(d) {}
void EventDataB::extractData() const {
std::cout << "EventDataB: " << data << std::endl;
}
// EventDataC implementation
EventDataC::EventDataC(double d) : data(d) {}
void EventDataC::extractData() const {
std::cout << "EventDataC: " << data << std::endl;
}
// EventDispatcher implementation
EventDispatcher::EventDispatcher() : stopFlag(false) {}
EventDispatcher::~EventDispatcher() {
stop();
}
void EventDispatcher::start() {
dispatcherThreadHandle = std::thread(&EventDispatcher::dispatcherThread, this);
}
void EventDispatcher::stop() {
{
std::lock_guard<std::mutex> lock(handlersMutex);
stopFlag = true;
}
for (auto& [type, cv] : queueCVs) {
cv.notify_all();
}
if (dispatcherThreadHandle.joinable()) {
dispatcherThreadHandle.join();
}
for (auto& [type, thread] : workerThreads) {
if (thread.joinable()) {
thread.join();
}
}
}
void EventDispatcher::dispatcherThread() {
std::stringstream ss;
ss << "Dispatcher thread started. Thread ID: " << std::this_thread::get_id() << "\n";
std::cout << ss.str();
// Initialize worker threads for each event type
for (auto type : {EventType::EVENT_TYPE_A, EventType::EVENT_TYPE_B, EventType::EVENT_TYPE_C}) {
workerThreads[type] = std::thread(&EventDispatcher::eventWorker, this, type);
}
}
void EventDispatcher::registerEventHandler(EventType type, std::shared_ptr<EventHandler> handler) {
std::lock_guard<std::mutex> lock(handlersMutex);
eventHandlers[type].emplace_back(handler);
}
void EventDispatcher::enqueueEvent(const Event& event) {
{
std::lock_guard<std::mutex> lock(queueMutexes[event.type]);
eventQueues[event.type].push(event);
}
queueCVs[event.type].notify_one();
}
void EventDispatcher::eventWorker(EventType type) {
std::stringstream ss;
ss << "Thread for event type " << static_cast<int>(type) << " started. Thread ID: " << std::this_thread::get_id() << "\n";
std::cout << ss.str();
while (true) {
Event event;
{
std::unique_lock<std::mutex> lock(queueMutexes[type]);
queueCVs[type].wait(lock, [&event, type, this] {
return !eventQueues[type].empty() || stopFlag;
});
if (stopFlag && eventQueues[type].empty()) {
return;
}
event = eventQueues[type].front();
eventQueues[type].pop();
}
ss.str("");
ss << "Thread ID: " << std::this_thread::get_id() << " processing event of type " << static_cast<int>(type) << "\n";
std::cout << ss.str();
dispatchEvent(event);
}
}
void EventDispatcher::dispatchEvent(const Event& event) {
std::lock_guard<std::mutex> lock(handlersMutex);
if (eventHandlers.find(event.type) != eventHandlers.end()) {
for (auto& handler : eventHandlers[event.type]) {
handler->handleEvent(event.data);
}
}
}