Jpp  debug
the software that should make you happy
JLigierMirror.cc
Go to the documentation of this file.
1 #include <chrono>
2 #include <iostream>
3 #include <set>
4 #include <string>
5 
6 #include "JNet/JControlHost.hh"
7 #include "JIO/JByteArrayIO.hh"
8 #include "JDAQ/JDAQTags.hh"
9 #include "JDAQ/JDAQPreambleIO.hh"
11 #include "JSystem/JDateAndTime.hh"
12 
13 #include "Jeep/JParser.hh"
14 #include "Jeep/JMessage.hh"
15 
16 /**
17  * \file
18  *
19  * A tool to forward messages with the given tags from one ControlHost server (e.g. JLigier) to another.
20  *
21  * The options <tt>-H <source>[:port]</tt> and <tt>-X <target>[:port]</tt>
22  * correspond to the host name and the port of the source and target server, respectively.\n
23  * The options <tt>-t</tt> and <tt>-T</tt> correspond to the ControlHost tag(s)
24  * with the mode subscription "any" and subscription "all", respectively.
25  * \author tgal, mdejong
26  */
27 int main(int argc, const char *argv[])
28 {
29  using namespace std;
30  using namespace JPP;
31  using namespace KM3NETDAQ;
32 
33  string source;
34  string target;
35  int report_interval; // in seconds
36  set<JNET::JTag> tagList;
37  set<JNET::JTag> TagList;
38  JDAQTriggerMask trigger_mask;
39  int debug;
40 
41  try {
42 
43  JParser<> zap("Program to forward messages from one ControlHost server to another.");
44 
45  zap['H'] = make_field(source) = "localhost";
46  zap['X'] = make_field(target) = "localhost";
47  zap['t'] = make_field(tagList) = JPARSER::initialised();
48  zap['T'] = make_field(TagList) = JPARSER::initialised();
49  zap['@'] = make_field(trigger_mask) = TRIGGER_MASK_ON;
50  zap['i'] = make_field(report_interval) = 30;
51  zap['d'] = make_field(debug) = 2;
52 
53  zap(argc, argv);
54  }
55  catch(const exception &error) {
56  FATAL(error.what() << endl);
57  }
58 
59 
60  if (tagList.empty() && TagList.empty()) {
61  FATAL("No tags specified.");
62  }
63 
64  NOTICE("Forwarding messages from " << endl << " " << source << " -> " << target << endl);
65 
66 
67  JControlHost::Throw(true);
68 
69  try {
70 
71  JControlHost in (source);
72  JControlHost out(target);
73 
74  {
75  NOTICE("with the following tags: ");
76 
77  JSubscriptionList buffer;
78 
79  for (set<JTag>::const_iterator i = tagList.begin(); i != tagList.end(); ++i) {
80  buffer.add(JSubscriptionAny(*i));
81  NOTICE(*i << "(any) ");
82  }
83 
84  for (set<JTag>::const_iterator i = TagList.begin(); i != TagList.end(); ++i) {
85  buffer.add(JSubscriptionAll(*i));
86  NOTICE(*i << "(all) ");
87  }
88 
89  NOTICE(endl);
90 
91  in.Subscribe(buffer);
92  in.SendMeAlways();
93  }
94 
95 
96  JPrefix prefix;
97  vector<char> buffer;
98  unsigned int message_count[] = { 0, 0 };
99  float milliseconds_passed;
100  std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();
101 
102  for (const string stop("stop"); buffer.size() != stop.size() || string(buffer.data(), stop.size()) != stop; ) {
103 
104  in.WaitHead(prefix);
105 
106  buffer.resize(prefix.getSize());
107 
108  in.GetFullData(buffer.data(), buffer.size());
109 
110  message_count[0] += 1;
111 
112  DEBUG(getDateAndTime() << ' ' << left << setw(8) << prefix.getTag() << ' ' << right << setw(8) << prefix.getSize() << endl);
113 
114  bool dos = false;
115 
116  if (prefix.getTag() == IO_EVENT) {
117 
118  JDAQPreamble preamble;
119  Version_t version;
120  JDAQEventHeader header;
121 
122  JByteArrayReader reader(buffer.data(), buffer.size());
123 
124  reader >> preamble;
125  reader >> version;
126  reader >> header;
127 
128  dos = header.hasTriggerMask(trigger_mask);
129 
130  } else {
131 
132  dos = true;
133  }
134 
135  if (dos) {
136 
137  message_count[1] += 1;
138 
139  out.PutFullData(prefix.getTag(), buffer.data(), buffer.size());
140  }
141 
142  milliseconds_passed = chrono::duration_cast<chrono::milliseconds>(std::chrono::steady_clock::now() - start_time).count();
143 
144  if (milliseconds_passed > report_interval * 1e3) {
145 
146  STATUS(getDateAndTime() << " : " << "Message rate: "
147  << message_count[0] / milliseconds_passed * 1e3 << " "
148  << message_count[1] / milliseconds_passed * 1e3 << " Hz" << endl);
149 
150  start_time = std::chrono::steady_clock::now();
151 
152  message_count[0] = 0;
153  message_count[1] = 0;
154  }
155  }
156  }
157  catch(const JControlHostException& error) {
158  ERROR(error << endl);
159  }
160 }
Fixed parameters and ControlHost tags for KM3NeT DAQ.
Date and time functions.
int main(int argc, const char *argv[])
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
#define STATUS(A)
Definition: JMessage.hh:63
#define ERROR(A)
Definition: JMessage.hh:66
#define NOTICE(A)
Definition: JMessage.hh:64
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2158
Byte array binary input.
Definition: JByteArrayIO.hh:27
Exception for ControlHost.
Definition: JException.hh:486
ControlHost class.
int WaitHead(JPrefix &prefix)
Wait for header.
int GetFullData(void *buffer, long long int length)
Receive data.
int SendMeAlways()
Tell server to send messages forever.
int PutFullData(const JTag &tag, const void *buffer, const long long int length)
Send data.
int Subscribe(const JSubscription &subscription)
Subscribe to single tag.
ControlHost prefix.
Definition: JPrefix.hh:33
int getSize() const
Get size.
Definition: JPrefix.hh:62
Subscription list.
JSubscriptionList & add(const JSubscription &subscription)
Add subscription.
const JTag & getTag() const
Get tag.
Definition: JTag.hh:86
Utility class to parse command line options.
Definition: JParser.hh:1714
Auxiliary class for trigger mask.
bool hasTriggerMask(const JDAQTriggerMask &mask) const
Has trigger bit pattern.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
static JDateAndTime getDateAndTime
Function object to get current date and time.
KM3NeT DAQ data structures and auxiliaries.
Definition: DataQueue.cc:39
static const JDAQTriggerMask TRIGGER_MASK_ON
Trigger mask on;.
static const JNET::JTag IO_EVENT
Definition: JDAQTags.hh:88
Definition: JSTDTypes.hh:14
Target.
Definition: JHead.hh:300
Auxiliary class for all subscription.
Definition: JControlHost.hh:99
Auxiliary class for any subscription.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:84