3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 var PROTO_PATH = __dirname + '/../../../proto/math/math.proto';
21 var grpc = require('grpc');
22 var protoLoader = require('@grpc/proto-loader');
23 var packageDefinition = protoLoader.loadSync(
31 var math_proto = grpc.loadPackageDefinition(packageDefinition).math;
34 * Implements the Div RPC method.
36 function Div(call, callback) {
37 var divisor = call.request.divisor;
38 var dividend = call.request.dividend;
41 code: grpc.status.INVALID_ARGUMENT,
42 details: 'Cannot divide by zero'
45 setTimeout(function () {
47 quotient: Math.floor(dividend / divisor),
48 remainder: dividend % divisor
50 }, 1); // 1 millisecond, to make sure 1 microsecond timeout from test
51 // will hit. TODO: Consider fixing this.
56 * Implements the Fib RPC method.
58 function Fib(stream) {
59 var previous = 0, current = 1;
60 for (var i = 0; i < stream.request.limit; i++) {
72 * Implements the Sum RPC method.
74 function Sum(call, callback) {
76 call.on('data', function(data) {
77 sum += parseInt(data.num);
79 call.on('end', function() {
87 * Implements the DivMany RPC method.
89 function DivMany(stream) {
90 stream.on('data', function(div_args) {
91 var divisor = div_args.divisor;
92 var dividend = div_args.dividend;
94 stream.emit('error', {
95 code: grpc.status.INVALID_ARGUMENT,
96 details: 'Cannot divide by zero'
100 quotient: Math.floor(dividend / divisor),
101 remainder: dividend % divisor
105 stream.on('end', function() {
112 * Starts an RPC server that receives requests for the Math service at the
116 var server = new grpc.Server();
117 server.addService(math_proto.Math.service, {
123 server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());