OLD | NEW |
(Empty) | |
| 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 |
| 34 'use strict'; |
| 35 |
| 36 var grpc = require('../..'); |
| 37 var math = grpc.load(__dirname + '/../../../proto/math/math.proto').math; |
| 38 |
| 39 |
| 40 /** |
| 41 * Server function for division. Provides the /Math/DivMany and /Math/Div |
| 42 * functions (Div is just DivMany with only one stream element). For each |
| 43 * DivArgs parameter, responds with a DivReply with the results of the division |
| 44 * @param {Object} call The object containing request and cancellation info |
| 45 * @param {function(Error, *)} cb Response callback |
| 46 */ |
| 47 function mathDiv(call, cb) { |
| 48 var req = call.request; |
| 49 // Unary + is explicit coersion to integer |
| 50 if (+req.divisor === 0) { |
| 51 cb(new Error('cannot divide by zero')); |
| 52 } else { |
| 53 cb(null, { |
| 54 quotient: req.dividend / req.divisor, |
| 55 remainder: req.dividend % req.divisor |
| 56 }); |
| 57 } |
| 58 } |
| 59 |
| 60 /** |
| 61 * Server function for Fibonacci numbers. Provides the /Math/Fib function. Reads |
| 62 * a single parameter that indicates the number of responses, and then responds |
| 63 * with a stream of that many Fibonacci numbers. |
| 64 * @param {stream} stream The stream for sending responses. |
| 65 */ |
| 66 function mathFib(stream) { |
| 67 // Here, call is a standard writable Node object Stream |
| 68 var previous = 0, current = 1; |
| 69 for (var i = 0; i < stream.request.limit; i++) { |
| 70 stream.write({num: current}); |
| 71 var temp = current; |
| 72 current += previous; |
| 73 previous = temp; |
| 74 } |
| 75 stream.end(); |
| 76 } |
| 77 |
| 78 /** |
| 79 * Server function for summation. Provides the /Math/Sum function. Reads a |
| 80 * stream of number parameters, then responds with their sum. |
| 81 * @param {stream} call The stream of arguments. |
| 82 * @param {function(Error, *)} cb Response callback |
| 83 */ |
| 84 function mathSum(call, cb) { |
| 85 // Here, call is a standard readable Node object Stream |
| 86 var sum = 0; |
| 87 call.on('data', function(data) { |
| 88 sum += (+data.num); |
| 89 }); |
| 90 call.on('end', function() { |
| 91 cb(null, {num: sum}); |
| 92 }); |
| 93 } |
| 94 |
| 95 function mathDivMany(stream) { |
| 96 stream.on('data', function(div_args) { |
| 97 if (+div_args.divisor === 0) { |
| 98 stream.emit('error', new Error('cannot divide by zero')); |
| 99 } else { |
| 100 stream.write({ |
| 101 quotient: div_args.dividend / div_args.divisor, |
| 102 remainder: div_args.dividend % div_args.divisor |
| 103 }); |
| 104 } |
| 105 }); |
| 106 stream.on('end', function() { |
| 107 stream.end(); |
| 108 }); |
| 109 } |
| 110 |
| 111 function getMathServer() { |
| 112 var server = new grpc.Server(); |
| 113 server.addProtoService(math.Math.service, { |
| 114 div: mathDiv, |
| 115 fib: mathFib, |
| 116 sum: mathSum, |
| 117 divMany: mathDivMany |
| 118 }); |
| 119 return server; |
| 120 } |
| 121 |
| 122 if (require.main === module) { |
| 123 var server = getMathServer(); |
| 124 server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure()); |
| 125 server.start(); |
| 126 } |
| 127 |
| 128 /** |
| 129 * See docs for server |
| 130 */ |
| 131 module.exports = getMathServer; |
OLD | NEW |