forked from eclipse-uprotocol/up-transport-zenoh-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZenohUTransport.cpp
More file actions
287 lines (241 loc) · 8.98 KB
/
Copy pathZenohUTransport.cpp
File metadata and controls
287 lines (241 loc) · 8.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache License Version 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: Apache-2.0
#include "up-transport-zenoh-cpp/ZenohUTransport.h"
#include <spdlog/spdlog.h>
#include <up-cpp/datamodel/serializer/UUri.h>
#include <up-cpp/datamodel/serializer/Uuid.h>
#include <stdexcept>
namespace uprotocol::transport {
constexpr char UATTRIBUTE_VERSION = 1;
constexpr uint32_t WILDCARD_ENTITY_ID = 0x0000FFFF;
constexpr uint32_t WILDCARD_ENTITY_VERSION = 0x000000FF;
constexpr uint32_t WILDCARD_RESOURCE_ID = 0x0000FFFF;
v1::UStatus ZenohUTransport::uError(v1::UCode code, std::string_view message) {
v1::UStatus status;
status.set_code(code);
status.set_message(std::string(message));
return status;
}
std::string ZenohUTransport::toZenohKeyString(
const std::string& default_authority_name, const v1::UUri& source,
const std::optional<v1::UUri>& sink) {
std::ostringstream zenoh_key;
auto write_u_uri = [&](const v1::UUri& uuri) {
zenoh_key << "/";
// authority_name
if (uuri.authority_name().empty()) {
zenoh_key << default_authority_name;
} else {
zenoh_key << uuri.authority_name();
}
zenoh_key << "/";
// ue_id
if (uuri.ue_id() == WILDCARD_ENTITY_ID) {
zenoh_key << "*";
} else {
zenoh_key << std::uppercase << std::hex << uuri.ue_id();
}
zenoh_key << "/";
// ue_version_major
if (uuri.ue_version_major() == WILDCARD_ENTITY_VERSION) {
zenoh_key << "*";
} else {
zenoh_key << std::uppercase << std::hex << uuri.ue_version_major();
}
zenoh_key << "/";
// resource_id
if (uuri.resource_id() == WILDCARD_RESOURCE_ID) {
zenoh_key << "*";
} else {
zenoh_key << std::uppercase << std::hex << uuri.resource_id();
}
};
zenoh_key << "up";
write_u_uri(source);
if (sink.has_value()) {
write_u_uri(*sink);
} else {
zenoh_key << "/{}/{}/{}/{}";
}
return zenoh_key.str();
}
std::vector<std::pair<std::string, std::vector<uint8_t>>>
ZenohUTransport::uattributesToAttachment(const v1::UAttributes& attributes) {
std::vector<std::pair<std::string, std::vector<uint8_t>>> res;
std::vector<uint8_t> version = {UATTRIBUTE_VERSION};
std::vector<uint8_t> data(attributes.ByteSizeLong());
attributes.SerializeToArray(data.data(), static_cast<int>(data.size()));
res.emplace_back("", version);
res.emplace_back("", data);
return res;
}
v1::UAttributes ZenohUTransport::attachmentToUAttributes(
const zenoh::Bytes& attachment) {
auto attachment_vec = zenoh::ext::deserialize<
std::vector<std::pair<std::string, std::vector<uint8_t>>>>(attachment);
if (attachment_vec.size() != 2) {
spdlog::error("attachmentToUAttributes: attachment size != 2");
// TODO(unknown) error report, exception?
}
if (attachment_vec[0].second.size() == 1) {
if (attachment_vec[0].second[0] != UATTRIBUTE_VERSION) {
spdlog::error("attachmentToUAttributes: incorrect version");
// TODO(unknown) error report, exception?
}
};
v1::UAttributes res;
const std::vector<uint8_t> data = attachment_vec[1].second;
res.ParseFromArray(data.data(), static_cast<int>(data.size()));
return res;
}
zenoh::Priority ZenohUTransport::mapZenohPriority(v1::UPriority upriority) {
switch (upriority) {
case v1::UPriority::UPRIORITY_CS0:
return Z_PRIORITY_BACKGROUND;
case v1::UPriority::UPRIORITY_CS1:
return Z_PRIORITY_DATA_LOW;
case v1::UPriority::UPRIORITY_CS2:
return Z_PRIORITY_DATA;
case v1::UPriority::UPRIORITY_CS3:
return Z_PRIORITY_DATA_HIGH;
case v1::UPriority::UPRIORITY_CS4:
return Z_PRIORITY_INTERACTIVE_LOW;
case v1::UPriority::UPRIORITY_CS5:
return Z_PRIORITY_INTERACTIVE_HIGH;
case v1::UPriority::UPRIORITY_CS6:
return Z_PRIORITY_REAL_TIME;
// These sentinel values come from the protobuf compiler.
// They are illegal for the enum, but cause linting problems.
// In order to suppress the linting error, they need to
// be included in the switch-case statement.
// It is deemed acceptable to use an exception here because
// it is in the sending code. An exception would not be
// acceptable in receiving code. The correct strategy wopuld be
// to drop the message.
case v1::UPriority::UPriority_INT_MIN_SENTINEL_DO_NOT_USE_:
case v1::UPriority::UPriority_INT_MAX_SENTINEL_DO_NOT_USE_:
throw std::runtime_error(
"Sentinel values detected in priority switch-case");
case v1::UPriority::UPRIORITY_UNSPECIFIED:
default:
return Z_PRIORITY_DATA_LOW;
}
}
std::optional<v1::UMessage> ZenohUTransport::sampleToUMessage(
const zenoh::Sample& sample) {
v1::UMessage message;
const auto attachment = sample.get_attachment();
if (attachment.has_value()) {
*message.mutable_attributes() =
attachmentToUAttributes(attachment.value());
} else {
spdlog::error(
"sampleToUMessage: empty attachment, cannot read uAttributes");
return std::nullopt;
}
auto payload(zenoh::ext::deserialize<std::string>(sample.get_payload()));
message.set_payload(payload);
return message;
}
std::optional<v1::UMessage> ZenohUTransport::queryToUMessage(
const zenoh::Query& query) {
v1::UMessage message;
const auto attachment = query.get_attachment();
if (attachment.has_value()) {
*message.mutable_attributes() =
attachmentToUAttributes(attachment.value());
} else {
spdlog::error(
"queryToUMessage: empty attachment, cannot read uAttributes");
return std::nullopt;
}
if (query.get_payload().has_value()) {
auto payload(zenoh::ext::deserialize<std::string>(
query.get_payload().value().get()));
message.set_payload(payload);
}
return message;
}
ZenohUTransport::ZenohUTransport(const v1::UUri& default_uri,
const std::filesystem::path& config_file)
: UTransport(default_uri),
session_(zenoh::Session::open(
zenoh::Config::from_file(config_file.string()))) {
// TODO(unknown) add to setup or remove
spdlog::set_level(spdlog::level::debug);
spdlog::info("ZenohUTransport init");
}
v1::UStatus ZenohUTransport::registerPublishNotificationListener_(
const std::string& zenoh_key, CallableConn listener) {
spdlog::info("registerPublishNotificationListener_: {}", zenoh_key);
// NOTE: listener is captured by copy here so that it does not go out
// of scope when this function returns.
auto on_sample = [listener](const zenoh::Sample& sample) mutable {
auto maybe_message = sampleToUMessage(sample);
if (maybe_message.has_value()) {
listener(maybe_message.value());
} else {
spdlog::error("on_sample: failed to retrieve uMessage");
}
};
auto on_drop = []() {};
auto subscriber = session_.declare_subscriber(
zenoh_key, std::move(on_sample), std::move(on_drop));
subscriber_map_.emplace(listener, std::move(subscriber));
return {};
}
v1::UStatus ZenohUTransport::sendPublishNotification_(
const std::string& zenoh_key, const std::string& payload,
const v1::UAttributes& attributes) {
spdlog::debug("sendPublishNotification_: {}: {}", zenoh_key, payload);
auto attachment = uattributesToAttachment(attributes);
auto priority = mapZenohPriority(attributes.priority());
try {
// -Wpedantic disallows named member initialization until C++20,
// so PutOptions needs to be explicitly created and passed with
// std::move()
zenoh::Session::PutOptions options;
options.priority = priority;
options.encoding = zenoh::Encoding("app/custom");
options.attachment = zenoh::ext::serialize(attachment);
session_.put(zenoh::KeyExpr(zenoh_key), zenoh::ext::serialize(payload),
std::move(options));
} catch (const zenoh::ZException& e) {
return uError(v1::UCode::INTERNAL, e.what());
}
return {};
}
// NOTE: Messages have already been validated by the base class. It does not
// need to be re-checked here.
v1::UStatus ZenohUTransport::sendImpl(const v1::UMessage& message) {
const auto& payload = message.payload();
const auto& attributes = message.attributes();
std::string zenoh_key;
if (attributes.type() == v1::UMessageType::UMESSAGE_TYPE_PUBLISH) {
zenoh_key = toZenohKeyString(getEntityUri().authority_name(),
attributes.source(), {});
} else {
zenoh_key = toZenohKeyString(getEntityUri().authority_name(),
attributes.source(), attributes.sink());
}
return sendPublishNotification_(zenoh_key, payload, attributes);
}
v1::UStatus ZenohUTransport::registerListenerImpl(
CallableConn&& listener, const v1::UUri& source_filter,
std::optional<v1::UUri>&& sink_filter) {
std::string zenoh_key = toZenohKeyString(getEntityUri().authority_name(),
source_filter, sink_filter);
return registerPublishNotificationListener_(zenoh_key, std::move(listener));
}
void ZenohUTransport::cleanupListener(const CallableConn& listener) {
subscriber_map_.erase(listener);
}
} // namespace uprotocol::transport