source: branches/FACT++_lidctrl_usb/src/gcn.cc@ 18725

Last change on this file since 18725 was 16981, checked in by tbretz, 11 years ago
Added some preliminary code to extract the type of GCn report.
File size: 20.9 KB
Line 
1#include <functional>
2
3#include "Dim.h"
4#include "Event.h"
5#include "Shell.h"
6#include "StateMachineDim.h"
7#include "StateMachineAsio.h"
8#include "Connection.h"
9#include "LocalControl.h"
10#include "Configuration.h"
11#include "Console.h"
12#include "Converter.h"
13
14#include "tools.h"
15#include "../externals/nova.h"
16
17#include "HeadersGCN.h"
18
19#include <QtXml/QDomDocument>
20
21namespace ba = boost::asio;
22namespace bs = boost::system;
23namespace dummy = ba::placeholders;
24
25using namespace std;
26using namespace GCN;
27
28// ------------------------------------------------------------------------
29
30class ConnectionGCN : public Connection
31{
32private:
33 map<uint16_t, GCN::PaketType_t> fTypes;
34
35 vector<string> fEndPoints;
36 int fEndPoint;
37
38 bool fIsVerbose;
39 bool fDebugRx;
40
41 uint32_t fRxSize;
42 vector<char> fRxData;
43
44 Time fLastKeepAlive;
45
46 GCN::PaketType_t GetType(const QDomElement &what)
47 {
48 const QDomNodeList param = what.elementsByTagName("Param");
49 for (int i=0; i<param.count(); i++)
50 {
51 const QDomElement elem = param.at(i).toElement();
52 if (elem.attribute("name").toStdString()!="Packet_Type")
53 continue;
54
55 const uint16_t val = elem.attribute("value").toUInt();
56 const auto it = fTypes.find(val);
57 if (it!=fTypes.end())
58 return it->second;
59
60 Warn("Unknown paket type "+to_string(val)+".");
61 }
62
63 return { -1, "", "" };
64 }
65
66
67 int ProcessXml(const QDomElement &root)
68 {
69 if (root.isNull())
70 return -1;
71
72 const string role = root.attribute("role", "").toStdString();
73 const string name = root.tagName().toStdString();
74
75 // A full description can be found at http://voevent.dc3.com/schema/default.html
76
77 if (name=="trn:Transport")
78 {
79 if (role=="iamalive")
80 {
81 const QDomElement orig = root.firstChildElement("Origin");
82 const QDomElement time = root.firstChildElement("TimeStamp");
83 if (orig.isNull() || time.isNull())
84 return -1;
85
86 fLastKeepAlive = Time(time.text().toStdString());
87
88 if (fIsVerbose)
89 {
90 Out() << Time().GetAsStr() << " ----- " << name << " [" << role << "] -----" << endl;
91 Out() << " " << time.tagName().toStdString() << " = " << fLastKeepAlive.GetAsStr() << '\n';
92 Out() << " " << orig.tagName().toStdString() << " = " << orig.text().toStdString() << '\n';
93 Out() << endl;
94 }
95
96 return true;
97 }
98
99 return false;
100 }
101
102 ofstream fout("gcn.stream", ios::app);
103 fout << "------------------------------------------------------------------------------\n" << fRxData.data() << endl;
104
105 if (name=="voe:VOEvent")
106 {
107 // WHAT: http://gcn.gsfc.nasa.gov/tech_describe.html
108 const QDomElement who = root.firstChildElement("Who");
109 const QDomElement what = root.firstChildElement("What");
110 const QDomElement when = root.firstChildElement("WhereWhen");
111 //const QDomElement how = root.firstChildElement("How");
112 //const QDomElement why = root.firstChildElement("Why");
113 //const QDomElement cite = root.firstChildElement("Citations");
114 //const QDomElement desc = root.firstChildElement("Description");
115 //const QDomElement ref = root.firstChildElement("Reference");
116 if (who.isNull() || what.isNull() || when.isNull())
117 return -1;
118
119 const QDomElement date = who.firstChildElement("Date");
120 const QDomElement author = who.firstChildElement("Author");
121 const QDomElement sname = author.firstChildElement("shortName");
122 const QDomElement desc = what.firstChildElement("Description");
123
124 const QDomElement obsdat = when.firstChildElement("ObsDataLocation");
125 const QDomElement obsloc = obsdat.firstChildElement("ObservationLocation");
126 const QDomElement coord = obsloc.firstChildElement("AstroCoords");
127
128 const QDomElement time = coord.firstChildElement("Time").firstChildElement("TimeInstant").firstChildElement("ISOTime");
129 const QDomElement pos2d = coord.firstChildElement("Position2D");
130 const QDomElement name1 = pos2d.firstChildElement("Name1");
131 const QDomElement name2 = pos2d.firstChildElement("Name2");
132 const QDomElement val2 = pos2d.firstChildElement("Value2");
133 const QDomElement c1 = val2.firstChildElement("C1");
134 const QDomElement c2 = val2.firstChildElement("C2");
135 const QDomElement errad = pos2d.firstChildElement("Error2Radius");
136
137 if (date.isNull() || author.isNull() || sname.isNull() || desc.isNull() ||
138 obsdat.isNull() || obsloc.isNull() || coord.isNull() || time.isNull() ||
139 pos2d.isNull() || name1.isNull() || name2.isNull() || val2.isNull() ||
140 c1.isNull() || c2.isNull() || errad.isNull())
141 return -1;
142
143 const GCN::PaketType_t ptype = GetType(what);
144
145 // 59/31: Konus LC / IPN raw [observation]
146 // 110: Fermi GBM (ART) [observation] (Initial) // Stop data taking
147 // 111: Fermi GBM (FLT) [observation] (after ~2s) // Start pointing/run
148 // 112: Fermi GBM (GND) [observation] (after 2-20s) // Refine pointing
149 // 115: Fermi GBM position [observation] (final ~hours)
150 //
151 // 51: Intergal pointdir [utility]
152 // 83: Swift pointdir [utility]
153 // 129: Fermi pointdir [utility]
154 //
155 // 2: Test coord ( 1) [test]
156 // 44: HETE test ( 41- 43) [test]
157 // 52: Integral SPIACS [test]
158 // 53: Integral Wakeup [test]
159 // 54: Integral refined [test]
160 // 55: Integral Offline [test]
161 // 56: Integral Weak [test]
162 // 82: BAT GRB pos test ( 61) [test]
163 // 109: AGILE GRB pos test (100-103) [test]
164 // 119: Fermi GRB pos test (111-113) [test]
165 // 124: Fermi LAT pos upd test (120-122) [test]
166 // 136: MAXI coord test ( 134) [test]
167 //
168 // Integral: RA=1.2343, Dec=2.3456
169 //
170
171 /*
172 54
173 ==
174 <Group name="Test_mpos" >
175 <Param name="Test_Notice" value="true" />
176 </Group>
177
178
179 82
180 ==
181 <Group name="Solution_Status" >
182 <Param name="Test_Submission" value="false" />
183 </Group>
184
185
186 115
187 ===
188 2013-07-20 19:04:13: TIME = 2013-07-20 02:46:40
189
190 <Group name="Trigger_ID" >
191 <Param name="Test_Submission" value="false" />
192 </Group>
193 */
194
195 const string unit = pos2d.attribute("unit").toStdString();
196
197 const double ra = c1.text().toDouble();
198 const double dec = c2.text().toDouble();
199 const double err = errad.text().toDouble();
200
201 const string n1 = name1.text().toStdString();
202 const string n2 = name2.text().toStdString();
203
204 Out() << Time(date.text().toStdString()).GetAsStr() << " ----- " << sname.text().toStdString() << " [" << role << "]\n";
205 Out() << "[" << desc.text().toStdString() << "]\n";
206 Out() << ptype.name << "[" << ptype.type << "]: " << ptype.description << endl;
207 Out() << left;
208 Out() << " " << setw(5) << "TIME" << "= " << Time(time.text().toStdString()).GetAsStr() << '\n';
209 Out() << " " << setw(5) << n1 << "= " << ra << unit << '\n';
210 Out() << " " << setw(5) << n2 << "= " << dec << unit << '\n';
211 Out() << " " << setw(5) << "ERR" << "= " << err << unit << '\n';
212
213 if (n1=="RA" && n2=="Dec" && unit=="deg")
214 {
215 const double jd = Time().JD();
216
217 Nova::EquPosn equ;
218 equ.ra = ra;
219 equ.dec = dec;
220
221 const Nova::ZdAzPosn pos = Nova::GetHrzFromEqu(equ, jd);
222 const Nova::EquPosn moon = Nova::GetLunarEquCoords(jd);
223 const Nova::ZdAzPosn sun = Nova::GetHrzFromEqu(Nova::GetSolarEquCoords(jd), jd);
224
225 const double disk = Nova::GetLunarDisk(jd);
226 const double dist = Nova::GetAngularSeparation(equ, moon);
227
228 Out() << " " << setw(5) << "ZD" << "= " << pos.zd << "deg\n";
229 Out() << " " << setw(5) << "Az" << "= " << pos.az << "deg\n";
230
231 Out() << " " << setw(5) << "MOON" << "= " << int(disk*100) << "%\n";
232 Out() << " " << setw(5) << "DIST" << "= " << dist << "deg\n";
233
234 if (dist>10 && dist<170 && pos.zd<80 && sun.zd>108)
235 {
236 Out() << " visible ";
237 if (pos.zd<70)
238 Out() << '+';
239 if (pos.zd<60)
240 Out() << '+';
241 if (pos.zd<45)
242 Out() << '+';
243 Out() << '\n';
244 }
245 }
246
247 Out() << endl;
248
249 if (role=="observation")
250 {
251 return true;
252 }
253
254 if (role=="test")
255 {
256 return true;
257 }
258
259 if (role=="retraction")
260 {
261 return true;
262 }
263
264 if (role=="utility")
265 {
266 return true;
267 }
268
269 return false;
270 }
271
272 Out() << Time().GetAsStr() << " ----- " << name << " [" << role << "] -----" << endl;
273
274 return false;
275 }
276
277 void HandleReceivedData(const bs::error_code& err, size_t bytes_received, int type)
278 {
279 // Do not schedule a new read if the connection failed.
280 if (bytes_received==0 || err)
281 {
282 if (err==ba::error::eof)
283 Warn("Connection closed by remote host (GCN).");
284
285 // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category))
286 // 125: Operation canceled
287 if (err && err!=ba::error::eof && // Connection closed by remote host
288 err!=ba::error::basic_errors::not_connected && // Connection closed by remote host
289 err!=ba::error::basic_errors::operation_aborted) // Connection closed by us
290 {
291 ostringstream str;
292 str << "Reading from " << URL() << ": " << err.message() << " (" << err << ")";// << endl;
293 Error(str);
294 }
295 PostClose(err!=ba::error::basic_errors::operation_aborted);
296 return;
297 }
298
299 if (type==0)
300 {
301 fRxSize = ntohl(fRxSize);
302 fRxData.assign(fRxSize+1, 0);
303 ba::async_read(*this, ba::buffer(fRxData, fRxSize),
304 boost::bind(&ConnectionGCN::HandleReceivedData, this,
305 dummy::error, dummy::bytes_transferred, 1));
306 return;
307 }
308
309 if (fDebugRx)
310 {
311 Out() << "------------------------------------------------------\n";
312 Out() << fRxData.data() << '\n';
313 Out() << "------------------------------------------------------" << endl;
314 }
315
316 QDomDocument doc;
317 if (!doc.setContent(QString(fRxData.data()), false))
318 {
319 Warn("Parsing of xml failed [0].");
320 PostClose(false);
321 return;
322 }
323
324 if (fDebugRx)
325 Out() << "Parsed:\n-------\n" << doc.toString().toStdString() << endl;
326
327 const int rc = ProcessXml(doc.documentElement());
328 if (rc<0)
329 {
330 Warn("Parsing of xml failed [1].");
331 PostClose(false);
332 return;
333 }
334
335 if (!rc)
336 {
337 Out() << "------------------------------------------------------\n";
338 Out() << doc.toString().toStdString() << '\n';
339 Out() << "------------------------------------------------------" << endl;
340 }
341
342 StartRead();
343 }
344
345 void StartRead()
346 {
347 ba::async_read(*this, ba::buffer(&fRxSize, 4),
348 boost::bind(&ConnectionGCN::HandleReceivedData, this,
349 dummy::error, dummy::bytes_transferred, 0));
350 }
351
352 // This is called when a connection was established
353 void ConnectionEstablished()
354 {
355 StartRead();
356 }
357
358public:
359 ConnectionGCN(ba::io_service& ioservice, MessageImp &imp) : Connection(ioservice, imp()),
360 fIsVerbose(false), fDebugRx(false), fLastKeepAlive(Time::none)
361 {
362 SetLogStream(&imp);
363
364 for (auto it=GCN::kTypes; it->type>0; it++)
365 fTypes[it->type] = *it;
366 }
367
368 void SetVerbose(bool b)
369 {
370 fIsVerbose = b;
371 }
372
373 void SetDebugRx(bool b)
374 {
375 fDebugRx = b;
376 Connection::SetVerbose(b);
377 }
378
379 void SetEndPoints(const vector<string> &v)
380 {
381 fEndPoints = v;
382 fEndPoint = 0;
383 }
384
385 void StartConnect()
386 {
387 if (fEndPoints.size()>0)
388 SetEndpoint(fEndPoints[fEndPoint++%fEndPoints.size()]);
389 Connection::StartConnect();
390 }
391
392 bool IsValid()
393 {
394 return fLastKeepAlive.IsValid() ? Time()-fLastKeepAlive<boost::posix_time::minutes(2) : false;
395 }
396};
397
398// ------------------------------------------------------------------------
399
400#include "DimDescriptionService.h"
401
402class ConnectionDimGCN : public ConnectionGCN
403{
404private:
405
406public:
407 ConnectionDimGCN(ba::io_service& ioservice, MessageImp &imp) : ConnectionGCN(ioservice, imp)
408 {
409 }
410};
411
412// ------------------------------------------------------------------------
413
414template <class T, class S>
415class StateMachineGCN : public StateMachineAsio<T>
416{
417private:
418 S fGCN;
419
420 int Disconnect()
421 {
422 // Close all connections
423 fGCN.PostClose(false);
424
425 return T::GetCurrentState();
426 }
427
428 int Reconnect(const EventImp &evt)
429 {
430 // Close all connections to supress the warning in SetEndpoint
431 fGCN.PostClose(false);
432
433 // Now wait until all connection have been closed and
434 // all pending handlers have been processed
435 ba::io_service::poll();
436
437 if (evt.GetBool())
438 fGCN.SetEndpoint(evt.GetString());
439
440 // Now we can reopen the connection
441 fGCN.PostClose(true);
442
443 return T::GetCurrentState();
444 }
445
446 int Execute()
447 {
448 if (!fGCN.IsConnected())
449 return State::kDisconnected;
450
451 return fGCN.IsValid() ? State::kValid : State::kConnected;
452 }
453
454 bool CheckEventSize(size_t has, const char *name, size_t size)
455 {
456 if (has==size)
457 return true;
458
459 ostringstream msg;
460 msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
461 T::Fatal(msg);
462 return false;
463 }
464
465 int SetVerbosity(const EventImp &evt)
466 {
467 if (!CheckEventSize(evt.GetSize(), "SetVerbosity", 1))
468 return T::kSM_FatalError;
469
470 fGCN.SetVerbose(evt.GetBool());
471
472 return T::GetCurrentState();
473 }
474
475 int SetDebugRx(const EventImp &evt)
476 {
477 if (!CheckEventSize(evt.GetSize(), "SetDebugRx", 1))
478 return T::kSM_FatalError;
479
480 fGCN.SetDebugRx(evt.GetBool());
481
482 return T::GetCurrentState();
483 }
484
485public:
486 StateMachineGCN(ostream &out=cout) :
487 StateMachineAsio<T>(out, "GCN"), fGCN(*this, *this)
488 {
489 // State names
490 T::AddStateName(State::kDisconnected, "Disconnected",
491 "No connection to GCN.");
492 T::AddStateName(State::kConnected, "Connected",
493 "Connection to GCN established.");
494 T::AddStateName(State::kValid, "Valid",
495 "Connection valid (keep alive received within past 2min)");
496
497 // Verbosity commands
498 T::AddEvent("SET_VERBOSE", "B:1")
499 (bind(&StateMachineGCN::SetVerbosity, this, placeholders::_1))
500 ("set verbosity state"
501 "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
502 T::AddEvent("SET_DEBUG_RX", "B:1")
503 (bind(&StateMachineGCN::SetDebugRx, this, placeholders::_1))
504 ("Set debux-rx state"
505 "|debug[bool]:dump received text and parsed text to console (yes/no)");
506
507
508 // Conenction commands
509 T::AddEvent("DISCONNECT", State::kConnected)
510 (bind(&StateMachineGCN::Disconnect, this))
511 ("disconnect from ethernet");
512 T::AddEvent("RECONNECT", "O", State::kDisconnected, State::kConnected)
513 (bind(&StateMachineGCN::Reconnect, this, placeholders::_1))
514 ("(Re)connect ethernet connection to FTM, a new address can be given"
515 "|[host][string]:new ethernet address in the form <host:port>");
516
517 fGCN.StartConnect();
518 }
519
520 void SetEndpoint(const string &url)
521 {
522 vector<string> v;
523 v.push_back(url);
524 fGCN.SetEndPoints(v);
525 }
526
527 vector<string> fEndPoints;
528
529 int EvalOptions(Configuration &conf)
530 {
531 fGCN.SetVerbose(!conf.Get<bool>("quiet"));
532 fGCN.SetEndPoints(conf.Vec<string>("addr"));
533
534 return -1;
535 }
536};
537
538// ------------------------------------------------------------------------
539
540#include "Main.h"
541
542template<class T, class S, class R>
543int RunShell(Configuration &conf)
544{
545 return Main::execute<T, StateMachineGCN<S, R>>(conf);
546}
547
548void SetupConfiguration(Configuration &conf)
549{
550 po::options_description control("FTM control options");
551 control.add_options()
552 ("no-dim", po_bool(), "Disable dim services")
553 ("addr,a", vars<string>(), "Network addresses of GCN server")
554 ("quiet,q", po_bool(true), "Disable printing contents of all received messages (except dynamic data) in clear text.")
555 ;
556
557 conf.AddOptions(control);
558}
559
560/*
561 Extract usage clause(s) [if any] for SYNOPSIS.
562 Translators: "Usage" and "or" here are patterns (regular expressions) which
563 are used to match the usage synopsis in program output. An example from cp
564 (GNU coreutils) which contains both strings:
565 Usage: cp [OPTION]... [-T] SOURCE DEST
566 or: cp [OPTION]... SOURCE... DIRECTORY
567 or: cp [OPTION]... -t DIRECTORY SOURCE...
568 */
569void PrintUsage()
570{
571 cout <<
572 "The gcn reads and evaluates alerts from the GCN network.\n"
573 "\n"
574 "The default is that the program is started without user intercation. "
575 "All actions are supposed to arrive as DimCommands. Using the -c "
576 "option, a local shell can be initialized. With h or help a short "
577 "help message about the usuage can be brought to the screen.\n"
578 "\n"
579 "Usage: gcn [-c type] [OPTIONS]\n"
580 " or: gcn [OPTIONS]\n";
581 cout << endl;
582}
583
584void PrintHelp()
585{
586 Main::PrintHelp<StateMachineGCN<StateMachine, ConnectionGCN>>();
587
588 /* Additional help text which is printed after the configuration
589 options goes here */
590
591 /*
592 cout << "bla bla bla" << endl << endl;
593 cout << endl;
594 cout << "Environment:" << endl;
595 cout << "environment" << endl;
596 cout << endl;
597 cout << "Examples:" << endl;
598 cout << "test exam" << endl;
599 cout << endl;
600 cout << "Files:" << endl;
601 cout << "files" << endl;
602 cout << endl;
603 */
604}
605
606int main(int argc, const char* argv[])
607{
608 Configuration conf(argv[0]);
609 conf.SetPrintUsage(PrintUsage);
610 Main::SetupConfiguration(conf);
611 SetupConfiguration(conf);
612
613 if (!conf.DoParse(argc, argv, PrintHelp))
614 return 127;
615
616 //try
617 {
618 // No console access at all
619 if (!conf.Has("console"))
620 {
621 if (conf.Get<bool>("no-dim"))
622 return RunShell<LocalStream, StateMachine, ConnectionGCN>(conf);
623 else
624 return RunShell<LocalStream, StateMachineDim, ConnectionDimGCN>(conf);
625 }
626 // Cosole access w/ and w/o Dim
627 if (conf.Get<bool>("no-dim"))
628 {
629 if (conf.Get<int>("console")==0)
630 return RunShell<LocalShell, StateMachine, ConnectionGCN>(conf);
631 else
632 return RunShell<LocalConsole, StateMachine, ConnectionGCN>(conf);
633 }
634 else
635 {
636 if (conf.Get<int>("console")==0)
637 return RunShell<LocalShell, StateMachineDim, ConnectionDimGCN>(conf);
638 else
639 return RunShell<LocalConsole, StateMachineDim, ConnectionDimGCN>(conf);
640 }
641 }
642 /*catch (std::exception& e)
643 {
644 cerr << "Exception: " << e.what() << endl;
645 return -1;
646 }*/
647
648 return 0;
649}
Note: See TracBrowser for help on using the repository browser.