forked from RangeNetworks/NodeManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeManager.cpp
304 lines (254 loc) · 8.81 KB
/
NodeManager.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
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
/* NodeManager/NodeManager.cpp */
/*-
* Copyright 2013, 2014 Range Networks, Inc.
*
* This software is distributed under the terms of the GNU Affero Public License.
* See the COPYING file in the main directory for details.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "NodeManager.h"
// TODO : not a member of NodeManager, fails to compile...
void *NodeManager::commandsLoop(void *arg)
{
// TODO : i hate this, need to figure out a way to satisfy the compiler and pass
// NodeManager::worker directly to mServer.start();
// (pat) You make it a static method and call it via NodeManager::commandLoop.
// In general, 'this' would be passed as the void* arg, which allows multiple instances, instead of using
// gNodeManager.
NodeManager *object = static_cast<NodeManager *>(arg);
void *ret = NULL;
try {
// This errors out when OpenBTS exits.
ret = object->commandsWorker(NULL);
} catch (...) {
LOG(INFO) << "Exception occurred in NodeManager"; // (pat 3-2014) added. Evidently this happens
// regularly, so make it an INFO message.
}
return ret;
}
void *NodeManager::commandsWorker(void *)
{
while (1) {
JsonBox::Value in;
JsonBox::Value out;
JsonBox::Object jsonIn;
JsonBox::Object jsonOut;
in.loadFromString(readRequest());
jsonIn = in.getObject();
std::string command = jsonIn["command"].getString();
if (command.compare("version") == 0) {
jsonOut = version(jsonIn);
} else if (command.compare("config") == 0) {
jsonOut = config(jsonIn);
} else if (mAppLogicHandler != NULL) {
jsonOut = mAppLogicHandler(jsonIn);
} else {
jsonOut["code"] = JsonBox::Value(501);
}
std::stringstream jsonResponse;
JsonBox::Value(jsonOut).writeToStream(jsonResponse);
writeResponse(jsonResponse.str());
}
return NULL;
}
void NodeManager::start(int commandsPort, int eventsPort)
{
char commandsAddress[24];
char eventsAddress[24];
signal(SIGPIPE, SIG_IGN);
snprintf(commandsAddress, sizeof(commandsAddress), "tcp://127.0.0.1:%d", commandsPort);
// (pat 3-2014) The zmq library throws a standard exception if it cannot bind, with no useful description in the
// exception whatsoever.
try {
mCommandsSocket.bind(commandsAddress);
} catch (...) {
LOG(EMERG) << "Could not bind commandsAddress: " << commandsAddress
<< " (possibly in use?) Exiting...";
exit(0); // Thats it.
}
mCommandsServer.start(NodeManager::commandsLoop, static_cast<void *>(this));
if (eventsPort) {
snprintf(eventsAddress, sizeof(eventsAddress), "tcp://127.0.0.1:%d", eventsPort);
try {
mEventsSocket.bind(eventsAddress);
} catch (...) {
LOG(EMERG) << "Could not bind eventsAddress: " << eventsAddress
<< " (possibly in use?) Exiting...";
exit(0);
}
}
}
std::string NodeManager::readRequest()
{
zmq::message_t payload;
mCommandsSocket.recv(&payload);
return std::string(static_cast<char *>(payload.data()), payload.size());
}
void NodeManager::writeResponse(const std::string &message)
{
zmq::message_t payload(message.length());
memcpy((void *)payload.data(), message.c_str(), message.length());
mCommandsSocket.send(payload);
}
void NodeManager::publishEvent(const std::string &name, const std::string &version, const JsonBox::Object &data)
{
ScopedLock lock(mLock);
std::stringstream ss;
JsonBox::Object e;
struct timeval tv;
std::string timestamp;
gettimeofday(&tv, NULL);
unsigned long long tmp = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
ss << tmp;
timestamp = ss.str();
ss.str("");
e["name"] = JsonBox::Value(name);
e["version"] = JsonBox::Value(version);
e["timestamp"] = JsonBox::Value(timestamp);
e["data"] = JsonBox::Object(data);
JsonBox::Value(e).writeToStream(ss);
std::string message = ss.str();
zmq::message_t payload(message.length());
memcpy((void *)payload.data(), message.c_str(), message.length());
mEventsSocket.send(payload);
}
JsonBox::Object NodeManager::version(JsonBox::Object command)
{
JsonBox::Object response;
response["code"] = JsonBox::Value(200);
response["data"] = JsonBox::Value(gVersionString);
return response;
}
JsonBox::Object NodeManager::configKeyToJSON(const std::string &key)
{
JsonBox::Object o;
o["key"] = JsonBox::Value(key);
o["value"] = JsonBox::Value(gConfig.getStr(key));
o["description"] = gConfig.mSchema[key].getDescription();
o["units"] = gConfig.mSchema[key].getUnits();
o["type"] = ConfigurationKey::typeToString(gConfig.mSchema[key].getType());
o["defaultValue"] = gConfig.mSchema[key].getDefaultValue();
o["validValues"] = gConfig.mSchema[key].getValidValues();
o["visibility"] = ConfigurationKey::visibilityLevelToString(gConfig.mSchema[key].getVisibility());
o["static"] = gConfig.mSchema[key].isStatic();
o["scope"] = JsonBox::Value((int)gConfig.mSchema[key].getScope());
if (gConfig.mSchema[key].getType() == ConfigurationKey::VALRANGE) {
std::string min;
std::string max;
std::string stepping;
ConfigurationKey::getMinMaxStepping(gConfig.mSchema[key], min, max, stepping);
o["min"] = min;
o["max"] = max;
o["stepping"] = stepping;
}
return o;
}
JsonBox::Object NodeManager::config(JsonBox::Object command)
{
JsonBox::Object response;
std::string action = command["action"].getString();
std::string key = command["key"].getString();
if (action.compare("read") == 0) {
if (key.length() == 0) {
ConfigurationKeyMap::iterator mp;
JsonBox::Array a;
JsonBox::Object o;
mp = gConfig.mSchema.begin();
while (mp != gConfig.mSchema.end()) {
a.push_back(configKeyToJSON(mp->first));
mp++;
}
response["code"] = JsonBox::Value(200);
response["data"] = a;
} else if (!gConfig.keyDefinedInSchema(key)) {
response["code"] = JsonBox::Value(404);
} else {
response["code"] = JsonBox::Value(200);
response["data"] = configKeyToJSON(key);
}
} else if (action.compare("update") == 0) {
std::string value = command["value"].getString();
// bail if we don't have this key in the schema
if (!gConfig.keyDefinedInSchema(key)) {
response["code"] = JsonBox::Value(404);
return response;
}
// bail if the new value is invalid
if (!gConfig.isValidValue(key, value)) {
response["code"] = JsonBox::Value(406);
return response;
}
// we're going to try to update the value, grab the old one
std::string oldValue = gConfig.getStr(key);
// bail if we're not actually changing anything here
if (value.compare(oldValue) == 0) {
response["code"] = JsonBox::Value(304);
return response;
}
if (!gConfig.set(key, value)) {
response["code"] = JsonBox::Value(500);
} else {
if (gConfig.mSchema[key].isStatic()) {
addDirtyConfigurationKey(key, oldValue, value);
}
if (mAppConfigChangeHandler != NULL) {
mAppConfigChangeHandler(key, oldValue, value);
}
response["dirty"] = JsonBox::Value(getDirtyConfigurationKeyCount());
response["code"] = JsonBox::Value(204);
}
} else if (action.compare("dirty") == 0) {
response["code"] = JsonBox::Value(200);
response["data"] = JsonBox::Value(getDirtyConfigurationKeys());
} else {
response["code"] = JsonBox::Value(501);
}
return response;
}
void NodeManager::setAppLogicHandler(JsonBox::Object (*wAppLogicHandler)(JsonBox::Object &))
{
mAppLogicHandler = wAppLogicHandler;
}
void NodeManager::setAppConfigChangeHandler(
bool (*wAppConfigChangeHandler)(std::string &, std::string &, std::string &))
{
mAppConfigChangeHandler = wAppConfigChangeHandler;
}
int NodeManager::addDirtyConfigurationKey(std::string &name, std::string &oldValue, std::string &newValue)
{
std::map<std::string, std::string>::iterator it = mDirtyConfigurationKeys.find(name);
// key is new to us, just add to map
if (it == mDirtyConfigurationKeys.end()) {
mDirtyConfigurationKeys[name] = oldValue;
// key was already marked dirty, lets see if we can mark it clean
} else if (mDirtyConfigurationKeys[name] == newValue) {
mDirtyConfigurationKeys.erase(it);
}
return mDirtyConfigurationKeys.size();
}
JsonBox::Object NodeManager::getDirtyConfigurationKeys()
{
JsonBox::Object o;
std::map<std::string, std::string>::iterator it = mDirtyConfigurationKeys.begin();
while (it != mDirtyConfigurationKeys.end()) {
o[it->first] = it->second;
it++;
}
return o;
}