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 proper encoding of standard Goby header
23 
24 #include "goby/acomms/dccl.h"
25 #include "goby/common/time.h"
26 #include "goby/util/as.h"
27 #include "goby/util/binary.h"
28 #include "test.pb.h"
29 
30 using goby::acomms::operator<<;
31 
32 int main(int argc, char* argv[])
33 {
34  goby::glog.add_stream(goby::common::logger::DEBUG3, &std::cerr);
35  goby::glog.set_name(argv[0]);
36 
39  codec->set_cfg(cfg);
40 
41  GobyMessage msg_in1;
42 
43  msg_in1.set_telegram("hello!");
44  msg_in1.mutable_header()->set_time(
45  goby::util::as<goby::uint64>(boost::posix_time::second_clock::universal_time()));
46  msg_in1.mutable_header()->set_source_platform(1);
47  msg_in1.mutable_header()->set_dest_platform(3);
48  msg_in1.mutable_header()->set_dest_type(Header::PUBLISH_OTHER);
49 
50  codec->info(msg_in1.GetDescriptor(), &std::cout);
51  std::cout << "Message in:\n" << msg_in1.DebugString() << std::endl;
52  codec->validate(msg_in1.GetDescriptor());
53  std::cout << "Try encode..." << std::endl;
54  std::string bytes1;
55  codec->encode(&bytes1, msg_in1);
56  std::cout << "... got bytes (hex): " << goby::util::hex_encode(bytes1) << std::endl;
57 
58  // test that adding garbage to the end does not affect decoding
59  bytes1 += std::string(10, '\0');
60 
61  std::cout << "Try decode..." << std::endl;
62 
63  GobyMessage* msg_out1 = codec->decode<GobyMessage*>(bytes1);
64  std::cout << "... got Message out:\n" << msg_out1->DebugString() << std::endl;
65  assert(msg_in1.SerializeAsString() == msg_out1->SerializeAsString());
66  delete msg_out1;
67 
68  std::cout << "all tests passed" << std::endl;
69 }
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