25 #include "goby/acomms/dccl.h" 26 #include "goby/common/time.h" 27 #include "goby/util/as.h" 28 #include "goby/util/binary.h" 29 #include "goby/util/sci.h" 32 using goby::acomms::operator<<;
33 using goby::acomms::Bitset;
38 unsigned size() {
return (part() == goby::acomms::MessageHandler::HEAD) ? 0 : A_SIZE + B_SIZE; }
39 Bitset encode() {
return Bitset(size()); }
43 if (part() == goby::acomms::MessageHandler::HEAD)
49 Bitset a(A_SIZE, static_cast<unsigned long>(msg.a()));
50 Bitset b(B_SIZE, static_cast<unsigned long>(msg.b()));
52 std::cout <<
"a: " << a << std::endl;
53 std::cout <<
"b: " << b << std::endl;
62 if (part() == goby::acomms::MessageHandler::HEAD)
64 throw(goby::acomms::DCCLNullValueException());
75 msg.set_a(a.to_ulong());
76 msg.set_b(b.to_ulong());
98 REPEAT_STORAGE_BITS = 4
102 MAX_REPEAT_SIZE = 1 << REPEAT_STORAGE_BITS
105 goby::int32 max() {
return DCCLFieldCodecBase::dccl_field_options().max(); }
106 goby::int32 min() {
return DCCLFieldCodecBase::dccl_field_options().min(); }
107 goby::int32 max_repeat() {
return DCCLFieldCodecBase::dccl_field_options().max_repeat(); }
109 Bitset encode_repeated(
const std::vector<goby::int32>& wire_values)
113 static_cast<int>(wire_values.size()) > max_repeat() ? max_repeat() : wire_values.size();
115 std::cout <<
"repeat size is " << repeat_size << std::endl;
117 for (
int i = 0, n = repeat_size; i < n; ++i)
121 value_bits.append(Bitset(singular_size(), static_cast<unsigned long>(wire_value)));
124 std::cout << value_bits << std::endl;
125 std::cout << Bitset(REPEAT_STORAGE_BITS, repeat_size) << std::endl;
126 Bitset out(REPEAT_STORAGE_BITS, repeat_size);
127 out.append(value_bits);
131 std::vector<goby::int32> decode_repeated(Bitset* bits)
133 int repeat_size = bits->to_ulong();
134 std::cout <<
"repeat size is " << repeat_size << std::endl;
136 bits->get_more_bits(repeat_size * singular_size());
138 Bitset value_bits = *bits;
139 value_bits >>= REPEAT_STORAGE_BITS;
141 std::vector<goby::int32> out;
142 for (
int i = 0; i < repeat_size; ++i)
144 goby::int32 value = value_bits.to_ulong() & ((1 << singular_size()) - 1);
146 out.push_back(value);
147 value_bits >>= singular_size();
152 unsigned size_repeated(
const std::vector<goby::int32>& field_values)
154 return REPEAT_STORAGE_BITS + field_values.size() * singular_size();
157 unsigned singular_size() {
return goby::util::ceil_log2((max() - min()) + 1); }
159 unsigned max_size_repeated() {
return REPEAT_STORAGE_BITS + max_repeat() * singular_size(); }
161 unsigned min_size_repeated() {
return REPEAT_STORAGE_BITS; }
165 DCCLFieldCodecBase::require(DCCLFieldCodecBase::dccl_field_options().has_min(),
166 "missing (dccl.field).min");
167 DCCLFieldCodecBase::require(DCCLFieldCodecBase::dccl_field_options().has_max(),
168 "missing (dccl.field).max");
169 DCCLFieldCodecBase::require(
170 DCCLFieldCodecBase::dccl_field_options().max_repeat() < MAX_REPEAT_SIZE,
171 "(dccl.field).max_repeat must be less than " +
172 goby::util::as<std::string>(static_cast<int>(MAX_REPEAT_SIZE)));
176 int main(
int argc,
char* argv[])
182 goby::acomms::DCCLFieldCodecManager::add<CustomCodec>(
"custom_codec");
183 goby::acomms::DCCLFieldCodecManager::add<Int32RepeatedCodec>(
"int32_test_codec");
186 cfg.set_crypto_passphrase(
"my_passphrase!");
194 codec->info(msg_in1.GetDescriptor(), &std::cout);
195 std::cout <<
"Message in:\n" << msg_in1.DebugString() << std::endl;
196 codec->validate(msg_in1.GetDescriptor());
197 std::cout <<
"Try encode..." << std::endl;
199 codec->encode(&bytes1, msg_in1);
200 std::cout <<
"... got bytes (hex): " << goby::util::hex_encode(bytes1) << std::endl;
201 std::cout <<
"Try decode..." << std::endl;
202 boost::shared_ptr<google::protobuf::Message> msg_out1 =
203 codec->decode<boost::shared_ptr<google::protobuf::Message> >(bytes1);
204 std::cout <<
"... got Message out:\n" << msg_out1->DebugString() << std::endl;
205 assert(msg_in1.SerializeAsString() == msg_out1->SerializeAsString());
209 msg_in2.mutable_msg()->CopyFrom(msg_in1);
213 codec->info(msg_in2.GetDescriptor(), &std::cout);
214 std::cout <<
"Message in:\n" << msg_in2.DebugString() << std::endl;
215 codec->validate(msg_in2.GetDescriptor());
216 std::cout <<
"Try encode..." << std::endl;
218 codec->encode(&bytes2, msg_in2);
219 std::cout <<
"... got bytes (hex): " << goby::util::hex_encode(bytes2) << std::endl;
220 std::cout <<
"Try decode..." << std::endl;
221 codec->decode(bytes2, &msg_out2);
222 std::cout <<
"... got Message out:\n" << msg_out2.DebugString() << std::endl;
223 assert(msg_in2.SerializeAsString() == msg_out2.SerializeAsString());
225 std::cout <<
"all tests passed" << std::endl;
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
static DCCLCodec * get()
DCCLCodec is a singleton class; use this to get a pointer to the class.
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...
google::protobuf::int32 int32
a signed 32 bit integer