Goby v2
encode_on_demand.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/queue.h"
24 #include "goby/util/binary.h"
25 #include "on_demand.pb.h"
26 
27 // demonstrates "encode_on_demand" functionality
28 
29 using goby::acomms::operator<<;
30 
31 void handle_encode_on_demand(const goby::acomms::protobuf::ModemTransmission& request_msg,
32  google::protobuf::Message* data_msg);
33 
34 void handle_receive(const google::protobuf::Message& msg);
35 
36 int main(int argc, char* argv[])
37 {
38  // get a pointer to the QueueManager singleton
40 
41  // configure the QueueManager with our modem id
43  const int MY_MODEM_ID = 1;
44  cfg.set_modem_id(MY_MODEM_ID);
45  q_manager.set_cfg(cfg);
46 
47  // add our queue
48  q_manager.add_queue<GobyMessage>();
49 
50  // take a look at what the QueueManager contains
51  std::cout << q_manager << std::endl;
52 
53  // connect the encode on demand slot
54  goby::acomms::connect(&q_manager.signal_data_on_demand, &handle_encode_on_demand);
55  // connect the receive slot
56  goby::acomms::connect(&q_manager.signal_receive, &handle_receive);
57 
58  // make a request for a 32 byte message
60  request_msg.set_max_frame_bytes(32);
61  request_msg.set_max_num_frames(1);
62 
63  std::cout << "requesting data..." << std::endl;
64  std::cout << "request message:" << request_msg << std::endl;
65 
66  q_manager.handle_modem_data_request(&request_msg);
67 
68  std::cout << "data message: " << request_msg << std::endl;
69  std::cout << "\tdata as hex: " << goby::util::hex_encode(request_msg.frame(0)) << std::endl;
70 
71  std::cout << "feeding the requested data back to the QueueManager as incoming... " << std::endl;
72  q_manager.handle_modem_receive(request_msg);
73 }
74 
75 void handle_encode_on_demand(const goby::acomms::protobuf::ModemTransmission& request_msg,
76  google::protobuf::Message* data_msg)
77 {
78  GobyMessage msg;
79 
80  static int i = 0;
81  msg.set_a(i++);
82  msg.set_b(i++);
83 
84  std::cout << "encoded on demand: " << msg << std::endl;
85 
86  // put our message into the data_msg for return
87  data_msg->CopyFrom(msg);
88 }
89 
90 void handle_receive(const google::protobuf::Message& in_msg)
91 {
92  GobyMessage msg;
93  msg.CopyFrom(in_msg);
94 
95  std::cout << "Received: " << msg << std::endl;
96 }
provides an API to the goby-acomms Queuing Library.
Definition: queue_manager.h:49
boost::signals2::signal< void(const protobuf::ModemTransmission &request_msg, google::protobuf::Message *data_msg)> signal_data_on_demand
Forwards the data request to the application layer. This advanced feature is used when queue...
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 add_queue(const protobuf::QueuedMessageEntry &queue_cfg)
Add a DCCL queue for use with QueueManager. Note that the queue must be added before receiving messag...
Definition: queue_manager.h:72
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...