Repository navigation
Expand file tree
/
Copy pathServiceMonitor.cpp
More file actions
266 lines (225 loc) · 7.53 KB
/
Copy pathServiceMonitor.cpp
File metadata and controls
266 lines (225 loc) · 7.53 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
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2016 Sky UK
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* File: ServiceMonitor.cpp
*
*/
#include "ServiceMonitor.h"
#include <Logging.h>
ServiceMonitor::ServiceMonitor(const std::shared_ptr<IDobbyIPCUtils>& ipcUtils,
const std::shared_ptr<IDobbyUtils>& utils,
const IDobbyIPCUtils::BusType& bus,
const std::string& serviceName,
const AI_IPC::Signal& readySignal,
const AI_IPC::Method& queryMethod,
const std::function<void(State)>& stateChangeHandler)
: mUtilities(utils)
, mIPCUtilities(ipcUtils)
, mBusType(bus)
, mServiceName(serviceName)
, mReadySignal(readySignal)
, mQueryMethod(queryMethod)
, mStateChangeHandler(stateChangeHandler)
, mState(State::NotRunning)
, mServiceHandlerId(-1)
, mSignalHandlerId(-1)
, mTimerId(-1)
{
AI_LOG_FN_ENTRY();
// setup the ipc connection monitor, we want to know if the service falls
// off the bus
std::function<void(bool)> notifyFn = std::bind(&ServiceMonitor::onServiceNotification,
this, std::placeholders::_1);
mServiceHandlerId = mIPCUtilities->ipcRegisterServiceHandler(bus, serviceName, notifyFn);
// and install a listener for when the service tells us it's 'ready'
const AI_IPC::SignalHandler signalFn = std::bind(&ServiceMonitor::onReadyNotification,
this, std::placeholders::_1);
mSignalHandlerId = mIPCUtilities->ipcRegisterSignalHandler(bus, readySignal, signalFn);
// for extra belts and braces we add a period timer that runs every second
// and sends out a ping request to the service daemon if not already in
// the ready state
const std::function<bool()> timerFn = std::bind(&ServiceMonitor::onTimer, this);
mTimerId = mUtilities->startTimer(std::chrono::seconds(1), false, timerFn);
// and finally check if the service is currently available, typically the
// service is not expected to be available when the plugin is first loaded
if (mIPCUtilities->ipcServiceAvailable(bus, serviceName))
{
// the service is running
mState = State::Running;
// so send a request for it's current status
sendIsReadyRequest();
}
AI_LOG_FN_EXIT();
}
ServiceMonitor::~ServiceMonitor()
{
AI_LOG_FN_ENTRY();
// can cancel the periodic ping timer
if (mTimerId >= 0)
{
mUtilities->cancelTimer(mTimerId);
mTimerId = -1;
}
// we no longer care about the jumper daemon's state
if (mServiceHandlerId >= 0)
{
mIPCUtilities->ipcUnregisterHandler(mBusType, mServiceHandlerId);
mServiceHandlerId = -1;
}
if (mSignalHandlerId >= 0)
{
mIPCUtilities->ipcUnregisterHandler(mBusType, mSignalHandlerId);
mSignalHandlerId = -1;
}
AI_LOG_FN_EXIT();
}
// -----------------------------------------------------------------------------
/**
* @brief Returns the current state of the service
*
*
*/
ServiceMonitor::State ServiceMonitor::state() const
{
std::lock_guard<std::mutex> locker(mLock);
return mState;
}
// -----------------------------------------------------------------------------
/**
* @brief Sends out an 'is ready' method request to the service
*
*
*/
void ServiceMonitor::forceReadyCheck()
{
sendIsReadyRequest();
}
// -----------------------------------------------------------------------------
/**
* @brief Callback function called when the daemon service has been added or
* removed from the bus.
*
* We use this point to adjust the @a mState state.
*
*
*
* @param[in] added Will be true if the service arrived on the bus,
* false if it has left the bus.
*
*/
void ServiceMonitor::onServiceNotification(bool added)
{
AI_LOG_INFO("%s service %s", mServiceName.c_str(), added ? "added" : "removed");
State newState = State::NotRunning;
std::function<void(State)> handler;
{
std::unique_lock<std::mutex> locker(mLock);
newState = mState;
if (added)
{
// move our internal state to running, this is not the same as 'ready'
// which we expect to receive shortly
if (mState == State::NotRunning)
newState = State::Running;
}
else
{
newState = State::NotRunning;
}
// call the registered handler
if (newState != mState)
{
mState = newState;
handler = mStateChangeHandler;
}
}
if (handler)
handler(newState);
}
// -----------------------------------------------------------------------------
/**
* @brief Callback function called when the service daemon has sent a signal
* saying it's ready to process requests
*
* We use this point to adjust the @a mState state.
*
*
* @param[in] args Arguments supplied with the signal (ignored).
*
*/
void ServiceMonitor::onReadyNotification(const AI_IPC::VariantList& args)
{
AI_LOG_INFO("%s service is ready", mServiceName.c_str());
bool notify = false;
{std::unique_lock<std::mutex> locker(mLock);
if (mState != State::Ready)
{
// set the state back to ready
mState = State::Ready;
notify = true;
}
}
// call the registered handler
if (notify && mStateChangeHandler)
mStateChangeHandler(State::Ready);
}
// -----------------------------------------------------------------------------
/**
* @brief Timer handler called every second, it sends out a ping request to
* the daemon if we think it is not alive
*
*
*/
bool ServiceMonitor::onTimer()
{
// take the lock and check if we think the daemon isn't there
{
std::unique_lock<std::mutex> locker(mLock);
if (mState == State::Ready)
{
return true;
}
}
sendIsReadyRequest();
return true;
}
// -----------------------------------------------------------------------------
/**
* @brief Sends a method call over dbus asking the service daemon to reply with
* a ready notification / signal if it is actually there
*
*
*/
void ServiceMonitor::sendIsReadyRequest() const
{
AI_LOG_FN_ENTRY();
// send a method call to the daemon asking it to send an 'is ready' signal
// if it is in fact ready ... we are not expecting a reply to this method
// so we throw the async reply getter away
std::shared_ptr<AI_IPC::IAsyncReplyGetter> reply;
reply = mIPCUtilities->ipcInvokeMethod(mBusType, mQueryMethod, { });
if (!reply)
{
AI_LOG_ERROR("failed to invoke method '%s'",
mQueryMethod.name.c_str());
}
// there is no reply to this method, instead if the daemon is alive it will
// broadcast a 'is ready' message
AI_LOG_FN_EXIT();
}