Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class Subscription extends Entity {
*/
setContentFilter(contentFilter) {
return contentFilter?.expression
? rclnodejs.setContentFilter(this.handle, contentFilter)
? rclnodejs.setContentFilter(this.handle, contentFilter, this._node)
: this.clearContentFilter();
}

Expand All @@ -196,7 +196,7 @@ class Subscription extends Entity {
*/
clearContentFilter() {
return this.hasContentFilter()
? rclnodejs.clearContentFilter(this.handle)
? rclnodejs.clearContentFilter(this.handle, this._node)
: true;
}

Expand Down
11 changes: 11 additions & 0 deletions src/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,17 @@ void Executor::SynchronizeHandles(const Napi::Object& node) {
node, IsRunning() ? &interrupt_guard_condition_ : nullptr);
}

rcl_ret_t Executor::SetSubscriptionContentFilter(
const rcl_subscription_t* subscription,
const rcl_subscription_content_filter_options_t* options) {
rcl_ret_t result = RCL_RET_ERROR;
handle_manager_->RunWithWaitSetWriteLock(
env_, IsRunning() ? &interrupt_guard_condition_ : nullptr, [&] {
result = rcl_subscription_set_content_filter(subscription, options);
});
return result;
}

std::string Executor::ReleaseResources() {
state_.store(ExecutorState::ReleasingResources);
handle_manager_->TakeReadyHandles();
Expand Down
4 changes: 4 additions & 0 deletions src/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <napi.h>
#include <rcl/guard_condition.h>
#include <rcl/subscription.h>
#include <rcl/wait.h>
#include <uv.h>

Expand Down Expand Up @@ -50,6 +51,9 @@ class Executor {
void Stop();
void SpinOnce(rcl_context_t* context, int32_t time_out);
void SynchronizeHandles(const Napi::Object& node);
rcl_ret_t SetSubscriptionContentFilter(
const rcl_subscription_t* subscription,
const rcl_subscription_content_filter_options_t* options);
int32_t time_out() { return time_out_; }
bool IsStopped() const;

Expand Down
49 changes: 27 additions & 22 deletions src/handle_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,9 @@ HandleManager::~HandleManager() {
uv_sem_destroy(&wait_handle_sem_);
}

void HandleManager::SynchronizeHandles(
const Napi::Object& node,
rcl_guard_condition_t* interrupt_guard_condition) {
Napi::HandleScope scope(node.Env());
Napi::Value timers = node.Get("_timers");
Napi::Value subscriptions = node.Get("_subscriptions");
Napi::Value clients = node.Get("_clients");
Napi::Value services = node.Get("_services");
Napi::Value guard_conditions = node.Get("_guards");
Napi::Value action_clients = node.Get("_actionClients");
Napi::Value action_servers = node.Get("_actionServers");
Napi::Value events = node.Get("_events");

uint32_t sum = 0;
uint32_t sum_was = 0;
void HandleManager::RunWithWaitSetWriteLock(
Napi::Env env, rcl_guard_condition_t* interrupt_guard_condition,
const std::function<void()>& operation) {
is_synchronizing_.store(true);
struct SynchronizationGuard {
HandleManager* manager;
Expand All @@ -69,17 +57,34 @@ void HandleManager::SynchronizeHandles(
rcl_ret_t ret = rcl_trigger_guard_condition(interrupt_guard_condition);
if (ret != RCL_RET_OK) {
std::string error_message =
std::string(
"Failed to interrupt executor for handle synchronization: ") +
std::string("Failed to interrupt executor for a wait set update: ") +
rcl_get_error_string().str;
rcl_reset_error();
throw Napi::Error::New(node.Env(), error_message);
throw Napi::Error::New(env, error_message);
}
}

{
ScopedReadWriteLock scoped_lock(&sync_handles_rwlock_,
ScopedReadWriteLock::LockType::kWrite);
ScopedReadWriteLock scoped_lock(&sync_handles_rwlock_,
ScopedReadWriteLock::LockType::kWrite);
operation();
}

void HandleManager::SynchronizeHandles(
const Napi::Object& node,
rcl_guard_condition_t* interrupt_guard_condition) {
Napi::HandleScope scope(node.Env());
Napi::Value timers = node.Get("_timers");
Napi::Value subscriptions = node.Get("_subscriptions");
Napi::Value clients = node.Get("_clients");
Napi::Value services = node.Get("_services");
Napi::Value guard_conditions = node.Get("_guards");
Napi::Value action_clients = node.Get("_actionClients");
Napi::Value action_servers = node.Get("_actionServers");
Napi::Value events = node.Get("_events");

uint32_t sum = 0;
uint32_t sum_was = 0;
RunWithWaitSetWriteLock(node.Env(), interrupt_guard_condition, [&] {
ClearHandles();
sum += SynchronizeHandlesByType(timers.As<Napi::Object>(), &timers_);
sum += SynchronizeHandlesByType(subscriptions.As<Napi::Object>(),
Expand All @@ -95,7 +100,7 @@ void HandleManager::SynchronizeHandles(
sum += SynchronizeHandlesByType(events.As<Napi::Object>(), &events_);
PruneRemovedReadyHandles(sum);
sum_was = sum_.exchange(sum);
}
});

// Wakeup the backgroud thread if the sum was zero, but now it is greater than
// zero.
Expand Down
4 changes: 4 additions & 0 deletions src/handle_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <atomic>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <vector>

Expand Down Expand Up @@ -59,6 +60,9 @@ class HandleManager {
void SynchronizeHandles(
const Napi::Object& node,
rcl_guard_condition_t* interrupt_guard_condition = nullptr);
void RunWithWaitSetWriteLock(Napi::Env env,
rcl_guard_condition_t* interrupt_guard_condition,
const std::function<void()>& operation);
void WaitForSynchronizing();

// Waits the handles to be attached from the background thread.
Expand Down
127 changes: 51 additions & 76 deletions src/rcl_subscription_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
#include <cstdio>
#include <memory>
#include <string>
#include <vector>

#include <rcpputils/scope_exit.hpp>

#include "macros.h"
#include "rcl_handle.h"
#include "rcl_utilities.h"
#include "shadow_node.h"

namespace rclnodejs {

Expand Down Expand Up @@ -310,8 +312,40 @@ Napi::Value HasContentFilter(const Napi::CallbackInfo& info) {
return Napi::Boolean::New(env, is_valid);
}

namespace {

Napi::Value ApplyContentFilter(
Napi::Env env, ShadowNode* node, rcl_subscription_t* subscription,
rcl_subscription_content_filter_options_t* options) {
auto cleanup = rcpputils::make_scope_exit([&] {
if (rcl_subscription_content_filter_options_fini(subscription, options) !=
RCL_RET_OK) {
rcl_reset_error();
}
});

if (node->executor()->SetSubscriptionContentFilter(subscription, options) !=
RCL_RET_OK) {
std::string error_message = rcl_get_error_string().str;
rcl_reset_error();
throw Napi::Error::New(env, error_message);
}

cleanup.cancel();
if (rcl_subscription_content_filter_options_fini(subscription, options) !=
RCL_RET_OK) {
std::string error_message = rcl_get_error_string().str;
rcl_reset_error();
throw Napi::Error::New(env, error_message);
}
return Napi::Boolean::New(env, true);
}

} // namespace

Napi::Value SetContentFilter(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
auto* node = ShadowNode::Unwrap(info[2].As<Napi::Object>());

RclHandle* subscription_handle =
RclHandle::Unwrap(info[0].As<Napi::Object>());
Expand All @@ -323,79 +357,39 @@ Napi::Value SetContentFilter(const Napi::CallbackInfo& info) {
std::string expression =
contentFilter.Get("expression").As<Napi::String>().Utf8Value();

// parameters property (string[]) is optional
int argc = 0;
char** argv = nullptr;

std::vector<std::string> parameters;
if (contentFilter.Has("parameters")) {
Napi::Array jsArgv = contentFilter.Get("parameters").As<Napi::Array>();
argc = jsArgv.Length();
if (argc > 0) {
argv = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
for (int i = 0; i < argc; i++) {
std::string arg = jsArgv.Get(i).As<Napi::String>().Utf8Value();
int len = arg.length() + 1;
argv[i] = reinterpret_cast<char*>(malloc(len * sizeof(char)));
snprintf(argv[i], len, "%s", arg.c_str());
}
auto js_parameters = contentFilter.Get("parameters").As<Napi::Array>();
parameters.reserve(js_parameters.Length());
for (uint32_t index = 0; index < js_parameters.Length(); ++index) {
parameters.push_back(
js_parameters.Get(index).As<Napi::String>().Utf8Value());
}
}
std::vector<const char*> argv;
argv.reserve(parameters.size());
for (const auto& parameter : parameters) {
argv.push_back(parameter.c_str());
}

// create ctf options
rcl_subscription_content_filter_options_t options =
rcl_get_zero_initialized_subscription_content_filter_options();

rcl_ret_t ret = rcl_subscription_content_filter_options_set(
subscription, expression.c_str(), argc, (const char**)argv, &options);
subscription, expression.c_str(), argv.size(), argv.data(), &options);

if (ret != RCL_RET_OK) {
if (argc) {
for (int i = 0; i < argc; i++) {
free(argv[i]);
}
free(argv);
}
std::string error_string = rcl_get_error_string().str;
rcl_reset_error();
Napi::Error::New(env, error_string).ThrowAsJavaScriptException();
return env.Undefined();
throw Napi::Error::New(env, error_string);
}

ret = rcl_subscription_set_content_filter(subscription, &options);

if (argc) {
for (int i = 0; i < argc; i++) {
free(argv[i]);
}
free(argv);
}

std::string error_string = "";
if (ret != RCL_RET_OK) {
error_string = rcl_get_error_string().str;
rcl_reset_error();
}

rcl_ret_t fini_ret =
rcl_subscription_content_filter_options_fini(subscription, &options);

if (ret != RCL_RET_OK) {
Napi::Error::New(env, error_string).ThrowAsJavaScriptException();
return env.Undefined();
}

if (fini_ret != RCL_RET_OK) {
error_string = rcl_get_error_string().str;
rcl_reset_error();
Napi::Error::New(env, error_string).ThrowAsJavaScriptException();
return env.Undefined();
}

return Napi::Boolean::New(env, true);
return ApplyContentFilter(env, node, subscription, &options);
}

Napi::Value ClearContentFilter(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
auto* node = ShadowNode::Unwrap(info[1].As<Napi::Object>());

RclHandle* subscription_handle =
RclHandle::Unwrap(info[0].As<Napi::Object>());
Expand All @@ -412,29 +406,10 @@ Napi::Value ClearContentFilter(const Napi::CallbackInfo& info) {
if (ret != RCL_RET_OK) {
std::string error_string = rcl_get_error_string().str;
rcl_reset_error();
Napi::Error::New(env, error_string).ThrowAsJavaScriptException();
return env.Undefined();
}

ret = rcl_subscription_set_content_filter(subscription, &options);
rcl_ret_t fini_ret =
rcl_subscription_content_filter_options_fini(subscription, &options);

if (ret != RCL_RET_OK) {
std::string error_string = rcl_get_error_string().str;
rcl_reset_error();
Napi::Error::New(env, error_string).ThrowAsJavaScriptException();
return env.Undefined();
throw Napi::Error::New(env, error_string);
}

if (fini_ret != RCL_RET_OK) {
std::string error_string = rcl_get_error_string().str;
rcl_reset_error();
Napi::Error::New(env, error_string).ThrowAsJavaScriptException();
return env.Undefined();
}

return Napi::Boolean::New(env, true);
return ApplyContentFilter(env, node, subscription, &options);
}

Napi::Value GetContentFilter(const Napi::CallbackInfo& info) {
Expand Down
1 change: 1 addition & 0 deletions src/shadow_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ShadowNode : public Napi::ObjectWrap<ShadowNode>,
void RunOnce(rcl_context_t* context, int32_t timeout);

HandleManager* handle_manager() { return handle_manager_.get(); }
Executor* executor() { return executor_.get(); }

// Executor::Delegate overrides:
void Execute(const std::vector<rclnodejs::RclHandle*>& handles) override;
Expand Down
Loading
Loading