Jpp  18.0.1-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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::high_resolution_clock::time_point start_time = std::chrono::high_resolution_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 = (std::chrono::high_resolution_clock::now() - start_time) / std::chrono::milliseconds(1);
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::high_resolution_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 }
ControlHost prefix.
Definition: JPrefix.hh:31
Utility class to parse command line options.
Definition: JParser.hh:1514
int main(int argc, char *argv[])
Definition: Main.cc:15
Target.
Definition: JHead.hh:298
ControlHost class.
Auxiliary class for trigger mask.
version
Definition: JEditTuneHV.sh:5
#define STATUS(A)
Definition: JMessage.hh:63
int Subscribe(const JSubscription &subscription)
Subscribe to single tag.
int SendMeAlways()
Tell server to send messages forever.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
int getSize() const
Get size.
Definition: JPrefix.hh:62
Subscription list.
Date and time functions.
then usage $script[port]< option > nPossible stop
static const JDAQTriggerMask TRIGGER_MASK_ON
Trigger mask on;.
static JDateAndTime getDateAndTime
Function object to get current date and time.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
Byte array binary input.
Definition: JByteArrayIO.hh:25
#define NOTICE(A)
Definition: JMessage.hh:64
Auxiliary class for any subscription.
#define ERROR(A)
Definition: JMessage.hh:66
then awk string
bool hasTriggerMask(const JDAQTriggerMask &mask) const
Has trigger bit pattern.
JSubscriptionList & add(const JSubscription &subscription)
Add subscription.
Exception for ControlHost.
Definition: JException.hh:468
Auxiliary class for all subscription.
Definition: JControlHost.hh:97
const JTag & getTag() const
Get tag.
Definition: JTag.hh:86
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
int WaitHead(JPrefix &prefix)
Wait for header.
int GetFullData(void *buffer, long long int length)
Receive data.
Utility class to parse command line options.
Fixed parameters and ControlHost tags for KM3NeT DAQ.
static const JNET::JTag IO_EVENT
Definition: JDAQTags.hh:80
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
int PutFullData(const JTag &tag, const void *buffer, const long long int length)
Send data.
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62