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