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