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 
29 // tests multi-frame DCCL queuing with non-BROADCAST destination
30 
31 using goby::acomms::operator<<;
32 
33 int receive_count = 0;
34 bool handle_ack_called = false;
35 int goby_message_qsize = 0;
36 GobyMessage msg_in1, msg_in2;
37 
38 void handle_ack(const goby::acomms::protobuf::ModemTransmission& ack_msg,
39  const google::protobuf::Message& orig_msg);
40 
41 void qsize(goby::acomms::protobuf::QueueSize size);
42 
43 void handle_receive(const google::protobuf::Message& msg);
44 
45 int main(int argc, char* argv[])
46 {
47  goby::glog.add_stream(goby::common::logger::DEBUG3, &std::cerr);
48  goby::glog.set_name(argv[0]);
49 
52  const int MY_MODEM_ID = 1;
53  const int UNICORN_MODEM_ID = 3;
54  cfg.set_modem_id(MY_MODEM_ID);
55 
56  goby::acomms::protobuf::QueuedMessageEntry* q_entry = cfg.add_message_entry();
57  q_entry->set_protobuf_name("GobyMessage");
58  q_entry->set_newest_first(true);
59 
60  goby::acomms::protobuf::QueuedMessageEntry::Role* src_role = q_entry->add_role();
61  src_role->set_type(goby::acomms::protobuf::QueuedMessageEntry::SOURCE_ID);
62  src_role->set_field("header.source_platform");
63 
64  goby::acomms::protobuf::QueuedMessageEntry::Role* dest_role = q_entry->add_role();
65  dest_role->set_type(goby::acomms::protobuf::QueuedMessageEntry::DESTINATION_ID);
66  dest_role->set_field("header.dest_platform");
67 
68  goby::acomms::protobuf::QueuedMessageEntry::Role* time_role = q_entry->add_role();
69  time_role->set_type(goby::acomms::protobuf::QueuedMessageEntry::TIMESTAMP);
70  time_role->set_field("header.time");
71 
72  q_manager.set_cfg(cfg);
73 
74  goby::acomms::connect(&q_manager.signal_receive, &handle_receive);
76  goby::acomms::connect(&q_manager.signal_ack, &handle_ack);
77 
78  msg_in1.set_telegram("hello!");
79  msg_in1.mutable_header()->set_time(
80  goby::util::as<goby::uint64>(boost::posix_time::second_clock::universal_time()));
81  msg_in1.mutable_header()->set_source_platform(MY_MODEM_ID);
82  msg_in1.mutable_header()->set_dest_platform(UNICORN_MODEM_ID);
83  msg_in1.mutable_header()->set_dest_type(Header::PUBLISH_OTHER);
84  msg_in1.set_telegram("hello 1");
85 
86  msg_in2 = msg_in1;
87  msg_in2.set_telegram("hello 2");
88 
89  std::cout << "Pushed: " << msg_in2 << std::endl;
90  q_manager.push_message(msg_in2);
91  std::cout << "Pushed: " << msg_in1 << std::endl;
92  q_manager.push_message(msg_in1);
93 
95  transmit_msg.set_max_frame_bytes(16);
96  transmit_msg.set_max_num_frames(2);
97  transmit_msg.set_dest(UNICORN_MODEM_ID);
98  q_manager.handle_modem_data_request(&transmit_msg);
99 
100  std::cout << "requesting data, got: " << transmit_msg << std::endl;
101  std::cout << "\tdata frame 0 as hex: " << goby::util::hex_encode(transmit_msg.frame(0))
102  << std::endl;
103  std::cout << "\tdata frame 1 as hex: " << goby::util::hex_encode(transmit_msg.frame(1))
104  << std::endl;
105 
106  // fake an ack from unicorn
108  ack.set_type(goby::acomms::protobuf::ModemTransmission::ACK);
109  ack.set_src(UNICORN_MODEM_ID);
110  ack.set_dest(MY_MODEM_ID);
111  ack.add_acked_frame(0);
112  ack.add_acked_frame(1);
113  q_manager.handle_modem_receive(ack);
114 
115  assert(goby_message_qsize == 0);
116  assert(handle_ack_called == true);
117 
118  std::cout << "all tests passed" << std::endl;
119 }
120 
121 void handle_receive(const google::protobuf::Message& msg)
122 {
123  std::cout << "Received: " << msg << std::endl;
124 
125  ++receive_count;
126 }
127 
128 void qsize(goby::acomms::protobuf::QueueSize size) { goby_message_qsize = size.size(); }
129 
130 void handle_ack(const goby::acomms::protobuf::ModemTransmission& ack_msg,
131  const google::protobuf::Message& orig_msg)
132 {
133  std::cout << "got an ack: " << ack_msg << "\n"
134  << "of original: " << orig_msg << std::endl;
135  handle_ack_called = true;
136 }
provides an API to the goby-acomms Queuing Library.
Definition: queue_manager.h:49
boost::signals2::signal< void(const protobuf::ModemTransmission &ack_msg, const google::protobuf::Message &orig_msg)> signal_ack
Signals when acknowledgment of proper message receipt has been received. This is only sent for queues...
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)