Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions example/ros-graph-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ rclnodejs
}
);

let clientNode = new rclnodejs.Node('client_node', 'ns2');
clientNode.createClient(
'example_interfaces/srv/AddTwoInts',
'add_two_ints'
);
let node = rclnodejs.createNode('ros_graph_display_node', ns);
let nodeNamesAndNameSpaces = node.getNodeNamesAndNamespaces();

Expand Down Expand Up @@ -118,6 +123,15 @@ rclnodejs
' '
)
);

console.log('CLIENTS BY NODE');
console.log(
JSON.stringify(
node.getClientNamesAndTypesByNode('client_node', '/ns2'),
undefined,
' '
)
);
})
.catch((e) => {
console.log(`Error: ${e}`);
Expand Down
16 changes: 15 additions & 1 deletion lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ class Node extends rclnodejs.ShadowNode {
}

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

/**
* Get service names and types for which a remote node has clients.
* @param {string} nodeName - The name of the node.
* @param {string} namespace - The name of the namespace.
* @return {Array<{name: string, types: Array<string>}>} - An array of the names and types.
*/
getClientNamesAndTypesByNode(nodeName, namespace) {
return rclnodejs.getClientNamesAndTypesByNode(
this.handle,
nodeName,
namespace
);
}

/**
* Get the list of topics discovered by the provided node.
* @param {boolean} noDemangle - If true topic names and types returned will not be demangled, default: false.
Expand Down
37 changes: 34 additions & 3 deletions src/rcl_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1738,15 +1738,45 @@ NAN_METHOD(GetServiceNamesAndTypesByNode) {
rcl_get_service_names_and_types_by_node(
node, &allocator, node_name.c_str(), node_namespace.c_str(),
&service_names_and_types),
"Failed to get_publisher_names_and_types.");
"Failed to get_service_names_and_types.");

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

THROW_ERROR_IF_NOT_EQUAL(
RCL_RET_OK, rcl_names_and_types_fini(&service_names_and_types),
"Failed to destroy rcl_get_zero_initialized_names_and_types");

info.GetReturnValue().Set(result_list);
}

NAN_METHOD(GetClientNamesAndTypesByNode) {
v8::Local<v8::Context> currentContent = Nan::GetCurrentContext();
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(
Nan::To<v8::Object>(info[0]).ToLocalChecked());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
std::string node_name =
*Nan::Utf8String(info[1]->ToString(currentContent).ToLocalChecked());
std::string node_namespace =
*Nan::Utf8String(info[2]->ToString(currentContent).ToLocalChecked());

rcl_names_and_types_t client_names_and_types =
rcl_get_zero_initialized_names_and_types();
rcl_allocator_t allocator = rcl_get_default_allocator();
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
rcl_names_and_types_fini(&service_names_and_types),
"Failed to destroy topic_names_and_types");
rcl_get_client_names_and_types_by_node(
node, &allocator, node_name.c_str(),
node_namespace.c_str(), &client_names_and_types),
"Failed to get_client_names_and_types.");

v8::Local<v8::Array> result_list =
Nan::New<v8::Array>(client_names_and_types.names.size);
ExtractNamesAndTypes(client_names_and_types, &result_list);

THROW_ERROR_IF_NOT_EQUAL(
RCL_RET_OK, rcl_names_and_types_fini(&client_names_and_types),
"Failed to destroy rcl_get_zero_initialized_names_and_types");

info.GetReturnValue().Set(result_list);
}
Expand Down Expand Up @@ -2033,6 +2063,7 @@ std::vector<BindingMethod> binding_methods = {
{"getPublisherNamesAndTypesByNode", GetPublisherNamesAndTypesByNode},
{"getSubscriptionNamesAndTypesByNode", GetSubscriptionNamesAndTypesByNode},
{"getServiceNamesAndTypesByNode", GetServiceNamesAndTypesByNode},
{"getClientNamesAndTypesByNode", GetClientNamesAndTypesByNode},
{"getTopicNamesAndTypes", GetTopicNamesAndTypes},
{"getServiceNamesAndTypes", GetServiceNamesAndTypes},
{"getNodeNames", GetNodeNames},
Expand Down
3 changes: 3 additions & 0 deletions test/types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ expectType<rclnodejs.NamesAndTypesQueryResult[]>(
expectType<rclnodejs.NamesAndTypesQueryResult[]>(
node.getServiceNamesAndTypesByNode(NODE_NAME)
);
expectType<rclnodejs.NamesAndTypesQueryResult[]>(
node.getClientNamesAndTypesByNode(NODE_NAME)
);
expectType<rclnodejs.NamesAndTypesQueryResult[]>(
node.getSubscriptionNamesAndTypesByNode(NODE_NAME)
);
Expand Down
16 changes: 16 additions & 0 deletions types/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,22 @@ declare module 'rclnodejs' {
namespace?: string
): Array<NamesAndTypesQueryResult>;

/**
* Get a remote node's client topics.
*
* @param remoteNodeName - Name of the remote node.
* @param namespace - Name of the remote namespace.
* @returns An array of the names and types.
* [
* { name: '/rosout', types: [ 'rcl_interfaces/msg/Log' ] },
* ...
* ]
*/
getClientNamesAndTypesByNode(
remoteNodeName: string,
namespace?: string
): Array<NamesAndTypesQueryResult>;

/**
* Get this node's topics and corresponding types.
*
Expand Down
Loading