Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds base support for services. #86

Merged
merged 24 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
81b4f70
Adds base support for services.
francocipollone Dec 4, 2023
0a42aca
Addresses Yadu's comments.
francocipollone Dec 22, 2023
6ec1fb7
Addresses Yadu's comments.
francocipollone Dec 26, 2023
d24c3cd
Fixes memory leak.
francocipollone Dec 26, 2023
60b0c67
Removes unnecessary declaration.
francocipollone Dec 26, 2023
e181435
Cleanup services implementation (#88)
Yadunund Dec 27, 2023
336cd24
Merge branch 'rolling' into francocipollone/service_support_via_query…
Yadunund Dec 28, 2023
37ddf24
Merge branch 'rolling' into francocipollone/service_support_via_query…
Yadunund Jan 3, 2024
68a0be1
Use anynomous space instead of static functions.
francocipollone Jan 3, 2024
b0940f1
Fix style.
francocipollone Jan 3, 2024
a414685
Use zero_allocate where needed.
francocipollone Jan 3, 2024
177451a
Use a scope_exit to drop the keystr.
clalancette Jan 8, 2024
9d9f6f6
Rename find_type_support to find_message_type_support.
clalancette Jan 8, 2024
ce6af2b
Add error checking into ros_topic_name_to_zenoh_key
clalancette Jan 8, 2024
7773aee
Make sure to always free response_bytes.
clalancette Jan 8, 2024
8054227
Remove unnecessary TODO comment.
clalancette Jan 9, 2024
391cc39
Remember to free request_bytes.
clalancette Jan 9, 2024
066c5cd
Small change to take requests from the front of the deque.
clalancette Jan 10, 2024
59cdecc
Initial work for attachments and sequence numbers.
clalancette Jan 11, 2024
389c71d
Add in error checking for getting attachments.
clalancette Jan 11, 2024
212bf3b
More error checking for attachments.
clalancette Jan 11, 2024
735c048
Further cleanup.
clalancette Jan 11, 2024
37f44b5
Remove unnecessary (and incorrect) copy of sequence_number
clalancette Jan 11, 2024
da73fdc
Style
Yadunund Jan 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rmw_zenoh_cpp/src/detail/graph_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ void GraphCache::parse_put(const std::string & keyexpr)
} else {
RCUTILS_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Unable to add a new node /%s with id %s an existing namespace %s in the graph. Report this bug.",
"Unable to add a new node /%s with id %s an "
"existing namespace %s in the graph. Report this bug.",
entity.node_name().c_str(),
entity.id().c_str(),
entity.node_namespace().c_str());
Expand Down
89 changes: 88 additions & 1 deletion rmw_zenoh_cpp/src/detail/rmw_data_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,28 @@
#include <mutex>
#include <utility>

#include "rcpputils/scope_exit.hpp"
#include "rcutils/logging_macros.h"

#include "rmw_data_types.hpp"

///==============================================================================
saved_msg_data::saved_msg_data(zc_owned_payload_t p, uint64_t recv_ts, const uint8_t pub_gid[16])
: payload(p), recv_timestamp(recv_ts)
{
memcpy(publisher_gid, pub_gid, 16);
Yadunund marked this conversation as resolved.
Show resolved Hide resolved
}

//==============================================================================
void sub_data_handler(
const z_sample_t * sample,
void * data)
{
z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr);
auto drop_keystr = rcpputils::make_scope_exit(
[&keystr]() {
z_drop(z_move(keystr));
});

auto sub_data = static_cast<rmw_subscription_data_t *>(data);
if (sub_data == nullptr) {
Expand Down Expand Up @@ -67,6 +79,81 @@ void sub_data_handler(
sub_data->condition->notify_one();
}
}
}

//==============================================================================
void service_data_handler(const z_query_t * query, void * data)
{
RCUTILS_LOG_INFO_NAMED(
"rmw_zenoh_cpp",
"[service_data_handler] triggered"
);
z_owned_str_t keystr = z_keyexpr_to_string(z_query_keyexpr(query));
auto drop_keystr = rcpputils::make_scope_exit(
[&keystr]() {
z_drop(z_move(keystr));
});

clalancette marked this conversation as resolved.
Show resolved Hide resolved
rmw_service_data_t * service_data = static_cast<rmw_service_data_t *>(data);
if (service_data == nullptr) {
RCUTILS_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Unable to obtain rmw_service_data_t from data for "
"service for %s",
z_loan(keystr)
);
return;
francocipollone marked this conversation as resolved.
Show resolved Hide resolved
}

