Skip to content

Commit

Permalink
Implement getNodeNames from rcl_get_node_names
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrichard authored and Minggang Wang committed Jan 26, 2020
1 parent b84455f commit d4579b2
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,14 @@ class Node {
getServiceNamesAndTypes() {
return rclnodejs.getServiceNamesAndTypes(this.handle);
}

/**
* Get the list of nodes discovered by the provided node.
* @return {array} - An array of the names and namespaces.
*/
getNodeNames() {
return rclnodejs.getNodeNames(this.handle);
}
}

module.exports = Node;
42 changes: 42 additions & 0 deletions src/rcl_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,47 @@ NAN_METHOD(GetServiceNamesAndTypes) {
info.GetReturnValue().Set(result_list);
}

NAN_METHOD(GetNodeNames) {
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
rcutils_string_array_t node_names =
rcutils_get_zero_initialized_string_array();
rcutils_string_array_t node_namespaces =
rcutils_get_zero_initialized_string_array();
rcl_allocator_t allocator = rcl_get_default_allocator();

THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
rcl_get_node_names(
node, allocator, &node_names, &node_namespaces),
"Failed to get_node_names.");

v8::Local<v8::Array> result_list = Nan::New<v8::Array>(node_names.size);

for (int i = 0; i < node_names.size; ++i) {
auto item = v8::Object::New(v8::Isolate::GetCurrent());

item->Set(Nan::New("name").ToLocalChecked(),
Nan::New(node_names.data[i]).ToLocalChecked());
item->Set(Nan::New("namespace").ToLocalChecked(),
Nan::New(node_namespaces.data[i]).ToLocalChecked());

Nan::Set(result_list, i, item);
}

rcutils_ret_t fini_names_ret = rcutils_string_array_fini(&node_names);
rcutils_ret_t fini_namespaces_ret =
rcutils_string_array_fini(&node_namespaces);

THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
fini_names_ret,
"Failed to destroy node_names");
THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK,
fini_namespaces_ret,
"Failed to destroy node_namespaces");

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

NAN_METHOD(ServiceServerIsAvailable) {
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
Expand Down Expand Up @@ -1300,6 +1341,7 @@ BindingMethod binding_methods[] = {
{"getServiceNamesAndTypesByNode", GetServiceNamesAndTypesByNode},
{"getTopicNamesAndTypes", GetTopicNamesAndTypes},
{"getServiceNamesAndTypes", GetServiceNamesAndTypes},
{"getNodeNames", GetNodeNames},
{"serviceServerIsAvailable", ServiceServerIsAvailable},
{"", nullptr}};

Expand Down
11 changes: 11 additions & 0 deletions test/test-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ describe('rcl node methods testing', function() {
});
});
});

it('node.getNodeNames', function() {
var nodeNames = node.getNodeNames();

var currentNode = nodeNames.find(function(nodeName) {
return nodeName.name === 'my_node';
});

assert.ok(currentNode);
assert.strictEqual(currentNode.namespace, '/my_ns');
});
});

describe('topic & serviceName getter/setter', function() {
Expand Down
2 changes: 2 additions & 0 deletions test/types/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ node.getSubscriptionNamesAndTypesByNode(NODE_NAME);
// $ExpectType NamesAndTypesQueryResult[]
node.getTopicNamesAndTypes();

// $ExpectType NodeNamesQueryResult[]
node.getNodeNames();


// ---- Publisher ----
Expand Down
31 changes: 30 additions & 1 deletion types/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ declare module 'rclnodejs' {
type NamesAndTypesQueryResult = {
name: string;
types: Array<string>;
}

/**
* Result of Node.getNodeNames() query
*
* @example
* ```
* [
* { name: 'gazebo', namespace: '/' },
* { name: 'robot_state_publisher', namespace: '/' },
* { name: 'cam2image', namespace: '/demo' }
* ]
* ```
*/
type NodeNamesQueryResult = {
name: string;
namespace: string;
}


Expand Down Expand Up @@ -308,7 +325,19 @@ declare module 'rclnodejs' {
* ]
*/
getServiceNamesAndTypes(): Array<NamesAndTypesQueryResult>


/**
* Get the list of nodes discovered by the provided node.
*
* @returns An array of the names and namespaces.
* [
* { name: 'gazebo', namespace: '/' },
* { name: 'robot_state_publisher', namespace: '/' },
* { name: 'cam2image', namespace: '/demo' }
* ]
*/
getNodeNames(): Array<NodeNamesQueryResult>;

}


Expand Down

0 comments on commit d4579b2

Please sign in to comment.