1 | #include <functional>
|
---|
2 | #include <boost/algorithm/string/join.hpp>
|
---|
3 |
|
---|
4 | #include "Database.h"
|
---|
5 |
|
---|
6 | #include "Dim.h"
|
---|
7 | #include "Event.h"
|
---|
8 | #include "Shell.h"
|
---|
9 | #include "StateMachineDim.h"
|
---|
10 | #include "StateMachineAsio.h"
|
---|
11 | #include "Connection.h"
|
---|
12 | #include "LocalControl.h"
|
---|
13 | #include "Configuration.h"
|
---|
14 | #include "Console.h"
|
---|
15 | #include "Converter.h"
|
---|
16 |
|
---|
17 | #include "tools.h"
|
---|
18 | #include "../externals/nova.h"
|
---|
19 |
|
---|
20 | #include "HeadersGCN.h"
|
---|
21 | #include "HeadersToO.h"
|
---|
22 |
|
---|
23 | #include <QtXml/QDomDocument>
|
---|
24 |
|
---|
25 | namespace ba = boost::asio;
|
---|
26 | namespace bs = boost::system;
|
---|
27 | namespace dummy = ba::placeholders;
|
---|
28 |
|
---|
29 | using namespace std;
|
---|
30 | using namespace GCN;
|
---|
31 |
|
---|
32 | // ------------------------------------------------------------------------
|
---|
33 |
|
---|
34 | class ConnectionGCN : public Connection
|
---|
35 | {
|
---|
36 | private:
|
---|
37 | map<uint16_t, GCN::PaketType_t> fTypes;
|
---|
38 |
|
---|
39 | vector<string> fEndPoints;
|
---|
40 | int fEndPoint;
|
---|
41 |
|
---|
42 | bool fIsVerbose;
|
---|
43 | bool fDebugRx;
|
---|
44 |
|
---|
45 | string fUri;
|
---|
46 |
|
---|
47 | uint32_t fRxSize;
|
---|
48 | vector<char> fRxData;
|
---|
49 |
|
---|
50 | Time fLastKeepAlive;
|
---|
51 |
|
---|
52 | QString GetParamValue(const QDomElement &what, const string &name)
|
---|
53 | {
|
---|
54 | const QDomNodeList param = what.elementsByTagName("Param");
|
---|
55 | for (int i=0; i<param.count(); i++)
|
---|
56 | {
|
---|
57 | const QDomElement elem = param.at(i).toElement();
|
---|
58 | if (elem.attribute("name").toStdString()==name)
|
---|
59 | return elem.attribute("value");
|
---|
60 | }
|
---|
61 |
|
---|
62 | return "";
|
---|
63 | }
|
---|
64 |
|
---|
65 | GCN::PaketPtr GetType(const QDomElement &what)
|
---|
66 | {
|
---|
67 | const auto value = GetParamValue(what, "Packet_Type");
|
---|
68 | if (value.isEmpty())
|
---|
69 | return GCN::PaketTypes.end();
|
---|
70 |
|
---|
71 | const uint16_t val = value.toUInt();
|
---|
72 |
|
---|
73 | const auto it = GCN::PaketTypes.find(val);
|
---|
74 |
|
---|
75 | if (it==GCN::PaketTypes.end())
|
---|
76 | Warn("Unknown paket type "+to_string(val)+".");
|
---|
77 |
|
---|
78 | return it;
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | int ProcessXml(const QDomElement &root)
|
---|
83 | {
|
---|
84 | if (root.isNull())
|
---|
85 | return -255;
|
---|
86 |
|
---|
87 | const string role = root.attribute("role", "").toStdString();
|
---|
88 | const string trn = root.tagName().toStdString();
|
---|
89 |
|
---|
90 | // A full description can be found at http://voevent.dc3.com/schema/default.html
|
---|
91 |
|
---|
92 | if (trn=="trn:Transport")
|
---|
93 | {
|
---|
94 | if (role=="iamalive")
|
---|
95 | {
|
---|
96 | const QDomElement orig = root.firstChildElement("Origin");
|
---|
97 | const QDomElement time = root.firstChildElement("TimeStamp");
|
---|
98 | if (orig.isNull() || time.isNull())
|
---|
99 | return -254;
|
---|
100 |
|
---|
101 | fLastKeepAlive = Time(time.text().toStdString());
|
---|
102 |
|
---|
103 | if (fIsVerbose)
|
---|
104 | {
|
---|
105 | Out() << Time().GetAsStr() << " ----- " << trn << " [" << role << "] -----" << endl;
|
---|
106 | Out() << " " << time.tagName().toStdString() << " = " << fLastKeepAlive.GetAsStr() << '\n';
|
---|
107 | Out() << " " << orig.tagName().toStdString() << " = " << orig.text().toStdString() << '\n';
|
---|
108 | Out() << endl;
|
---|
109 | }
|
---|
110 |
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 |
|
---|
114 | return false;
|
---|
115 | }
|
---|
116 |
|
---|
117 | ofstream fout("gcn.stream", ios::app);
|
---|
118 | fout << "------------------------------------------------------------------------------\n" << fRxData.data() << endl;
|
---|
119 |
|
---|
120 | if (trn=="voe:VOEvent")
|
---|
121 | {
|
---|
122 | // WHAT: http://gcn.gsfc.nasa.gov/tech_describe.html
|
---|
123 | const QDomElement who = root.firstChildElement("Who");
|
---|
124 | const QDomElement what = root.firstChildElement("What");
|
---|
125 | const QDomElement when = root.firstChildElement("WhereWhen");
|
---|
126 | //const QDomElement how = root.firstChildElement("How");
|
---|
127 | //const QDomElement why = root.firstChildElement("Why");
|
---|
128 | //const QDomElement cite = root.firstChildElement("Citations");
|
---|
129 | //const QDomElement desc = root.firstChildElement("Description");
|
---|
130 | //const QDomElement ref = root.firstChildElement("Reference");
|
---|
131 | if (who.isNull() || what.isNull() || when.isNull())
|
---|
132 | return -253;
|
---|
133 |
|
---|
134 | const auto ptype = GetType(what);
|
---|
135 |
|
---|
136 | const QDomElement date = who.firstChildElement("Date");
|
---|
137 | const QDomElement author = who.firstChildElement("Author");
|
---|
138 | const QDomElement sname = author.firstChildElement("shortName");
|
---|
139 | const QDomElement desc = what.firstChildElement("Description");
|
---|
140 |
|
---|
141 | const QDomElement obsdat = when.firstChildElement("ObsDataLocation");
|
---|
142 | const QDomElement obsloc = obsdat.firstChildElement("ObservationLocation");
|
---|
143 | const QDomElement coord = obsloc.firstChildElement("AstroCoords");
|
---|
144 |
|
---|
145 | const QDomElement time = coord.firstChildElement("Time").firstChildElement("TimeInstant").firstChildElement("ISOTime");
|
---|
146 | const QDomElement pos2d = coord.firstChildElement("Position2D");
|
---|
147 | const QDomElement name1 = pos2d.firstChildElement("Name1");
|
---|
148 | const QDomElement name2 = pos2d.firstChildElement("Name2");
|
---|
149 | const QDomElement val2 = pos2d.firstChildElement("Value2");
|
---|
150 | const QDomElement c1 = val2.firstChildElement("C1");
|
---|
151 | const QDomElement c2 = val2.firstChildElement("C2");
|
---|
152 | const QDomElement errad = pos2d.firstChildElement("Error2Radius");
|
---|
153 |
|
---|
154 | const auto &id = ptype->first;
|
---|
155 |
|
---|
156 | if (id==0)
|
---|
157 | {
|
---|
158 | Warn("Packet ID = 0");
|
---|
159 | return -252;
|
---|
160 | }
|
---|
161 |
|
---|
162 | // Gravitational wave event
|
---|
163 | const bool is_gw = id==150 || id==151 || id==152 || id==153 || id==164;
|
---|
164 |
|
---|
165 | // No shortName
|
---|
166 | const bool no_sn = id==171 || id==173 || id==174;
|
---|
167 |
|
---|
168 | // Required keywords
|
---|
169 | vector<string> missing;
|
---|
170 | if (date.isNull())
|
---|
171 | missing.emplace_back("Date");
|
---|
172 | if (author.isNull())
|
---|
173 | missing.emplace_back("Author");
|
---|
174 | if (sname.isNull() && !is_gw && !no_sn)
|
---|
175 | missing.emplace_back("shortName");
|
---|
176 | if (obsdat.isNull())
|
---|
177 | missing.emplace_back("ObsDataLocation");
|
---|
178 | if (obsloc.isNull())
|
---|
179 | missing.emplace_back("ObservationLocation");
|
---|
180 | if (coord.isNull())
|
---|
181 | missing.emplace_back("AstroCoords");
|
---|
182 | if (time.isNull())
|
---|
183 | missing.emplace_back("Time/TimeInstant/ISOTime");
|
---|
184 | if (pos2d.isNull() && !is_gw)
|
---|
185 | missing.emplace_back("Position2D");
|
---|
186 | if (name1.isNull() && !is_gw)
|
---|
187 | missing.emplace_back("Name1");
|
---|
188 | if (name1.isNull() && !is_gw)
|
---|
189 | missing.emplace_back("Name2");
|
---|
190 | if (val2.isNull() && !is_gw)
|
---|
191 | missing.emplace_back("Value2");
|
---|
192 | if (c1.isNull() && !is_gw)
|
---|
193 | missing.emplace_back("C1");
|
---|
194 | if (c2.isNull() && !is_gw)
|
---|
195 | missing.emplace_back("C2");
|
---|
196 | if (errad.isNull() && !is_gw)
|
---|
197 | missing.emplace_back("Error2Radius");
|
---|
198 |
|
---|
199 | if (!missing.empty())
|
---|
200 | {
|
---|
201 | Warn("Missing elements: "+boost::algorithm::join(missing, ", "));
|
---|
202 | return -id;
|
---|
203 | }
|
---|
204 |
|
---|
205 | // 59/31: Konus LC / IPN raw [observation]
|
---|
206 | // 110: Fermi GBM (ART) [observation] (Initial) // Stop data taking
|
---|
207 | // 111: Fermi GBM (FLT) [observation] (after ~2s) // Start pointing/run
|
---|
208 | // 112: Fermi GBM (GND) [observation] (after 2-20s) // Refine pointing
|
---|
209 | // 115: Fermi GBM position [observation] (final ~hours)
|
---|
210 | //
|
---|
211 | // 51: Intergal pointdir [utility]
|
---|
212 | // 83: Swift pointdir [utility]
|
---|
213 | // 129: Fermi pointdir [utility]
|
---|
214 | //
|
---|
215 | // 2: Test coord ( 1) [test]
|
---|
216 | // 44: HETE test ( 41- 43) [test]
|
---|
217 | // 52: Integral SPIACS [test]
|
---|
218 | // 53: Integral Wakeup [test]
|
---|
219 | // 54: Integral refined [test]
|
---|
220 | // 55: Integral Offline [test]
|
---|
221 | // 56: Integral Weak [test]
|
---|
222 | // 82: BAT GRB pos test ( 61) [test]
|
---|
223 | // 109: AGILE GRB pos test (100-103) [test]
|
---|
224 | // 119: Fermi GRB pos test (111-113) [test]
|
---|
225 | // 124: Fermi LAT pos upd test (120-122) [test]
|
---|
226 | // 136: MAXI coord test ( 134) [test]
|
---|
227 | //
|
---|
228 | // Integral: RA=1.2343, Dec=2.3456
|
---|
229 | //
|
---|
230 |
|
---|
231 | /*
|
---|
232 | 54
|
---|
233 | ==
|
---|
234 | <Group name="Test_mpos" >
|
---|
235 | <Param name="Test_Notice" value="true" />
|
---|
236 | </Group>
|
---|
237 |
|
---|
238 |
|
---|
239 | 82
|
---|
240 | ==
|
---|
241 | <Group name="Solution_Status" >
|
---|
242 | <Param name="Test_Submission" value="false" />
|
---|
243 | </Group>
|
---|
244 |
|
---|
245 |
|
---|
246 | 115
|
---|
247 | ===
|
---|
248 | 2013-07-20 19:04:13: TIME = 2013-07-20 02:46:40
|
---|
249 |
|
---|
250 | <Group name="Trigger_ID" >
|
---|
251 | <Param name="Test_Submission" value="false" />
|
---|
252 | </Group>
|
---|
253 | */
|
---|
254 |
|
---|
255 | // ----------------------------- Create Name ----------------------
|
---|
256 |
|
---|
257 | const auto &paket = ptype->second;
|
---|
258 |
|
---|
259 | string name;
|
---|
260 | bool prefix = true;
|
---|
261 |
|
---|
262 | switch (id)
|
---|
263 | {
|
---|
264 | // case 60:
|
---|
265 | // case 61:
|
---|
266 | // case 62:
|
---|
267 | //
|
---|
268 | // case 110:
|
---|
269 | // case 111:
|
---|
270 | // case 112:
|
---|
271 | // case 115:
|
---|
272 | // name = GetParamValue(what, "TRIGGER_NUM").toStdString();
|
---|
273 | // break;
|
---|
274 | //
|
---|
275 | // case 123:
|
---|
276 | // name = GetParamValue(what, "REF_NUM").toStdString();
|
---|
277 | // break;
|
---|
278 |
|
---|
279 | case 125:
|
---|
280 | name = GetParamValue(what, "SourceName").toStdString();
|
---|
281 | prefix = false;
|
---|
282 | break;
|
---|
283 |
|
---|
284 | case 140:
|
---|
285 | name = GetParamValue(what, "TrigID").toStdString();
|
---|
286 | if (name[0]=='-')
|
---|
287 | name.erase(name.begin());
|
---|
288 | break;
|
---|
289 |
|
---|
290 | case 157:
|
---|
291 | case 158:
|
---|
292 | {
|
---|
293 | const string event_id = GetParamValue(what, "event_id").toStdString();
|
---|
294 | const string run_id = GetParamValue(what, "run_id").toStdString();
|
---|
295 | name = event_id+"_"+run_id;
|
---|
296 | break;
|
---|
297 | }
|
---|
298 |
|
---|
299 | case 171:
|
---|
300 | case 173:
|
---|
301 | case 174:
|
---|
302 | {
|
---|
303 | const string run_id = GetParamValue(what, "run_id").toStdString();
|
---|
304 | const string event_id = GetParamValue(what, "event_id").toStdString();
|
---|
305 | name = run_id+"_"+event_id;
|
---|
306 | break;
|
---|
307 | }
|
---|
308 |
|
---|
309 | // Missing ID
|
---|
310 | // case 51:
|
---|
311 | // case 83:
|
---|
312 | // case 119:
|
---|
313 | // case 129:
|
---|
314 | default:
|
---|
315 | name = GetParamValue(what, "TrigID").toStdString();
|
---|
316 | }
|
---|
317 |
|
---|
318 | if (name.empty() || name=="0")
|
---|
319 | {
|
---|
320 | Warn("Missing ID... cannot create default source name... using date instead.");
|
---|
321 | name = Time().GetAsStr("%Y%m%d_%H%M%S");
|
---|
322 | prefix = true;
|
---|
323 | }
|
---|
324 |
|
---|
325 | if (prefix)
|
---|
326 | name.insert(0, paket.instrument+"#");
|
---|
327 |
|
---|
328 | // ----------------------------------------------------------------
|
---|
329 |
|
---|
330 | const string unit = pos2d.attribute("unit").toStdString();
|
---|
331 |
|
---|
332 | const double ra = c1.text().toDouble();
|
---|
333 | const double dec = c2.text().toDouble();
|
---|
334 | const double err = errad.text().toDouble();
|
---|
335 |
|
---|
336 | const string n1 = name1.text().toStdString();
|
---|
337 | const string n2 = name2.text().toStdString();
|
---|
338 |
|
---|
339 | const bool has_coordinates = n1=="RA" && n2=="Dec" && unit=="deg" && ra!=0 && dec!=0;
|
---|
340 |
|
---|
341 | const std::set<int16_t> typelist =
|
---|
342 | {
|
---|
343 | 51, // INTEGRAL_POINTDIR
|
---|
344 |
|
---|
345 | 53, // INTEGRAL_WAKEUP
|
---|
346 | 54, // INTEGRAL_REFINED
|
---|
347 | 55, // INTEGRAL_OFFLINE
|
---|
348 |
|
---|
349 | // 56, // INTEGRAL_WEAK
|
---|
350 | // 59, // KONUS_LC
|
---|
351 |
|
---|
352 | 60, // SWIFT_BAT_GRB_ALERT
|
---|
353 | 61, // SWIFT_BAT_GRB_POS_ACK
|
---|
354 | 62, // SWIFT_BAT_GRB_POS_NACK
|
---|
355 |
|
---|
356 | 83, // SWIFT_POINTDIR
|
---|
357 |
|
---|
358 | 97, // SWIFT_BAT_QL_POS
|
---|
359 |
|
---|
360 | 100, // AGILE_GRB_WAKEUP
|
---|
361 | 101, // AGILE_GRB_GROUND
|
---|
362 | 102, // AGILE_GRB_REFINED
|
---|
363 |
|
---|
364 | // 110, // FERMI_GBM_ALERT [0/0]
|
---|
365 | 111, // FERMI_GBM_FLT_POS
|
---|
366 | 112, // FERMI_GBM_GND_POS
|
---|
367 | 115, // FERMI_GBM_FIN_POS
|
---|
368 |
|
---|
369 | 123, // FERMI_LAT_TRANS
|
---|
370 | 125, // FERMI_LAT_MONITOR
|
---|
371 |
|
---|
372 | // 134, // MAXI_UNKNOWN
|
---|
373 | // 135, // MAXI_KNOWN
|
---|
374 | // 136, // MAXI_TEST
|
---|
375 |
|
---|
376 | 157, // AMON_ICECUBE_COINC
|
---|
377 | 158, // AMON_ICECUBE_HESE
|
---|
378 |
|
---|
379 | 169, // AMON_ICECUBE_EHE
|
---|
380 | 171, // HAWC_BURST_MONITOR
|
---|
381 | 173, // ICECUBE_GOLD
|
---|
382 | 174, // ICECUBE_BRONZE
|
---|
383 | };
|
---|
384 |
|
---|
385 | const bool integral_test = role=="test" && id>=53 && id<=56;
|
---|
386 |
|
---|
387 | const bool valid_id = typelist.find(id)!=typelist.end();
|
---|
388 |
|
---|
389 | if (valid_id && has_coordinates && !integral_test)
|
---|
390 | {
|
---|
391 | const ToO::DataGRB data =
|
---|
392 | {
|
---|
393 | .type = id,
|
---|
394 | .ra = ra,
|
---|
395 | .dec = dec,
|
---|
396 | .err = err,
|
---|
397 | };
|
---|
398 |
|
---|
399 | vector<char> dim(sizeof(ToO::DataGRB) + name.size() + 1);
|
---|
400 |
|
---|
401 | memcpy(dim.data(), &data, sizeof(ToO::DataGRB));
|
---|
402 | memcpy(dim.data()+sizeof(ToO::DataGRB), name.c_str(), name.size());
|
---|
403 |
|
---|
404 | Dim::SendCommandNB("SCHEDULER/GCN", dim);
|
---|
405 | Info("Sent ToO '"+name+"' ["+role+"]");
|
---|
406 | }
|
---|
407 |
|
---|
408 | Out() << Time(date.text().toStdString()).GetAsStr() << " ----- " << sname.text().toStdString() << " [" << role << "]\n";
|
---|
409 | if (!desc.isNull())
|
---|
410 | Out() << "[" << desc.text().toStdString() << "]\n";
|
---|
411 | Out() << name << " [" << paket.name << ":" << id << "]: " << paket.description << endl;
|
---|
412 | Out() << left;
|
---|
413 | Out() << " " << setw(5) << "TIME" << "= " << Time(time.text().toStdString()).GetAsStr() << '\n';
|
---|
414 | Out() << " " << setw(5) << n1 << "= " << ra << unit << '\n';
|
---|
415 | Out() << " " << setw(5) << n2 << "= " << dec << unit << '\n';
|
---|
416 | Out() << " " << setw(5) << "ERR" << "= " << err << unit << '\n';
|
---|
417 | Out() << endl;
|
---|
418 |
|
---|
419 | if (role=="observation")
|
---|
420 | {
|
---|
421 | return true;
|
---|
422 | }
|
---|
423 |
|
---|
424 | if (role=="test")
|
---|
425 | {
|
---|
426 | return true;
|
---|
427 | }
|
---|
428 |
|
---|
429 | if (role=="retraction")
|
---|
430 | {
|
---|
431 | return true;
|
---|
432 | }
|
---|
433 |
|
---|
434 | if (role=="utility")
|
---|
435 | {
|
---|
436 | return true;
|
---|
437 | }
|
---|
438 |
|
---|
439 | return false;
|
---|
440 | }
|
---|
441 |
|
---|
442 | Out() << Time().GetAsStr() << " ----- " << trn << " [" << role << "] -----" << endl;
|
---|
443 |
|
---|
444 | return false;
|
---|
445 | }
|
---|
446 |
|
---|
447 | void HandleReceivedData(const bs::error_code& err, size_t bytes_received, int type)
|
---|
448 | {
|
---|
449 | // Do not schedule a new read if the connection failed.
|
---|
450 | if (bytes_received==0 || err)
|
---|
451 | {
|
---|
452 | if (err==ba::error::eof)
|
---|
453 | Warn("Connection closed by remote host (GCN).");
|
---|
454 |
|
---|
455 | // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category))
|
---|
456 | // 125: Operation canceled
|
---|
457 | if (err && err!=ba::error::eof && // Connection closed by remote host
|
---|
458 | err!=ba::error::basic_errors::not_connected && // Connection closed by remote host
|
---|
459 | err!=ba::error::basic_errors::operation_aborted) // Connection closed by us
|
---|
460 | {
|
---|
461 | ostringstream str;
|
---|
462 | str << "Reading from " << URL() << ": " << err.message() << " (" << err << ")";// << endl;
|
---|
463 | Error(str);
|
---|
464 | }
|
---|
465 | PostClose(err!=ba::error::basic_errors::operation_aborted);
|
---|
466 | return;
|
---|
467 | }
|
---|
468 |
|
---|
469 | if (type==0)
|
---|
470 | {
|
---|
471 | fRxSize = ntohl(fRxSize);
|
---|
472 | fRxData.assign(fRxSize+1, 0);
|
---|
473 | ba::async_read(*this, ba::buffer(fRxData, fRxSize),
|
---|
474 | boost::bind(&ConnectionGCN::HandleReceivedData, this,
|
---|
475 | dummy::error, dummy::bytes_transferred, 1));
|
---|
476 | return;
|
---|
477 | }
|
---|
478 |
|
---|
479 | if (fDebugRx)
|
---|
480 | {
|
---|
481 | Out() << "------------------------------------------------------\n";
|
---|
482 | Out() << fRxData.data() << '\n';
|
---|
483 | Out() << "------------------------------------------------------" << endl;
|
---|
484 | }
|
---|
485 |
|
---|
486 | QDomDocument doc;
|
---|
487 | if (!doc.setContent(QString(fRxData.data()), false))
|
---|
488 | {
|
---|
489 | Warn("Parsing of xml failed [0].");
|
---|
490 | Out() << "------------------------------------------------------\n";
|
---|
491 | Out() << fRxData.data() << '\n';
|
---|
492 | Out() << "------------------------------------------------------" << endl;
|
---|
493 | PostClose(true);
|
---|
494 | return;
|
---|
495 | }
|
---|
496 |
|
---|
497 | if (fDebugRx)
|
---|
498 | Out() << "Parsed:\n-------\n" << doc.toString().toStdString() << endl;
|
---|
499 |
|
---|
500 | const int rc = ProcessXml(doc.documentElement());
|
---|
501 | if (rc<0)
|
---|
502 | {
|
---|
503 | Warn("Parsing of xml failed [1].");
|
---|
504 | Out() << "------------------------------------------------------\n";
|
---|
505 | Out() << fRxData.data() << '\n';
|
---|
506 | Out() << "------------------------------------------------------" << endl;
|
---|
507 | //PostClose(false);
|
---|
508 |
|
---|
509 | try
|
---|
510 | {
|
---|
511 | auto db = Database(fUri);
|
---|
512 |
|
---|
513 | // Make an entry in the alter databse to issue a call
|
---|
514 | const string query =
|
---|
515 | "INSERT FlareAlerts.FlareTriggers SET\n"
|
---|
516 | " fTriggerInserted=Now(),\n"
|
---|
517 | " fNight="+to_string(Time().NightAsInt())+",\n"
|
---|
518 | " fPacketTypeKey="+to_string(-rc)+",\n"
|
---|
519 | " fTriggerType=8";
|
---|
520 |
|
---|
521 | auto q = db.query(query);
|
---|
522 | q.execute();
|
---|
523 | if (!q.info().empty())
|
---|
524 | Info(q.info());
|
---|
525 | }
|
---|
526 | catch (const mysqlpp::ConnectionFailed &e)
|
---|
527 | {
|
---|
528 | Error(e.what());
|
---|
529 | }
|
---|
530 | }
|
---|
531 |
|
---|
532 | if (!rc)
|
---|
533 | {
|
---|
534 | Out() << "------------------------------------------------------\n";
|
---|
535 | Out() << doc.toString().toStdString() << '\n';
|
---|
536 | Out() << "------------------------------------------------------" << endl;
|
---|
537 | }
|
---|
538 |
|
---|
539 | StartRead();
|
---|
540 | }
|
---|
541 |
|
---|
542 | void StartRead()
|
---|
543 | {
|
---|
544 | ba::async_read(*this, ba::buffer(&fRxSize, 4),
|
---|
545 | boost::bind(&ConnectionGCN::HandleReceivedData, this,
|
---|
546 | dummy::error, dummy::bytes_transferred, 0));
|
---|
547 | }
|
---|
548 |
|
---|
549 | // This is called when a connection was established
|
---|
550 | void ConnectionEstablished()
|
---|
551 | {
|
---|
552 | StartRead();
|
---|
553 | }
|
---|
554 |
|
---|
555 | public:
|
---|
556 | ConnectionGCN(ba::io_service& ioservice, MessageImp &imp) : Connection(ioservice, imp()),
|
---|
557 | fIsVerbose(false), fDebugRx(false), fLastKeepAlive(Time::none)
|
---|
558 | {
|
---|
559 | SetLogStream(&imp);
|
---|
560 | }
|
---|
561 |
|
---|
562 | void SetVerbose(bool b)
|
---|
563 | {
|
---|
564 | fIsVerbose = b;
|
---|
565 | }
|
---|
566 |
|
---|
567 | void SetDebugRx(bool b)
|
---|
568 | {
|
---|
569 | fDebugRx = b;
|
---|
570 | Connection::SetVerbose(b);
|
---|
571 | }
|
---|
572 |
|
---|
573 | void SetURI(const string &uri)
|
---|
574 | {
|
---|
575 | fUri = uri;
|
---|
576 | }
|
---|
577 |
|
---|
578 | void SetEndPoints(const vector<string> &v)
|
---|
579 | {
|
---|
580 | fEndPoints = v;
|
---|
581 | fEndPoint = 0;
|
---|
582 | }
|
---|
583 |
|
---|
584 | void StartConnect()
|
---|
585 | {
|
---|
586 | if (fEndPoints.size()>0)
|
---|
587 | SetEndpoint(fEndPoints[fEndPoint++%fEndPoints.size()]);
|
---|
588 | Connection::StartConnect();
|
---|
589 | }
|
---|
590 |
|
---|
591 | bool IsValid()
|
---|
592 | {
|
---|
593 | return fLastKeepAlive.IsValid() ? Time()-fLastKeepAlive<boost::posix_time::minutes(2) : false;
|
---|
594 | }
|
---|
595 | };
|
---|
596 |
|
---|
597 | // ------------------------------------------------------------------------
|
---|
598 |
|
---|
599 | #include "DimDescriptionService.h"
|
---|
600 |
|
---|
601 | class ConnectionDimGCN : public ConnectionGCN
|
---|
602 | {
|
---|
603 | private:
|
---|
604 |
|
---|
605 | public:
|
---|
606 | ConnectionDimGCN(ba::io_service& ioservice, MessageImp &imp) : ConnectionGCN(ioservice, imp)
|
---|
607 | {
|
---|
608 | }
|
---|
609 | };
|
---|
610 |
|
---|
611 | // ------------------------------------------------------------------------
|
---|
612 |
|
---|
613 | template <class T, class S>
|
---|
614 | class StateMachineGCN : public StateMachineAsio<T>
|
---|
615 | {
|
---|
616 | private:
|
---|
617 | S fGCN;
|
---|
618 |
|
---|
619 | int Disconnect()
|
---|
620 | {
|
---|
621 | // Close all connections
|
---|
622 | fGCN.PostClose(false);
|
---|
623 |
|
---|
624 | return T::GetCurrentState();
|
---|
625 | }
|
---|
626 |
|
---|
627 | int Reconnect(const EventImp &evt)
|
---|
628 | {
|
---|
629 | // Close all connections to supress the warning in SetEndpoint
|
---|
630 | fGCN.PostClose(false);
|
---|
631 |
|
---|
632 | // Now wait until all connection have been closed and
|
---|
633 | // all pending handlers have been processed
|
---|
634 | ba::io_service::poll();
|
---|
635 |
|
---|
636 | if (evt.GetBool())
|
---|
637 | fGCN.SetEndpoint(evt.GetString());
|
---|
638 |
|
---|
639 | // Now we can reopen the connection
|
---|
640 | fGCN.PostClose(true);
|
---|
641 |
|
---|
642 | return T::GetCurrentState();
|
---|
643 | }
|
---|
644 |
|
---|
645 | int Execute()
|
---|
646 | {
|
---|
647 | if (!fGCN.IsConnected())
|
---|
648 | return State::kDisconnected;
|
---|
649 |
|
---|
650 | return fGCN.IsValid() ? State::kValid : State::kConnected;
|
---|
651 | }
|
---|
652 |
|
---|
653 | bool CheckEventSize(size_t has, const char *name, size_t size)
|
---|
654 | {
|
---|
655 | if (has==size)
|
---|
656 | return true;
|
---|
657 |
|
---|
658 | ostringstream msg;
|
---|
659 | msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
|
---|
660 | T::Fatal(msg);
|
---|
661 | return false;
|
---|
662 | }
|
---|
663 |
|
---|
664 | int SetVerbosity(const EventImp &evt)
|
---|
665 | {
|
---|
666 | if (!CheckEventSize(evt.GetSize(), "SetVerbosity", 1))
|
---|
667 | return T::kSM_FatalError;
|
---|
668 |
|
---|
669 | fGCN.SetVerbose(evt.GetBool());
|
---|
670 |
|
---|
671 | return T::GetCurrentState();
|
---|
672 | }
|
---|
673 |
|
---|
674 | int SetDebugRx(const EventImp &evt)
|
---|
675 | {
|
---|
676 | if (!CheckEventSize(evt.GetSize(), "SetDebugRx", 1))
|
---|
677 | return T::kSM_FatalError;
|
---|
678 |
|
---|
679 | fGCN.SetDebugRx(evt.GetBool());
|
---|
680 |
|
---|
681 | return T::GetCurrentState();
|
---|
682 | }
|
---|
683 |
|
---|
684 | public:
|
---|
685 | StateMachineGCN(ostream &out=cout) :
|
---|
686 | StateMachineAsio<T>(out, "GCN"), fGCN(*this, *this)
|
---|
687 | {
|
---|
688 | // State names
|
---|
689 | T::AddStateName(State::kDisconnected, "Disconnected",
|
---|
690 | "No connection to GCN.");
|
---|
691 | T::AddStateName(State::kConnected, "Connected",
|
---|
692 | "Connection to GCN established.");
|
---|
693 | T::AddStateName(State::kValid, "Valid",
|
---|
694 | "Connection valid (keep alive received within past 2min)");
|
---|
695 |
|
---|
696 | // Verbosity commands
|
---|
697 | T::AddEvent("SET_VERBOSE", "B:1")
|
---|
698 | (bind(&StateMachineGCN::SetVerbosity, this, placeholders::_1))
|
---|
699 | ("set verbosity state"
|
---|
700 | "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
|
---|
701 | T::AddEvent("SET_DEBUG_RX", "B:1")
|
---|
702 | (bind(&StateMachineGCN::SetDebugRx, this, placeholders::_1))
|
---|
703 | ("Set debux-rx state"
|
---|
704 | "|debug[bool]:dump received text and parsed text to console (yes/no)");
|
---|
705 |
|
---|
706 |
|
---|
707 | // Conenction commands
|
---|
708 | T::AddEvent("DISCONNECT", State::kConnected)
|
---|
709 | (bind(&StateMachineGCN::Disconnect, this))
|
---|
710 | ("disconnect from ethernet");
|
---|
711 | T::AddEvent("RECONNECT", "O", State::kDisconnected, State::kConnected)
|
---|
712 | (bind(&StateMachineGCN::Reconnect, this, placeholders::_1))
|
---|
713 | ("(Re)connect ethernet connection to FTM, a new address can be given"
|
---|
714 | "|[host][string]:new ethernet address in the form <host:port>");
|
---|
715 |
|
---|
716 | fGCN.StartConnect();
|
---|
717 | }
|
---|
718 |
|
---|
719 | void SetEndpoint(const string &url)
|
---|
720 | {
|
---|
721 | vector<string> v;
|
---|
722 | v.push_back(url);
|
---|
723 | fGCN.SetEndPoints(v);
|
---|
724 | }
|
---|
725 |
|
---|
726 | int EvalOptions(Configuration &conf)
|
---|
727 | {
|
---|
728 | fGCN.SetVerbose(!conf.Get<bool>("quiet"));
|
---|
729 | fGCN.SetEndPoints(conf.Vec<string>("addr"));
|
---|
730 |
|
---|
731 | fGCN.SetURI(conf.Get<string>("schedule-database"));
|
---|
732 |
|
---|
733 | return -1;
|
---|
734 | }
|
---|
735 | };
|
---|
736 |
|
---|
737 | // ------------------------------------------------------------------------
|
---|
738 |
|
---|
739 | #include "Main.h"
|
---|
740 |
|
---|
741 | template<class T, class S, class R>
|
---|
742 | int RunShell(Configuration &conf)
|
---|
743 | {
|
---|
744 | return Main::execute<T, StateMachineGCN<S, R>>(conf);
|
---|
745 | }
|
---|
746 |
|
---|
747 | void SetupConfiguration(Configuration &conf)
|
---|
748 | {
|
---|
749 | po::options_description control("FTM control options");
|
---|
750 | control.add_options()
|
---|
751 | ("no-dim", po_bool(), "Disable dim services")
|
---|
752 | ("addr,a", vars<string>(), "Network addresses of GCN server")
|
---|
753 | ("quiet,q", po_bool(true), "Disable printing contents of all received messages (except dynamic data) in clear text.")
|
---|
754 | ("schedule-database", var<string>(), "Database link as in\n\tuser:password@server[:port]/database[?compress=0|1].")
|
---|
755 | ;
|
---|
756 |
|
---|
757 | conf.AddOptions(control);
|
---|
758 | }
|
---|
759 |
|
---|
760 | /*
|
---|
761 | Extract usage clause(s) [if any] for SYNOPSIS.
|
---|
762 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
---|
763 | are used to match the usage synopsis in program output. An example from cp
|
---|
764 | (GNU coreutils) which contains both strings:
|
---|
765 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
---|
766 | or: cp [OPTION]... SOURCE... DIRECTORY
|
---|
767 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
---|
768 | */
|
---|
769 | void PrintUsage()
|
---|
770 | {
|
---|
771 | cout <<
|
---|
772 | "The gcn reads and evaluates alerts from the GCN network.\n"
|
---|
773 | "\n"
|
---|
774 | "The default is that the program is started without user intercation. "
|
---|
775 | "All actions are supposed to arrive as DimCommands. Using the -c "
|
---|
776 | "option, a local shell can be initialized. With h or help a short "
|
---|
777 | "help message about the usuage can be brought to the screen.\n"
|
---|
778 | "\n"
|
---|
779 | "Usage: gcn [-c type] [OPTIONS]\n"
|
---|
780 | " or: gcn [OPTIONS]\n";
|
---|
781 | cout << endl;
|
---|
782 | }
|
---|
783 |
|
---|
784 | void PrintHelp()
|
---|
785 | {
|
---|
786 | Main::PrintHelp<StateMachineGCN<StateMachine, ConnectionGCN>>();
|
---|
787 |
|
---|
788 | /* Additional help text which is printed after the configuration
|
---|
789 | options goes here */
|
---|
790 |
|
---|
791 | /*
|
---|
792 | cout << "bla bla bla" << endl << endl;
|
---|
793 | cout << endl;
|
---|
794 | cout << "Environment:" << endl;
|
---|
795 | cout << "environment" << endl;
|
---|
796 | cout << endl;
|
---|
797 | cout << "Examples:" << endl;
|
---|
798 | cout << "test exam" << endl;
|
---|
799 | cout << endl;
|
---|
800 | cout << "Files:" << endl;
|
---|
801 | cout << "files" << endl;
|
---|
802 | cout << endl;
|
---|
803 | */
|
---|
804 | }
|
---|
805 |
|
---|
806 | int main(int argc, const char* argv[])
|
---|
807 | {
|
---|
808 | Configuration conf(argv[0]);
|
---|
809 | conf.SetPrintUsage(PrintUsage);
|
---|
810 | Main::SetupConfiguration(conf);
|
---|
811 | SetupConfiguration(conf);
|
---|
812 |
|
---|
813 | if (!conf.DoParse(argc, argv, PrintHelp))
|
---|
814 | return 127;
|
---|
815 |
|
---|
816 | //try
|
---|
817 | {
|
---|
818 | // No console access at all
|
---|
819 | if (!conf.Has("console"))
|
---|
820 | {
|
---|
821 | if (conf.Get<bool>("no-dim"))
|
---|
822 | return RunShell<LocalStream, StateMachine, ConnectionGCN>(conf);
|
---|
823 | else
|
---|
824 | return RunShell<LocalStream, StateMachineDim, ConnectionDimGCN>(conf);
|
---|
825 | }
|
---|
826 | // Cosole access w/ and w/o Dim
|
---|
827 | if (conf.Get<bool>("no-dim"))
|
---|
828 | {
|
---|
829 | if (conf.Get<int>("console")==0)
|
---|
830 | return RunShell<LocalShell, StateMachine, ConnectionGCN>(conf);
|
---|
831 | else
|
---|
832 | return RunShell<LocalConsole, StateMachine, ConnectionGCN>(conf);
|
---|
833 | }
|
---|
834 | else
|
---|
835 | {
|
---|
836 | if (conf.Get<int>("console")==0)
|
---|
837 | return RunShell<LocalShell, StateMachineDim, ConnectionDimGCN>(conf);
|
---|
838 | else
|
---|
839 | return RunShell<LocalConsole, StateMachineDim, ConnectionDimGCN>(conf);
|
---|
840 | }
|
---|
841 | }
|
---|
842 | /*catch (std::exception& e)
|
---|
843 | {
|
---|
844 | cerr << "Exception: " << e.what() << endl;
|
---|
845 | return -1;
|
---|
846 | }*/
|
---|
847 |
|
---|
848 | return 0;
|
---|
849 | }
|
---|