| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 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 |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 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. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
var PROTO_PATH = __dirname + '/../../../proto/math/math.proto'; |
| 20 |
|
| 21 |
var grpc = require('grpc'); |
| 22 |
var protoLoader = require('@grpc/proto-loader'); |
| 23 |
var packageDefinition = protoLoader.loadSync( |
| 24 |
PROTO_PATH, |
| 25 |
{keepCase: true, |
| 26 |
longs: String, |
| 27 |
enums: String, |
| 28 |
defaults: true, |
| 29 |
oneofs: true |
| 30 |
}); |
| 31 |
var math_proto = grpc.loadPackageDefinition(packageDefinition).math; |
| 32 |
|
| 33 |
/** |
| 34 |
* Implements the Div RPC method. |
| 35 |
*/ |
| 36 |
function Div(call, callback) { |
| 37 |
var divisor = call.request.divisor; |
| 38 |
var dividend = call.request.dividend; |
| 39 |
if (divisor == 0) { |
| 40 |
callback({ |
| 41 |
code: grpc.status.INVALID_ARGUMENT, |
| 42 |
details: 'Cannot divide by zero' |
| 43 |
}); |
| 44 |
} else { |
| 45 |
setTimeout(function () { |
| 46 |
callback(null, { |
| 47 |
quotient: Math.floor(dividend / divisor), |
| 48 |
remainder: dividend % divisor |
| 49 |
}); |
| 50 |
}, 1); // 1 millisecond, to make sure 1 microsecond timeout from test |
| 51 |
// will hit. TODO: Consider fixing this. |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Implements the Fib RPC method. |
| 57 |
*/ |
| 58 |
function Fib(stream) { |
| 59 |
var previous = 0, current = 1; |
| 60 |
for (var i = 0; i < stream.request.limit; i++) { |
| 61 |
stream.write({ |
| 62 |
num: current |
| 63 |
}); |
| 64 |
var temp = current; |
| 65 |
current += previous; |
| 66 |
previous = temp; |
| 67 |
} |
| 68 |
stream.end(); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Implements the Sum RPC method. |
| 73 |
*/ |
| 74 |
function Sum(call, callback) { |
| 75 |
var sum = 0; |
| 76 |
call.on('data', function(data) { |
| 77 |
sum += parseInt(data.num); |
| 78 |
}); |
| 79 |
call.on('end', function() { |
| 80 |
callback(null, { |
| 81 |
num: sum |
| 82 |
}); |
| 83 |
}); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Implements the DivMany RPC method. |
| 88 |
*/ |
| 89 |
function DivMany(stream) { |
| 90 |
stream.on('data', function(div_args) { |
| 91 |
var divisor = div_args.divisor; |
| 92 |
var dividend = div_args.dividend; |
| 93 |
if (divisor == 0) { |
| 94 |
stream.emit('error', { |
| 95 |
code: grpc.status.INVALID_ARGUMENT, |
| 96 |
details: 'Cannot divide by zero' |
| 97 |
}); |
| 98 |
} else { |
| 99 |
stream.write({ |
| 100 |
quotient: Math.floor(dividend / divisor), |
| 101 |
remainder: dividend % divisor |
| 102 |
}); |
| 103 |
} |
| 104 |
}); |
| 105 |
stream.on('end', function() { |
| 106 |
stream.end(); |
| 107 |
}); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Starts an RPC server that receives requests for the Math service at the |
| 113 |
* sample server port |
| 114 |
*/ |
| 115 |
function main() { |
| 116 |
var server = new grpc.Server(); |
| 117 |
server.addService(math_proto.Math.service, { |
| 118 |
Div: Div, |
| 119 |
Fib: Fib, |
| 120 |
Sum: Sum, |
| 121 |
DivMany: DivMany, |
| 122 |
}); |
| 123 |
server.bind('0.0.0.0:50052', grpc.ServerCredentials.createInsecure()); |
| 124 |
var fs = require('fs'); |
| 125 |
var key_data = fs.readFileSync(__dirname + '/../data/server1.key'); |
| 126 |
var pem_data = fs.readFileSync(__dirname + '/../data/server1.pem'); |
| 127 |
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createSsl(null, [{private_key: key_data, |
| 128 |
cert_chain: pem_data}])); |
| 129 |
server.start(); |
| 130 |
} |
| 131 |
|
| 132 |
main(); |