// Copyright (c) 2017 Intel Corporation. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "shadow_node.h" #include #include #include "executor.h" #include "handle_manager.h" #include "rcl_handle.h" namespace rclnodejs { Napi::FunctionReference ShadowNode::constructor; ShadowNode::ShadowNode(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info), handle_manager_(std::make_unique()) { executor_ = std::make_unique(info.Env(), handle_manager_.get(), this); } ShadowNode::~ShadowNode() { StopRunning(); } void ShadowNode::Init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); Napi::Function func = DefineClass( env, "ShadowNode", { InstanceMethod("start", &ShadowNode::Start), InstanceMethod("stop", &ShadowNode::Stop), InstanceMethod("_isExecutorStopped", &ShadowNode::IsExecutorStopped), InstanceMethod("syncHandles", &ShadowNode::SyncHandles), InstanceMethod("spinOnce", &ShadowNode::SpinOnce), }); constructor = Napi::Persistent(func); constructor.SuppressDestruct(); exports.Set("ShadowNode", func); } void ShadowNode::StopRunning() { executor_->Stop(); if (executor_->IsStopped()) { handle_manager_->ClearHandles(); } } void ShadowNode::StartRunning(rcl_context_t* context, int32_t timeout) { executor_->SynchronizeHandles(this->Value()); executor_->Start(context, timeout); } void ShadowNode::RunOnce(rcl_context_t* context, int32_t timeout) { executor_->SynchronizeHandles(this->Value()); executor_->SpinOnce(context, timeout); } Napi::Value ShadowNode::Start(const Napi::CallbackInfo& info) { Napi::Object context_handle = info[0].As(); RclHandle* handle = RclHandle::Unwrap(context_handle); int32_t timeout = info[1].As().Int32Value(); rcl_context_t* context = reinterpret_cast(handle->ptr()); StartRunning(context, timeout); return info.Env().Undefined(); } Napi::Value ShadowNode::Stop(const Napi::CallbackInfo& info) { StopRunning(); return info.Env().Undefined(); } Napi::Value ShadowNode::IsExecutorStopped(const Napi::CallbackInfo& info) { return Napi::Boolean::New(info.Env(), executor_->IsStopped()); } Napi::Value ShadowNode::SpinOnce(const Napi::CallbackInfo& info) { Napi::Object context_handle = info[0].As(); RclHandle* handle = RclHandle::Unwrap(context_handle); int32_t timeout = info[1].As().Int32Value(); rcl_context_t* context = reinterpret_cast(handle->ptr()); RunOnce(context, timeout); return info.Env().Undefined(); } Napi::Value ShadowNode::SyncHandles(const Napi::CallbackInfo& info) { executor_->SynchronizeHandles(this->Value()); return info.Env().Undefined(); } void ShadowNode::Execute(const std::vector& handles) { Napi::Env env = Env(); Napi::HandleScope scope(env); Napi::Array results = Napi::Array::New(env, handles.size()); for (size_t i = 0; i < handles.size(); ++i) { handles[i]->SyncProperties(); results[i] = handles[i]->Value(); } Napi::Function execute = Value().As().Get("execute").As(); execute.MakeCallback(Value(), {results}); } void ShadowNode::CatchException(std::exception_ptr e_ptr) { try { std::rethrow_exception(e_ptr); } catch (const std::exception& e) { Napi::Error::New(Env(), e.what()).ThrowAsJavaScriptException(); } } } // namespace rclnodejs