Goby v2
queue_simple.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 a single message 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()
35 {
36  //
37  // 1. Initialize the QueueManager
38  //
40 
41  // our modem id (arbitrary, but must be unique in the network)
42  const int our_id = 1;
43 
45  cfg.set_modem_id(our_id);
46  cfg.add_message_entry()->set_protobuf_name("Simple");
47  q_manager.set_cfg(cfg);
48 
49  // set up the callback to handle received DCCL messages
50  goby::acomms::connect(&q_manager.signal_receive, &received_data);
51 
52  //
53  // 2. Push a message to a queue
54  //
55 
56  // let's make a message to store in the queue
57  Simple msg;
58  msg.set_telegram("hello all!");
59  q_manager.push_message(msg);
60 
61  std::cout << "1. pushing message to queue 1: " << msg << std::endl;
62 
63  // see what our QueueManager contains
64  std::cout << "2. " << q_manager << std::endl;
65 
66  //
67  // 3. Create a loopback to simulate the Link Layer (libmodemdriver & modem firmware)
68  //
69 
70  std::cout
71  << "3. executing loopback (simulating sending a message to ourselves over the modem link)"
72  << std::endl;
73 
74  // pretend the modem is requesting data of up to 32 bytes
75 
77  request_msg.set_max_frame_bytes(32);
78  request_msg.set_max_num_frames(1);
79 
80  q_manager.handle_modem_data_request(&request_msg);
81 
82  std::cout << "4. requesting data, got: " << request_msg << std::endl;
83  std::cout << "\tdata as hex: " << goby::util::hex_encode(request_msg.frame(0)) << std::endl;
84 
85  //
86  // 4. Pass the received message to the QueueManager (same as outgoing message)
87  //
88  q_manager.handle_modem_receive(request_msg);
89 
90  return 0;
91 }
92 
93 //
94 // 5. Do something with the received message
95 //
96 void received_data(const google::protobuf::Message& msg)
97 {
98  std::cout << "5. received message: " << msg << std::endl;
99 }
provides an API to the goby-acomms Queuing Library.
Definition: queue_manager.h:49
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
void set_cfg(const protobuf::QueueManagerConfig &cfg)
Set (and overwrite completely if present) the current configuration. (protobuf::QueueManagerConfig de...
void push_message(const google::protobuf::Message &new_message)
Push a message (and add the queue if it does not exist)