Skip to content

Commit

Permalink
Use rcl functions to grab the service name (#953)
Browse files Browse the repository at this point in the history
This patch implements:

1. Leverage rcl_client_get_service_name() to get the service name for
client.
2. Leverage rcl_service_get_service_name() to get the service name for
service.

The unit tests get updated accordingly.

Fix: #952
  • Loading branch information
minggangw committed Jan 10, 2024
1 parent e7d144f commit c1bf170
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class Client extends Entity {
* @type {string}
*/
get serviceName() {
return this._serviceName;
return rclnodejs.getClientServiceName(this._handle);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ class Service extends Entity {
* @type {string}
*/
get serviceName() {
const fullServiceName = rclnodejs.getServiceName(this._handle); // returns /my_node/myservice
return fullServiceName.split('/').pop();
return rclnodejs.getServiceServiceName(this._handle);
}

/**
Expand Down
33 changes: 22 additions & 11 deletions src/rcl_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1107,16 +1107,6 @@ NAN_METHOD(RclTakeRequest) {
info.GetReturnValue().Set(Nan::Undefined());
}

NAN_METHOD(GetServiceName) {
rcl_service_t* service = reinterpret_cast<rcl_service_t*>(
RclHandle::Unwrap<RclHandle>(
Nan::To<v8::Object>(info[0]).ToLocalChecked())
->ptr());

const char* name = rcl_service_get_service_name(service);
info.GetReturnValue().Set(Nan::New(name).ToLocalChecked());
}

NAN_METHOD(SendResponse) {
rcl_service_t* service = reinterpret_cast<rcl_service_t*>(
RclHandle::Unwrap<RclHandle>(
Expand Down Expand Up @@ -2017,6 +2007,26 @@ NAN_METHOD(RclTakeRaw) {
"Failed to deallocate message buffer");
}

NAN_METHOD(GetClientServiceName) {
rcl_client_t* client = reinterpret_cast<rcl_client_t*>(
RclHandle::Unwrap<RclHandle>(
Nan::To<v8::Object>(info[0]).ToLocalChecked())
->ptr());

const char* service_name = rcl_client_get_service_name(client);
info.GetReturnValue().Set(Nan::New(service_name).ToLocalChecked());
}

NAN_METHOD(GetServiceServiceName) {
rcl_service_t* service = reinterpret_cast<rcl_service_t*>(
RclHandle::Unwrap<RclHandle>(
Nan::To<v8::Object>(info[0]).ToLocalChecked())
->ptr());

const char* service_name = rcl_service_get_service_name(service);
info.GetReturnValue().Set(Nan::New(service_name).ToLocalChecked());
}

std::vector<BindingMethod> binding_methods = {
{"init", Init},
{"createNode", CreateNode},
Expand Down Expand Up @@ -2055,7 +2065,6 @@ std::vector<BindingMethod> binding_methods = {
{"rclTakeResponse", RclTakeResponse},
{"sendRequest", SendRequest},
{"createService", CreateService},
{"getServiceName", GetServiceName},
{"rclTakeRequest", RclTakeRequest},
{"sendResponse", SendResponse},
{"shutdown", Shutdown},
Expand Down Expand Up @@ -2088,6 +2097,8 @@ std::vector<BindingMethod> binding_methods = {
{"serviceServerIsAvailable", ServiceServerIsAvailable},
{"publishRawMessage", PublishRawMessage},
{"rclTakeRaw", RclTakeRaw},
{"getClientServiceName", GetClientServiceName},
{"getServiceServiceName", GetServiceServiceName},
{"", nullptr}
#if ROS_VERSION > 2205 // 2205 == Humble
,
Expand Down
4 changes: 2 additions & 2 deletions test/test-node-oo.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,14 @@ describe('topic & serviceName getter/setter', function () {
it('client: serviceName property getter', function () {
var node = new rclnodejs.Node('client', '/servicename_getter');
var client = node.createClient(AddTwoInts, 'add_two_ints');
assert.deepStrictEqual(client.serviceName, 'add_two_ints');
assert.deepStrictEqual(client.serviceName, '/servicename_getter/add_two_ints');
node.destroy();
});

it('service: topic property getter', function () {
var node = new rclnodejs.Node('service', '/servicename_getter');
var service = node.createService(AddTwoInts, 'add_two_ints', (req) => {});
assert.deepStrictEqual(service.serviceName, 'add_two_ints');
assert.deepStrictEqual(service.serviceName, '/servicename_getter/add_two_ints');
node.destroy();
});
});
4 changes: 2 additions & 2 deletions test/test-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,14 @@ describe('topic & serviceName getter/setter', function () {
it('client: serviceName property getter', function () {
var node = rclnodejs.createNode('client', '/servicename_getter');
var client = node.createClient(AddTwoInts, 'add_two_ints');
assert.deepStrictEqual(client.serviceName, 'add_two_ints');
assert.deepStrictEqual(client.serviceName, '/servicename_getter/add_two_ints');
node.destroy();
});

it('service: topic property getter', function () {
var node = rclnodejs.createNode('service', '/servicename_getter');
var service = node.createService(AddTwoInts, 'add_two_ints', (req) => {});
assert.deepStrictEqual(service.serviceName, 'add_two_ints');
assert.deepStrictEqual(service.serviceName, '/servicename_getter/add_two_ints');
node.destroy();
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/test-remapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('rcl node remapping', function () {
(request) => {}
);

assert.equal(service.serviceName, 'foo_service');
assert.equal(service.serviceName, '/foo_service');
});

it('remap node, namespace, topic and servicename', async function () {
Expand Down Expand Up @@ -95,6 +95,6 @@ describe('rcl node remapping', function () {
assert.equal(node.name(), 'foo_node');
assert.equal(node.namespace(), '/foo_ns');
assert.equal(publisher.topic, '/foo_ns/foo_topic');
assert.equal(service.serviceName, 'foo_service');
assert.equal(service.serviceName, '/foo_ns/foo_service');
});
});

0 comments on commit c1bf170

Please sign in to comment.