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

Adding required structs and methods to get a list of publishers or subscribers with their respective qos #186

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
cce521a
Added two structs:
jaisontj Sep 27, 2019
8fe5fd2
Added two functions to retrieve the list of all subscribers and publi…
jaisontj Sep 27, 2019
69744dd
Allocator and free functions for rmw_participant_qos_profile
jaisontj Sep 27, 2019
20bedc2
Fixed code indentation issues
jaisontj Sep 28, 2019
94b27b3
Tests to rmw_participant_qos_profile_t_allocator
jaisontj Oct 4, 2019
ed9651e
Modified tests to avoid memory leak
jaisontj Oct 4, 2019
5c5ccbf
- Updated rmw_participants_t to use rmw_participant_qos_profile_t *
jaisontj Oct 8, 2019
06c5d4c
Changed rmw_participants_t to use a pointer to an array.
jaisontj Oct 8, 2019
a5e113b
PR rework after design changes
jaisontj Oct 15, 2019
1714008
- Changed member variables from const type const to const type in
jaisontj Oct 25, 2019
583687e
- Added zero_init, check_zero, init_with_size and fini functions for
jaisontj Oct 29, 2019
91a5668
- More informative comments
jaisontj Nov 13, 2019
091988c
- Fixed warnings on deallocate by casting const char * to char *
jaisontj Nov 14, 2019
ad84fb3
Fixed code formatting issues
jaisontj Nov 15, 2019
9b22c98
- Using strncpy instead of strcpy
jaisontj Nov 16, 2019
383e4a6
Using memcpy instead of strncpy to prevent warnings on Windows
jaisontj Nov 20, 2019
5e0e27f
- Comment modification to be more explicit.
jaisontj Nov 22, 2019
1d9e1fe
- Fixed function comments
jaisontj Nov 26, 2019
cbd91b3
Using memcpy in topic_info_set_gid
jaisontj Nov 27, 2019
e24e1f4
Added init and fini functions for rmw_topic_info_t and tests for the
jaisontj Nov 27, 2019
272a2dc
change c-strings under topic_info to be non-const
mm318 Dec 11, 2019
c00ad69
revert the change that removed const qualifiers
mm318 Dec 12, 2019
3e8f892
address PR comments
mm318 Dec 12, 2019
a1c35de
address PR comments
mm318 Dec 18, 2019
747c4c9
add rmw_qos_profile_unknown
mm318 Jan 2, 2020
06bb093
use constant instead of literal for history depth
mm318 Jan 2, 2020
c82859b
address more PR comments
mm318 Jan 10, 2020
702aef2
rename *topic_info* to *topic_endpoint_info*
mm318 Jan 10, 2020
8ddb815
fix formatting
mm318 Jan 10, 2020
6d9675a
remove unused enum value from rmw_endpoint_type_t
mm318 Jan 10, 2020
2915b73
fix clang compiler warnings
mm318 Jan 13, 2020
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
1 change: 1 addition & 0 deletions rmw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ set(rmw_sources
"src/validate_full_topic_name.c"
"src/validate_namespace.c"
"src/validate_node_name.c"
"src/topic_info_array.c"
jaisontj marked this conversation as resolved.
Show resolved Hide resolved
)
set_source_files_properties(${rmw_sources} PROPERTIES LANGUAGE "C")
add_library(${PROJECT_NAME} ${rmw_sources})
Expand Down
64 changes: 64 additions & 0 deletions rmw/include/rmw/rmw.h
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,70 @@ RMW_WARN_UNUSED
rmw_ret_t
rmw_set_log_severity(rmw_log_severity_t severity);

/// Retrieves the information for all publishers to a given topic.
/**
* The retrieved information will contain the publisher's node name, node namespace,
* associated topic type, gid and qos profile.
jaisontj marked this conversation as resolved.
Show resolved Hide resolved
*
* The node parameter must not be `NULL` and must point to a valid node.
*
* The topic_name parameter must not be `NULL` and must follow the topic naming rules
* mentioned at http://design.ros2.org/articles/topic_and_service_names.html
* Non existent topic names are allowed. They will return an empty array.
*
* \param[in] node the handle to the node being used to query the ROS graph.
* \param[in] allocator the allocator to be used when allocating space for the array.
* \param[in] topic_name the name of the topic for which the list of publishers will be retrieved.
* \param[in] no_mangle if true, the topic name will not be mangled.
* \param[out] publishers_info an array of rmw_topic_info_t.
* \return `RMW_RET_OK` if the query was successful, or
* \return `RMW_RET_INVALID_ARGUMENT` if the node is invalid, or
* \return `RMW_RET_INVALID_ARGUMENT` if any arguments are invalid, or
* \return `RMW_RET_BAD_ALLOC` if memory allocation fails, or
* \return `RMW_RET_ERROR` if an unspecified error occurs.
*/
RMW_PUBLIC
RMW_WARN_UNUSED
rmw_ret_t
rmw_get_publishers_info_by_topic(
const rmw_node_t * node,
rcutils_allocator_t * allocator,
const char * topic_name,
bool no_mangle,
rmw_topic_info_array_t * publishers_info);

