UNPKG

rclnodejs

Version:
163 lines (144 loc) 4.61 kB
// 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. 'use strict'; const rclnodejs = require('bindings')('rclnodejs'); const DistroUtils = require('./distro.js'); const Entity = require('./entity.js'); const debug = require('debug')('rclnodejs:service'); /** * The response to client * * @property {Object} template - Get an empty response object that is being sent to client. * @property {Service} service - The service that this response object is attaching to. * * @hideconstructor */ class Response { constructor(service, header) { this._service = service; this._header = header; this.sent = false; } get template() { let response = new this._service._typeClass.Response(); return response.toPlainObject(); } get service() { return this._service; } /** * Send response to client (the service caller) * @param {object} response - The plain JavaScript representing the response. Note: you can use .template to get an empty result object. * @return {undefined} * @see {@link Response#template} */ send(response) { let responseToReturn = new this._service._typeClass.Response(response); const rawResponse = responseToReturn.serialize(); rclnodejs.sendResponse(this._service._handle, rawResponse, this._header); this.sent = true; } } /** * @class - Class representing a Service in ROS * @hideconstructor */ class Service extends Entity { constructor(nodeHandle, handle, serviceName, typeClass, options, callback) { super(handle, typeClass, options); this._nodeHandle = nodeHandle; this._callback = callback; } processRequest(headerHandle, request) { debug(`Service has received a ${this._serviceName} request from client.`); const plainObj = request.toPlainObject(this.typedArrayEnabled); const response = new Response(this, headerHandle); Promise.resolve(this._callback(plainObj, response)).then( (responseToReturn) => { if (!response.sent && responseToReturn) { responseToReturn = new this._typeClass.Response(responseToReturn); const rawResponse = responseToReturn.serialize(); rclnodejs.sendResponse(this._handle, rawResponse, headerHandle); } debug( `Service has processed the ${this._serviceName} request and sent the response.` ); } ); } static createService(nodeHandle, serviceName, typeClass, options, callback) { let type = typeClass.type(); let handle = rclnodejs.createService( nodeHandle, serviceName, type.interfaceName, type.pkgName, options.qos ); return new Service( nodeHandle, handle, serviceName, typeClass, options, callback ); } /** * @type {string} */ get serviceName() { return rclnodejs.getServiceServiceName(this._handle); } /** * Configure introspection. * @param {Clock} clock - Clock to use for service event timestamps * @param {QoS} qos - QoSProfile for the service event publisher * @param {ServiceIntrospectionState} introspectionState - State to set introspection to */ configureIntrospection(clock, qos, introspectionState) { if (DistroUtils.getDistroId() <= DistroUtils.getDistroId('humble')) { console.warn( 'Service introspection is not supported by this version of ROS 2' ); return; } let type = this.typeClass.type(); rclnodejs.configureServiceIntrospection( this.handle, this._nodeHandle, clock.handle, type.interfaceName, type.pkgName, qos, introspectionState ); } /** * Get the options of this service. * @return {object} The options of this service. */ getOptions() { return rclnodejs.getOptions(this._handle); } /** * Get the logger name for this service. * @returns {string} The logger name for this service. */ get loggerName() { return rclnodejs.getNodeLoggerName(this._nodeHandle); } } module.exports = Service;