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