Skip to content

Commit

Permalink
Implement rmw_get_gid_for_publisher properly. (#116)
Browse files Browse the repository at this point in the history
By creating and generating a publisher_guid.

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette authored Mar 1, 2024
1 parent 3667e9e commit 191a56b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
2 changes: 2 additions & 0 deletions rmw_zenoh_cpp/src/detail/rmw_data_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ struct rmw_publisher_data_t

// Context for memory allocation for messages.
rmw_context_t * context;

uint8_t pub_guid[RMW_GID_STORAGE_SIZE];
};

///==============================================================================
Expand Down
37 changes: 22 additions & 15 deletions rmw_zenoh_cpp/src/rmw_zenoh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,18 @@ rmw_fini_publisher_allocation(
return RMW_RET_UNSUPPORTED;
}

static void generate_random_guid(uint8_t guid[RMW_GID_STORAGE_SIZE])
{
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(
std::numeric_limits<unsigned char>::min(), std::numeric_limits<unsigned char>::max());

for (size_t i = 0; i < RMW_GID_STORAGE_SIZE; ++i) {
guid[i] = dist(rng);
}
}

//==============================================================================
/// Create a publisher and return a handle to that publisher.
rmw_publisher_t *
Expand Down Expand Up @@ -497,6 +509,8 @@ rmw_create_publisher(
publisher_data->~rmw_publisher_data_t(), rmw_publisher_data_t);
});

generate_random_guid(publisher_data->pub_guid);

// Adapt any 'best available' QoS options
publisher_data->adapted_qos_profile = *qos_profile;
rmw_ret_t ret = rmw_dds_common::qos_profile_get_best_available_for_topic_publisher(
Expand Down Expand Up @@ -1772,18 +1786,6 @@ rmw_return_loaned_message_from_subscription(
return RMW_RET_UNSUPPORTED;
}

static void generate_random_guid(uint8_t guid[RMW_GID_STORAGE_SIZE])
{
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(
std::numeric_limits<unsigned char>::min(), std::numeric_limits<unsigned char>::max());

for (size_t i = 0; i < RMW_GID_STORAGE_SIZE; ++i) {
guid[i] = dist(rng);
}
}

//==============================================================================
/// Create a service client that can send requests to and receive replies from a service server.
rmw_client_t *
Expand Down Expand Up @@ -3529,9 +3531,14 @@ rmw_count_services(
rmw_ret_t
rmw_get_gid_for_publisher(const rmw_publisher_t * publisher, rmw_gid_t * gid)
{
static_cast<void>(publisher);
static_cast<void>(gid);
// TODO(clalancette): Implement me
RMW_CHECK_ARGUMENT_FOR_NULL(publisher, RMW_RET_INVALID_ARGUMENT);
RMW_CHECK_ARGUMENT_FOR_NULL(gid, RMW_RET_INVALID_ARGUMENT);

rmw_publisher_data_t * pub_data = static_cast<rmw_publisher_data_t *>(publisher->data);

gid->implementation_identifier = rmw_zenoh_identifier;
memcpy(gid->data, pub_data->pub_guid, RMW_GID_STORAGE_SIZE);

return RMW_RET_OK;
}

Expand Down

0 comments on commit 191a56b

Please sign in to comment.