Skip to content

Commit 299cb6b

Browse files
authored
Merge pull request #153 from minggangw/fix-issue-148
Add '/' when the namespace doesn't stat with it.
2 parents b23af44 + fa6f1a6 commit 299cb6b

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

lib/node.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class Node {
3535
this._services = [];
3636
this._timers = [];
3737
this._name = name;
38+
39+
if (namespace.length === 0) {
40+
namespace = '/';
41+
} else if (!namespace.startsWith('/')) {
42+
namespace = '/' + namespace;
43+
}
3844
this._namespace = namespace;
3945
this.spinning = false;
4046
}
@@ -244,14 +250,14 @@ class Node {
244250
* @return {string}
245251
*/
246252
name() {
247-
return this._name;
253+
return rclnodejs.getNodeName(this.handle);
248254
}
249255

250256
/* Get the namespace of the node.
251257
* @return {string}
252258
*/
253259
namespace() {
254-
return this._namespace;
260+
return rclnodejs.getNamespace(this.handle);
255261
}
256262
}
257263

src/rcl_bindings.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,29 @@ NAN_METHOD(ExpandTopicName) {
593593
info.GetReturnValue().Set(Nan::New<v8::String>(topic).ToLocalChecked());
594594
}
595595

596+
NAN_METHOD(GetNodeName) {
597+
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
598+
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
599+
const char * node_name = rcl_node_get_name(node);
600+
if (!node_name) {
601+
info.GetReturnValue().Set(Nan::Undefined());
602+
} else {
603+
info.GetReturnValue().Set(Nan::New<v8::String>(node_name).ToLocalChecked());
604+
}
605+
}
606+
607+
NAN_METHOD(GetNamespace) {
608+
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(info[0]->ToObject());
609+
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
610+
const char * node_namespace = rcl_node_get_namespace(node);
611+
if (!node_namespace) {
612+
info.GetReturnValue().Set(Nan::Undefined());
613+
} else {
614+
info.GetReturnValue().Set(
615+
Nan::New<v8::String>(node_namespace).ToLocalChecked());
616+
}
617+
}
618+
596619
const rmw_qos_profile_t* GetQoSProfileFromString(
597620
const std::string& profile) {
598621
const rmw_qos_profile_t* qos_profile = nullptr;
@@ -685,6 +708,8 @@ BindingMethod binding_methods[] = {
685708
{"validateTopicName", ValidateTopicName},
686709
{"validateNamespace", ValidateNamespace},
687710
{"expandTopicName", ExpandTopicName},
711+
{"getNodeName", GetNodeName},
712+
{"getNamespace", GetNamespace},
688713
{"", nullptr}};
689714

690715
} // namespace rclnodejs

0 commit comments

Comments
 (0)