Goby v2
zeromq_packet.h
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 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them 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 // The Goby Libraries are distributed in the hope that they 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 Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef ZeroMQPacket20160413H
24 #define ZeroMQPacket20160413H
25 
26 #include "goby/common/core_constants.h"
27 #include <sstream>
28 
29 namespace goby
30 {
31 namespace common
32 {
33 std::string zeromq_packet_make_header(MarshallingScheme marshalling_scheme,
34  const std::string& identifier)
35 {
36  std::string zmq_filter;
37 
38  google::protobuf::uint32 marshalling_int =
39  static_cast<google::protobuf::uint32>(marshalling_scheme);
40 
41  for (int i = 0, n = BITS_IN_UINT32 / BITS_IN_BYTE; i < n; ++i)
42  { zmq_filter.push_back((marshalling_int >> (BITS_IN_BYTE * (n - i - 1))) & 0xFF); }
43  zmq_filter += identifier + '\0';
44 
45  return zmq_filter;
46 }
47 
49 void zeromq_packet_encode(std::string* raw, MarshallingScheme marshalling_scheme,
50  const std::string& identifier, const std::string& body)
51 {
52  *raw = zeromq_packet_make_header(marshalling_scheme, identifier);
53  *raw += body;
54 }
55 
57 void zeromq_packet_decode(const std::string& raw, MarshallingScheme* marshalling_scheme,
58  std::string* identifier, std::string* body)
59 {
60  // byte size of marshalling id
61  const unsigned MARSHALLING_SIZE = BITS_IN_UINT32 / BITS_IN_BYTE;
62 
63  if (raw.size() < MARSHALLING_SIZE)
64  throw(std::runtime_error("Message is too small"));
65 
66  google::protobuf::uint32 marshalling_int = 0;
67  for (int i = 0, n = MARSHALLING_SIZE; i < n; ++i)
68  {
69  marshalling_int <<= BITS_IN_BYTE;
70  marshalling_int ^= raw[i];
71  }
72 
73  if (marshalling_int >= MARSHALLING_UNKNOWN && marshalling_int <= MARSHALLING_MAX)
74  *marshalling_scheme = static_cast<MarshallingScheme>(marshalling_int);
75  else
76  {
77  std::stringstream ss;
78  ss << "Invalid marshalling value = " << marshalling_int;
79  throw(std::runtime_error(ss.str()));
80  }
81 
82  *identifier = raw.substr(MARSHALLING_SIZE, raw.find('\0', MARSHALLING_SIZE) - MARSHALLING_SIZE);
83 
84  // +1 for null terminator
85  const int HEADER_SIZE = MARSHALLING_SIZE + identifier->size() + 1;
86  *body = raw.substr(HEADER_SIZE);
87 }
88 } // namespace common
89 } // namespace goby
90 
91 #endif
void zeromq_packet_encode(std::string *raw, MarshallingScheme marshalling_scheme, const std::string &identifier, const std::string &body)
Encodes a packet for Goby over ZeroMQ.
Definition: zeromq_packet.h:49
The global namespace for the Goby project.
void zeromq_packet_decode(const std::string &raw, MarshallingScheme *marshalling_scheme, std::string *identifier, std::string *body)
Decodes a packet for Goby over ZeroMQ.
Definition: zeromq_packet.h:57