Goby v2
test.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 //
5 //
6 // This file is part of the Goby Underwater Autonomy Project Binaries
7 // ("The Goby Binaries").
8 //
9 // The Goby Binaries are free software: you can redistribute them and/or modify
10 // them under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // The Goby Binaries are distributed in the hope that they will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
21 
22 // tests basic REP/REQ functionality of ZeroMQService class
23 
24 #include "goby/common/zeromq_service.h"
25 
26 void inbox(goby::common::MarshallingScheme marshalling_scheme, const std::string& identifier,
27  const std::string& data, int socket_id);
28 
29 const std::string reply_identifier_ = "RESPONSE/";
30 const std::string request_identifier_ = "REQUEST/";
31 int inbox_count_ = 0;
32 const char request_data_[] = {'h', 'i', '\0'};
33 const char reply_data_[] = {'w', 'e', 'l', 'l', ' ', 'h', 'e', 'l', 'l', 'o', '\0'};
34 
35 // pick some misc values
36 enum
37 {
38  SOCKET_REQUESTOR = 104952,
39  SOCKET_REPLIER = 2304
40 };
41 
42 int main(int argc, char* argv[])
43 {
44  goby::glog.add_stream(goby::common::logger::DEBUG3, &std::cerr);
45  goby::glog.set_name(argv[0]);
46 
48 
49  {
51 
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;
61 
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);
69 
70  std::cout << replier_socket->DebugString() << std::endl;
71 
72  node.merge_cfg(requestor_cfg);
73  node.merge_cfg(replier_cfg);
74  }
75 
76  node.connect_inbox_slot(&inbox);
77 
78  usleep(1e4);
79 
80  int test_count = 3;
81  for (int i = 0; i < test_count; ++i)
82  {
83  std::cout << "requesting " << request_data_ << std::endl;
84  node.send(goby::common::MARSHALLING_CSTR, request_identifier_, std::string(request_data_),
85  SOCKET_REQUESTOR);
86  node.poll(1e6);
87  std::cout << "replying " << reply_data_ << std::endl;
88  node.send(goby::common::MARSHALLING_CSTR, reply_identifier_, std::string(reply_data_),
89  SOCKET_REPLIER);
90  node.poll(1e6);
91  }
92 
93  assert(inbox_count_ == 2 * test_count);
94 
95  std::cout << "all tests passed" << std::endl;
96 }
97 
98 void inbox(goby::common::MarshallingScheme marshalling_scheme, const std::string& identifier,
99  const std::string& data, int socket_id)
100 {
101  if (socket_id == SOCKET_REPLIER)
102  {
103  assert(identifier == request_identifier_);
104  assert(marshalling_scheme == goby::common::MARSHALLING_CSTR);
105  assert(!strcmp(data.c_str(), request_data_));
106 
107  std::cout << "Replier Received: " << data << std::endl;
108  ++inbox_count_;
109  }
110  else if (socket_id == SOCKET_REQUESTOR)
111  {
112  assert(identifier == reply_identifier_);
113  assert(marshalling_scheme == goby::common::MARSHALLING_CSTR);
114  assert(!strcmp(data.c_str(), reply_data_));
115 
116  std::cout << "Requestor Received: " << data << std::endl;
117  ++inbox_count_;
118  }
119  else
120  {
121  assert(false);
122  }
123 }
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
Definition: flex_ostream.h:67
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...
Definition: flex_ostream.h:96