Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPythonEvent.cpp
More file actions
363 lines (288 loc) · 10 KB
/
Copy pathPythonEvent.cpp
File metadata and controls
363 lines (288 loc) · 10 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <chrono>
#include <thread>
#include <queue>
#include <algorithm>
#include "scripting/PythonEvent.hpp"
// =============== PythonEventHandler Implementation ===============
PythonEventHandler::PythonEventHandler(const std::string& name,
const std::string& moduleName,
const std::string& functionName,
int priority)
: name_(name), moduleName_(moduleName), functionName_(functionName), priority_(priority) {
}
bool PythonEventHandler::HandleEvent(const nlohmann::json& eventData) {
// This would call Python via the scripting engine
// For now, it's a stub
auto& scripting = PythonScripting::GetInstance();
return scripting.CallFunction(moduleName_, functionName_, eventData);
}
bool PythonEventHandler::IsValid() const {
auto& scripting = PythonScripting::GetInstance();
std::vector<std::string> modules = scripting.GetLoadedModules();
return std::find(modules.begin(), modules.end(), moduleName_) != modules.end();
}
// =============== EventDispatcher Implementation ===============
EventDispatcher& EventDispatcher::GetInstance() {
static EventDispatcher instance;
return instance;
}
void EventDispatcher::RegisterHandler(const std::string& eventName,
std::shared_ptr<IEventHandler> handler) {
std::unique_lock<std::shared_mutex> lock(handlersMutex_);
// Insert handler in priority order
auto& handlers = handlers_[eventName];
auto it = std::lower_bound(handlers.begin(), handlers.end(), handler,
[](const std::shared_ptr<IEventHandler>& a,
const std::shared_ptr<IEventHandler>& b) {
return a->GetPriority() > b->GetPriority();
});
handlers.insert(it, handler);
}
void EventDispatcher::UnregisterHandler(const std::string& eventName,
const std::string& handlerName) {
std::unique_lock<std::shared_mutex> lock(handlersMutex_);
auto it = handlers_.find(eventName);
if (it != handlers_.end()) {
auto& handlers = it->second;
handlers.erase(
std::remove_if(handlers.begin(), handlers.end(),
[&handlerName](const std::shared_ptr<IEventHandler>& handler) {
return handler->GetName() == handlerName;
}),
handlers.end()
);
if (handlers.empty()) {
handlers_.erase(it);
}
}
}
bool EventDispatcher::DispatchEvent(const std::string& eventName,
const nlohmann::json& eventData) {
std::shared_lock<std::shared_mutex> lock(handlersMutex_);
auto it = handlers_.find(eventName);
if (it == handlers_.end()) {
return false;
}
bool anySuccess = false;
for (const auto& handler : it->second) {
if (handler->HandleEvent(eventData)) {
anySuccess = true;
}
}
return anySuccess;
}
std::vector<std::string> EventDispatcher::GetRegisteredEvents() const {
std::shared_lock<std::shared_mutex> lock(handlersMutex_);
std::vector<std::string> events;
for (const auto& [eventName, handlers] : handlers_) {
events.push_back(eventName);
}
return events;
}
std::vector<std::string> EventDispatcher::GetHandlersForEvent(const std::string& eventName) const {
std::shared_lock<std::shared_mutex> lock(handlersMutex_);
auto it = handlers_.find(eventName);
if (it == handlers_.end()) {
return {};
}
std::vector<std::string> handlerNames;
for (const auto& handler : it->second) {
handlerNames.push_back(handler->GetName());
}
return handlerNames;
}
// =============== EventQueue Implementation ===============
EventQueue::EventQueue(size_t maxSize)
: maxSize_(maxSize), running_(false) {
}
EventQueue::~EventQueue() {
StopProcessing();
}
bool EventQueue::PushEvent(const std::string& eventName,
const nlohmann::json& eventData,
int priority) {
std::lock_guard<std::mutex> lock(queueMutex_);
if (queue_.size() >= maxSize_) {
return false;
}
QueuedEvent event{
eventName,
eventData,
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count(),
priority
};
queue_.push(std::move(event));
queueCV_.notify_one();
return true;
}
bool EventQueue::PopEvent(QueuedEvent& event) {
std::lock_guard<std::mutex> lock(queueMutex_);
if (queue_.empty()) {
return false;
}
event = queue_.top();
queue_.pop();
return true;
}
size_t EventQueue::Size() const {
std::lock_guard<std::mutex> lock(queueMutex_);
return queue_.size();
}
size_t EventQueue::Capacity() const {
return maxSize_;
}
void EventQueue::Clear() {
std::lock_guard<std::mutex> lock(queueMutex_);
while (!queue_.empty()) {
queue_.pop();
}
}
void EventQueue::StartProcessing() {
if (running_) {
return;
}
running_ = true;
processThread_ = std::thread(&EventQueue::ProcessLoop, this);
}
void EventQueue::StopProcessing() {
if (!running_) {
return;
}
running_ = false;
queueCV_.notify_all();
if (processThread_.joinable()) {
processThread_.join();
}
}
void EventQueue::ProcessLoop() {
while (running_) {
QueuedEvent event;
{
std::unique_lock<std::mutex> lock(queueMutex_);
if (queue_.empty()) {
queueCV_.wait(lock, [this]() {
return !queue_.empty() || !running_;
});
}
if (!running_) {
break;
}
if (queue_.empty()) {
continue;
}
event = queue_.top();
queue_.pop();
}
// Dispatch the event
auto& dispatcher = EventDispatcher::GetInstance();
dispatcher.DispatchEvent(event.name, event.data);
}
}
// =============== ScheduledEvent Implementation ===============
ScheduledEvent::ScheduledEvent(const std::string& eventName,
const nlohmann::json& eventData,
int64_t executeAt,
bool repeat,
int64_t interval)
: eventName_(eventName),
eventData_(eventData),
executeAt_(executeAt),
repeat_(repeat),
interval_(interval) {
}
bool ScheduledEvent::ShouldExecute() const {
auto now = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
return now >= executeAt_;
}
bool ScheduledEvent::Execute() {
auto& dispatcher = EventDispatcher::GetInstance();
return dispatcher.DispatchEvent(eventName_, eventData_);
}
void ScheduledEvent::Reschedule() {
if (repeat_) {
executeAt_ += interval_;
}
}
// =============== EventScheduler Implementation ===============
EventScheduler& EventScheduler::GetInstance() {
static EventScheduler instance;
return instance;
}
EventScheduler::EventScheduler() : running_(false) {
StartProcessing();
}
EventScheduler::~EventScheduler() {
StopProcessing();
}
void EventScheduler::ScheduleEvent(const std::string& eventName,
const nlohmann::json& eventData,
int64_t delayMs,
bool repeat,
int64_t intervalMs) {
int64_t executeAt = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count() + delayMs;
std::lock_guard<std::mutex> lock(scheduledEventsMutex_);
scheduledEvents_.push_back(
std::make_unique<ScheduledEvent>(eventName, eventData, executeAt, repeat, intervalMs)
);
}
void EventScheduler::CancelEvent(const std::string& eventName) {
std::lock_guard<std::mutex> lock(scheduledEventsMutex_);
scheduledEvents_.erase(
std::remove_if(scheduledEvents_.begin(), scheduledEvents_.end(),
[&eventName](const std::unique_ptr<ScheduledEvent>& event) {
return event->GetName() == eventName;
}),
scheduledEvents_.end()
);
}
void EventScheduler::Update() {
ProcessScheduledEvents();
}
void EventScheduler::StartProcessing() {
if (running_) {
return;
}
running_ = true;
schedulerThread_ = std::thread(&EventScheduler::ProcessScheduledEvents, this);
}
void EventScheduler::StopProcessing() {
if (!running_) {
return;
}
running_ = false;
if (schedulerThread_.joinable()) {
schedulerThread_.join();
}
}
void EventScheduler::ProcessScheduledEvents() {
while (running_) {
{
std::lock_guard<std::mutex> lock(scheduledEventsMutex_);
auto now = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
for (auto& event : scheduledEvents_) {
if (event->ShouldExecute()) {
event->Execute();
if (event->repeat_) {
event->Reschedule();
} else {
// Mark for removal
event.reset();
}
}
}
// Remove executed non-repeating events
scheduledEvents_.erase(
std::remove_if(scheduledEvents_.begin(), scheduledEvents_.end(),
[](const std::unique_ptr<ScheduledEvent>& event) {
return !event;
}),
scheduledEvents_.end()
);
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
You can’t perform that action at this time.
