diff options
Diffstat (limited to 'src/node/examples')
-rw-r--r-- | src/node/examples/math.proto | 41 | ||||
-rw-r--r-- | src/node/examples/math_server.js | 83 |
2 files changed, 42 insertions, 82 deletions
diff --git a/src/node/examples/math.proto b/src/node/examples/math.proto index 14eff5daaf..c49787ad54 100644 --- a/src/node/examples/math.proto +++ b/src/node/examples/math.proto @@ -1,15 +1,15 @@ -syntax = "proto2"; +syntax = "proto3"; package math; message DivArgs { - required int64 dividend = 1; - required int64 divisor = 2; + optional int64 dividend = 1; + optional int64 divisor = 2; } message DivReply { - required int64 quotient = 1; - required int64 remainder = 2; + optional int64 quotient = 1; + optional int64 remainder = 2; } message FibArgs { @@ -17,9 +17,34 @@ message FibArgs { } message Num { - required int64 num = 1; + optional int64 num = 1; } message FibReply { - required int64 count = 1; -}
\ No newline at end of file + optional int64 count = 1; +} + +service Math { + // Div divides args.dividend by args.divisor and returns the quotient and + // remainder. + rpc Div (DivArgs) returns (DivReply) { + } + + // DivMany accepts an arbitrary number of division args from the client stream + // and sends back the results in the reply stream. The stream continues until + // the client closes its end; the server does the same after sending all the + // replies. The stream ends immediately if either end aborts. + rpc DivMany (stream DivArgs) returns (stream DivReply) { + } + + // Fib generates numbers in the Fibonacci sequence. If args.limit > 0, Fib + // generates up to limit numbers; otherwise it continues until the call is + // canceled. Unlike Fib above, Fib has no final FibReply. + rpc Fib (FibArgs) returns (stream Num) { + } + + // Sum sums a stream of numbers, returning the final result once the stream + // is closed. + rpc Sum (stream Num) returns (Num) { + } +} diff --git a/src/node/examples/math_server.js b/src/node/examples/math_server.js index 87336b61e5..366513dc17 100644 --- a/src/node/examples/math_server.js +++ b/src/node/examples/math_server.js @@ -38,77 +38,10 @@ var util = require('util'); var Transform = require('stream').Transform; -var builder = ProtoBuf.loadProtoFile(__dirname + '/math.proto'); -var math = builder.build('math'); +var grpc = require('..'); +var math = grpc.load(__dirname + '/math.proto').math; -var makeConstructor = require('../surface_server.js').makeServerConstructor; - -/** - * Get a function that deserializes a specific type of protobuf. - * @param {function()} cls The constructor of the message type to deserialize - * @return {function(Buffer):cls} The deserialization function - */ -function deserializeCls(cls) { - /** - * Deserialize a buffer to a message object - * @param {Buffer} arg_buf The buffer to deserialize - * @return {cls} The resulting object - */ - return function deserialize(arg_buf) { - return cls.decode(arg_buf); - }; -} - -/** - * Get a function that serializes objects to a buffer by protobuf class. - * @param {function()} Cls The constructor of the message type to serialize - * @return {function(Cls):Buffer} The serialization function - */ -function serializeCls(Cls) { - /** - * Serialize an object to a Buffer - * @param {Object} arg The object to serialize - * @return {Buffer} The serialized object - */ - return function serialize(arg) { - return new Buffer(new Cls(arg).encode().toBuffer()); - }; -} - -/* This function call creates a server constructor for servers that that expose - * the four specified methods. This specifies how to serialize messages that the - * server sends and deserialize messages that the client sends, and whether the - * client or the server will send a stream of messages, for each method. This - * also specifies a prefix that will be added to method names when sending them - * on the wire. This function call and all of the preceding code in this file - * are intended to approximate what the generated code will look like for the - * math service */ -var Server = makeConstructor({ - Div: { - serialize: serializeCls(math.DivReply), - deserialize: deserializeCls(math.DivArgs), - client_stream: false, - server_stream: false - }, - Fib: { - serialize: serializeCls(math.Num), - deserialize: deserializeCls(math.FibArgs), - client_stream: false, - server_stream: true - }, - Sum: { - serialize: serializeCls(math.Num), - deserialize: deserializeCls(math.Num), - client_stream: true, - server_stream: false - }, - DivMany: { - serialize: serializeCls(math.DivReply), - deserialize: deserializeCls(math.DivArgs), - client_stream: true, - server_stream: true - } -}, '/Math/'); +var Server = grpc.buildServer([math.Math.service]); /** * Server function for division. Provides the /Math/DivMany and /Math/Div @@ -185,10 +118,12 @@ function mathDivMany(stream) { } var server = new Server({ - Div: mathDiv, - Fib: mathFib, - Sum: mathSum, - DivMany: mathDivMany + 'math.Math' : { + Div: mathDiv, + Fib: mathFib, + Sum: mathSum, + DivMany: mathDivMany + } }); if (require.main === module) { |