Jpp  18.0.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JGetMessage.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <sstream>
4 #include <iomanip>
5 #include <set>
6 
7 #include "JNet/JControlHost.hh"
8 
10 
11 #include "Jeep/JParser.hh"
12 #include "Jeep/JMessage.hh"
13 
14 
15 /**
16  * \file
17  *
18  * Auxiliary program to receive messages from ControlHost server.
19  *
20  * The option <tt>-H <hostname>[:port]</tt> correponds to the hostname and the port of the server, respectively.\n
21  * The options <tt>-t</tt> and <tt>-T</tt> correspond to the ControlHost tag(s),
22  * with subscriptions any and all, respectively.\n
23  * Each message tag will be printed and for the latter also the message content.\n
24  * The program will terminate when it receives message <stop>.
25  * \author mdejong
26  */
27 int main(int argc, const char *argv[])
28 {
29  using namespace std;
30  using namespace JPP;
31 
32  string hostname;
33  set<JTag> tagList;
34  set<JTag> TagList;
35  bool date;
36  int debug;
37 
38  try {
39 
40  JParser<> zap("Auxiliary program to receive messages from ControlHost server.");
41 
42  zap['H'] = make_field(hostname, "host name of message srver") = "localhost";
43  zap['t'] = make_field(tagList, "list of tags with subscription any") = JPARSER::initialised();
44  zap['T'] = make_field(TagList, "list of tags with subscription all") = JPARSER::initialised();
45  zap['D'] = make_field(date, "print date and time of message receipt");
46  zap['d'] = make_field(debug) = 3;
47 
48  zap(argc, argv);
49  }
50  catch(const exception &error) {
51  FATAL(error.what() << endl);
52  }
53 
54 
55  if (tagList.empty() && TagList.empty()) {
56  FATAL("No tags specified.");
57  }
58 
59 
60  JControlHost::Throw(true);
61 
62  try {
63 
64  JControlHost in(hostname);
65 
66  {
67  JSubscriptionList buffer;
68 
69  for (set<JTag>::const_iterator i = tagList.begin(); i != tagList.end(); ++i) {
70  buffer.add(JSubscriptionAny(*i));
71  }
72 
73  for (set<JTag>::const_iterator i = TagList.begin(); i != TagList.end(); ++i) {
74  buffer.add(JSubscriptionAll(*i));
75  }
76 
77  in.MyId("JGetMessage");
78  in.Subscribe(buffer);
79  in.SendMeAlways();
80  }
81 
82 
83  JPrefix prefix;
84  vector<char> buffer;
85 
86  for (const string stop("stop"); buffer.size() != stop.size() || string(buffer.data(), stop.size()) != stop; ) {
87 
88  in.WaitHead(prefix);
89 
90  buffer.resize(prefix.getSize());
91 
92  in.GetFullData(buffer.data(), buffer.size());
93 
94  if (date) {
95  cout << getDateAndTime() << ' ';
96  }
97 
98  cout << left << setw(8) << prefix.getTag() << ' ' << right << setw(8) << prefix.getSize();
99 
100  if (TagList.find(prefix) != TagList.end()) {
101  cout << ' ' << string(buffer.data(), buffer.size());
102  }
103 
104  cout << endl;
105  }
106  }
107  catch(const JControlHostException& error) {
108  FATAL(error << endl);
109  }
110 
111  return 0;
112 }
113 
114 
Utility class to parse command line options.
Definition: JParser.hh:1514
int main(int argc, char *argv[])
Definition: Main.cc:15
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
Date and time functions.
then usage $script[port]< option > nPossible stop
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
then awk string
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
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 debug
debug level