Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
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

◆ main()

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}
#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:72
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
General exception.
Definition JException.hh:24
virtual const char * what() const override
Get error message.
Definition JException.hh:64
Exception for socket.
ControlHost class.
ControlHost prefix.
Definition JPrefix.hh:33
int getSize() const
Get size.
Definition JPrefix.hh:62
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:1698
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
Auxiliary class for all subscription.
Auxiliary class for any subscription.