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 publish/subscribe over EPGM functionality of ZeroMQService class
23 
24 #include "goby/common/zeromq_service.h"
25 
26 #include <boost/thread.hpp>
27 
28 void node_inbox(goby::common::MarshallingScheme marshalling_scheme, const std::string& identifier,
29  const std::string& data, int socket_id);
30 
31 const std::string identifier_ = "HI/";
32 int inbox_count_ = 0;
33 const char data_[] = {'h', 'i', '\0'};
34 
35 enum
36 {
37  SOCKET_SUBSCRIBE = 240,
38  SOCKET_PUBLISH = 211
39 };
40 
41 int main(int argc, char* argv[])
42 {
43  goby::glog.add_stream(goby::common::logger::DEBUG3, &std::cerr);
44  goby::glog.set_name(argv[0]);
45 
47 
49 
50  {
52  pubsub_cfg.add_socket();
53  subscriber_socket->set_socket_type(
54  goby::common::protobuf::ZeroMQServiceConfig::Socket::SUBSCRIBE);
55  subscriber_socket->set_socket_id(SOCKET_SUBSCRIBE);
56  subscriber_socket->set_ethernet_address("127.0.0.1");
57  std::cout << subscriber_socket->DebugString() << std::endl;
58  }
59 
60  {
62  pubsub_cfg.add_socket();
63  publisher_socket->set_socket_type(
64  goby::common::protobuf::ZeroMQServiceConfig::Socket::PUBLISH);
65  publisher_socket->set_ethernet_address("127.0.0.1");
66  publisher_socket->set_socket_id(SOCKET_PUBLISH);
67  std::cout << publisher_socket->DebugString() << std::endl;
68  }
69 
70  node.set_cfg(pubsub_cfg);
71  node.connect_inbox_slot(&node_inbox);
72  node.subscribe_all(SOCKET_SUBSCRIBE);
73 
74  usleep(1e3);
75 
76  int test_count = 3;
77  for (int i = 0; i < test_count; ++i)
78  {
79  std::cout << "publishing " << data_ << std::endl;
80  node.send(goby::common::MARSHALLING_CSTR, identifier_, data_, SOCKET_PUBLISH);
81  node.poll(1e6);
82  }
83 
84  assert(inbox_count_ == test_count);
85 
86  std::cout << "all tests passed" << std::endl;
87 }
88 
89 void node_inbox(goby::common::MarshallingScheme marshalling_scheme, const std::string& identifier,
90  const std::string& data, int socket_id)
91 {
92  assert(identifier == identifier_);
93  assert(marshalling_scheme == goby::common::MARSHALLING_CSTR);
94  assert(!strcmp(data.c_str(), data_));
95  assert(socket_id == SOCKET_SUBSCRIBE);
96 
97  std::cout << "Received: " << data << std::endl;
98  ++inbox_count_;
99 }
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