#include #include #include #include #include "JNet/JControlHost.hh" #include "JSystem/JNetwork.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" /** * \file * * Example program to test JNET::JControlHost. * \author mdejong */ int main(int argc, char* argv[]) { using namespace std; using namespace JPP; string hostname; JTag tag; string message; bool any; int debug; try { JParser<> zap("Example program to test communication with JControlHost server."); zap['H'] = make_field(hostname) = "localhost"; zap['T'] = make_field(tag) = JTag("ME"); zap['m'] = make_field(message) = "hello world"; zap['Y'] = make_field(any); zap['d'] = make_field(debug) = 3; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } const string pid = argv[0]; try { JControlHost socket(hostname); DEBUG("Subscription: " << tag << endl); socket.MyId(pid); if (any) { socket.Subscribe(JSubscriptionAny(tag)); } else { socket.Subscribe(JSubscriptionAll(tag)); } socket.SendMeAlways(); DEBUG("Send message: " << "tag <" << tag << "> " << "string <" << message << ">" << endl); socket.PutFullString(tag, message); { DEBUG("Test GetFullString()." << endl); JPrefix prefix; string buffer; for (useconds_t i = 0; i != 10000; ++i) { if (socket.CheckHead(prefix) > 0) { DEBUG("Got message: " << "time " << i << " us " << "tag <" << prefix.getTag() << "> " << "length " << prefix.getSize() << endl); socket.GetFullString(buffer); NOTICE("string <" << buffer << ">" << endl); ASSERT(buffer == message); break; } else usleep(1); } if (prefix.toString() == "") { FATAL("Timeout." << endl); } } { DEBUG("Test WhereIs()." << endl); string answer; ControlHost::WhereIs(hostname, pid, answer); istringstream is(answer); for (string buffer; is >> buffer; ) { NOTICE("Host: " << buffer << endl); ASSERT(buffer == getHostname(getIPnumber())); } } try { DEBUG("Test Throw(true)." << endl); socket.close(); try { ControlHost::Throw(true); socket.PutFullString(tag[0], message); ASSERT(false); } catch(const JException& error) { NOTICE("Exception test: " << error.what() << endl); ASSERT(true); } DEBUG("Test Throw(false)." << endl); ControlHost::Throw(false); int retcd = socket.PutFullString(tag[0], message); NOTICE("Return code test: " << retcd << endl); ASSERT(retcd != 0); } catch(const JSocketException& error) { NOTICE("Exception test failed: " << error.what() << endl); } } catch(const JSocketException& error) { FATAL(error.what() << endl); } return 0; }