aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/node/test/math/math_server.js
diff options
context:
space:
mode:
authorGravatar Craig Tiller <ctiller@google.com>2016-05-03 12:56:40 -0700
committerGravatar Craig Tiller <ctiller@google.com>2016-05-03 12:56:40 -0700
commit7c6ba9bae47bdeb6f532b457d9848e7dc94c8e14 (patch)
treedd56d6b41d03feedf1f51420507b958af7473c1a /src/node/test/math/math_server.js
parent27f59afecb04b63b7c83a842d400efb1ad09049a (diff)
parent6bac7d3467c99dccc30e8447bc84237bb54b99fe (diff)
Merge github.com:grpc/grpc into error
Diffstat (limited to 'src/node/test/math/math_server.js')
-rw-r--r--src/node/test/math/math_server.js40
1 files changed, 24 insertions, 16 deletions
diff --git a/src/node/test/math/math_server.js b/src/node/test/math/math_server.js
index 9f67c52ab0..fa05ed0165 100644
--- a/src/node/test/math/math_server.js
+++ b/src/node/test/math/math_server.js
@@ -34,8 +34,8 @@
'use strict';
var grpc = require('../..');
-var math = grpc.load(__dirname + '/../../../proto/math/math.proto').math;
-
+var grpcMath = require('./math_grpc_pb');
+var math = require('./math_pb');
/**
* Server function for division. Provides the /Math/DivMany and /Math/Div
@@ -46,14 +46,16 @@ var math = grpc.load(__dirname + '/../../../proto/math/math.proto').math;
*/
function mathDiv(call, cb) {
var req = call.request;
+ var divisor = req.getDivisor();
+ var dividend = req.getDividend();
// Unary + is explicit coersion to integer
- if (+req.divisor === 0) {
+ if (req.getDivisor() === 0) {
cb(new Error('cannot divide by zero'));
} else {
- cb(null, {
- quotient: req.dividend / req.divisor,
- remainder: req.dividend % req.divisor
- });
+ var response = new math.DivReply();
+ response.setQuotient(Math.floor(dividend / divisor));
+ response.setRemainder(dividend % divisor);
+ cb(null, response);
}
}
@@ -67,7 +69,9 @@ function mathFib(stream) {
// Here, call is a standard writable Node object Stream
var previous = 0, current = 1;
for (var i = 0; i < stream.request.limit; i++) {
- stream.write({num: current});
+ var response = new math.Num();
+ response.setNum(current);
+ stream.write(response);
var temp = current;
current += previous;
previous = temp;
@@ -85,22 +89,26 @@ function mathSum(call, cb) {
// Here, call is a standard readable Node object Stream
var sum = 0;
call.on('data', function(data) {
- sum += (+data.num);
+ sum += data.getNum();
});
call.on('end', function() {
- cb(null, {num: sum});
+ var response = new math.Num();
+ response.setNum(sum);
+ cb(null, response);
});
}
function mathDivMany(stream) {
stream.on('data', function(div_args) {
- if (+div_args.divisor === 0) {
+ var divisor = div_args.getDivisor();
+ var dividend = div_args.getDividend();
+ if (divisor === 0) {
stream.emit('error', new Error('cannot divide by zero'));
} else {
- stream.write({
- quotient: div_args.dividend / div_args.divisor,
- remainder: div_args.dividend % div_args.divisor
- });
+ var response = new math.DivReply();
+ response.setQuotient(Math.floor(dividend / divisor));
+ response.setRemainder(dividend % divisor);
+ stream.write(response);
}
});
stream.on('end', function() {
@@ -110,7 +118,7 @@ function mathDivMany(stream) {
function getMathServer() {
var server = new grpc.Server();
- server.addProtoService(math.Math.service, {
+ server.addService(grpcMath.MathService, {
div: mathDiv,
fib: mathFib,
sum: mathSum,