Skip to content

Commit e634a40

Browse files
committed
Support setting domain id for Context
1 parent 4cff06a commit e634a40

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ let rcl = {
255255
throw new TypeError('argv elements must be strings (and not null).');
256256
}
257257

258-
rclnodejs.init(context.handle, argv);
258+
rclnodejs.init(context.handle, argv, context._domainId);
259259

260260
if (_rosVersionChecked) {
261261
// no further processing required

lib/context.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ class Context {
6363
* Call rcl.init(context) to initialize this context state for
6464
* use in creating nodes, etc.
6565
* @constructor
66+
* @param {bigint} - Optional, The domain ID of this context.
6667
*/
67-
constructor() {
68+
constructor(domainId) {
6869
this._handle = rclnodejs.createContext();
6970
this._isShutdown = false;
7071
this._nodes = [];
72+
this._domainId = domainId;
7173
Context._instances.push(this);
7274
}
7375

@@ -222,7 +224,7 @@ class Context {
222224

223225
/**
224226
* Get the domain ID of this context.
225-
* @returns {Number} domain ID of this context
227+
* @returns {bigint} domain ID of this context
226228
*/
227229
get domainId() {
228230
return rclnodejs.getDomainId(this.handle);

src/rcl_context_bindings.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <rcl/rcl.h>
1919

2020
#include <cstdio>
21+
#include <rcpputils/scope_exit.hpp>
2122
#include <string>
2223

2324
#include "macros.h"
@@ -35,6 +36,15 @@ Napi::Value Init(const Napi::CallbackInfo& info) {
3536
rcl_init_options_init(&init_options, allocator),
3637
rcl_get_error_string().str);
3738

39+
RCPPUTILS_SCOPE_EXIT({
40+
rcl_ret_t fini_ret = rcl_init_options_fini(&init_options);
41+
if (RCL_RET_OK != fini_ret) {
42+
Napi::Error::New(env, rcl_get_error_string().str)
43+
.ThrowAsJavaScriptException();
44+
rcl_reset_error();
45+
}
46+
});
47+
3848
// Preprocess Context
3949
RclHandle* context_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
4050
rcl_context_t* context =
@@ -54,6 +64,18 @@ Napi::Value Init(const Napi::CallbackInfo& info) {
5464
snprintf(argv[i], len, "%s", arg.c_str());
5565
}
5666
}
67+
// Set up the domain id.
68+
size_t domain_id = RCL_DEFAULT_DOMAIN_ID;
69+
if (info.Length() > 2 && info[2].IsBigInt()) {
70+
bool lossless;
71+
domain_id = info[2].As<Napi::BigInt>().Uint64Value(&lossless);
72+
}
73+
rcl_ret_t ret = rcl_init_options_set_domain_id(&init_options, domain_id);
74+
if (RCL_RET_OK != ret) {
75+
Napi::Error::New(env, "failed to set domain id to init options")
76+
.ThrowAsJavaScriptException();
77+
return env.Undefined();
78+
}
5779

5880
THROW_ERROR_IF_NOT_EQUAL(
5981
RCL_RET_OK,
@@ -141,7 +163,7 @@ Napi::Value GetDomainId(const Napi::CallbackInfo& info) {
141163
return env.Undefined();
142164
}
143165

144-
return Napi::Number::New(env, domain_id);
166+
return Napi::BigInt::New(env, domain_id);
145167
}
146168

147169
Napi::Object InitContextBindings(Napi::Env env, Napi::Object exports) {

test/test-context.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,16 @@ describe('context test suite', function () {
8181
assert.strictEqual(context.nodes.length, 0);
8282
});
8383

84-
it('context number id', async function () {
84+
it('context domain id', async function () {
85+
let context = new rclnodejs.Context(BigInt(123));
86+
await rclnodejs.init(context);
87+
assert.strictEqual(typeof context.domainId, 'bigint');
88+
assert.strictEqual(context.domainId, BigInt(123));
89+
});
90+
91+
it('context with default domain id', async function () {
8592
let context = new rclnodejs.Context();
8693
await rclnodejs.init(context);
87-
assert.strictEqual(typeof context.domainId, 'number');
94+
assert.strictEqual(typeof context.domainId, 'bigint');
8895
});
8996
});

test/types/index.test-d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ expectType<string | undefined>(rclnodejs.DistroUtils.getDistroName(2105));
2828

2929
// ---- Context -----
3030
expectType<rclnodejs.Context>(rclnodejs.Context.defaultContext());
31-
expectType<number>(rclnodejs.Context.defaultContext().domainId());
31+
expectType<rclnodejs.Context>(new rclnodejs.Context(123n));
32+
expectType<bigint>(rclnodejs.Context.defaultContext().domainId());
3233

3334
// ---- NodeOptions ----
3435
const nodeOptions = new rclnodejs.NodeOptions();

types/context.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ declare module 'rclnodejs' {
3838
* Create a new instance in uninitialized state.
3939
* Call rcl.init(context) to initialize this context state for
4040
* use in creating nodes, etc.
41+
* @param {bigint} - Optional, The domain ID of this context.
4142
*/
42-
constructor();
43+
constructor(domainId?: bigint);
4344

4445
/**
4546
* Test if this context has not been initialized by rcl.init(context).
@@ -92,6 +93,6 @@ declare module 'rclnodejs' {
9293
* Get the domain ID of this context.
9394
* @returns domain ID of this context
9495
*/
95-
domainId(): number;
96+
domainId(): bigint;
9697
}
9798
}

0 commit comments

Comments
 (0)