DCCL v3
test.cpp
1 // Copyright 2009-2017 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (for 2013-)
3 // Massachusetts Institute of Technology (for 2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Dynamic Compact Control Language Library
8 // ("DCCL").
9 //
10 // DCCL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // DCCL is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
22 // tests receiving one of several static types
23 
24 #include "dccl/codec.h"
25 #include "test.pb.h"
26 
27 using namespace dccl::test;
28 
29 using dccl::operator<<;
30 
31 GobyMessage1 msg_in1;
32 GobyMessage2 msg_in2;
33 GobyMessage3 msg_in3;
34 dccl::Codec codec;
35 
36 void decode(const std::string& bytes);
37 
38 int main(int argc, char* argv[])
39 {
40  dccl::dlog.connect(dccl::logger::ALL, &std::cerr);
41 
42  codec.load<GobyMessage1>();
43  codec.load<GobyMessage2>();
44  codec.load<GobyMessage3>();
45 
46  codec.info<GobyMessage1>(&dccl::dlog);
47  codec.info<GobyMessage2>(&dccl::dlog);
48  codec.info<GobyMessage3>(&dccl::dlog);
49 
50 
51 
52  msg_in1.set_int32_val(1);
53  msg_in2.set_bool_val(false);
54  msg_in3.set_string_val("string1");
55 
56  std::cout << "Try encode..." << std::endl;
57  std::string bytes1, bytes2, bytes3;
58  codec.encode(&bytes1, msg_in1);
59  std::cout << "... got bytes for GobyMessage1 (hex): " << dccl::hex_encode(bytes1) << std::endl;
60  codec.encode(&bytes2, msg_in2);
61  std::cout << "... got bytes for GobyMessage2 (hex): " << dccl::hex_encode(bytes2) << std::endl;
62  codec.encode(&bytes3, msg_in3);
63  std::cout << "... got bytes for GobyMessage3 (hex): " << dccl::hex_encode(bytes3) << std::endl;
64 
65  std::cout << "Try decode..." << std::endl;
66 
67  // mix up the order
68  decode(bytes2);
69  decode(bytes1);
70  decode(bytes3);
71 
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(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 
105 }
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:56
void load()
All messages must be explicited loaded and validated (size checks, option extensions checks...
Definition: codec.h:96
unsigned id() const
Gives the DCCL id (defined by the custom message option extension "(dccl.msg).id" in the ...
Definition: codec.h:156
void info(std::ostream *os=0) const
Writes a human readable summary (including field sizes) of the provided DCCL type to the stream provi...
Definition: codec.h:139
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar) ...
Definition: logger.h:161
void hex_encode(CharIterator begin, CharIterator end, std::string *out, bool upper_case=false)
Encodes a (little-endian) hexadecimal string from a byte string. Index 0 of begin is written to index...
Definition: binary.h:98
Unit test namespace.
Definition: test.pb.h:36
CharIterator decode(CharIterator begin, CharIterator end, google::protobuf::Message *msg, bool header_only=false)
Decode a DCCL message when the type is known at compile time.
Definition: codec.h:405
void encode(std::string *bytes, const google::protobuf::Message &msg, bool header_only=false)
Encodes a DCCL message.
Definition: codec.cpp:266