Skip to content

Commit 2d70163

Browse files
authored
Add missing methods for client (#1114)
This PR adds the missing client methods by implementing and exposing a getOptions function for services. The changes include updating TypeScript tests to verify the new method, adding a new service options test in JavaScript, implementing the ConvertToQoS function in C++ along with the GetOptions binding, and adding a getOptions method in the Service JavaScript class. Fix: #1115
1 parent 5dc2276 commit 2d70163

File tree

7 files changed

+77
-20
lines changed

7 files changed

+77
-20
lines changed

lib/service.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ class Service extends Entity {
142142
true
143143
);
144144
}
145+
146+
/**
147+
* Get the options of this service.
148+
* @return {object} The options of this service.
149+
*/
150+
getOptions() {
151+
return rclnodejs.getOptions(this._handle);
152+
}
145153
}
146154

147155
module.exports = Service;

src/rcl_service_bindings.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Napi::Value CreateService(const Napi::CallbackInfo& info) {
5151
if (qos_profile) {
5252
service_ops.qos = *qos_profile;
5353
}
54+
5455
THROW_ERROR_IF_NOT_EQUAL(
5556
rcl_service_init(service, node, ts, service_name.c_str(), &service_ops),
5657
RCL_RET_OK, rcl_get_error_string().str);
@@ -173,6 +174,18 @@ Napi::Value ConfigureServiceIntrospection(const Napi::CallbackInfo& info) {
173174
}
174175
#endif
175176

177+
Napi::Value GetOptions(const Napi::CallbackInfo& info) {
178+
Napi::Env env = info.Env();
179+
180+
rcl_service_t* service = reinterpret_cast<rcl_service_t*>(
181+
RclHandle::Unwrap(info[0].As<Napi::Object>())->ptr());
182+
183+
const rcl_service_options_t* options = rcl_service_get_options(service);
184+
auto qos_profile = ConvertToQoS(env, &options->qos);
185+
186+
return qos_profile;
187+
}
188+
176189
Napi::Object InitServiceBindings(Napi::Env env, Napi::Object exports) {
177190
exports.Set("createService", Napi::Function::New(env, CreateService));
178191
exports.Set("rclTakeRequest", Napi::Function::New(env, RclTakeRequest));
@@ -183,6 +196,7 @@ Napi::Object InitServiceBindings(Napi::Env env, Napi::Object exports) {
183196
exports.Set("configureServiceIntrospection",
184197
Napi::Function::New(env, ConfigureServiceIntrospection));
185198
#endif
199+
exports.Set("getOptions", Napi::Function::New(env, GetOptions));
186200
return exports;
187201
}
188202

src/rcl_utilities.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,6 @@ Napi::Value ConvertToHashObject(Napi::Env env,
8888
return obj;
8989
}
9090

91-
Napi::Value ConvertToQoS(Napi::Env env, const rmw_qos_profile_t* qos_profile) {
92-
Napi::Object qos = Napi::Object::New(env);
93-
qos.Set("depth", Napi::Number::New(env, qos_profile->depth));
94-
qos.Set("history", Napi::Number::New(env, qos_profile->history));
95-
qos.Set("reliability", Napi::Number::New(env, qos_profile->reliability));
96-
qos.Set("durability", Napi::Number::New(env, qos_profile->durability));
97-
qos.Set("lifespan", ConvertRMWTimeToDuration(env, &qos_profile->lifespan));
98-
qos.Set("deadline", ConvertRMWTimeToDuration(env, &qos_profile->deadline));
99-
qos.Set("liveliness", Napi::Number::New(env, qos_profile->liveliness));
100-
qos.Set(
101-
"liveliness_lease_duration",
102-
ConvertRMWTimeToDuration(env, &qos_profile->liveliness_lease_duration));
103-
qos.Set(
104-
"avoid_ros_namespace_conventions",
105-
Napi::Boolean::New(env, qos_profile->avoid_ros_namespace_conventions));
106-
return qos;
107-
}
108-
10991
Napi::Value ConvertToJSTopicEndpoint(
11092
Napi::Env env, const rmw_topic_endpoint_info_t* topic_endpoint_info) {
11193
Napi::Array endpoint_gid = Napi::Array::New(env, RMW_GID_STORAGE_SIZE);
@@ -128,7 +110,7 @@ Napi::Value ConvertToJSTopicEndpoint(
128110
env, static_cast<int>(topic_endpoint_info->endpoint_type)));
129111
endpoint.Set("endpoint_gid", endpoint_gid);
130112
endpoint.Set("qos_profile",
131-
ConvertToQoS(env, &topic_endpoint_info->qos_profile));
113+
rclnodejs::ConvertToQoS(env, &topic_endpoint_info->qos_profile));
132114
return endpoint;
133115
}
134116

