Goby v2
dccl_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 // encodes/decodes a string using the DCCL codec library
23 // assumes prior knowledge of the message format (required fields)
24 
25 #include "goby/acomms/acomms_helpers.h" // for operator<< of google::protobuf::Message
26 #include "goby/acomms/dccl.h" // for DCCLCodec
27 #include "goby/util/binary.h" // for goby::util::hex_encode
28 #include "simple.pb.h" // for `Simple` protobuf message defined in simple.proto
29 
30 using goby::acomms::operator<<;
31 
32 int main()
33 {
35 
36  // validate the Simple protobuf message type as a valid DCCL message type
37  dccl->validate<Simple>();
38 
39  // read message content (in this case from the command line)
40  Simple message;
41  std::cout << "input a string to send up to 10 characters: " << std::endl;
42  getline(std::cin, *message.mutable_telegram());
43 
44  std::cout << "passing message to encoder:\n" << message << std::endl;
45 
46  // encode the message to a byte string
47  std::string bytes;
48  dccl->encode(&bytes, message);
49 
50  std::cout << "received hexadecimal string: " << goby::util::hex_encode(bytes) << std::endl;
51 
52  message.Clear();
53  // input contents right back to decoder
54  std::cout << "passed hexadecimal string to decoder: " << goby::util::hex_encode(bytes)
55  << std::endl;
56 
57  dccl->decode(bytes, &message);
58 
59  std::cout << "received message:" << message << std::endl;
60 
61  return 0;
62 }
static DCCLCodec * get()
DCCLCodec is a singleton class; use this to get a pointer to the class.
Definition: dccl.h:124