Goby3 3.7.0
2026.11.12
Loading...
Searching...
No Matches
dccl_serializer_parser.h
Go to the documentation of this file.
1// Copyright 2019-2026:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Goby Underwater Autonomy Project Libraries
9// ("The Goby Libraries").
10//
11// The Goby Libraries are free software: you can redistribute them and/or modify
12// them under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// The Goby Libraries are distributed in the hope that they will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with Goby. If not, see <http://www.gnu.org/licenses/>.
23
24#ifndef GOBY_MIDDLEWARE_MARSHALLING_DETAIL_DCCL_SERIALIZER_PARSER_H
25#define GOBY_MIDDLEWARE_MARSHALLING_DETAIL_DCCL_SERIALIZER_PARSER_H
26
27#include <memory> // for unique_ptr
28#include <mutex> // for mutex, lock_guard
29#include <ostream> // for basic_ostream
30#include <set> // for set
31#include <string> // for string, operat...
32#include <unordered_map> // for unordered_map
33#include <utility> // for pair, make_pair
34#include <vector> // for vector
35
36#include <dccl/codec.h> // for Codec
37#include <dccl/dynamic_protobuf_manager.h> // for DynamicProtobu...
38
39#include "goby/middleware/protobuf/intervehicle.pb.h" // for DCCLForwardedData
40#include "goby/util/debug_logger/flex_ostream.h" // for operator<<
41
42namespace google
43{
44namespace protobuf
45{
46class Descriptor;
47} // namespace protobuf
48} // namespace google
49
50namespace goby
51{
52namespace middleware
53{
54namespace protobuf
55{
56class SerializerProtobufMetadata;
57} // namespace protobuf
58
59namespace detail
60{
63{
64 private:
65 static std::unique_ptr<dccl::Codec> codec_;
66 static std::vector<void*> loaded_libs_;
67
68 protected:
69 static std::mutex dccl_mutex_;
70
72 {
73 LoaderBase() = default;
74 virtual ~LoaderBase() = default;
75 };
76
77 template <typename DataType> struct Loader : public LoaderBase
78 {
79 Loader() { codec().load<DataType>(); }
80 ~Loader() override { codec().unload<DataType>(); }
81 };
82
83 struct LoaderDynamic : public LoaderBase
84 {
85 LoaderDynamic(const google::protobuf::Descriptor* desc) : desc_(desc)
86 {
87 codec().load(desc_);
88 }
89 ~LoaderDynamic() override { codec().unload(desc_); }
90
91 private:
92 const google::protobuf::Descriptor* desc_;
93 };
94
95 static std::unordered_map<const google::protobuf::Descriptor*, std::unique_ptr<LoaderBase>>
97
98 static std::set<std::string> loaded_proto_files_;
99
100 template <typename DataType> static void check_load()
101 {
102 const auto* desc = DataType::descriptor();
103 if (!loader_map_.count(desc))
104 loader_map_.insert(
105 std::make_pair(desc, std::unique_ptr<LoaderBase>(new Loader<DataType>())));
106 }
107
108 static void check_load(const google::protobuf::Descriptor* desc)
109 {
110 if (!loader_map_.count(desc))
111 loader_map_.insert(
112 std::make_pair(desc, std::unique_ptr<LoaderBase>(new LoaderDynamic(desc))));
113 }
114
115 static dccl::Codec& codec()
116 {
117 if (!codec_)
118 {
119 auto new_codec = std::make_unique<dccl::Codec>();
120 for (void* handle : loaded_libs_) new_codec->load_library(handle);
121 codec_ = std::move(new_codec);
122 }
123 return *codec_;
124 }
125
126 static dccl::Codec& set_codec(dccl::Codec* new_codec)
127 {
128 std::unique_ptr<dccl::Codec> codec(new_codec);
129 for (void* handle : loaded_libs_) codec->load_library(handle);
130 // Loader destructors unload from codec(), so must run before codec_ is replaced
131 loader_map_.clear();
132 codec_ = std::move(codec);
133 return *codec_;
134 }
135
136 public:
139
140 constexpr static int INVALID_DCCL_ID{0};
141
142 template <typename CharIterator> static unsigned id(CharIterator begin, CharIterator end)
143 {
144 std::lock_guard<std::mutex> lock(dccl_mutex_);
145 return codec().id(begin, end);
146 }
147
148 static unsigned id(const std::string& full_name)
149 {
150 std::lock_guard<std::mutex> lock(dccl_mutex_);
151 auto* desc = dccl::DynamicProtobufManager::find_descriptor(full_name);
152 if (desc)
153 {
154 return codec().id(desc);
155 }
156 else
157 {
158 goby::glog.is_warn() && goby::glog << "No DCCL message found with name: " << full_name
159 << std::endl;
160 return 0;
161 }
162 }
163
166 unpack(const std::string& bytes);
167
175 static void load_library(const std::string& library);
176
179 static void load_library(void* dl_handle);
180
182 static void setup_dlog();
183
184 static void
185 set_crypto_passphrase(const std::string& passphrase,
186 const std::set<dccl::int32>& do_not_encrypt_ids = std::set<dccl::int32>())
187 {
188 codec().set_crypto_passphrase(passphrase, do_not_encrypt_ids);
189 }
190};
191} // namespace detail
192
193} // namespace middleware
194} // namespace goby
195
196#endif
detail namespace with internal helper functions
Definition json.hpp:263
The global namespace for the Goby project.
util::FlexOstream glog
Access the Goby logger through this object.
Definition dccl.h:58
Wraps a dccl::Codec in a thread-safe way to make it usable by SerializerParserHelper.
static unsigned id(const std::string &full_name)
static void set_crypto_passphrase(const std::string &passphrase, const std::set< dccl::int32 > &do_not_encrypt_ids=std::set< dccl::int32 >())
static void load_library(void *dl_handle)
Load DCCL field codecs ("plugins") and compiled Protobuf messages from an already opened shared libra...
static void check_load(const google::protobuf::Descriptor *desc)
static dccl::Codec & set_codec(dccl::Codec *new_codec)
static void setup_dlog()
Enable dlog output to glog using same verbosity settings as glog.
static void load_library(const std::string &library)
Load DCCL field codecs ("plugins") and compiled Protobuf messages from one or more shared libraries.
static void load_metadata(const goby::middleware::protobuf::SerializerProtobufMetadata &meta)
static std::unordered_map< const google::protobuf::Descriptor *, std::unique_ptr< LoaderBase > > loader_map_
static goby::middleware::intervehicle::protobuf::DCCLForwardedData unpack(const std::string &bytes)
static unsigned id(CharIterator begin, CharIterator end)