math_server.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. 'use strict';
  34. var util = require('util');
  35. var Transform = require('stream').Transform;
  36. var grpc = require('..');
  37. var math = grpc.load(__dirname + '/math.proto').math;
  38. var Server = grpc.buildServer([math.Math.service]);
  39. /**
  40. * Server function for division. Provides the /Math/DivMany and /Math/Div
  41. * functions (Div is just DivMany with only one stream element). For each
  42. * DivArgs parameter, responds with a DivReply with the results of the division
  43. * @param {Object} call The object containing request and cancellation info
  44. * @param {function(Error, *)} cb Response callback
  45. */
  46. function mathDiv(call, cb) {
  47. var req = call.request;
  48. // Unary + is explicit coersion to integer
  49. if (+req.divisor === 0) {
  50. cb(new Error('cannot divide by zero'));
  51. }
  52. cb(null, {
  53. quotient: req.dividend / req.divisor,
  54. remainder: req.dividend % req.divisor
  55. });
  56. }
  57. /**
  58. * Server function for Fibonacci numbers. Provides the /Math/Fib function. Reads
  59. * a single parameter that indicates the number of responses, and then responds
  60. * with a stream of that many Fibonacci numbers.
  61. * @param {stream} stream The stream for sending responses.
  62. */
  63. function mathFib(stream) {
  64. // Here, call is a standard writable Node object Stream
  65. var previous = 0, current = 1;
  66. for (var i = 0; i < stream.request.limit; i++) {
  67. stream.write({num: current});
  68. var temp = current;
  69. current += previous;
  70. previous = temp;
  71. }
  72. stream.end();
  73. }
  74. /**
  75. * Server function for summation. Provides the /Math/Sum function. Reads a
  76. * stream of number parameters, then responds with their sum.
  77. * @param {stream} call The stream of arguments.
  78. * @param {function(Error, *)} cb Response callback
  79. */
  80. function mathSum(call, cb) {
  81. // Here, call is a standard readable Node object Stream
  82. var sum = 0;
  83. call.on('data', function(data) {
  84. sum += (+data.num);
  85. });
  86. call.on('end', function() {
  87. cb(null, {num: sum});
  88. });
  89. }
  90. function mathDivMany(stream) {
  91. // Here, call is a standard duplex Node object Stream
  92. util.inherits(DivTransform, Transform);
  93. function DivTransform() {
  94. var options = {objectMode: true};
  95. Transform.call(this, options);
  96. }
  97. DivTransform.prototype._transform = function(div_args, encoding, callback) {
  98. if (+div_args.divisor === 0) {
  99. callback(new Error('cannot divide by zero'));
  100. }
  101. callback(null, {
  102. quotient: div_args.dividend / div_args.divisor,
  103. remainder: div_args.dividend % div_args.divisor
  104. });
  105. };
  106. var transform = new DivTransform();
  107. stream.pipe(transform);
  108. transform.pipe(stream);
  109. }
  110. var server = new Server({
  111. 'math.Math' : {
  112. div: mathDiv,
  113. fib: mathFib,
  114. sum: mathSum,
  115. divMany: mathDivMany
  116. }
  117. });
  118. if (require.main === module) {
  119. server.bind('0.0.0.0:7070');
  120. server.listen();
  121. }
  122. /**
  123. * See docs for server
  124. */
  125. module.exports = server;