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/acomms_constants.h"
23 #include "goby/acomms/connect.h"
24 #include "goby/acomms/queue.h"
25 #include "goby/common/logger.h"
26 #include "goby/util/binary.h"
27 #include "test.pb.h"
28 
29 // tests basic DCCL queuing
30 
31 using goby::acomms::operator<<;
32 
33 int receive_count = 0;
34 TestMsg test_msg1;
35 
36 void handle_receive(const google::protobuf::Message& msg);
37 
38 int main(int argc, char* argv[])
39 {
40  goby::glog.add_stream(goby::common::logger::DEBUG3, &std::cerr);
41  goby::glog.set_name(argv[0]);
42 
45  const int MY_MODEM_ID = 1;
46  cfg.set_modem_id(MY_MODEM_ID);
47  cfg.add_message_entry()->set_protobuf_name("TestMsg");
48 
49  q_manager.set_cfg(cfg);
50 
51  goby::acomms::connect(&q_manager.signal_receive, &handle_receive);
52 
53  test_msg1.set_double_default_optional(1.23);
54  test_msg1.set_float_default_optional(0.2);
55 
56  std::cout << "Pushed: " << test_msg1 << std::endl;
57  q_manager.push_message(test_msg1);
58 
60  msg.set_max_frame_bytes(256);
61  q_manager.handle_modem_data_request(&msg);
62 
63  std::cout << "requesting data, got: " << msg << std::endl;
64  std::cout << "\tdata as hex: " << goby::util::hex_encode(msg.frame(0)) << std::endl;
65 
66  std::string encoded;
67  goby::acomms::DCCLCodec::get()->encode(&encoded, test_msg1);
68 
69  assert(msg.frame(0) == encoded);
70  assert(msg.src() == MY_MODEM_ID);
71  assert(msg.dest() == goby::acomms::BROADCAST_ID);
72 
73  assert(msg.ack_requested() == false);
74 
75  // feed back the modem layer
76  q_manager.handle_modem_receive(msg);
77 
78  assert(receive_count == 1);
79 
80  std::cout << "all tests passed" << std::endl;
81 }
82 
83 void handle_receive(const google::protobuf::Message& msg)
84 {
85  std::cout << "Received: " << msg << std::endl;
86 
87  assert(test_msg1.SerializeAsString() == msg.SerializeAsString());
88 
89  ++receive_count;
90 }
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
static DCCLCodec * get()
DCCLCodec is a singleton class; use this to get a pointer to the class.
Definition: dccl.h:124
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.
const int BROADCAST_ID
special modem id for the broadcast destination - no one is assigned this address. Analogous to 192...
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...
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)