Goby3 3.7.0
2026.11.12
Loading...
Searching...
No Matches
serialization_handlers.h
Go to the documentation of this file.
1// Copyright 2016-2026:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6// James D. Turner <james.turner@nrl.navy.mil>
7//
8//
9// This file is part of the Goby Underwater Autonomy Project Libraries
10// ("The Goby Libraries").
11//
12// The Goby Libraries are free software: you can redistribute them and/or modify
13// them under the terms of the GNU Lesser General Public License as published by
14// the Free Software Foundation, either version 2.1 of the License, or
15// (at your option) any later version.
16//
17// The Goby Libraries are distributed in the hope that they will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU Lesser General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public License
23// along with Goby. If not, see <http://www.gnu.org/licenses/>.
24
25#ifndef GOBY_MIDDLEWARE_TRANSPORT_SERIALIZATION_HANDLERS_H
26#define GOBY_MIDDLEWARE_TRANSPORT_SERIALIZATION_HANDLERS_H
27
28#include <chrono>
29#include <memory>
30#include <regex>
31#include <thread>
32#include <type_traits>
33#include <unordered_map>
34
35#include "goby/exception.h"
36#include "goby/util/binary.h"
37
42
43#include "interface.h"
44#include "null.h"
45
46namespace goby
47{
48namespace middleware
49{
51template <typename Metadata, typename Enable = void> class SerializationHandlerPostSelector
52{
53};
54
56template <typename Metadata>
58 typename std::enable_if_t<std::is_void<Metadata>::value>>
59{
60 public:
63
64 virtual std::string::const_iterator post(std::string::const_iterator b,
65 std::string::const_iterator e) const = 0;
66#ifndef _LIBCPP_VERSION
67 virtual std::vector<char>::const_iterator post(std::vector<char>::const_iterator b,
68 std::vector<char>::const_iterator e) const = 0;
69#endif
70 virtual const char* post(const char* b, const char* e) const = 0;
71};
72
74template <typename Metadata>
76 typename std::enable_if_t<!std::is_void<Metadata>::value>>
77{
78 public:
81
82 virtual std::string::const_iterator post(std::string::const_iterator b,
83 std::string::const_iterator e,
84 const Metadata& metadata) const = 0;
85#ifndef _LIBCPP_VERSION
86 virtual std::vector<char>::const_iterator post(std::vector<char>::const_iterator b,
87 std::vector<char>::const_iterator e,
88 const Metadata& metadata) const = 0;
89#endif
90 virtual const char* post(const char* b, const char* e, const Metadata& metadata) const = 0;
91};
92
96template <typename Metadata = void>
98{
99 public:
101 virtual ~SerializationHandlerBase() = default;
102
103 virtual const std::string& type_name() const = 0;
104 virtual const Group& subscribed_group() const = 0;
105
106 virtual int scheme() const = 0;
107
109 {
110 SUBSCRIBE,
113 };
114 virtual SubscriptionAction action() const = 0;
115
116 std::thread::id thread_id() const { return thread_id_; }
117 virtual std::string subscriber_id() const { return subscriber_id_; }
118
119 private:
120 const std::thread::id thread_id_{std::this_thread::get_id()};
121 const std::string subscriber_id_{goby::middleware::thread_id(thread_id_)};
122};
123
124template <typename Metadata>
127{
128 return s1.scheme() == s2.scheme() && s1.type_name() == s2.type_name() &&
129 s1.subscribed_group() == s2.subscribed_group() && s1.action() == s2.action();
130}
131
136template <typename Data, int scheme_id>
138{
139 public:
140 typedef std::function<void(std::shared_ptr<const Data> data)> HandlerType;
141
144 const Subscriber<Data>& subscriber = Subscriber<Data>())
145 : handler_(handler),
146 type_name_(SerializerParserHelper<Data, scheme_id>::type_name()),
147 group_(group),
148 subscriber_(subscriber)
149 {
150 }
151
152 // handle an incoming message
153 std::string::const_iterator post(std::string::const_iterator b,
154 std::string::const_iterator e) const override
155 {
156 return _post(b, e);
157 }
158
159#ifndef _LIBCPP_VERSION
160 std::vector<char>::const_iterator post(std::vector<char>::const_iterator b,
161 std::vector<char>::const_iterator e) const override
162 {
163 return _post(b, e);
164 }
165#endif
166
167 const char* post(const char* b, const char* e) const override { return _post(b, e); }
168
173
174 // getters
175 const std::string& type_name() const override { return type_name_; }
176 const Group& subscribed_group() const override { return group_; }
177 int scheme() const override { return scheme_id; }
178
179 private:
180 template <typename CharIterator>
181 CharIterator _post(CharIterator bytes_begin, CharIterator bytes_end) const
182 {
183 CharIterator actual_end;
184 auto msg = SerializerParserHelper<Data, scheme_id>::parse(bytes_begin, bytes_end,
185 actual_end, type_name_);
186
187 if ((!subscriber_.group_in_message() || subscribed_group() == subscriber_.group(*msg)) &&
188 handler_)
189 handler_(msg);
190
191 return actual_end;
192 }
193
194 private:
195 HandlerType handler_;
196 const std::string type_name_;
197 const Group group_;
198 Subscriber<Data> subscriber_;
199};
200
205template <typename Data, int scheme_id>
207 : public SerializationHandlerBase<intervehicle::protobuf::Header>
208{
209 public:
210 typedef std::function<void(std::shared_ptr<const Data> data)> HandlerType;
211
214 const Subscriber<Data>& subscriber = Subscriber<Data>())
215 : handler_(handler),
216 type_name_(SerializerParserHelper<Data, scheme_id>::type_name()),
217 group_(group),
218 subscriber_(subscriber)
219 {
220 }
221
224 const Group& group, const Subscriber<Data>& subscriber)
225 : handler_(handler),
226 type_name_(std::move(type_name)),
227 group_(group),
228 subscriber_(subscriber)
229 {
230 }
231
232 // handle an incoming message
233 std::string::const_iterator post(std::string::const_iterator b, std::string::const_iterator e,
234 const intervehicle::protobuf::Header& header) const override
235 {
236 return _post(b, e, header);
237 }
238
239#ifndef _LIBCPP_VERSION
240 std::vector<char>::const_iterator
241 post(std::vector<char>::const_iterator b, std::vector<char>::const_iterator e,
242 const intervehicle::protobuf::Header& header) const override
243 {
244 return _post(b, e, header);
245 }
246#endif
247
248 const char* post(const char* b, const char* e,
249 const intervehicle::protobuf::Header& header) const override
250 {
251 return _post(b, e, header);
252 }
253
255 action() const override
256 {
258 intervehicle::protobuf::Header>::SubscriptionAction::SUBSCRIBE;
259 }
260
261 // getters
262 const std::string& type_name() const override { return type_name_; }
263 const Group& subscribed_group() const override { return group_; }
264 int scheme() const override { return scheme_id; }
265
266 private:
267 template <typename CharIterator>
268 CharIterator _post(CharIterator bytes_begin, CharIterator bytes_end,
269 const intervehicle::protobuf::Header& header) const
270 {
271 CharIterator actual_end;
272 auto msg = SerializerParserHelper<Data, scheme_id>::parse(bytes_begin, bytes_end,
273 actual_end, type_name_);
274
275 subscriber_.set_link_data(*msg, header);
276
277 if ((!subscriber_.group_in_message() || subscribed_group() == subscriber_.group(*msg)) &&
278 handler_)
279 handler_(msg);
280
281 return actual_end;
282 }
283
284 private:
285 HandlerType handler_;
286 const std::string type_name_;
287 const Group group_;
288 Subscriber<Data> subscriber_;
289};
290
292template <typename Data, int scheme_id, typename Metadata>
294{
295 public:
296 typedef std::function<void(const Data& data, const Metadata& md)> HandlerType;
297
299 : handler_(handler), type_name_(SerializerParserHelper<Data, scheme_id>::type_name())
300 {
301 }
302
305 PublisherCallback(HandlerType handler, const Data& data)
306 : handler_(handler),
307 type_name_(SerializerParserHelper<Data, scheme_id>::type_name(data)),
308 original_(copy(data))
309 {
310 }
311
312 // handle an incoming message
313 std::string::const_iterator post(std::string::const_iterator b, std::string::const_iterator e,
314 const Metadata& md) const override
315 {
316 return _post(b, e, md);
317 }
318
319#ifndef _LIBCPP_VERSION
320 std::vector<char>::const_iterator post(std::vector<char>::const_iterator b,
321 std::vector<char>::const_iterator e,
322 const Metadata& md) const override
323 {
324 return _post(b, e, md);
325 }
326#endif
327
328 const char* post(const char* b, const char* e, const Metadata& md) const override
329 {
330 return _post(b, e, md);
331 }
332
337
338 // getters
339 const std::string& type_name() const override { return type_name_; }
340 const Group& subscribed_group() const override { return group_; }
341 int scheme() const override { return scheme_id; }
342
343 private:
344 template <typename CharIterator>
345 CharIterator _post(CharIterator bytes_begin, CharIterator bytes_end, const Metadata& md) const
346 {
347 CharIterator actual_end;
348 // decoded even when the original is held, to find where this message ends
349 auto msg = SerializerParserHelper<Data, scheme_id>::parse(bytes_begin, bytes_end,
350 actual_end, type_name_);
351
352 if (handler_)
353 handler_(original_ ? *original_ : *msg, md);
354 return actual_end;
355 }
356
357 static std::shared_ptr<const Data> copy(const Data& data)
358 {
359 if constexpr (std::is_same_v<Data, google::protobuf::Message>)
360 {
361 std::shared_ptr<google::protobuf::Message> c(data.New());
362 c->CopyFrom(data);
363 return c;
364 }
365 else
366 {
367 return std::make_shared<const Data>(data);
368 }
369 }
370
371 private:
372 HandlerType handler_;
373 const std::string type_name_;
374 Group group_{Group(Group::broadcast_group)};
375 std::shared_ptr<const Data> original_;
376};
377
382template <typename Data, int scheme_id>
384{
385 public:
387 : type_name_(SerializerParserHelper<Data, scheme_id>::type_name()), group_(group)
388 {
389 }
390
391 std::string::const_iterator post(std::string::const_iterator b,
392 std::string::const_iterator e) const override
393 {
394 throw(goby::Exception("Cannot call post on an UnSubscription"));
395 }
396
397#ifndef _LIBCPP_VERSION
398 std::vector<char>::const_iterator post(std::vector<char>::const_iterator b,
399 std::vector<char>::const_iterator e) const override
400 {
401 throw(goby::Exception("Cannot call post on an UnSubscription"));
402 }
403#endif
404
405 const char* post(const char* b, const char* e) const override
406 {
407 throw(goby::Exception("Cannot call post on an UnSubscription"));
408 }
409
414
415 // getters
416 const std::string& type_name() const override { return type_name_; }
417 const Group& subscribed_group() const override { return group_; }
418 int scheme() const override { return scheme_id; }
419
420 private:
421 const std::string type_name_;
422 const Group group_;
423};
424
427{
428 public:
429 typedef std::function<void(const std::vector<unsigned char>&, int scheme,
430 const std::string& type, const Group& group)>
432
433 SerializationSubscriptionRegex(HandlerType handler, const std::set<int>& schemes,
434 const std::string& type_regex = ".*",
435 const std::string& group_regex = ".*")
436 : handler_(handler), schemes_(schemes), type_regex_(type_regex), group_regex_(group_regex)
437 {
438 }
439
440 void update_type_regex(const std::string& type_regex) { type_regex_.assign(type_regex); }
441 void update_group_regex(const std::string& group_regex) { group_regex_.assign(group_regex); }
442
443 // handle an incoming message
444 // return true if posted
445 template <typename CharIterator>
446 bool post(CharIterator bytes_begin, CharIterator bytes_end, int scheme, const std::string& type,
447 const std::string& group) const
448 {
450 schemes_.count(scheme)) &&
451 std::regex_match(type, type_regex_) && std::regex_match(group, group_regex_))
452 {
453 std::vector<unsigned char> data(bytes_begin, bytes_end);
454 handler_(data, scheme, type, goby::middleware::DynamicGroup(group));
455 return true;
456 }
457 else
458 {
459 return false;
460 }
461 }
462
463 std::thread::id thread_id() const { return thread_id_; }
464 std::string subscriber_id() const { return subscriber_id_; }
465
466 private:
467 HandlerType handler_;
468 const std::set<int> schemes_;
469 std::regex type_regex_;
470 std::regex group_regex_;
471 const std::thread::id thread_id_{std::this_thread::get_id()};
472 const std::string subscriber_id_{goby::middleware::thread_id(thread_id_)};
473};
474
477{
478 public:
479 std::thread::id thread_id() const { return thread_id_; }
480 std::string subscriber_id() const { return subscriber_id_; }
481
482 private:
483 const std::thread::id thread_id_{std::this_thread::get_id()};
484 const std::string subscriber_id_{goby::middleware::thread_id(thread_id_)};
485};
486
489{
490 public:
491 typedef std::function<void(const protobuf::SerializerTransporterMessage& d)> HandlerType;
492
495 : handler_(handler), sub_cfg_(sub), group_(sub_cfg_.key().group()), subscriber_id_(sub.id())
496 {
497 }
498
499 // handle an incoming message
500 std::string::const_iterator post(std::string::const_iterator b,
501 std::string::const_iterator e) const override
502 {
503 return _post(b, e);
504 }
505
506#ifndef _LIBCPP_VERSION
507 std::vector<char>::const_iterator post(std::vector<char>::const_iterator b,
508 std::vector<char>::const_iterator e) const override
509 {
510 return _post(b, e);
511 }
512#endif
513
514 const char* post(const char* b, const char* e) const override { return _post(b, e); }
515
528
529 // getters
530 const std::string& type_name() const override { return sub_cfg_.key().type(); }
531 const Group& subscribed_group() const override { return group_; }
532 int scheme() const override { return sub_cfg_.key().marshalling_scheme(); }
533
534 std::string subscriber_id() const override { return subscriber_id_; }
535
536 private:
537 template <typename CharIterator>
538 CharIterator _post(CharIterator bytes_begin, CharIterator bytes_end) const
539 {
541 std::string* sbytes = new std::string(bytes_begin, bytes_end);
542 *msg.mutable_key() = sub_cfg_.key();
543 msg.set_allocated_data(sbytes);
544 handler_(msg);
545
546 CharIterator actual_end = bytes_end;
547 return actual_end;
548 }
549
550 private:
551 HandlerType handler_;
552 intermodule::protobuf::Subscription sub_cfg_;
553 DynamicGroup group_;
554 const std::thread::id thread_id_;
555 const std::string subscriber_id_;
556};
557
558} // namespace middleware
559} // namespace goby
560
561#endif
simple exception class for goby applications
Definition exception.h:35
Implementation of Group for dynamic (run-time) instantiations. Use Group directly for static (compile...
Definition group.h:120
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition group.h:60
static constexpr std::uint32_t broadcast_group
Special group number representing the broadcast group (used when no grouping is required for a given ...
Definition group.h:63
Represents a subscription to a serialized data type (intervehicle layer).
SerializationHandlerBase< intervehicle::protobuf::Header >::SubscriptionAction action() const override
const char * post(const char *b, const char *e, const intervehicle::protobuf::Header &header) const override
std::vector< char >::const_iterator post(std::vector< char >::const_iterator b, std::vector< char >::const_iterator e, const intervehicle::protobuf::Header &header) const override
std::string::const_iterator post(std::string::const_iterator b, std::string::const_iterator e, const intervehicle::protobuf::Header &header) const override
std::function< void(std::shared_ptr< const Data > data)> HandlerType
IntervehicleSerializationSubscription(HandlerType handler, const Group &group=Group(Group::broadcast_group), const Subscriber< Data > &subscriber=Subscriber< Data >())
IntervehicleSerializationSubscription(HandlerType handler, std::string type_name, const Group &group, const Subscriber< Data > &subscriber)
Subscription to a type named at run time (e.g. Data = google::protobuf::Message)
Represents a callback for a published data type (e.g. acked_func or expired_func)
std::function< void(const Data &data, const Metadata &md)> HandlerType
const char * post(const char *b, const char *e, const Metadata &md) const override
std::string::const_iterator post(std::string::const_iterator b, std::string::const_iterator e, const Metadata &md) const override
const std::string & type_name() const override
PublisherCallback(HandlerType handler, const Data &data)
Callback for a single publication: the handler is given data as published, not the message decoded fr...
SerializationHandlerBase< Metadata >::SubscriptionAction action() const override
const Group & subscribed_group() const override
std::vector< char >::const_iterator post(std::vector< char >::const_iterator b, std::vector< char >::const_iterator e, const Metadata &md) const override
Base class for handling posting callbacks for serialized data types (interprocess and outer)
virtual const std::string & type_name() const =0
virtual SubscriptionAction action() const =0
virtual const Group & subscribed_group() const =0
virtual std::vector< char >::const_iterator post(std::vector< char >::const_iterator b, std::vector< char >::const_iterator e) const =0
virtual std::string::const_iterator post(std::string::const_iterator b, std::string::const_iterator e) const =0
virtual std::vector< char >::const_iterator post(std::vector< char >::const_iterator b, std::vector< char >::const_iterator e, const Metadata &metadata) const =0
virtual std::string::const_iterator post(std::string::const_iterator b, std::string::const_iterator e, const Metadata &metadata) const =0
virtual const char * post(const char *b, const char *e, const Metadata &metadata) const =0
Selector class for enabling SerializationHandlerBase::post() override signature based on whether the ...
Represents a(n) (un)subscription from an InterModuleForwarder.
SerializationInterModuleSubscription(HandlerType handler, const intermodule::protobuf::Subscription sub)
std::string::const_iterator post(std::string::const_iterator b, std::string::const_iterator e) const override
const char * post(const char *b, const char *e) const override
SerializationHandlerBase ::SubscriptionAction action() const override
std::vector< char >::const_iterator post(std::vector< char >::const_iterator b, std::vector< char >::const_iterator e) const override
std::function< void(const protobuf::SerializerTransporterMessage &d)> HandlerType
Represents a regex subscription to a serialized data type (interprocess and outer layers).
SerializationSubscriptionRegex(HandlerType handler, const std::set< int > &schemes, const std::string &type_regex=".*", const std::string &group_regex=".*")
void update_type_regex(const std::string &type_regex)
void update_group_regex(const std::string &group_regex)
std::function< void(const std::vector< unsigned char > &, int scheme, const std::string &type, const Group &group)> HandlerType
bool post(CharIterator bytes_begin, CharIterator bytes_end, int scheme, const std::string &type, const std::string &group) const
Represents a subscription to a serialized data type (interprocess layer).
SerializationSubscription(HandlerType handler, const Group &group=Group(Group::broadcast_group), const Subscriber< Data > &subscriber=Subscriber< Data >())
const std::string & type_name() const override
SerializationHandlerBase ::SubscriptionAction action() const override
std::function< void(std::shared_ptr< const Data > data)> HandlerType
std::vector< char >::const_iterator post(std::vector< char >::const_iterator b, std::vector< char >::const_iterator e) const override
const char * post(const char *b, const char *e) const override
std::string::const_iterator post(std::string::const_iterator b, std::string::const_iterator e) const override
Represents an unsubscription to all subscribed data for a given thread.
Represents an unsubscription to a serialized data type (interprocess and outer layers).
std::vector< char >::const_iterator post(std::vector< char >::const_iterator b, std::vector< char >::const_iterator e) const override
std::string::const_iterator post(std::string::const_iterator b, std::string::const_iterator e) const override
const std::string & type_name() const override
const char * post(const char *b, const char *e) const override
SerializationHandlerBase ::SubscriptionAction action() const override
Class that holds additional metadata and callback functions related to a subscription (and is optiona...
Definition subscriber.h:37
::goby::middleware::intermodule::protobuf::Subscription_Action action() const
const ::goby::middleware::protobuf::SerializerTransporterKey & key() const
goby::util::logger::GroupSetter group(std::string n)
bool operator==(const Group &a, const Group &b)
Definition group.h:106
constexpr int scheme()
Placeholder to provide an interface for the scheme() function family.
Definition cstr.h:65
std::string thread_id(std::thread::id i=std::this_thread::get_id())
Definition common.h:53
The global namespace for the Goby project.
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::PROTOBUF_NAMESPACE_ID::MessageOptions, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg
STL namespace.
Class for parsing and serializing a given marshalling scheme. Must be specialized for a particular sc...
Definition interface.h:98
static std::shared_ptr< DataType > parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator &actual_end, const std::string &type=type_name())
Given a beginning and end iterator to bytes, parse the data and return it.
Definition interface.h:129