-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathcontroller_connectors.cpp
More file actions
268 lines (251 loc) · 9.83 KB
/
controller_connectors.cpp
File metadata and controls
268 lines (251 loc) · 9.83 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
#include "controller_connectors.h"
#include "controller_storage.h"
#include <cstring> // strcpy
#include <mist/config.h>
#include <mist/defines.h>
#include <mist/json.h>
#include <mist/procs.h>
#include <mist/shared_memory.h>
#include <mist/timing.h>
#include <mist/tinythread.h>
#include <mist/triggers.h>
#include <mist/util.h>
#include <stdio.h> // cout, cerr
#include <string>
#include <sys/stat.h> //stat
#include <iostream>
#include <unistd.h>
///\brief Holds everything unique to the controller.
namespace Controller{
static std::set<size_t> needsReload; ///< List of connector indices that needs a reload
static std::map<std::string, pid_t> currentConnectors; ///< The currently running connectors.
void reloadProtocol(size_t indice){needsReload.insert(indice);}
/// Updates the shared memory page with active connectors
void saveActiveConnectors(bool forceOverride){
IPC::sharedPage f("MstCnns", 4096, forceOverride, false);
if (!f.mapped){
if (!forceOverride){
saveActiveConnectors(true);
return;
}
if (!f.mapped){
FAIL_MSG("Could not store connector data!");
return;
}
}
memset(f.mapped, 0, 32);
Util::RelAccX A(f.mapped, false);
if (!A.isReady()){
A.addField("cmd", RAX_128STRING);
A.addField("pid", RAX_64UINT);
A.setReady();
}
uint32_t count = 0;
std::map<std::string, pid_t>::iterator it;
for (it = currentConnectors.begin(); it != currentConnectors.end(); ++it){
A.setString("cmd", it->first, count);
A.setInt("pid", it->second, count);
++count;
}
A.setRCount(count);
f.master = false; // Keep the shm page around, don't kill it
}
/// Reads active connectors from the shared memory pages
void loadActiveConnectors(){
IPC::sharedPage f("MstCnns", 4096, false, false);
const Util::RelAccX A(f.mapped, false);
if (A.isReady()){
INFO_MSG("Reloading existing connectors to complete rolling restart");
for (uint32_t i = 0; i < A.getRCount(); ++i){
char *p = A.getPointer("cmd", i);
if (p != 0 && p[0] != 0){
currentConnectors[p] = A.getInt("pid", i);
Util::Procs::remember(A.getInt("pid", i));
kill(A.getInt("pid", i), SIGUSR1);
}
}
}
}
/// Deletes the shared memory page with connector information
/// in preparation of shutdown.
void prepareActiveConnectorsForShutdown(){
IPC::sharedPage f("MstCnns", 4096, false, false);
if (f){f.master = true;}
}
/// Forgets all active connectors, preventing them from being killed,
/// in preparation of reload.
void prepareActiveConnectorsForReload(){
saveActiveConnectors();
std::map<std::string, pid_t>::iterator it;
for (it = currentConnectors.begin(); it != currentConnectors.end(); ++it){
Util::Procs::forget(it->second);
}
currentConnectors.clear();
}
static inline void buildPipedPart(JSON::Value &p, std::deque<std::string> &argDeq, const JSON::Value &argset){
jsonForEachConst(argset, it){
if (it->isMember("option") && p.isMember(it.key())){
if (!it->isMember("type")){
if (JSON::Value(p[it.key()]).asBool()){
argDeq.push_back((*it)["option"]);
}
continue;
}
if ((*it)["type"].asStringRef() == "str" && !p[it.key()].isString()){
p[it.key()] = p[it.key()].asString();
}
if ((*it)["type"].asStringRef() == "uint" || (*it)["type"].asStringRef() == "int" ||
(*it)["type"].asStringRef() == "debug"){
p[it.key()] = JSON::Value(p[it.key()].asInt()).asString();
}
if ((*it)["type"].asStringRef() == "inputlist" && p[it.key()].isArray()){
jsonForEach(p[it.key()], iVal){
argDeq.push_back((*it)["option"]);
argDeq.push_back(iVal->asString());
}
continue;
}
if (p[it.key()].asStringRef().size() > 0){
argDeq.push_back((*it)["option"]);
argDeq.push_back(p[it.key()].asString());
}else{
argDeq.push_back((*it)["option"]);
}
}
}
}
static inline void buildPipedArguments(JSON::Value &p, std::deque<std::string> &argDeq, const JSON::Value &capabilities){
static std::string tmparg;
tmparg = std::string("MistOut") + p["connector"].asStringRef();
if (!Util::Procs::HasMistBinary(tmparg)){
tmparg = std::string("MistConn") + p["connector"].asStringRef();
if (!Util::Procs::HasMistBinary(tmparg)) {
return;
}
}
argDeq.push_back(tmparg);
const JSON::Value &pipedCapa = capabilities["connectors"][p["connector"].asStringRef()];
if (pipedCapa.isMember("required")){buildPipedPart(p, argDeq, pipedCapa["required"]);}
if (pipedCapa.isMember("optional")){buildPipedPart(p, argDeq, pipedCapa["optional"]);}
}
///\brief Checks current protocol configuration, updates state of enabled connectors if
/// neccessary. \param p An object containing all protocols. \param capabilities An object
/// containing the detected capabilities. \returns True if any action was taken
///
/// \triggers
/// The `"OUTPUT_START"` trigger is global, and is ran whenever a new protocol listener is
/// started. It cannot be cancelled. Its payload is:
/// ~~~~~~~~~~~~~~~
/// output listener commandline
/// ~~~~~~~~~~~~~~~
/// The `"OUTPUT_STOP"` trigger is global, and is ran whenever a protocol listener is terminated.
/// It cannot be cancelled. Its payload is:
/// ~~~~~~~~~~~~~~~
/// output listener commandline
/// ~~~~~~~~~~~~~~~
bool CheckProtocols(JSON::Value &p, const JSON::Value &capabilities){
std::set<std::string> runningConns;
// used for building args
int err = fileno(stderr);
std::string tmp;
jsonForEach(p, ait){
std::string prevOnline = (*ait)["online"].asString();
const std::string &connName = (*ait)["connector"].asStringRef();
// do not further parse if there's no connector name
if (!(*ait).isMember("connector") || connName == ""){
(*ait)["online"] = "Missing connector name";
continue;
}
// ignore connectors that are not installed
if (!capabilities.isMember("connectors") || !capabilities["connectors"].isMember(connName)){
(*ait)["online"] = "Not installed";
if ((*ait)["online"].asString() != prevOnline){
Log("WARN",
connName + " connector is enabled but doesn't exist on system! Ignoring connector.");
}
continue;
}
if (capabilities["connectors"][connName].isMember("PUSHONLY")){
(*ait)["online"] = "Push-only";
if ((*ait)["online"].asString() != prevOnline){
Log("WARN",
connName + " connector is enabled but can only be used by the pushing system! Ignoring connector.");
}
continue;
}
// list connectors that go through HTTP as 'enabled' without actually running them.
const JSON::Value &connCapa = capabilities["connectors"][connName];
if (connCapa.isMember("socket") || (connCapa.isMember("deps") && connCapa["deps"].asStringRef() == "HTTP")){
(*ait)["online"] = "Enabled";
continue;
}
// check required parameters, skip if anything is missing
if (connCapa.isMember("required")){
bool gotAll = true;
jsonForEachConst(connCapa["required"], it){
if (!(*ait).isMember(it.key()) || (*ait)[it.key()].asStringRef().size() < 1){
gotAll = false;
(*ait)["online"] = "Invalid configuration";
if ((*ait)["online"].asString() != prevOnline){
Log("WARN", connName + " connector is missing required parameter " + it.key() + "! Ignoring connector.");
}
break;
}
}
if (!gotAll){continue;}
}
// remove current online status
(*ait).removeMember("online");
/// \todo Check dependencies?
// set current online status
std::string myCmd = (*ait).toString();
runningConns.insert(myCmd);
if (currentConnectors.count(myCmd) && Util::Procs::isActive(currentConnectors[myCmd])){
(*ait)["online"] = 1;
// Reload connectors that need it
if (needsReload.count(ait.num())){
kill(currentConnectors[myCmd], SIGUSR1);
needsReload.erase(ait.num());
}
}else{
(*ait)["online"] = 0;
}
}
bool action = false;
// shut down deleted/changed connectors
std::map<std::string, pid_t>::iterator it;
if (currentConnectors.size()){
for (it = currentConnectors.begin(); it != currentConnectors.end(); it++){
if (!runningConns.count(it->first)){
if (Util::Procs::isActive(it->second)){
Log("CONF", "Stopping connector " + it->first);
action = true;
Util::Procs::Stop(it->second);
Triggers::doTrigger("OUTPUT_STOP", it->first); // LTS
}
currentConnectors.erase(it);
if (!currentConnectors.size()){break;}
it = currentConnectors.begin();
}
}
}
// start up new/changed connectors
while (runningConns.size() && conf.is_active){
if (!currentConnectors.count(*runningConns.begin()) ||
!Util::Procs::isActive(currentConnectors[*runningConns.begin()])){
Log("CONF", "Starting connector: " + *runningConns.begin());
action = true;
std::deque<std::string> argDeq;
// get args for this connector
JSON::Value p = JSON::fromString(*runningConns.begin());
buildPipedArguments(p, argDeq, capabilities);
// start piped w/ generated args
currentConnectors[*runningConns.begin()] = Util::Procs::StartPipedMist(argDeq, 0, 0, &err);
Triggers::doTrigger("OUTPUT_START", *runningConns.begin()); // LTS
}
runningConns.erase(runningConns.begin());
}
if (action){saveActiveConnectors();}
return action;
}
}// namespace Controller