/// Retrieves the information for all subscriptions to a given topic.
/**
* The retrieved information will contain the subscriptions's node name, node namespace,
* associated topic type, gid and qos profile.
jaisontj marked this conversation as resolved.
Show resolved Hide resolved
*
* The node parameter must not be `NULL` and must point to a valid node.
*
* The topic_name parameter must not be `NULL` and must follow the topic naming rules
* mentioned at http://design.ros2.org/articles/topic_and_service_names.html
* Non existent topic names are allowed. They will return an empty array.
*
* \param[in] node the handle to the node being used to query the ROS graph.
* \param[in] allocator the allocator to be used when allocating space for the array.
* \param[in] topic_name the name of the topic for which the list of subscriptions will be retrieved.
* \param[in] no_mangle if true, the topic name will not be mangled.
* \param[out] subscriptions_info an array of rmw_topic_info_t..
* \return `RMW_RET_OK` if the query was successful, or
* \return `RMW_RET_INVALID_ARGUMENT` if the node is invalid, or
* \return `RMW_RET_INVALID_ARGUMENT` if any arguments are invalid, or
* \return `RMW_RET_BAD_ALLOC` if memory allocation fails, or
* \return `RMW_RET_ERROR` if an unspecified error occurs.
*/
RMW_PUBLIC
RMW_WARN_UNUSED
rmw_ret_t
rmw_get_subscriptions_info_by_topic(
const rmw_node_t * node,
rcutils_allocator_t * allocator,
const char * topic_name,
bool no_mangle,
rmw_topic_info_array_t * subscriptions_info);
jaisontj marked this conversation as resolved.
Show resolved Hide resolved

#ifdef __cplusplus
}
#endif
Expand Down
85 changes: 85 additions & 0 deletions rmw/include/rmw/topic_info_array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RMW__TOPIC_INFO_ARRAY_H_
#define RMW__TOPIC_INFO_ARRAY_H_

#ifdef __cplusplus
extern "C"
{
#endif

#include "rcutils/allocator.h"
#include "rmw/types.h"
#include "rmw/visibility_control.h"
jaisontj marked this conversation as resolved.
Show resolved Hide resolved

/// Return a rmw_topic_info_array_t struct with members initialized to `NULL`.
RMW_PUBLIC
RMW_WARN_UNUSED
rmw_topic_info_array_t
rmw_get_zero_initialized_topic_info_array(void);

/// Check that a rmw_topic_info_array_t struct is zero initialized.
wjwwood marked this conversation as resolved.
Show resolved Hide resolved
RMW_PUBLIC
RMW_WARN_UNUSED
rmw_ret_t
rmw_topic_info_array_check_zero(rmw_topic_info_array_t * topic_info_array);

/// Initialize the info_array member inside rmw_topic_info_array_t with the given size
/**
* The rmw_topic_info_array_t has a member variable info_array which is an array of
* type rmw_topic_info_t. This function allocates memory to this array to hold n elements,
wjwwood marked this conversation as resolved.
Show resolved Hide resolved
* where n is the value of the size param to this function.
*
* \param[in] allocator the allocator to be used to allocate space
* \param[in] size the size of the array
* \param[out] topic_info_array the data structure to initialise
jaisontj marked this conversation as resolved.
Show resolved Hide resolved
* \returns `RMW_RET_OK` on successful init, or
* \returns `RMW_RET_INVALID_ARGUMENT` if any of the parameters are NULL, or
* \returns `RMW_BAD_ALLOC` if memory allocation fails, or
* \returns `RMW_RET_ERROR` when an unspecified error occurs.
*/
RMW_PUBLIC
RMW_WARN_UNUSED
rmw_ret_t
rmw_topic_info_array_init_with_size(
rcutils_allocator_t * allocator,
size_t size,
rmw_topic_info_array_t * topic_info_array);

/// Finalize a rmw_topic_info_array_t object.
/**
* The info_array member variable inside of rmw_topic_info_array represents an array of
* rmw_topic_info_t. When initializing this array, memory is allocated for it using the allocator.
* This function reclaims any allocated resources within the object and also sets the value of count
* to 0.
wjwwood marked this conversation as resolved.
Show resolved Hide resolved
*
* \param[in] allocator the allocator used to allocate memory to the object
* \param[out] topic_info_array object to be finalized
jaisontj marked this conversation as resolved.
Show resolved Hide resolved
* \returns `RMW_RET_OK` on successfully reclaiming memory, or
* \returns `RMW_RET_INVALID_ARGUMENT` if any parameters are NULL, or
* \returns `RMW_RET_ERROR` when an unspecified error occurs.
*/
RMW_PUBLIC
RMW_WARN_UNUSED
rmw_ret_t
rmw_topic_info_array_fini(
rcutils_allocator_t * allocator,
rmw_topic_info_array_t * topic_info_array);

#ifdef __cplusplus
}
#endif

