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 // tests receiving one of several static types
23 
24 #include "goby/acomms/acomms_helpers.h"
25 #include "goby/acomms/dccl.h"
26 #include "test.pb.h"
27 
28 using goby::acomms::operator<<;
29 
30 GobyMessage1 msg_in1;
31 GobyMessage2 msg_in2;
32 GobyMessage3 msg_in3;
34 
35 void decode(const std::string& bytes);
36 
37 int main(int argc, char* argv[])
38 {
39  goby::glog.add_stream(goby::common::logger::DEBUG3, &std::cerr);
40  goby::glog.set_name(argv[0]);
41 
42  codec->validate<GobyMessage1>();
43  codec->validate<GobyMessage2>();
44  codec->validate<GobyMessage3>();
45 
46  codec->info<GobyMessage1>(&goby::glog);
47  codec->info<GobyMessage2>(&goby::glog);
48  codec->info<GobyMessage3>(&goby::glog);
49 
50  msg_in1.set_int32_val(1);
51  msg_in2.set_bool_val(false);
52  msg_in3.set_string_val("string1");
53 
54  std::cout << "Try encode..." << std::endl;
55  std::string bytes1, bytes2, bytes3;
56  codec->encode(&bytes1, msg_in1);
57  std::cout << "... got bytes for GobyMessage1 (hex): " << goby::util::hex_encode(bytes1)
58  << std::endl;
59  codec->encode(&bytes2, msg_in2);
60  std::cout << "... got bytes for GobyMessage2 (hex): " << goby::util::hex_encode(bytes2)
61  << std::endl;
62  codec->encode(&bytes3, msg_in3);
63  std::cout << "... got bytes for GobyMessage3 (hex): " << goby::util::hex_encode(bytes3)
64  << std::endl;
65 
66  std::cout << "Try decode..." << std::endl;
67 
68  // mix up the order
69  decode(bytes2);
70  decode(bytes1);
71  decode(bytes3);
72 
73  std::cout << "all tests passed" << std::endl;
74 }
75 
76 void decode(const std::string& bytes)
77 {
78  unsigned dccl_id = codec->id_from_encoded(bytes);
79 
80  if (dccl_id == codec->id<GobyMessage1>())
81  {
82  GobyMessage1 msg_out1;
83  codec->decode(bytes, &msg_out1);
84 
85  std::cout << "Got..." << msg_out1 << std::endl;
86  assert(msg_out1.SerializeAsString() == msg_in1.SerializeAsString());
87  }
88  else if (dccl_id == codec->id<GobyMessage2>())
89  {
90  GobyMessage2 msg_out2;
91  codec->decode(bytes, &msg_out2);
92 
93  std::cout << "Got..." << msg_out2 << std::endl;
94  assert(msg_out2.SerializeAsString() == msg_in2.SerializeAsString());
95  }
96  else if (dccl_id == codec->id<GobyMessage3>())
97  {
98  GobyMessage3 msg_out3;
99  codec->decode(bytes, &msg_out3);
100 
101  std::cout << "Got..." << msg_out3 << std::endl;
102  assert(msg_out3.SerializeAsString() == msg_in3.SerializeAsString());
103  }
104 }
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
Definition: flex_ostream.h:67
static DCCLCodec * get()
DCCLCodec is a singleton class; use this to get a pointer to the class.
Definition: dccl.h:124
common::FlexOstream glog
Access the Goby logger through this object.
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