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

Added clients by node implementation from rcl #383

Merged
merged 8 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
24 changes: 21 additions & 3 deletions rclpy/rclpy/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1448,18 +1448,36 @@ def get_service_names_and_types_by_node(
node_namespace: str
) -> List[Tuple[str, List[str]]]:
"""
Get a list of discovered service topics for a remote node.
Get a list of discovered service server topics for a remote node.

:param node_name: Name of a remote node to get services for.
:param node_namespace: Namespace of the remote node.
:return: List of tuples.
The first element of each tuple is the service name and the second element is a list of
service types.
The first element of each tuple is the service server name
and the second element is a list of service types.
"""
with self.handle as capsule:
return _rclpy.rclpy_get_service_names_and_types_by_node(
capsule, node_name, node_namespace)

def get_client_names_and_types_by_node(
self,
node_name: str,
node_namespace: str
) -> List[Tuple[str, List[str]]]:
"""
Get a list of discovered service client topics for a remote node.
skucheria marked this conversation as resolved.
Show resolved Hide resolved

:param node_name: Name of a remote node to get service clients for.
:param node_namespace: Namespace of the remote node.
:return: List of tuples.
The fist element of each tuple is the service client name
and the second element is a list of service client types.
"""
with self.handle as capsule:
return _rclpy.rclpy_get_client_names_and_types_by_node(
capsule, node_name, node_namespace)

def get_topic_names_and_types(self, no_demangle: bool = False) -> List[Tuple[str, List[str]]]:
"""
Get a list topic names and types for the node.
Expand Down
57 changes: 56 additions & 1 deletion rclpy/src/rclpy/_rclpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -3141,6 +3141,7 @@ rclpy_get_service_names_and_types_by_node(PyObject * Py_UNUSED(self), PyObject *
PyErr_Format(PyExc_RuntimeError,
"Failed to get_service_names_and_types: %s", rcl_get_error_string().str);
rcl_reset_error();
rclpy_names_and_types_fini(&service_names_and_types);
return NULL;
}

Expand All @@ -3153,6 +3154,55 @@ rclpy_get_service_names_and_types_by_node(PyObject * Py_UNUSED(self), PyObject *
return pyservice_names_and_types;
}

/// Get a list of service client names and types associated with the given node name.
/**
* Raises ValueError if pynode is not a node capsule
* Raises RuntimeError if there is an rcl error
*
* \param[in] pynode Capsule pointing to the node
* \param[in] node_name of a remote node to get publishers for
* \return Python list of tuples.
* The first element of each tuple is the service name (string) and the second element
* is a list of service types (list of strings).
*/
static PyObject *
rclpy_get_client_names_and_types_by_node(PyObject * Py_UNUSED(self), PyObject * args)
{
PyObject * pynode;
char * node_name;
char * node_namespace;

if (!PyArg_ParseTuple(args, "Oss", &pynode, &node_name, &node_namespace)) {
return NULL;
}

rcl_node_t * node = (rcl_node_t *)PyCapsule_GetPointer(pynode, "rcl_node_t");
if (!node) {
return NULL;
}

rcl_names_and_types_t client_names_and_types = rcl_get_zero_initialized_names_and_types();
rcl_allocator_t allocator = rcl_get_default_allocator();
rcl_ret_t ret =
rcl_get_client_names_and_types_by_node(node, &allocator, node_name, node_namespace,
&client_names_and_types);
if (ret != RCL_RET_OK) {
PyErr_Format(PyExc_RuntimeError,
"Failed to get_client_names_and_types: %s", rcl_get_error_string().str);
rcl_reset_error();
skucheria marked this conversation as resolved.
Show resolved Hide resolved
rclpy_names_and_types_fini(&client_names_and_types);
return NULL;
}

PyObject * pyclient_names_and_types = rclpy_convert_to_py_names_and_types(
&client_names_and_types);
if (!rclpy_names_and_types_fini(&client_names_and_types)) {
Py_XDECREF(pyclient_names_and_types);
return NULL;
}
return pyclient_names_and_types;
}

/// Get a list of topic names and types having at least one subscription from the given node name.
/**
* Raises ValueError if pynode is not a node capsule
Expand Down Expand Up @@ -4810,7 +4860,12 @@ static PyMethodDef rclpy_methods[] = {
{
"rclpy_get_service_names_and_types_by_node", rclpy_get_service_names_and_types_by_node,
METH_VARARGS,
"Get service list of specified node from graph API."
"Get service server list of specified node from graph API."
},
{
"rclpy_get_client_names_and_types_by_node", rclpy_get_client_names_and_types_by_node,
METH_VARARGS,
"Get a service client list of a specified node from graph API."
},
{
"rclpy_get_topic_names_and_types", rclpy_get_topic_names_and_types, METH_VARARGS,
Expand Down
6 changes: 6 additions & 0 deletions rclpy/test/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ def test_service_names_and_types(self):
# test that it doesn't raise
self.node.get_service_names_and_types()

def test_client_names_and_types_by_node(self):
# test that it doesnt raise
# node1 = rclpy.create_node(
skucheria marked this conversation as resolved.
Show resolved Hide resolved
# 'my_node', namespace='/my_ns', use_global_arguments=True, context=context)
self.node.get_client_names_and_types_by_node(TEST_NODE, TEST_NAMESPACE)
skucheria marked this conversation as resolved.
Show resolved Hide resolved

def test_topic_names_and_types(self):
# test that it doesn't raise
self.node.get_topic_names_and_types(no_demangle=True)
Expand Down