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/dccl.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 // tests basic DCCL queuing with non-BROADCAST destination
29 
30 using goby::acomms::operator<<;
31 
32 int receive_count = 0;
33 GobyMessage msg_in1;
34 
35 void handle_receive(const google::protobuf::Message& msg);
36 
37 int main(int argc, char* argv[])
38 {
39  goby::glog.add_stream(goby::common::logger::DEBUG3, &std::cerr);
40  goby::glog.set_name(argv[0]);
41 
44  const int MY_MODEM_ID = 1;
45  const int UNICORN_MODEM_ID = 3;
46  cfg.set_modem_id(MY_MODEM_ID);
47  goby::acomms::protobuf::QueuedMessageEntry* q_entry = cfg.add_message_entry();
48  q_entry->set_protobuf_name("GobyMessage");
49  q_entry->set_newest_first(true);
50 
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("header.dest_platform");
54 
55  goby::acomms::protobuf::QueuedMessageEntry::Role* time_role = q_entry->add_role();
56  time_role->set_type(goby::acomms::protobuf::QueuedMessageEntry::TIMESTAMP);
57  time_role->set_field("header.time");
58 
59  goby::acomms::protobuf::QueuedMessageEntry::Role* src_role = q_entry->add_role();
60  src_role->set_type(goby::acomms::protobuf::QueuedMessageEntry::SOURCE_ID);
61  // intentionally misspelled
62  src_role->set_field("hder.source_platform");
63 
64  try
65  {
66  q_manager.set_cfg(cfg);
67  bool FAILED_CHECK_FIELDS = false;
68  assert(FAILED_CHECK_FIELDS);
69  }
71  {
72  // good
73  }
74 
75  // fix the misspelling and try again
76  src_role->set_field("header.source_platform");
77  q_manager.set_cfg(cfg);
78 
79  goby::acomms::connect(&q_manager.signal_receive, &handle_receive);
80 
81  msg_in1.set_telegram("hello!");
82  msg_in1.mutable_header()->set_time(
83  goby::util::as<goby::uint64>(boost::posix_time::second_clock::universal_time()));
84  msg_in1.mutable_header()->set_source_platform(MY_MODEM_ID);
85  msg_in1.mutable_header()->set_dest_platform(UNICORN_MODEM_ID);
86  msg_in1.mutable_header()->set_dest_type(Header::PUBLISH_OTHER);
87 
88  std::cout << "Pushed: " << msg_in1 << std::endl;
89  q_manager.push_message(msg_in1);
90 
92  transmit_msg.set_max_frame_bytes(256);
93  transmit_msg.set_dest(UNICORN_MODEM_ID);
94 
95  q_manager.handle_modem_data_request(&transmit_msg);
96 
97  std::cout << "requesting data, got: " << transmit_msg << std::endl;
98  std::cout << "\tdata as hex: " << goby::util::hex_encode(transmit_msg.frame(0)) << std::endl;
99 
100  std::string encoded;
101  goby::acomms::DCCLCodec::get()->encode(&encoded, msg_in1);
102 
103  assert(transmit_msg.frame(0) == encoded);
104  assert(transmit_msg.src() == MY_MODEM_ID);
105  assert(transmit_msg.dest() == UNICORN_MODEM_ID);
106  assert(transmit_msg.ack_requested() == true);
107 
108  // feed back the modem layer - this will be rejected
109  q_manager.handle_modem_receive(transmit_msg);
110  assert(receive_count == 0);
111 
112  // pretend we're now Unicorn
113  cfg.set_modem_id(UNICORN_MODEM_ID);
114  q_manager.set_cfg(cfg);
115 
116  // feed back the modem layer
117  q_manager.handle_modem_receive(transmit_msg);
118 
119  assert(receive_count == 1);
120 
121  std::cout << "all tests passed" << std::endl;
122 }
123 
124 void handle_receive(const google::protobuf::Message& msg)
125 {
126  std::cout << "Received: " << msg << std::endl;
127 
128  assert(msg_in1.SerializeAsString() == msg.SerializeAsString());
129 
130  ++receive_count;
131 }
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.
Exception class for libdccl.
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...
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)