/
HsmEventDispatcherGLibmm.cpp
170 lines (131 loc) · 5.19 KB
/
HsmEventDispatcherGLibmm.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
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
// Copyright (C) 2021 Igor Krechetov
// Distributed under MIT license. See file LICENSE for details
#include "hsmcpp/HsmEventDispatcherGLibmm.hpp"
#include <algorithm>
#include "hsmcpp/logging.hpp"
#include "hsmcpp/os/CriticalSection.hpp"
namespace hsmcpp {
constexpr const char* HSM_TRACE_CLASS = "HsmEventDispatcherGLibmm";
HsmEventDispatcherGLibmm::HsmEventDispatcherGLibmm(const size_t eventsCacheSize)
: HsmEventDispatcherBase(eventsCacheSize)
, mMainContext(Glib::MainContext::get_default()) {
HSM_TRACE_CALL_DEBUG();
}
HsmEventDispatcherGLibmm::HsmEventDispatcherGLibmm(const Glib::RefPtr<Glib::MainContext>& context, const size_t eventsCacheSize)
: HsmEventDispatcherBase(eventsCacheSize)
, mMainContext(context) {
HSM_TRACE_CALL_DEBUG();
}
HsmEventDispatcherGLibmm::~HsmEventDispatcherGLibmm() {
HSM_TRACE_CALL_DEBUG();
stop();
}
std::shared_ptr<HsmEventDispatcherGLibmm> HsmEventDispatcherGLibmm::create(const size_t eventsCacheSize) {
return std::shared_ptr<HsmEventDispatcherGLibmm>(new HsmEventDispatcherGLibmm(eventsCacheSize),
&HsmEventDispatcherBase::handleDelete);
}
std::shared_ptr<HsmEventDispatcherGLibmm> HsmEventDispatcherGLibmm::create(const Glib::RefPtr<Glib::MainContext>& context,
const size_t eventsCacheSize) {
return std::shared_ptr<HsmEventDispatcherGLibmm>(new HsmEventDispatcherGLibmm(context, eventsCacheSize),
&HsmEventDispatcherBase::handleDelete);
}
bool HsmEventDispatcherGLibmm::deleteSafe() {
bool deleteNow = false;
if (mMainContext) {
mMainContext->signal_idle().connect(sigc::bind(
[](HsmEventDispatcherGLibmm* pThis) {
if (nullptr != pThis) {
pThis->stop();
delete pThis;
}
return false; // unregister idle signal
},
this));
} else {
deleteNow = true;
}
return deleteNow;
}
void HsmEventDispatcherGLibmm::emitEvent(const HandlerID_t handlerID) {
HSM_TRACE_CALL_DEBUG();
if (mDispatcherConnection.connected() == true) {
HsmEventDispatcherBase::emitEvent(handlerID);
}
}
bool HsmEventDispatcherGLibmm::start() {
bool result = false;
if (mMainContext) {
if (!mDispatcher) {
mDispatcher.reset(new Glib::Dispatcher(mMainContext));
if (mDispatcher) {
if (mDispatcherConnection.connected() == false) {
mDispatcherConnection =
mDispatcher->connect(sigc::mem_fun(this, &HsmEventDispatcherGLibmm::dispatchPendingEvents));
}
result = true;
}
} else {
result = true;
}
}
return result;
}
void HsmEventDispatcherGLibmm::stop() {
HSM_TRACE_CALL_DEBUG();
HsmEventDispatcherBase::stop();
if (true == mDispatcherConnection.connected()) {
mDispatcherConnection.disconnect();
}
unregisterAllTimerHandlers();
unregisterAllEventHandlers();
mDispatcher.reset();
}
void HsmEventDispatcherGLibmm::unregisterAllTimerHandlers() {
CriticalSection cs(mRunningTimersSync);
for (auto it = mNativeTimerHandlers.begin(); it != mNativeTimerHandlers.end(); ++it) {
it->second.disconnect();
}
mNativeTimerHandlers.clear();
}
void HsmEventDispatcherGLibmm::startTimerImpl(const TimerID_t timerID, const unsigned int intervalMs, const bool isSingleShot) {
HSM_TRACE_CALL_DEBUG_ARGS("timerID=%d, intervalMs=%d, isSingleShot=%d",
SC2INT(timerID),
intervalMs,
BOOL2INT(isSingleShot));
CriticalSection cs(mRunningTimersSync);
auto it = mNativeTimerHandlers.find(timerID);
if (mNativeTimerHandlers.end() == it) {
mNativeTimerHandlers[timerID] = mMainContext->signal_timeout().connect(
sigc::bind(sigc::mem_fun(this, &HsmEventDispatcherGLibmm::onTimerEvent), timerID),
intervalMs);
} else {
HSM_TRACE_ERROR("timer with id=%d already exists", timerID);
}
}
void HsmEventDispatcherGLibmm::stopTimerImpl(const TimerID_t timerID) {
HSM_TRACE_CALL_DEBUG_ARGS("timerID=%d", SC2INT(timerID));
CriticalSection cs(mRunningTimersSync);
auto it = mNativeTimerHandlers.find(timerID);
if (mNativeTimerHandlers.end() != it) {
it->second.disconnect();
mNativeTimerHandlers.erase(it);
}
}
void HsmEventDispatcherGLibmm::notifyDispatcherAboutEvent() {
mDispatcher->emit();
}
bool HsmEventDispatcherGLibmm::onTimerEvent(const TimerID_t timerID) {
HSM_TRACE_CALL_DEBUG_ARGS("timerID=%d", SC2INT(timerID));
const bool restartTimer = (handleTimerEvent(timerID) > 0);
if (false == restartTimer) {
CriticalSection cs(mRunningTimersSync);
auto itTimer = mNativeTimerHandlers.find(timerID);
if (mNativeTimerHandlers.end() != itTimer) {
mNativeTimerHandlers.erase(itTimer);
} else {
HSM_TRACE_ERROR("unexpected error. timer not found");
}
}
return restartTimer;
}
} // namespace hsmcpp