forked from coderdj/redax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccontrol.cc
134 lines (116 loc) · 4.23 KB
/
ccontrol.cc
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
#include "CControl_Handler.hh"
int main(int argc, char** argv){
mongocxx::instance instance{};
// Parse arguments
if(argc < 3){
std::cout<<"USAGE: ./ccontrol {ID} {MONGO_URI}"<<std::endl;
std::cout<<"Where MONGO_URI is the URI of the command DB"<<std::endl;
return -1;
}
std::string dbname = "xenonnt";
if(argc >=3)
dbname = argv[3];
// Control and status DB connectivity
// We're going to poll control for commands
// and update status with a heartbeat
std::string mongo_uri = argv[2];
mongocxx::uri uri(mongo_uri.c_str());
mongocxx::client client(uri);
mongocxx::database db = client[dbname];
mongocxx::collection control = db["control"];
mongocxx::collection status = db["status"];
mongocxx::collection options_collection = db["options"];
// Build a unique name for this process
// we trust that the user has given {ID} uniquely
char chostname[HOST_NAME_MAX];
gethostname(chostname, HOST_NAME_MAX);
std::string hostname = chostname;
hostname += "_controller_";
hostname += argv[1];
std::cout<<"I dub thee "<<hostname<<std::endl;
// Logging
MongoLog *logger = new MongoLog();
int ret = logger->Initialize(mongo_uri, dbname, "log", hostname,
"", true);
if(ret!=0){
std::cout<<"Log couldn't be initialized. Exiting."<<std::endl;
exit(-1);
}
// Options
Options *options = NULL;
// Holds session data
CControl_Handler *fHandler = new CControl_Handler(logger, hostname);
while(1){
mongocxx::cursor cursor = control.find
(
bsoncxx::builder::stream::document{}<< "host" << hostname <<"acknowledged" <<
bsoncxx::builder::stream::open_document << "$ne" << hostname <<
bsoncxx::builder::stream::close_document << bsoncxx::builder::stream::finalize
);
for (auto doc : cursor) {
// Acknowledge the commands
control.update_one
(bsoncxx::builder::stream::document{} << "_id" << doc["_id"].get_oid() <<
bsoncxx::builder::stream::finalize,
bsoncxx::builder::stream::document{} << "$push" <<
bsoncxx::builder::stream::open_document << "acknowledged" << hostname <<
bsoncxx::builder::stream::close_document <<
bsoncxx::builder::stream::finalize
);
// Strip data from the supplied doc
int run = -1;
std::string command = "";
try{
command = doc["command"].get_utf8().value.to_string();
if(command == "arm" )
run = doc["number"].get_int32();
}
catch(const std::exception E){
logger->Entry("ccontrol: Received a document from the dispatcher missing [command|number]",
MongoLog::Warning);
continue;
}
// If the command is arm gonna use the options file to load the V2718, DDC10, etc...settings
std::string mode = "";
if(command == "arm"){
try{
mode = doc["mode"].get_utf8().value.to_string();
}
catch(const std::exception E){
logger->Entry("ccontrol: Received an arm document with no run mode",MongoLog::Warning);
}
// Get an override doc from the 'options_override' field if it exists
std::string override_json = "";
try{
bsoncxx::document::view oopts = doc["options_override"].get_document().view();
override_json = bsoncxx::to_json(oopts);
}
catch(const std::exception E){
logger->Entry("No override options provided", MongoLog::Debug);
}
//Here are our options
if(options != NULL)
delete options;
options = new Options(logger, mode, options_collection, override_json);
// Initialise the V2178, V1495 and DDC10...etc.
if(fHandler->DeviceArm(run, options) != 0){
logger->Entry("Failed to initialize devices", MongoLog::Error);
}
} // end if "arm" command
else if(command == "start"){
if((fHandler->DeviceStart()) != 0){
logger->Entry("Failed to start devices", MongoLog::Debug);
}
}
else if(command == "stop"){
if((fHandler->DeviceStop()) != 0){
logger->Entry("Failed to stop devices", MongoLog::Debug);
}
}
} //end for
// Report back on what we are doing
status.insert_one(fHandler->GetStatusDoc(hostname));
usleep(1000000);
}
return 0;
}