24 #include "goby/common/zeromq_service.h" 26 void inbox(goby::common::MarshallingScheme marshalling_scheme,
const std::string& identifier,
27 const std::string& data,
int socket_id);
29 const std::string reply_identifier_ =
"RESPONSE/";
30 const std::string request_identifier_ =
"REQUEST/";
32 const char request_data_[] = {
'h',
'i',
'\0'};
33 const char reply_data_[] = {
'w',
'e',
'l',
'l',
' ',
'h',
'e',
'l',
'l',
'o',
'\0'};
38 SOCKET_REQUESTOR = 104952,
42 int main(
int argc,
char* argv[])
52 ZeroMQServiceConfig requestor_cfg;
53 ZeroMQServiceConfig::Socket* requestor_socket = requestor_cfg.add_socket();
54 requestor_socket->set_socket_type(ZeroMQServiceConfig::Socket::REQUEST);
55 requestor_socket->set_transport(ZeroMQServiceConfig::Socket::TCP);
56 requestor_socket->set_socket_id(SOCKET_REQUESTOR);
57 requestor_socket->set_ethernet_address(
"127.0.0.1");
58 requestor_socket->set_ethernet_port(54321);
59 requestor_socket->set_connect_or_bind(ZeroMQServiceConfig::Socket::CONNECT);
60 std::cout << requestor_socket->DebugString() << std::endl;
62 ZeroMQServiceConfig replier_cfg;
63 ZeroMQServiceConfig::Socket* replier_socket = replier_cfg.add_socket();
64 replier_socket->set_socket_type(ZeroMQServiceConfig::Socket::REPLY);
65 replier_socket->set_transport(ZeroMQServiceConfig::Socket::TCP);
66 replier_socket->set_ethernet_port(54321);
67 replier_socket->set_connect_or_bind(ZeroMQServiceConfig::Socket::BIND);
68 replier_socket->set_socket_id(SOCKET_REPLIER);
70 std::cout << replier_socket->DebugString() << std::endl;
72 node.merge_cfg(requestor_cfg);
73 node.merge_cfg(replier_cfg);
76 node.connect_inbox_slot(&inbox);
81 for (
int i = 0; i < test_count; ++i)
83 std::cout <<
"requesting " << request_data_ << std::endl;
84 node.send(goby::common::MARSHALLING_CSTR, request_identifier_, std::string(request_data_),
87 std::cout <<
"replying " << reply_data_ << std::endl;
88 node.send(goby::common::MARSHALLING_CSTR, reply_identifier_, std::string(reply_data_),
93 assert(inbox_count_ == 2 * test_count);
95 std::cout <<
"all tests passed" << std::endl;
98 void inbox(goby::common::MarshallingScheme marshalling_scheme,
const std::string& identifier,
99 const std::string& data,
int socket_id)
101 if (socket_id == SOCKET_REPLIER)
103 assert(identifier == request_identifier_);
104 assert(marshalling_scheme == goby::common::MARSHALLING_CSTR);
105 assert(!strcmp(data.c_str(), request_data_));
107 std::cout <<
"Replier Received: " << data << std::endl;
110 else if (socket_id == SOCKET_REQUESTOR)
112 assert(identifier == reply_identifier_);
113 assert(marshalling_scheme == goby::common::MARSHALLING_CSTR);
114 assert(!strcmp(data.c_str(), reply_data_));
116 std::cout <<
"Requestor Received: " << data << std::endl;
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
common::FlexOstream glog
Access the Goby logger through this object.
void add_stream(logger::Verbosity verbosity=logger::VERBOSE, std::ostream *os=0)
Attach a stream object (e.g. std::cout, std::ofstream, ...) to the logger with desired verbosity...