// Get the query parameters and payload
{
std::lock_guard<std::mutex> lock(service_data->query_queue_mutex);
service_data->query_queue.push_back(z_query_clone(query));
}
{
// Since we added new data, trigger the guard condition if it is available
std::lock_guard<std::mutex> internal_lock(service_data->internal_mutex);
if (service_data->condition != nullptr) {
service_data->condition->notify_one();
}
}
}

z_drop(z_move(keystr));
//==============================================================================
void client_data_handler(z_owned_reply_t * reply, void * data)
{
auto client_data = static_cast<rmw_client_data_t *>(data);
if (client_data == nullptr) {
RCUTILS_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Unable to obtain client_data_t "
);
return;
}
if (!z_reply_check(reply)) {
RCUTILS_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"z_reply_check returned False"
);
return;
}
if (!z_reply_is_ok(reply)) {
RCUTILS_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"z_reply_is_ok returned False"
);
return;
}
{
std::lock_guard<std::mutex> msg_lock(client_data->message_mutex);
// Take ownership of the reply.
client_data->replies.emplace_back(*reply);
*reply = z_reply_null();
}
{
std::lock_guard<std::mutex> internal_lock(client_data->internal_mutex);
if (client_data->condition != nullptr) {
client_data->condition->notify_one();
}
}
}
70 changes: 65 additions & 5 deletions rmw_zenoh_cpp/src/detail/rmw_data_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#include "rcutils/allocator.h"

#include "rmw/rmw.h"

#include "graph_cache.hpp"
#include "message_type_support.hpp"
#include "service_type_support.hpp"

/// Structs for various type erased data fields.

Expand Down Expand Up @@ -97,11 +100,7 @@ void sub_data_handler(const z_sample_t * sample, void * sub_data);

struct saved_msg_data
{
explicit saved_msg_data(zc_owned_payload_t p, uint64_t recv_ts, const uint8_t pub_gid[16])
: payload(p), recv_timestamp(recv_ts)
{
memcpy(publisher_gid, pub_gid, 16);
}
explicit saved_msg_data(zc_owned_payload_t p, uint64_t recv_ts, const uint8_t pub_gid[16]);
Yadunund marked this conversation as resolved.
Show resolved Hide resolved

zc_owned_payload_t payload;
uint64_t recv_timestamp;
Expand Down Expand Up @@ -131,4 +130,65 @@ struct rmw_subscription_data_t
std::condition_variable * condition{nullptr};
};


///==============================================================================

void service_data_handler(const z_query_t * query, void * service_data);

void client_data_handler(z_owned_reply_t * reply, void * client_data);

///==============================================================================

struct rmw_service_data_t
{
std::size_t get_new_uid();

z_owned_keyexpr_t keyexpr;
z_owned_queryable_t qable;

const void * request_type_support_impl;
const void * response_type_support_impl;
const char * typesupport_identifier;
RequestTypeSupport * request_type_support;
ResponseTypeSupport * response_type_support;

rmw_context_t * context;

// Deque to store the queries in the order they arrive.
std::deque<z_owned_query_t> query_queue;
std::mutex query_queue_mutex;

// Map to store the sequence_number -> query_id
std::map<int64_t, z_owned_query_t> sequence_to_query_map;
std::mutex sequence_to_query_map_mutex;

std::mutex internal_mutex;
std::condition_variable * condition{nullptr};
};

///==============================================================================

struct rmw_client_data_t
{
z_owned_keyexpr_t keyexpr;

z_owned_closure_reply_t zn_closure_reply;

std::mutex message_mutex;
std::deque<z_owned_reply_t> replies;

const void * request_type_support_impl;
const void * response_type_support_impl;
const char * typesupport_identifier;
RequestTypeSupport * request_type_support;
ResponseTypeSupport * response_type_support;

rmw_context_t * context;

std::mutex internal_mutex;
std::condition_variable * condition{nullptr};

size_t sequence_number{1};
};

#endif // DETAIL__RMW_DATA_TYPES_HPP_
Loading