Jpp  15.0.1-rc.1-highQE
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JControlHost.cc File Reference

Example program to test JNET::JControlHost. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include "JNet/JControlHost.hh"
#include "JSystem/JNetwork.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Detailed Description

Example program to test JNET::JControlHost.

Author
mdejong

Definition in file JControlHost.cc.

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 19 of file JControlHost.cc.

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 getIPnumber(const std::string &host_name)
Get IP number.
Definition: JNetwork.hh:117
Utility class to parse command line options.
Definition: JParser.hh:1500
is
Definition: JDAQCHSM.chsm:167
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
#define NOTICE(A)
Definition: JMessage.hh:64
int debug
debug level
Definition: JSirene.cc:63
std::string getHostname()
Get host name.
Definition: JNetwork.hh:77
#define FATAL(A)
Definition: JMessage.hh:67
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG