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 #include "goby/acomms/connect.h"
23 #include "goby/acomms/queue.h"
24 #include "goby/common/logger.h"
25 #include "goby/util/binary.h"
26 #include "test.pb.h"
27 
28 // tests various manipulators' functionality
29 
30 using goby::acomms::operator<<;
31 
33 const int MY_MODEM_ID = 1;
34 const unsigned TEST_MESSAGE_SIZE = 3;
35 
36 int receive_count = 0;
37 
38 void qsize(goby::acomms::protobuf::QueueSize size);
39 
40 void handle_receive(const google::protobuf::Message& msg);
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  cfg.set_modem_id(MY_MODEM_ID);
49  goby::acomms::protobuf::QueuedMessageEntry* q_entry = cfg.add_message_entry();
50  q_entry->set_protobuf_name("GobyMessage");
51  goby::acomms::protobuf::QueuedMessageEntry::Role* dest_role = q_entry->add_role();
52  dest_role->set_type(goby::acomms::protobuf::QueuedMessageEntry::DESTINATION_ID);
53  dest_role->set_field("dest");
54 
55  q_entry->set_ack(false);
56  q_manager.set_cfg(cfg);
57 
58  goby::glog << q_manager << std::endl;
59 
61  goby::acomms::connect(&q_manager.signal_receive, &handle_receive);
62 
63  GobyMessage test_msg1;
64  test_msg1.set_dest(0);
65  test_msg1.set_telegram(1);
66  std::cout << "Pushed: " << test_msg1 << std::endl;
67  q_manager.push_message(test_msg1);
68 
70  msg.set_max_frame_bytes(32);
71  q_manager.handle_modem_data_request(&msg);
72 
73  // message was queued
74  assert(msg.frame(0).size() == TEST_MESSAGE_SIZE);
75  // feed back the modem layer
76  q_manager.handle_modem_receive(msg);
77  // message was decoded
78  assert(receive_count == 1);
79 
80  {
81  receive_count = 0;
82  cfg.mutable_message_entry(0)->clear_manipulator();
83  cfg.mutable_message_entry(0)->add_manipulator(goby::acomms::protobuf::NO_DECODE);
84  q_manager.set_cfg(cfg);
85 
86  q_manager.push_message(test_msg1);
87  msg.Clear();
88  msg.set_max_frame_bytes(32);
89  q_manager.handle_modem_data_request(&msg);
90  // message was queued
91  assert(msg.frame(0).size() == TEST_MESSAGE_SIZE);
92  q_manager.handle_modem_receive(msg);
93  // message was not decoded
94  assert(receive_count == 0);
95  }
96 
97  {
98  receive_count = 0;
99  cfg.mutable_message_entry(0)->clear_manipulator();
100  cfg.mutable_message_entry(0)->add_manipulator(goby::acomms::protobuf::NO_QUEUE);
101  q_manager.set_cfg(cfg);
102 
103  q_manager.push_message(test_msg1);
104  msg.Clear();
105  msg.set_max_frame_bytes(32);
106  q_manager.handle_modem_data_request(&msg);
107  // message was not queued
108  assert(msg.frame(0).size() == 0);
109  q_manager.handle_modem_receive(msg);
110  // message was not decoded
111  assert(receive_count == 0);
112  }
113 
114  {
115  receive_count = 0;
116  cfg.mutable_message_entry(0)->clear_manipulator();
117  cfg.mutable_message_entry(0)->add_manipulator(goby::acomms::protobuf::LOOPBACK);
118  q_manager.set_cfg(cfg);
119 
120  q_manager.push_message(test_msg1);
121  // loop'd back
122  assert(receive_count == 1);
123 
124  msg.Clear();
125  msg.set_max_frame_bytes(32);
126  q_manager.handle_modem_data_request(&msg);
127  // message was queued
128  assert(msg.frame(0).size() == TEST_MESSAGE_SIZE);
129  q_manager.handle_modem_receive(msg);
130  // message was decoded
131  assert(receive_count == 2);
132  }
133 
134  {
135  receive_count = 0;
136  cfg.mutable_message_entry(0)->clear_manipulator();
137  cfg.mutable_message_entry(0)->add_manipulator(goby::acomms::protobuf::LOOPBACK_AS_SENT);
138  q_manager.set_cfg(cfg);
139 
140  q_manager.push_message(test_msg1);
141  // not loop'd back (yet)
142  assert(receive_count == 0);
143 
144  msg.Clear();
145  msg.set_max_frame_bytes(32);
146  q_manager.handle_modem_data_request(&msg);
147 
148  // loop'd back
149  assert(receive_count == 1);
150 
151  // message was queued
152  assert(msg.frame(0).size() == TEST_MESSAGE_SIZE);
153  q_manager.handle_modem_receive(msg);
154  // message was decoded
155  assert(receive_count == 2);
156  }
157 
158  {
159  test_msg1.set_dest(3);
160 
161  receive_count = 0;
162  cfg.mutable_message_entry(0)->clear_manipulator();
163  q_manager.set_cfg(cfg);
164 
165  q_manager.push_message(test_msg1);
166 
167  msg.Clear();
168  msg.set_max_frame_bytes(32);
169  q_manager.handle_modem_data_request(&msg);
170 
171  // message was queued
172  assert(msg.frame(0).size() == TEST_MESSAGE_SIZE);
173  q_manager.handle_modem_receive(msg);
174  // message was decoded (due to wrong destination)
175  assert(receive_count == 0);
176 
177  receive_count = 0;
178  cfg.mutable_message_entry(0)->clear_manipulator();
179  cfg.mutable_message_entry(0)->add_manipulator(goby::acomms::protobuf::PROMISCUOUS);
180  q_manager.set_cfg(cfg);
181 
182  q_manager.push_message(test_msg1);
183 
184  msg.Clear();
185  msg.set_max_frame_bytes(32);
186  q_manager.handle_modem_data_request(&msg);
187 
188  // message was queued
189  assert(msg.frame(0).size() == TEST_MESSAGE_SIZE);
190  q_manager.handle_modem_receive(msg);
191  // message was decoded (due to promiscuous)
192  assert(receive_count == 1);
193  }
194 
195  std::cout << "all tests passed" << std::endl;
196 }
197 
198 void qsize(goby::acomms::protobuf::QueueSize size) {}
199 
200 void handle_receive(const google::protobuf::Message& in_msg)
201 {
202  std::cout << "Received: " << in_msg << std::endl;
203  ++receive_count;
204 }
provides an API to the goby-acomms Queuing Library.
Definition: queue_manager.h:49
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
Definition: flex_ostream.h:67
boost::signals2::signal< void(const google::protobuf::Message &msg)> signal_receive
Signals when a DCCL message is received.
void handle_modem_data_request(protobuf::ModemTransmission *msg)
Finds data to send to the modem.
void handle_modem_receive(const protobuf::ModemTransmission &message)
Receive incoming data from the modem.
void connect(Signal *signal, Slot slot)
connect a signal to a slot (e.g. function pointer)
Definition: connect.h:36
common::FlexOstream glog
Access the Goby logger through this object.
void set_cfg(const protobuf::QueueManagerConfig &cfg)
Set (and overwrite completely if present) the current configuration. (protobuf::QueueManagerConfig de...
boost::signals2::signal< void(protobuf::QueueSize size)> signal_queue_size_change
Signals when any queue changes size (message is popped or pushed)
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
void push_message(const google::protobuf::Message &new_message)
Push a message (and add the queue if it does not exist)