Jpp
 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 
38  zap['Y'] = make_field(any);
39 
40  zap['d'] = make_field(debug) = 3;
41 
42  zap(argc, argv);
43  }
44  catch(const exception &error) {
45  FATAL(error.what() << endl);
46  }
47 
48 
49  const string pid = argv[0];
50 
51  try {
52 
53  JControlHost socket(hostname);
54 
55  DEBUG("Subscription: " << tag << endl);
56 
57  socket.MyId(pid);
58 
59  if (any) {
60  socket.Subscribe(JSubscriptionAny(tag));
61  }
62  else {
63  socket.Subscribe(JSubscriptionAll(tag));
64  }
65 
66  socket.SendMeAlways();
67 
68  DEBUG("Send message: "
69  << "tag <" << tag << "> "
70  << "string <" << message << ">" << endl);
71 
72  socket.PutFullString(tag, message);
73 
74  {
75  DEBUG("Test GetFullString()." << endl);
76 
77  JPrefix prefix;
78  string buffer;
79 
80  for (useconds_t i = 0; i != 10000; ++i) {
81 
82  if (socket.CheckHead(prefix) > 0) {
83 
84  DEBUG("Got message: "
85  << "time " << i << " us "
86  << "tag <" << prefix.getTag() << "> "
87  << "length " << prefix.getSize() << endl);
88 
89 
90  socket.GetFullString(buffer);
91 
92  NOTICE("string <" << buffer << ">" << endl);
93 
94  ASSERT(buffer == message);
95 
96  break;
97 
98  } else
99 
100  usleep(1);
101  }
102 
103  if (prefix.toString() == "") {
104  FATAL("Timeout." << endl);
105  }
106  }
107 
108 
109  {
110  DEBUG("Test WhereIs()." << endl);
111 
112  string answer;
113 
114  ControlHost::WhereIs(hostname, pid, answer);
115 
116  istringstream is(answer);
117 
118  for (string buffer; is >> buffer; ) {
119 
120  NOTICE("Host: " << buffer << endl);
121 
122  ASSERT(buffer == getHostname(getIPnumber()));
123  }
124  }
125 
126 
127  try {
128 
129  DEBUG("Test Throw(true)." << endl);
130 
131  socket.close();
132 
133  try {
134 
135  ControlHost::Throw(true);
136 
137  socket.PutFullString(tag[0], message);
138 
139  ASSERT(false);
140  }
141  catch(const JException& error) {
142 
143  NOTICE("Exception test: " << error.what() << endl);
144 
145  ASSERT(true);
146  }
147 
148 
149  DEBUG("Test Throw(false)." << endl);
150 
151  ControlHost::Throw(false);
152 
153  int retcd = socket.PutFullString(tag[0], message);
154 
155  NOTICE("Return code test: " << retcd << endl);
156 
157  ASSERT(retcd != 0);
158  }
159  catch(const JSocketException& error) {
160  NOTICE("Exception test failed: " << error.what() << endl);
161  }
162  }
163  catch(const JSocketException& error) {
164  FATAL(error.what() << endl);
165  }
166 
167  return 0;
168 }
int getIPnumber(const std::string &host_name)
Get IP number.
Definition: JNetwork.hh:115
Utility class to parse command line options.
Definition: JParser.hh:1410
#define ASSERT(A)
Assert macro.
Definition: JMessage.hh:72
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
#define NOTICE(A)
Definition: JMessage.hh:62
int debug
debug level
Definition: JSirene.cc:59
std::string getHostname()
Get host name.
Definition: JNetwork.hh:75
#define FATAL(A)
Definition: JMessage.hh:65
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60