@@ -246,6 +228,24 @@ void ExtractNamesAndTypes(rcl_names_and_types_t names_and_types,
246228
}
247229
}
248230

231+
Napi::Value ConvertToQoS(Napi::Env env, const rmw_qos_profile_t* qos_profile) {
232+
Napi::Object qos = Napi::Object::New(env);
233+
qos.Set("depth", Napi::Number::New(env, qos_profile->depth));
234+
qos.Set("history", Napi::Number::New(env, qos_profile->history));
235+
qos.Set("reliability", Napi::Number::New(env, qos_profile->reliability));
236+
qos.Set("durability", Napi::Number::New(env, qos_profile->durability));
237+
qos.Set("lifespan", ConvertRMWTimeToDuration(env, &qos_profile->lifespan));
238+
qos.Set("deadline", ConvertRMWTimeToDuration(env, &qos_profile->deadline));
239+
qos.Set("liveliness", Napi::Number::New(env, qos_profile->liveliness));
240+
qos.Set(
241+
"liveliness_lease_duration",
242+
ConvertRMWTimeToDuration(env, &qos_profile->liveliness_lease_duration));
243+
qos.Set(
244+
"avoid_ros_namespace_conventions",
245+
Napi::Boolean::New(env, qos_profile->avoid_ros_namespace_conventions));
246+
return qos;
247+
}
248+
249249
Napi::Array ConvertToJSTopicEndpointInfoList(
250250
Napi::Env env, const rmw_topic_endpoint_info_array_t* info_array) {
251251
Napi::Array list = Napi::Array::New(env, info_array->size);

src/rcl_utilities.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ void ExtractNamesAndTypes(rcl_names_and_types_t names_and_types,
5151
Napi::Array ConvertToJSTopicEndpointInfoList(
5252
Napi::Env env, const rmw_topic_endpoint_info_array_t* info_array);
5353

54+
Napi::Value ConvertToQoS(Napi::Env env, const rmw_qos_profile_t* qos_profile);
55+
5456
} // namespace rclnodejs
5557

5658
#endif // SRC_RCL_UTILITIES_H_

test/test-service-with-async-callback.js renamed to test/test-service.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
const assert = require('assert');
1818
const rclnodejs = require('../index.js');
19+
const { QoS } = rclnodejs;
1920

20-
describe('Test creating a service with an async callback', function () {
21+
describe('Test service class', function () {
2122
this.timeout(60 * 1000);
2223

2324
before(function () {
@@ -67,4 +68,29 @@ describe('Test creating a service with an async callback', function () {
6768
rclnodejs.spin(serviceNode);
6869
rclnodejs.spin(clientNode);
6970
});
71+
72+
it('Get service options', function () {
73+
const node = rclnodejs.createNode('test_node');
74+
const service = node.createService(
75+
'example_interfaces/srv/AddTwoInts',
76+
'add_two_ints',
77+
{ qos: rclnodejs.QoS.profileSystemDefault },
78+
(request, response) => {
79+
let result = response.template;
80+
result.sum = request.a + request.b;
81+
}
82+
);
83+
84+
const options = service.getOptions();
85+
assert.strictEqual(options.depth, 0);
86+
assert.strictEqual(
87+
options.durability,
88+
QoS.DurabilityPolicy.RMW_QOS_POLICY_DURABILITY_SYSTEM_DEFAULT
89+
);
90+
assert.strictEqual(
91+
options.reliability,
92+
QoS.ReliabilityPolicy.RMW_QOS_POLICY_RELIABILITY_SYSTEM_DEFAULT
93+
);
94+
node.destroy();
95+
});
7096
});

test/types/index.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ expectType<void>(
202202
)
203203
);
204204
expectType<boolean>(service.isDestroyed());
205+
expectType<object>(service.getOptions());
205206

206207
// ---- Client ----
207208
const client = node.createClient(

types/service.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,11 @@ declare module 'rclnodejs' {
8888
serviceEventPubQOS: QoS,
8989
introspectionState: ServiceIntrospectionStates
9090
): void;
91+
92+
/**
93+
* Get the options of this service.
94+
* @return The options of this service.
95+
*/
96+
getOptions(): object;
9197
}
9298
}

0 commit comments

Comments
 (0)