blob: 366513dc17db261e4daf94c3495ae30cfdcc612f [file] [log] [blame]
murgatroid99749666e2015-01-12 18:25:58 -08001/*
2 *
3 * Copyright 2014, 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
murgatroid99e5061512015-01-12 18:14:35 -080034var _ = require('underscore');
35var ProtoBuf = require('protobufjs');
36var fs = require('fs');
37var util = require('util');
38
39var Transform = require('stream').Transform;
40
murgatroid99cca5ffa2015-01-15 14:06:56 -080041var grpc = require('..');
42var math = grpc.load(__dirname + '/math.proto').math;
murgatroid99e5061512015-01-12 18:14:35 -080043
murgatroid99cca5ffa2015-01-15 14:06:56 -080044var Server = grpc.buildServer([math.Math.service]);
murgatroid99e5061512015-01-12 18:14:35 -080045
46/**
47 * Server function for division. Provides the /Math/DivMany and /Math/Div
48 * functions (Div is just DivMany with only one stream element). For each
49 * DivArgs parameter, responds with a DivReply with the results of the division
50 * @param {Object} call The object containing request and cancellation info
51 * @param {function(Error, *)} cb Response callback
52 */
53function mathDiv(call, cb) {
54 var req = call.request;
55 if (req.divisor == 0) {
56 cb(new Error('cannot divide by zero'));
57 }
58 cb(null, {
59 quotient: req.dividend / req.divisor,
60 remainder: req.dividend % req.divisor
61 });
62}
63
64/**
65 * Server function for Fibonacci numbers. Provides the /Math/Fib function. Reads
66 * a single parameter that indicates the number of responses, and then responds
67 * with a stream of that many Fibonacci numbers.
68 * @param {stream} stream The stream for sending responses.
69 */
70function mathFib(stream) {
71 // Here, call is a standard writable Node object Stream
72 var previous = 0, current = 1;
73 for (var i = 0; i < stream.request.limit; i++) {
74 stream.write({num: current});
75 var temp = current;
76 current += previous;
77 previous = temp;
78 }
79 stream.end();
80}
81
82/**
83 * Server function for summation. Provides the /Math/Sum function. Reads a
84 * stream of number parameters, then responds with their sum.
85 * @param {stream} call The stream of arguments.
86 * @param {function(Error, *)} cb Response callback
87 */
88function mathSum(call, cb) {
89 // Here, call is a standard readable Node object Stream
90 var sum = 0;
91 call.on('data', function(data) {
92 sum += data.num | 0;
93 });
94 call.on('end', function() {
95 cb(null, {num: sum});
96 });
97}
98
99function mathDivMany(stream) {
100 // Here, call is a standard duplex Node object Stream
101 util.inherits(DivTransform, Transform);
102 function DivTransform() {
103 var options = {objectMode: true};
104 Transform.call(this, options);
105 }
106 DivTransform.prototype._transform = function(div_args, encoding, callback) {
107 if (div_args.divisor == 0) {
108 callback(new Error('cannot divide by zero'));
109 }
110 callback(null, {
111 quotient: div_args.dividend / div_args.divisor,
112 remainder: div_args.dividend % div_args.divisor
113 });
114 };
115 var transform = new DivTransform();
116 stream.pipe(transform);
117 transform.pipe(stream);
118}
119
120var server = new Server({
murgatroid99cca5ffa2015-01-15 14:06:56 -0800121 'math.Math' : {
122 Div: mathDiv,
123 Fib: mathFib,
124 Sum: mathSum,
125 DivMany: mathDivMany
126 }
murgatroid99e5061512015-01-12 18:14:35 -0800127});
128
129if (require.main === module) {
130 server.bind('localhost:7070').listen();
131}
132
133/**
134 * See docs for server
135 */
136module.exports = server;