Jpp  debug
the software that should make you happy
JControlHost.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <sstream>
5 
6 #include "JNet/JControlHost.hh"
7 #include "JSystem/JNetwork.hh"
8 
9 #include "Jeep/JParser.hh"
10 #include "Jeep/JMessage.hh"
11 
12 
13 /**
14  * \file
15  *
16  * Example program to test JNET::JControlHost.
17  * \author mdejong
18  */
19 int main(int argc, char* argv[])
20 {
21  using namespace std;
22  using namespace JPP;
23 
24  string hostname;
25  JTag tag;
26  string message;
27  bool any;
28  int debug;
29 
30  try {
31 
32  JParser<> zap("Example program to test communication with JControlHost server.");
33 
34  zap['H'] = make_field(hostname) = "localhost";
35  zap['T'] = make_field(tag) = JTag("ME");
36  zap['m'] = make_field(message) = "hello world";
37  zap['Y'] = make_field(any);
38  zap['d'] = make_field(debug) = 3;
39 
40  zap(argc, argv);
41  }
42  catch(const exception &error) {
43  FATAL(error.what() << endl);
44  }
45 
46 
47  const string pid = argv[0];
48 
49  try {
50 
51  JControlHost socket(hostname);
52 
53  DEBUG("Subscription: " << tag << endl);
54 
55  socket.MyId(pid);
56 
57  if (any) {
58  socket.Subscribe(JSubscriptionAny(tag));
59  }
60  else {
61  socket.Subscribe(JSubscriptionAll(tag));
62  }
63 
64  socket.SendMeAlways();
65 
66  DEBUG("Send message: "
67  << "tag <" << tag << "> "
68  << "string <" << message << ">" << endl);
69 
70  socket.PutFullString(tag, message);
71 
72  {
73  DEBUG("Test GetFullString()." << endl);
74 
75  JPrefix prefix;
76  string buffer;
77 
78  for (useconds_t i = 0; i != 10000; ++i) {
79 
80  if (socket.CheckHead(prefix) > 0) {
81 
82  DEBUG("Got message: "
83  << "time " << i << " us "
84  << "tag <" << prefix.getTag() << "> "
85  << "length " << prefix.getSize() << endl);
86 
87 
88  socket.GetFullString(buffer);
89 
90  NOTICE("string <" << buffer << ">" << endl);
91 
92  ASSERT(buffer == message);
93 
94  break;
95 
96  } else
97 
98  usleep(1);
99  }
100 
101  if (prefix.toString() == "") {
102  FATAL("Timeout." << endl);
103  }
104  }
105 
106 
107  {
108  DEBUG("Test WhereIs()." << endl);
109 
110  string answer;
111 
112  ControlHost::WhereIs(hostname, pid, answer);
113 
114  istringstream is(answer);
115 
116  for (string buffer; is >> buffer; ) {
117 
118  NOTICE("Host: " << buffer << endl);
119 
120  ASSERT(buffer == getHostname(getIPnumber()));
121  }
122  }
123 
124 
125  try {
126 
127  DEBUG("Test Throw(true)." << endl);
128 
129  socket.close();
130 
131  try {
132 
133  ControlHost::Throw(true);
134 
135  socket.PutFullString(tag[0], message);
136 
137  ASSERT(false);
138  }
139  catch(const JException& error) {
140 
141  NOTICE("Exception test: " << error.what() << endl);
142 
143  ASSERT(true);
144  }
145 
146 
147  DEBUG("Test Throw(false)." << endl);
148 
149  ControlHost::Throw(false);
150 
151  int retcd = socket.PutFullString(tag[0], message);
152 
153  NOTICE("Return code test: " << retcd << endl);
154 
155  ASSERT(retcd != 0);
156  }
157  catch(const JSocketException& error) {
158  NOTICE("Exception test failed: " << error.what() << endl);
159  }
160  }
161  catch(const JSocketException& error) {
162  FATAL(error.what() << endl);
163  }
164 
165  return 0;
166 }
int main(int argc, char *argv[])
Definition: JControlHost.cc:19
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define NOTICE(A)
Definition: JMessage.hh:64
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
Hostname and IP address functions.
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2158
General exception.
Definition: JException.hh:24
virtual const char * what() const override
Get error message.
Definition: JException.hh:64
int close()
Close file.
Definition: JFile.hh:57
Exception for socket.
Definition: JException.hh:468
ControlHost class.
int MyId(const std::string &nick_name)
Identify.
int PutFullString(const JTag &tag, const std::string &buffer)
Send string.
int GetFullString(std::string &buffer)
Receive string.
int CheckHead(JPrefix &prefix, JTimeval timeout=JTimeval::min())
Check for header, without waiting.
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
ControlHost tag.
Definition: JTag.hh:38
std::string toString() const
Convert tag to string.
Definition: JTag.hh:171
const JTag & getTag() const
Get tag.
Definition: JTag.hh:86
Utility class to parse command line options.
Definition: JParser.hh:1714
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
int getIPnumber(const std::string &host_name)
Get IP number.
Definition: JNetwork.hh:117
std::string getHostname()
Get host name.
Definition: JNetwork.hh:77
Definition: JSTDTypes.hh:14
Auxiliary class for all subscription.
Definition: JControlHost.hh:99
Auxiliary class for any subscription.