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 5 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
116 changes: 116 additions & 0 deletions rmw_zenoh_cpp/src/detail/rmw_data_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
#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)
Expand Down Expand Up @@ -70,3 +77,112 @@ void sub_data_handler(

z_drop(z_move(keystr));
}


unsigned int rmw_service_data_t::get_new_uid()
{
return client_count++;
}

void service_data_handler(const z_query_t * query, void * service_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));

clalancette marked this conversation as resolved.
Show resolved Hide resolved
auto rmw_service_data = static_cast<rmw_service_data_t *>(service_data);
if (rmw_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)
);
z_drop(z_move(keystr));
return;
francocipollone marked this conversation as resolved.
Show resolved Hide resolved
}

// Get the query parameters and payload
{
std::lock_guard<std::mutex> lock(rmw_service_data->query_queue_mutex);

const unsigned int client_id = rmw_service_data->get_new_uid();
rmw_service_data->id_query_map.emplace(
std::make_pair(client_id, std::make_unique<z_owned_query_t>(z_query_clone(query))));
rmw_service_data->to_take.push_back(client_id);


// Since we added new data, trigger the guard condition if it is available
std::lock_guard<std::mutex> internal_lock(rmw_service_data->internal_mutex);
if (rmw_service_data->condition != nullptr) {
rmw_service_data->condition->notify_one();
}
}

z_drop(z_move(keystr));
}

void client_data_handler(z_owned_reply_t * reply, void * client_data)
{
auto rmw_client_data = static_cast<rmw_client_data_t *>(client_data);
if (rmw_client_data == nullptr) {
RCUTILS_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Unable to obtain rmw_client_data_t "
);
return;
}
RCUTILS_LOG_INFO_NAMED(
"rmw_zenoh_cpp",
"[client_data_handler] triggered for %s",
rmw_client_data->service_name
);
if (!z_check(*reply)) {
RCUTILS_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"z_check returned False"
);
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;
}

z_sample_t sample = z_reply_ok(reply);

z_owned_str_t keystr = z_keyexpr_to_string(sample.keyexpr);

RCUTILS_LOG_DEBUG_NAMED(
"rmw_zenoh_cpp",
"[client_data_handler] keyexpr of sample: %s",
z_loan(keystr)
);

{
std::lock_guard<std::mutex> msg_lock(rmw_client_data->message_mutex);
rmw_client_data->message = std::make_unique<saved_msg_data>(
zc_sample_payload_rcinc(&sample), sample.timestamp.time, sample.timestamp.id.id);
Yadunund marked this conversation as resolved.
Show resolved Hide resolved
}
{
std::lock_guard<std::mutex> internal_lock(rmw_client_data->internal_mutex);
if (rmw_client_data->condition != nullptr) {
rmw_client_data->condition->notify_one();
}
}

z_reply_drop(reply);
z_drop(z_move(keystr));
}
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,6 +22,7 @@
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>

Expand All @@ -31,6 +32,7 @@

#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 +99,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 +129,66 @@ struct rmw_subscription_data_t
std::condition_variable * condition{nullptr};
};


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

// z_owned_closure_query_t
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
{
unsigned int get_new_uid();

const char * 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;

// Map to store the query id and the query.
// The query handler is saved as it is needed to answer the query later on.
std::unordered_map<unsigned int, std::unique_ptr<z_owned_query_t>> id_query_map;
Yadunund marked this conversation as resolved.
Show resolved Hide resolved
// The query id's of the queries that need to be processed.
std::deque<unsigned int> to_take;
Yadunund marked this conversation as resolved.
Show resolved Hide resolved
std::mutex query_queue_mutex;

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

unsigned int client_count{};
};

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

struct rmw_client_data_t
{
const char * service_name;
Yadunund marked this conversation as resolved.
Show resolved Hide resolved

z_owned_closure_reply_t zn_closure_reply;

std::mutex message_mutex;
std::unique_ptr<saved_msg_data> message;
Yadunund marked this conversation as resolved.
Show resolved Hide resolved

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};
};

#endif // DETAIL__RMW_DATA_TYPES_HPP_
Loading
Loading