Skip to content

Commit bc6d4e2

Browse files
authored
Support rcl_get_client_names_and_types_by_node (#1097)
This patch implements to expose `rcl_get_client_names_and_types_by_node` provided by `rcl` library, including: 1. Expose `rcl_get_client_names_and_types_by_node` by binding to JavaScript method `GetClientNamesAndTypesByNode`. 2. Add the type definition for `GetClientNamesAndTypesByNode` into `rclnodejs.d.ts`. 3. Add unit test for type into `test/types/index.test-d.ts`. 4. Add example into `example/ros-graph-example.js`. Fix: #1096
1 parent baba669 commit bc6d4e2

File tree

5 files changed

+82
-4
lines changed

5 files changed

+82
-4
lines changed

example/ros-graph-example.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ rclnodejs
4646
}
4747
);
4848

49+
let clientNode = new rclnodejs.Node('client_node', 'ns2');
50+
clientNode.createClient(
51+
'example_interfaces/srv/AddTwoInts',
52+
'add_two_ints'
53+
);
4954
let node = rclnodejs.createNode('ros_graph_display_node', ns);
5055
let nodeNamesAndNameSpaces = node.getNodeNamesAndNamespaces();
5156

@@ -118,6 +123,15 @@ rclnodejs
118123
' '
119124
)
120125
);
126+
127+
console.log('CLIENTS BY NODE');
128+
console.log(
129+
JSON.stringify(
130+
node.getClientNamesAndTypesByNode('client_node', '/ns2'),
131+
undefined,
132+
' '
133+
)
134+
);
121135
})
122136
.catch((e) => {
123137
console.log(`Error: ${e}`);

lib/node.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ class Node extends rclnodejs.ShadowNode {
969969
}
970970

971971
/**
972-
* Get the list of service topics discovered by the provided node for the remote node name.
972+
* Get service names and types for which a remote node has servers.
973973
* @param {string} nodeName - The name of the node.
974974
* @param {string} namespace - The name of the namespace.
975975
* @return {Array<{name: string, types: Array<string>}>} - An array of the names and types.
@@ -982,6 +982,20 @@ class Node extends rclnodejs.ShadowNode {
982982
);
983983
}
984984

985+
/**
986+
* Get service names and types for which a remote node has clients.
987+
* @param {string} nodeName - The name of the node.
988+
* @param {string} namespace - The name of the namespace.
989+
* @return {Array<{name: string, types: Array<string>}>} - An array of the names and types.
990+
*/
991+
getClientNamesAndTypesByNode(nodeName, namespace) {
992+
return rclnodejs.getClientNamesAndTypesByNode(
993+
this.handle,
994+
nodeName,
995+
namespace
996+
);
997+
}
998+
985999
/**
9861000
* Get the list of topics discovered by the provided node.
9871001
* @param {boolean} noDemangle - If true topic names and types returned will not be demangled, default: false.

src/rcl_bindings.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,15 +1738,45 @@ NAN_METHOD(GetServiceNamesAndTypesByNode) {
17381738
rcl_get_service_names_and_types_by_node(
17391739
node, &allocator, node_name.c_str(), node_namespace.c_str(),
17401740
&service_names_and_types),
1741-
"Failed to get_publisher_names_and_types.");
1741+
"Failed to get_service_names_and_types.");
17421742

17431743
v8::Local<v8::Array> result_list =
17441744
Nan::New<v8::Array>(service_names_and_types.names.size);
17451745
ExtractNamesAndTypes(service_names_and_types, &result_list);
17461746

1747+
THROW_ERROR_IF_NOT_EQUAL(
1748+
RCL_RET_OK, rcl_names_and_types_fini(&service_names_and_types),
1749+
"Failed to destroy rcl_get_zero_initialized_names_and_types");
1750+
1751+
info.GetReturnValue().Set(result_list);
1752+
}
1753+
1754+
NAN_METHOD(GetClientNamesAndTypesByNode) {
1755+
v8::Local<v8::Context> currentContent = Nan::GetCurrentContext();
1756+
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(
1757+
Nan::To<v8::Object>(info[0]).ToLocalChecked());
1758+
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
1759+
std::string node_name =
1760+
*Nan::Utf8String(info[1]->ToString(currentContent).ToLocalChecked());
1761+
std::string node_namespace =
1762+
*Nan::Utf8String(info[2]->ToString(currentContent).ToLocalChecked());
1763+
1764+
rcl_names_and_types_t client_names_and_types =
1765+
rcl_get_zero_initialized_names_and_types();
1766+
rcl_allocator_t allocator = rcl_get_default_allocator();
17471767
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
1748-
rcl_names_and_types_fini(&service_names_and_types),
1749-
"Failed to destroy topic_names_and_types");
1768+
rcl_get_client_names_and_types_by_node(
1769+
node, &allocator, node_name.c_str(),
1770+
node_namespace.c_str(), &client_names_and_types),
1771+
"Failed to get_client_names_and_types.");
1772+
1773+
v8::Local<v8::Array> result_list =
1774+
Nan::New<v8::Array>(client_names_and_types.names.size);
1775+
ExtractNamesAndTypes(client_names_and_types, &result_list);
1776+
1777+
THROW_ERROR_IF_NOT_EQUAL(
1778+
RCL_RET_OK, rcl_names_and_types_fini(&client_names_and_types),
1779+
"Failed to destroy rcl_get_zero_initialized_names_and_types");
17501780

17511781
info.GetReturnValue().Set(result_list);
17521782
}
@@ -2033,6 +2063,7 @@ std::vector<BindingMethod> binding_methods = {
20332063
{"getPublisherNamesAndTypesByNode", GetPublisherNamesAndTypesByNode},
20342064
{"getSubscriptionNamesAndTypesByNode", GetSubscriptionNamesAndTypesByNode},
20352065
{"getServiceNamesAndTypesByNode", GetServiceNamesAndTypesByNode},
2066+
{"getClientNamesAndTypesByNode", GetClientNamesAndTypesByNode},
20362067
{"getTopicNamesAndTypes", GetTopicNamesAndTypes},
20372068
{"getServiceNamesAndTypes", GetServiceNamesAndTypes},
20382069
{"getNodeNames", GetNodeNames},

test/types/index.test-d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ expectType<rclnodejs.NamesAndTypesQueryResult[]>(
5656
expectType<rclnodejs.NamesAndTypesQueryResult[]>(
5757
node.getServiceNamesAndTypesByNode(NODE_NAME)
5858
);
59+
expectType<rclnodejs.NamesAndTypesQueryResult[]>(
60+
node.getClientNamesAndTypesByNode(NODE_NAME)
61+
);
5962
expectType<rclnodejs.NamesAndTypesQueryResult[]>(
6063
node.getSubscriptionNamesAndTypesByNode(NODE_NAME)
6164
);

types/node.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,22 @@ declare module 'rclnodejs' {
685685
namespace?: string
686686
): Array<NamesAndTypesQueryResult>;
687687

688+
/**
689+
* Get a remote node's client topics.
690+
*
691+
* @param remoteNodeName - Name of the remote node.
692+
* @param namespace - Name of the remote namespace.
693+
* @returns An array of the names and types.
694+
* [
695+
* { name: '/rosout', types: [ 'rcl_interfaces/msg/Log' ] },
696+
* ...
697+
* ]
698+
*/
699+
getClientNamesAndTypesByNode(
700+
remoteNodeName: string,
701+
namespace?: string
702+
): Array<NamesAndTypesQueryResult>;
703+
688704
/**
689705
* Get this node's topics and corresponding types.
690706
*

0 commit comments

Comments
 (0)