#endif // RMW__TOPIC_INFO_ARRAY_H_
25 changes: 25 additions & 0 deletions rmw/include/rmw/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,31 @@ typedef struct RMW_PUBLIC_TYPE rmw_offered_deadline_missed_status_t
int32_t total_count_change;
} rmw_offered_deadline_missed_status_t;

/// A Structure that encapsulates the name, namespace, topic_type, gid and qos_profile
/// of publishers and subscriptions for a topic
typedef struct RMW_PUBLIC_TYPE rmw_topic_info_t
jaisontj marked this conversation as resolved.
Show resolved Hide resolved
{
/// Name of the node
const char * node_name;
/// Namespace of the node
const char * node_namespace;
/// The associated TopicType
const char * topic_type;
/// The GID of the node
const char * gid;
jaisontj marked this conversation as resolved.
Show resolved Hide resolved
/// Qos profile of the node
const rmw_qos_profile_t * qos_profile;
jaisontj marked this conversation as resolved.
Show resolved Hide resolved
jaisontj marked this conversation as resolved.
Show resolved Hide resolved
} rmw_topic_info_t;

/// Array of rmw_topic_info_t
typedef struct RMW_PUBLIC_TYPE rmw_topic_info_array_t
jaisontj marked this conversation as resolved.
Show resolved Hide resolved
{
/// Size of the array.
size_t count;
/// Pointer representing an array of rmw_topic_info_t
rmw_topic_info_t * info_array;
} rmw_topic_info_array_t;

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions rmw/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<test_depend>ament_cmake_gmock</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<test_depend>osrf_testing_tools_cpp</test_depend>

<export>
<build_type>ament_cmake</build_type>
Expand Down
89 changes: 89 additions & 0 deletions rmw/src/topic_info_array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "rmw/topic_info_array.h"

#include "rcutils/logging_macros.h"
#include "rmw/error_handling.h"
#include "rmw/convert_rcutils_ret_to_rmw_ret.h"
#include "rmw/types.h"

rmw_topic_info_array_t
rmw_get_zero_initialized_topic_info_array(void)
{
rmw_topic_info_array_t zero = {0};
hidmic marked this conversation as resolved.
Show resolved Hide resolved
return zero;
}

rmw_ret_t
rmw_topic_info_array_check_zero(rmw_topic_info_array_t * topic_info_array)
{
if (!topic_info_array) {
RMW_SET_ERROR_MSG("topic_info_array is null");
return RMW_RET_INVALID_ARGUMENT;
}
if (topic_info_array->count != 0 || topic_info_array->info_array != NULL) {
RMW_SET_ERROR_MSG("topic_info_array is not zeroed");
return RMW_RET_INVALID_ARGUMENT;
wjwwood marked this conversation as resolved.
Show resolved Hide resolved
}
return RMW_RET_OK;
}

rmw_ret_t
rmw_topic_info_array_init_with_size(
rcutils_allocator_t * allocator,
size_t size,
rmw_topic_info_array_t * topic_info_array)
jaisontj marked this conversation as resolved.
Show resolved Hide resolved
{
if (!allocator) {
RMW_SET_ERROR_MSG("allocator is null");
return RMW_RET_INVALID_ARGUMENT;
}
if (!topic_info_array) {
RMW_SET_ERROR_MSG("topic_info_array is null");
return RMW_RET_INVALID_ARGUMENT;
}
topic_info_array->info_array = allocator->allocate(sizeof(*topic_info_array->info_array) * size,
ivanpauno marked this conversation as resolved.
Show resolved Hide resolved
allocator->state);
if (!topic_info_array->info_array) {
RMW_SET_ERROR_MSG("failed to allocate memory for info_array");
return RMW_RET_BAD_ALLOC;
}
return RMW_RET_OK;
}

