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