Source: lib/service.js

  1. // Copyright (c) 2017 Intel Corporation. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. 'use strict';
  15. const rclnodejs = require('bindings')('rclnodejs');
  16. const Entity = require('./entity.js');
  17. const debug = require('debug')('rclnodejs:service');
  18. /**
  19. * The response to client
  20. *
  21. * @property {Object} template - Get an empty response object that is being sent to client.
  22. * @property {Service} service - The service that this response object is attaching to.
  23. *
  24. * @hideconstructor
  25. */
  26. class Response {
  27. constructor(service, header) {
  28. this._service = service;
  29. this._header = header;
  30. this.sent = false;
  31. }
  32. get template() {
  33. let response = new this._service._typeClass.Response();
  34. return response.toPlainObject();
  35. }
  36. get service() {
  37. return this._service;
  38. }
  39. /**
  40. * Send response to client (the service caller)
  41. * @param {object} response - The plain JavaScript representing the response.
  42. Note: you can use .template to get an empty result object.
  43. * @return {undefined}
  44. * @see {@link Response#template}
  45. */
  46. send(response) {
  47. let responseToReturn = new this._service._typeClass.Response(response);
  48. const rawResponse = responseToReturn.serialize();
  49. rclnodejs.sendResponse(this._service._handle, rawResponse, this._header);
  50. this.sent = true;
  51. }
  52. }
  53. /**
  54. * @class - Class representing a Service in ROS
  55. * @hideconstructor
  56. */
  57. class Service extends Entity {
  58. constructor(handle, serviceName, typeClass, options, callback) {
  59. super(handle, typeClass, options);
  60. this._callback = callback;
  61. }
  62. processRequest(headerHandle, request) {
  63. debug(`Service has received a ${this._serviceName} request from client.`);
  64. const plainObj = request.toPlainObject(this.typedArrayEnabled);
  65. const response = new Response(this, headerHandle);
  66. let responseToReturn = this._callback(plainObj, response);
  67. if (!response.sent && responseToReturn) {
  68. responseToReturn = new this._typeClass.Response(responseToReturn);
  69. const rawResponse = responseToReturn.serialize();
  70. rclnodejs.sendResponse(this._handle, rawResponse, headerHandle);
  71. }
  72. debug(
  73. `Service has processed the ${this._serviceName} request and sent the response.`
  74. );
  75. }
  76. static createService(nodeHandle, serviceName, typeClass, options, callback) {
  77. let type = typeClass.type();
  78. let handle = rclnodejs.createService(
  79. nodeHandle,
  80. serviceName,
  81. type.interfaceName,
  82. type.pkgName,
  83. options.qos
  84. );
  85. return new Service(handle, serviceName, typeClass, options, callback);
  86. }
  87. /**
  88. * @type {string}
  89. */
  90. get serviceName() {
  91. const fullServiceName = rclnodejs.getServiceName(this._handle); // returns /my_node/myservice
  92. return fullServiceName.split('/').pop();
  93. }
  94. }
  95. module.exports = Service;