Goby v2
multimessage.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 // queues two messages from the DCCL library
23 
24 #include "goby/acomms/connect.h"
25 #include "goby/acomms/queue.h"
26 #include "goby/util/binary.h"
27 #include "simple.pb.h"
28 #include <iostream>
29 
30 using goby::acomms::operator<<;
31 
32 void received_data(const google::protobuf::Message& msg);
33 
34 int main(int argc, char* argv[])
35 {
36  //
37  // 0. Initialize logging
38  //
39  goby::glog.add_stream(goby::common::logger::VERBOSE, &std::cerr);
40  goby::glog.set_name(argv[0]);
41 
42  //
43  // 1. Initialize the QueueManager
44  //
46 
47  // our modem id (arbitrary, but must be unique in the network)
48  const int our_id = 1;
49 
51  cfg.set_modem_id(our_id);
52  cfg.add_message_entry()->set_protobuf_name("Simple");
53  q_manager.set_cfg(cfg);
54 
55  // set up the callback to handle received DCCL messages
56  goby::acomms::connect(&q_manager.signal_receive, &received_data);
57 
58  //
59  // 2. Push a message to a queue
60  //
61 
62  // let's make a message to store in the queue
63  Simple msg;
64  msg.set_telegram("hello 1!");
65  std::cout << "1.a. pushing message to queue 1: " << msg << std::endl;
66  q_manager.push_message(msg);
67 
68  msg.set_telegram("hello 2!");
69  std::cout << "1.b. pushing message to queue 1: " << msg << std::endl;
70  q_manager.push_message(msg);
71 
72  // see what our QueueManager contains
73  std::cout << "2. " << q_manager << std::endl;
74 
75  //
76  // 3. Create a loopback to simulate the Link Layer (libmodemdriver & modem firmware)
77  //
78 
79  std::cout
80  << "3. executing loopback (simulating sending a message to ourselves over the modem link)"
81  << std::endl;
82 
83  // pretend the modem is requesting data of up to 64 bytes
84 
86  request_msg.set_max_frame_bytes(64);
87  request_msg.set_max_num_frames(1);
88 
89  q_manager.handle_modem_data_request(&request_msg);
90 
91  std::cout << "4. requesting data, got: " << request_msg << std::endl;
92  std::cout << "\tdata as hex: " << goby::util::hex_encode(request_msg.frame(0)) << std::endl;
93 
94  //
95  // 4. Pass the received message to the QueueManager (same as outgoing message)
96  //
97  q_manager.handle_modem_receive(request_msg);
98 
99  return 0;
100 }
101 
102 //
103 // 5. Do something with the received message
104 //
105 void received_data(const google::protobuf::Message& msg)
106 {
107  std::cout << "5. received message: " << msg << std::endl;
108 }
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...
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)