Jpp  master_rocky-40-g5f0272dcd
the software that should make you happy
JGetData.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <set>
5 #include <limits>
6 
7 #include "Jeep/JParser.hh"
8 #include "Jeep/JMessage.hh"
9 #include "Jeep/JTimekeeper.hh"
10 #include "JSystem/JTime.hh"
11 #include "JNet/JControlHost.hh"
12 
13 
14 /**
15  * \file
16  *
17  * Auxiliary program to receive messages from ControlHost server.
18  *
19  * The option <tt>-H <hostname>[:port]</tt> correponds to the hostname and the port of the server, respectively.
20  * The options <tt>-t</tt> and <tt>-T</tt> correspond to the ControlHost tag(s) for free and all subscription, respectively.
21  * The option <tt>-R <rate [Hz]></tt> corresponds to the rate at which messages will be received.
22  * \author mdejong
23  */
24 int main(int argc, const char *argv[])
25 {
26  using namespace std;
27 
28  string hostname;
29  set<JNET::JTag> tagList;
30  set<JNET::JTag> TagList;
31  double rate_Hz;
32  int numberOfEvents;
33  int debug;
34 
35  try {
36 
37  JParser<> zap("Auxiliary program to receive messages from ControlHost server.");
38 
39  zap['H'] = make_field(hostname) = "localhost";
40  zap['t'] = make_field(tagList);
41  zap['T'] = make_field(TagList);
42  zap['R'] = make_field(rate_Hz);
43  zap['n'] = make_field(numberOfEvents) = numeric_limits<int>::max();
44  zap['d'] = make_field(debug) = 3;
45 
46  zap['t'] = JPARSER::initialised();
47  zap['T'] = JPARSER::initialised();
48 
49  zap(argc, argv);
50  }
51  catch(const exception &error) {
52  FATAL(error.what() << endl);
53  }
54 
55 
56  if (tagList.empty() && TagList.empty())
57  FATAL("No tags specified.");
58 
59  using namespace JPP;
60 
61  JControlHost::Throw(true);
62 
63  try {
64 
65  JControlHost in(hostname);
66 
67  {
68  JSubscriptionList buffer;
69 
70  for (set<JTag>::const_iterator i = tagList.begin(); i != tagList.end(); ++i) {
71  buffer.add(JSubscriptionAny(*i));
72  }
73 
74  for (set<JTag>::const_iterator i = TagList.begin(); i != TagList.end(); ++i) {
75  buffer.add(JSubscriptionAll(*i));
76  }
77 
78  DEBUG("Subscription: " << buffer.toString() << endl);
79 
80  in.Subscribe(buffer);
81  in.SendMeAlways();
82  }
83 
84  JTimekeeper timer((long long int) (1.0e6 / rate_Hz));
85 
86  JPrefix prefix;
87  vector<char> buffer;
88 
89  int all = 0;
90  int few = 0;
91 
92  long long int t0 = 0;
93 
94  for (int i = 1; i <= numberOfEvents; ++i) {
95 
96  in.WaitHead(prefix);
97 
98  if (i == 1) {
99 
100  timer.reset();
101 
102  t0 = getLocalTime();
103  }
104 
105  buffer.resize(prefix.getSize());
106 
107  in.GetFullData(buffer.data(), buffer.size());
108 
109  if (TagList.find(prefix) != TagList.end()) { ++all; }
110  if (tagList.find(prefix) != tagList.end()) { ++few; }
111 
112  if (i%1000 == 0) {
113 
114  const long long int t1 = getLocalTime();
115 
116  DEBUG("Time [us] " << setw(10) << t1 - t0 << endl);
117  DEBUG("<delay> [us] " << setw(10) << timer.getDelay() / i << endl);
118  DEBUG("Count all " << setw(10) << all << endl);
119  DEBUG("Count few " << setw(10) << few << endl);
120  }
121 
122  timer.wait();
123  }
124 
125  }
126  catch(const JControlHostException& error) {
127  ERROR(error << endl);
128  }
129 }
130 
131 
int main(int argc, const char *argv[])
Definition: JGetData.cc:24
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
#define ERROR(A)
Definition: JMessage.hh:66
#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:2142
System time information.
Scheduling of actions via fixed latency intervals.
Time keeper.
Definition: JTimekeeper.hh:34
long long int getDelay() const
Get total delay time.
Definition: JTimekeeper.hh:78
void wait() const
Wait until the number of time intervals has elapsed since the last call to the reset method.
Definition: JTimekeeper.hh:145
void reset(const long long int t0)
Reset time.
Definition: JTimekeeper.hh:100
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 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.
std::string toString() const
Convert subscription list to string.
JSubscriptionList & add(const JSubscription &subscription)
Add subscription.
Utility class to parse command line options.
Definition: JParser.hh:1698
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
static const JLocalTime getLocalTime
Function object to get local time in micro seconds.
Definition: JSTDTypes.hh:14
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:68