rmw_ret_t
rmw_topic_info_array_fini(
rcutils_allocator_t * allocator,
rmw_topic_info_array_t * topic_info_array)
jaisontj marked this conversation as resolved.
Show resolved Hide resolved
{
if (!allocator) {
RMW_SET_ERROR_MSG("allocator is null");
return RMW_RET_INVALID_ARGUMENT;
}

if (!topic_info_array) {
RMW_SET_ERROR_MSG("topic_info_array is null");
return RMW_RET_INVALID_ARGUMENT;
}

if (!topic_info_array->info_array) {
RMW_SET_ERROR_MSG("invalid topic_info_array");
return RMW_RET_INVALID_ARGUMENT;
}
allocator->deallocate(topic_info_array->info_array, allocator->state);
topic_info_array->info_array = NULL;
jaisontj marked this conversation as resolved.
Show resolved Hide resolved
topic_info_array->count = 0;
return RMW_RET_OK;
}
11 changes: 11 additions & 0 deletions rmw/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
find_package(ament_cmake_gmock REQUIRED)
find_package(osrf_testing_tools_cpp REQUIRED)

ament_add_gmock(test_serialized_message
test_serialized_message.cpp
Expand Down Expand Up @@ -41,3 +42,13 @@ if(TARGET test_validate_namespace)
set_target_properties(test_validate_namespace PROPERTIES COMPILE_FLAGS "-std=c++14")
endif()
endif()

ament_add_gmock(test_topic_info_array
test_topic_info_array.cpp
# Append the directory of librmw so it is found at test time.
APPEND_LIBRARY_DIRS "$<TARGET_FILE_DIR:${PROJECT_NAME}>"
)
if(TARGET test_topic_info_array)
target_link_libraries(test_topic_info_array ${PROJECT_NAME}
osrf_testing_tools_cpp::memory_tools)
endif()
63 changes: 63 additions & 0 deletions rmw/test/test_topic_info_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "gmock/gmock.h"
#include "osrf_testing_tools_cpp/scope_exit.hpp"
#include "rcutils/allocator.h"

#include "rmw/topic_info_array.h"
#include "rmw/types.h"

TEST(test_topic_info_array, zero_initialize) {
rmw_topic_info_array_t arr = rmw_get_zero_initialized_topic_info_array();
EXPECT_EQ(arr.count, 0u);
EXPECT_FALSE(arr.info_array);
}

TEST(test_topic_info_array, check_zero) {
rmw_topic_info_array_t arr = rmw_get_zero_initialized_topic_info_array();
EXPECT_EQ(rmw_topic_info_array_check_zero(&arr), RMW_RET_OK);
rmw_topic_info_array_t arr_count_not_zero = {1, nullptr};
EXPECT_EQ(rmw_topic_info_array_check_zero(&arr_count_not_zero), RMW_RET_INVALID_ARGUMENT);
rmw_topic_info_t topic_info;
rmw_topic_info_array_t arr_info_array_not_null = {1, &topic_info};
jaisontj marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_EQ(rmw_topic_info_array_check_zero(&arr_info_array_not_null), RMW_RET_INVALID_ARGUMENT);
EXPECT_EQ(rmw_topic_info_array_check_zero(nullptr), RMW_RET_INVALID_ARGUMENT);
}

TEST(test_topic_info_array, check_init_with_size) {
rcutils_allocator_t allocator = rcutils_get_default_allocator();
rmw_topic_info_array_t arr = rmw_get_zero_initialized_topic_info_array();
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
rmw_ret_t fini_ret = rmw_topic_info_array_fini(&allocator, &arr);
EXPECT_EQ(fini_ret, RMW_RET_OK);
});
EXPECT_EQ(rmw_topic_info_array_init_with_size(nullptr, 1, &arr), RMW_RET_INVALID_ARGUMENT);
EXPECT_EQ(rmw_topic_info_array_init_with_size(&allocator, 1, nullptr), RMW_RET_INVALID_ARGUMENT);
EXPECT_FALSE(arr.info_array);
rmw_ret_t ret = rmw_topic_info_array_init_with_size(&allocator, 5, &arr);
EXPECT_EQ(ret, RMW_RET_OK);
EXPECT_TRUE(arr.info_array);
}

TEST(test_topic_info_array, check_fini) {
rcutils_allocator_t allocator = rcutils_get_default_allocator();
rmw_topic_info_array_t arr = rmw_get_zero_initialized_topic_info_array();
rmw_ret_t ret = rmw_topic_info_array_init_with_size(&allocator, 5, &arr);
EXPECT_EQ(ret, RMW_RET_OK);
EXPECT_TRUE(arr.info_array);
ret = rmw_topic_info_array_fini(&allocator, &arr);
EXPECT_EQ(ret, RMW_RET_OK);
EXPECT_FALSE(arr.info_array);
}