Skip to content

Commit 0814156

Browse files
committed
Feedback
Signed-off-by: Alejandro Hernández Cordero <ahcorde@gmail.com>
1 parent bbb6d49 commit 0814156

7 files changed

+47
-17
lines changed

rmw_zenoh_cpp/src/detail/attachment_helpers.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ zenoh::Bytes AttachmentData::serialize_to_zbytes()
8787

8888
AttachmentData::AttachmentData(const zenoh::Bytes & bytes)
8989
{
90-
zenoh::ext::Deserializer deserializer(std::move(bytes));
90+
zenoh::ext::Deserializer deserializer(bytes);
9191
const auto sequence_number_str = deserializer.deserialize<std::string>();
9292
if (sequence_number_str != "sequence_number") {
9393
throw std::runtime_error("sequence_number is not found in the attachment.");

rmw_zenoh_cpp/src/detail/rmw_client_data.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ std::shared_ptr<ClientData> ClientData::make(
141141
response_type_support
142142
});
143143

144-
if (!client_data->init(session)) {
144+
if (!client_data->init()) {
145145
// init() already set the error.
146146
return nullptr;
147147
}
@@ -176,14 +176,13 @@ ClientData::ClientData(
176176
}
177177

178178
///=============================================================================
179-
bool ClientData::init(const std::shared_ptr<zenoh::Session> session)
179+
bool ClientData::init()
180180
{
181181
std::string topic_keyexpr = this->entity_->topic_info().value().topic_keyexpr_;
182182
keyexpr_ = zenoh::KeyExpr(topic_keyexpr);
183-
// TODO(ahcorde) check KeyExpr
184183
std::string liveliness_keyexpr = this->entity_->liveliness_keyexpr();
185184
zenoh::ZResult result;
186-
this->token_ = session->liveliness_declare_token(
185+
this->token_ = sess_->liveliness_declare_token(
187186
zenoh::KeyExpr(liveliness_keyexpr),
188187
zenoh::Session::LivelinessDeclarationOptions::create_default(),
189188
&result);
@@ -194,7 +193,6 @@ bool ClientData::init(const std::shared_ptr<zenoh::Session> session)
194193
return false;
195194
}
196195

197-
sess_ = session;
198196
initialized_ = true;
199197

200198
return true;

rmw_zenoh_cpp/src/detail/rmw_client_data.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class ClientData final : public std::enable_shared_from_this<ClientData>
115115
std::shared_ptr<ResponseTypeSupport> response_type_support);
116116

117117
// Initialize the Zenoh objects for this entity.
118-
bool init(const std::shared_ptr<zenoh::Session> session);
118+
bool init();
119119

120120
// Internal mutex.
121121
mutable std::recursive_mutex mutex_;

rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class rmw_context_impl_s::Data final
204204
zenoh::Session::LivelinessSubscriberOptions sub_options =
205205
zenoh::Session::LivelinessSubscriberOptions::create_default();
206206
sub_options.history = true;
207-
graph_subscriber_cpp_ = session_->liveliness_declare_subscriber(
207+
graph_subscriber_ = session_->liveliness_declare_subscriber(
208208
keyexpr_cpp,
209209
[&](const zenoh::Sample & sample) {
210210
// Look up the data shared_ptr in the global map. If it is in there, use it.
@@ -244,7 +244,7 @@ class rmw_context_impl_s::Data final
244244
}
245245

246246
zenoh::ZResult result;
247-
std::move(graph_subscriber_cpp_).value().undeclare(&result);
247+
std::move(graph_subscriber_).value().undeclare(&result);
248248
if (result != Z_OK) {
249249
RMW_ZENOH_LOG_ERROR_NAMED(
250250
"rmw_zenoh_cpp",
@@ -418,7 +418,16 @@ class rmw_context_impl_s::Data final
418418
// Graph cache.
419419
std::shared_ptr<rmw_zenoh_cpp::GraphCache> graph_cache_;
420420
// ROS graph liveliness subscriber.
421-
std::optional<zenoh::Subscriber<void>> graph_subscriber_cpp_;
421+
// The graph_subscriber *must* exist in order for anything in this Data class,
422+
// and hence rmw_zenoh_cpp, to work.
423+
// However, zenoh::Subscriber does not have an empty constructor,
424+
// so just declaring this as a zenoh::Subscriber fails to compile.
425+
// We work around that by wrapping it in a std::optional,
426+
// so the std::optional gets constructed at Data constructor time,
427+
// and then we initialize graph_subscriber_ later. Note that the zenoh-cpp API
428+
// liveliness_declare_subscriber() throws an exception if it fails,
429+
// so this should all be safe to do.
430+
std::optional<zenoh::Subscriber<void>> graph_subscriber_;
422431
// Equivalent to rmw_dds_common::Context's guard condition.
423432
// Guard condition that should be triggered when the graph changes.
424433
std::unique_ptr<rmw_guard_condition_t> graph_guard_condition_;

rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ rmw_ret_t PublisherData::publish(
226226
}
227227
});
228228

229+
// TODO(anyone): Move this zenoh_cpp API
229230
// Get memory from SHM buffer if available.
230231
// if (shm_provider.has_value()) {
231232
// RMW_ZENOH_LOG_DEBUG_NAMED("rmw_zenoh_cpp", "SHM is enabled.");
@@ -280,10 +281,10 @@ rmw_ret_t PublisherData::publish(
280281
entity_->copy_gid());
281282

282283
// TODO(ahcorde): shmbuf
283-
std::vector<uint8_t> raw_image(
284+
std::vector<uint8_t> raw_data(
284285
reinterpret_cast<const uint8_t *>(msg_bytes),
285286
reinterpret_cast<const uint8_t *>(msg_bytes) + data_length);
286-
zenoh::Bytes payload(raw_image);
287+
zenoh::Bytes payload(std::move(raw_data));
287288

288289
pub_.put(std::move(payload), std::move(options), &result);
289290
if (result != Z_OK) {
@@ -323,10 +324,10 @@ rmw_ret_t PublisherData::publish_serialized_message(
323324
auto options = zenoh::Publisher::PutOptions::create_default();
324325
options.attachment = create_map_and_set_sequence_num(sequence_number_++, entity_->copy_gid());
325326

326-
std::vector<uint8_t> raw_image(
327+
std::vector<uint8_t> raw_data(
327328
serialized_message->buffer,
328329
serialized_message->buffer + data_length);
329-
zenoh::Bytes payload(raw_image);
330+
zenoh::Bytes payload(std::move(raw_data));
330331

331332
pub_.put(std::move(payload), std::move(options), &result);
332333
if (result != Z_OK) {

rmw_zenoh_cpp/src/detail/rmw_service_data.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,10 @@ rmw_ret_t ServiceData::take_request(
326326
return RMW_RET_ERROR;
327327
}
328328

329+
auto writter_gid_v = attachment.copy_gid();
329330
memcpy(
330331
request_header->request_id.writer_guid,
331-
attachment.copy_gid().data(),
332+
writter_gid_v.data(),
332333
RMW_GID_STORAGE_SIZE);
333334

334335
request_header->source_timestamp = attachment.source_timestamp();
@@ -339,7 +340,7 @@ rmw_ret_t ServiceData::take_request(
339340
request_header->received_timestamp = query->get_received_timestamp();
340341

341342
// Add this query to the map, so that rmw_send_response can quickly look it up later.
342-
const size_t hash = rmw_zenoh_cpp::hash_gid_p(request_header->request_id.writer_guid);
343+
const size_t hash = rmw_zenoh_cpp::hash_gid(writter_gid_v);
343344
std::unordered_map<size_t, SequenceToQuery>::iterator it = sequence_to_query_map_.find(hash);
344345
if (it == sequence_to_query_map_.end()) {
345346
SequenceToQuery stq;
@@ -378,8 +379,13 @@ rmw_ret_t ServiceData::send_response(
378379
);
379380
return RMW_RET_OK;
380381
}
382+
383+
std::vector<uint8_t> writer_guid;
384+
writer_guid.resize(RMW_GID_STORAGE_SIZE);
385+
memcpy(writer_guid.data(), request_id->writer_guid, RMW_GID_STORAGE_SIZE);
386+
381387
// Create the queryable payload
382-
const size_t hash = hash_gid_p(request_id->writer_guid);
388+
const size_t hash = hash_gid(writer_guid);
383389
std::unordered_map<size_t, SequenceToQuery>::iterator it = sequence_to_query_map_.find(hash);
384390
if (it == sequence_to_query_map_.end()) {
385391
// If there is no data associated with this request, the higher layers of

rmw_zenoh_cpp/src/detail/rmw_service_data.hpp

+16
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,24 @@ class ServiceData final : public std::enable_shared_from_this<ServiceData>
117117
// The keyexpr string.
118118
std::string keyexpr_;
119119
// An owned queryable.
120+
// The Queryable *must* exist in order for anything in this ServiceData class,
121+
// and hence rmw_zenoh_cpp, to work.
122+
// However, zenoh::Queryable does not have an empty constructor,
123+
// so just declaring this as a zenoh::Queryable fails to compile.
124+
// We work around that by wrapping it in a std::optional, so the std::optional
125+
// gets constructed at ServiceData constructor time,
126+
// and then we initialize qable_ later. Note that the zenoh-cpp API declare_queryable() throws an
127+
// exception if it fails, so this should all be safe to do.
120128
std::optional<zenoh::Queryable<void>> qable_;
121129
// Liveliness token for the service.
130+
// The token_ *must* exist in order for anything in this ServiceData class,
131+
// and hence rmw_zenoh_cpp, to work.
132+
// However, zenoh::LivelinessToken does not have an empty constructor,
133+
// so just declaring this as a zenoh::LivelinessToken fails to compile.
134+
// We work around that by wrapping it in a std::optional, so the std::optional
135+
// gets constructed at ServiceData constructor time,
136+
// and then we initialize token_ later. Note that the zenoh-cpp API
137+
// liveliness_declare_token() throws an exception if it fails, so this should all be safe to do.
122138
std::optional<zenoh::LivelinessToken> token_;
123139
// Type support fields.
124140
const void * request_type_support_impl_;

0 commit comments

Comments
 (0)