|
|
@ -33,10 +33,6 @@ |
|
|
|
|
|
|
|
|
|
|
|
'use strict'; |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
var util = require('util'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var Transform = require('stream').Transform; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var grpc = require('..'); |
|
|
|
var grpc = require('..'); |
|
|
|
var math = grpc.load(__dirname + '/math.proto').math; |
|
|
|
var math = grpc.load(__dirname + '/math.proto').math; |
|
|
|
|
|
|
|
|
|
|
@ -54,11 +50,12 @@ function mathDiv(call, cb) { |
|
|
|
// Unary + is explicit coersion to integer
|
|
|
|
// Unary + is explicit coersion to integer
|
|
|
|
if (+req.divisor === 0) { |
|
|
|
if (+req.divisor === 0) { |
|
|
|
cb(new Error('cannot divide by zero')); |
|
|
|
cb(new Error('cannot divide by zero')); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
cb(null, { |
|
|
|
|
|
|
|
quotient: req.dividend / req.divisor, |
|
|
|
|
|
|
|
remainder: req.dividend % req.divisor |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
cb(null, { |
|
|
|
|
|
|
|
quotient: req.dividend / req.divisor, |
|
|
|
|
|
|
|
remainder: req.dividend % req.divisor |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -97,24 +94,19 @@ function mathSum(call, cb) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function mathDivMany(stream) { |
|
|
|
function mathDivMany(stream) { |
|
|
|
// Here, call is a standard duplex Node object Stream
|
|
|
|
stream.on('data', function(div_args) { |
|
|
|
util.inherits(DivTransform, Transform); |
|
|
|
|
|
|
|
function DivTransform() { |
|
|
|
|
|
|
|
var options = {objectMode: true}; |
|
|
|
|
|
|
|
Transform.call(this, options); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
DivTransform.prototype._transform = function(div_args, encoding, callback) { |
|
|
|
|
|
|
|
if (+div_args.divisor === 0) { |
|
|
|
if (+div_args.divisor === 0) { |
|
|
|
callback(new Error('cannot divide by zero')); |
|
|
|
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 |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
callback(null, { |
|
|
|
}); |
|
|
|
quotient: div_args.dividend / div_args.divisor, |
|
|
|
stream.on('end', function() { |
|
|
|
remainder: div_args.dividend % div_args.divisor |
|
|
|
stream.end(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
var transform = new DivTransform(); |
|
|
|
|
|
|
|
stream.pipe(transform); |
|
|
|
|
|
|
|
transform.pipe(stream); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var server = new Server({ |
|
|
|
var server = new Server({ |
|
|
|