internal sync

This commit is contained in:
Stanley Cheung 2018-10-12 23:34:33 -07:00 committed by Stanley Cheung
parent c1b07a70d6
commit 4ca20a4f9c
9 changed files with 123 additions and 12 deletions

View File

@ -16,12 +16,12 @@
* *
*/ */
#include <algorithm>
#include <google/protobuf/compiler/code_generator.h> #include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/compiler/plugin.h> #include <google/protobuf/compiler/plugin.h>
#include <google/protobuf/descriptor.h> #include <google/protobuf/descriptor.h>
#include <google/protobuf/io/printer.h> #include <google/protobuf/io/printer.h>
#include <google/protobuf/io/zero_copy_stream.h> #include <google/protobuf/io/zero_copy_stream.h>
#include <algorithm>
using google::protobuf::Descriptor; using google::protobuf::Descriptor;
using google::protobuf::FieldDescriptor; using google::protobuf::FieldDescriptor;
@ -740,12 +740,14 @@ void PrintServerStreamingCall(Printer* printer, std::map<string, string> vars) {
" * @return {!grpc.web.ClientReadableStream<!proto.$out$>}\n" " * @return {!grpc.web.ClientReadableStream<!proto.$out$>}\n"
" * The XHR Node Readable Stream\n" " * The XHR Node Readable Stream\n"
" */\n" " */\n"
"proto.$package_dot$$service_name$$client_type$.prototype.$js_method_name$ =\n"); "proto.$package_dot$$service_name$$client_type$.prototype."
"$js_method_name$ =\n");
printer->Indent(); printer->Indent();
if (vars["client_type"] == "PromiseClient") { if (vars["client_type"] == "PromiseClient") {
printer->Print( printer->Print(
" function(request, metadata) {\n" " function(request, metadata) {\n"
"return this.delegateClient_.client_.serverStreaming(this.delegateClient_.hostname_ +\n"); "return this.delegateClient_.client_.serverStreaming("
"this.delegateClient_.hostname_ +\n");
} else { } else {
printer->Print( printer->Print(
" function(request, metadata) {\n" " function(request, metadata) {\n"

View File

@ -16,12 +16,16 @@ syntax = "proto3";
package grpc.gateway.testing; package grpc.gateway.testing;
message Empty {
}
message EchoRequest { message EchoRequest {
string message = 1; string message = 1;
} }
message EchoResponse { message EchoResponse {
string message = 1; string message = 1;
int32 message_count = 2;
} }
// Request type for server side streaming echo. // Request type for server side streaming echo.
@ -44,6 +48,18 @@ message ServerStreamingEchoResponse {
string message = 1; string message = 1;
} }
// Request type for client side streaming echo.
message ClientStreamingEchoRequest {
// A special value "" indicates that there's no further messages.
string message = 1;
}
// Response type for client side streaming echo.
message ClientStreamingEchoResponse {
// Total number of client messages that have been received.
int32 message_count = 1;
}
// A simple echo service. // A simple echo service.
service EchoService { service EchoService {
// One request followed by one response // One request followed by one response
@ -54,8 +70,34 @@ service EchoService {
rpc EchoAbort(EchoRequest) returns (EchoResponse) { rpc EchoAbort(EchoRequest) returns (EchoResponse) {
}; };
// One empty request, ZERO processing, followed by one empty response
// (minimum effort to do message serialization).
rpc NoOp(Empty) returns (Empty);
// One request followed by a sequence of responses (streamed download). // One request followed by a sequence of responses (streamed download).
// The server will return the same client message repeatedly. // The server will return the same client message repeatedly.
rpc ServerStreamingEcho(ServerStreamingEchoRequest) rpc ServerStreamingEcho(ServerStreamingEchoRequest)
returns (stream ServerStreamingEchoResponse); returns (stream ServerStreamingEchoResponse);
// One request followed by a sequence of responses (streamed download).
// The server abort directly.
rpc ServerStreamingEchoAbort(ServerStreamingEchoRequest)
returns (stream ServerStreamingEchoResponse) {
}
// A sequence of requests followed by one response (streamed upload).
// The server returns the total number of messages as the result.
rpc ClientStreamingEcho(stream ClientStreamingEchoRequest)
returns (ClientStreamingEchoResponse);
// A sequence of requests with each message echoed by the server immediately.
// The server returns the same client messages in order.
// E.g. this is how the speech API works.
rpc FullDuplexEcho(stream EchoRequest) returns (stream EchoResponse);
// A sequence of requests followed by a sequence of responses.
// The server buffers all the client messages and then returns the same
// client messages one by one after the client half-closes the stream.
// This is how an image recognition API may work.
rpc HalfDuplexEcho(stream EchoRequest) returns (stream EchoResponse);
} }

View File

@ -30,6 +30,7 @@ using grpc::Status;
using grpc::gateway::testing::EchoRequest; using grpc::gateway::testing::EchoRequest;
using grpc::gateway::testing::EchoResponse; using grpc::gateway::testing::EchoResponse;
using grpc::gateway::testing::EchoService; using grpc::gateway::testing::EchoService;
using grpc::gateway::testing::Empty;
using grpc::gateway::testing::ServerStreamingEchoRequest; using grpc::gateway::testing::ServerStreamingEchoRequest;
using grpc::gateway::testing::ServerStreamingEchoResponse; using grpc::gateway::testing::ServerStreamingEchoResponse;
@ -67,6 +68,12 @@ Status EchoServiceImpl::EchoAbort(ServerContext* context,
"Aborted from server side."); "Aborted from server side.");
} }
Status EchoServiceImpl::NoOp(ServerContext* context, const Empty* request,
Empty* response) {
CopyClientMetadataToResponse(context);
return Status::OK;
}
Status EchoServiceImpl::ServerStreamingEcho( Status EchoServiceImpl::ServerStreamingEcho(
ServerContext* context, const ServerStreamingEchoRequest* request, ServerContext* context, const ServerStreamingEchoRequest* request,
ServerWriter<ServerStreamingEchoResponse>* writer) { ServerWriter<ServerStreamingEchoResponse>* writer) {
@ -82,3 +89,14 @@ Status EchoServiceImpl::ServerStreamingEcho(
} }
return Status::OK; return Status::OK;
} }
Status EchoServiceImpl::ServerStreamingEchoAbort(
ServerContext* context, const ServerStreamingEchoRequest* request,
ServerWriter<ServerStreamingEchoResponse>* writer) {
CopyClientMetadataToResponse(context);
ServerStreamingEchoResponse response;
response.set_message(request->message());
writer->Write(response);
return Status(grpc::StatusCode::ABORTED,
"Aborted from server side.");
}

View File

@ -40,11 +40,20 @@ class EchoServiceImpl final :
grpc::ServerContext* context, grpc::ServerContext* context,
const grpc::gateway::testing::EchoRequest* request, const grpc::gateway::testing::EchoRequest* request,
grpc::gateway::testing::EchoResponse* response) override; grpc::gateway::testing::EchoResponse* response) override;
grpc::Status NoOp(
grpc::ServerContext* context,
const grpc::gateway::testing::Empty* request,
grpc::gateway::testing::Empty* response) override;
grpc::Status ServerStreamingEcho( grpc::Status ServerStreamingEcho(
grpc::ServerContext* context, grpc::ServerContext* context,
const grpc::gateway::testing::ServerStreamingEchoRequest* request, const grpc::gateway::testing::ServerStreamingEchoRequest* request,
grpc::ServerWriter< grpc::ServerWriter<
grpc::gateway::testing::ServerStreamingEchoResponse>* writer) override; grpc::gateway::testing::ServerStreamingEchoResponse>* writer) override;
grpc::Status ServerStreamingEchoAbort(
grpc::ServerContext* context,
const grpc::gateway::testing::ServerStreamingEchoRequest* request,
grpc::ServerWriter<
grpc::gateway::testing::ServerStreamingEchoResponse>* writer) override;
}; };
#endif // NET_GRPC_GATEWAY_EXAMPLES_ECHO_ECHO_SERVICE_IMPL_H_ #endif // NET_GRPC_GATEWAY_EXAMPLES_ECHO_ECHO_SERVICE_IMPL_H_

View File

@ -1,6 +1,6 @@
/* /*
* *
* Copyright 2015 gRPC authors. * Copyright 2018 Google LLC
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,7 +33,10 @@ var packageDefinition = protoLoader.loadSync(
var protoDescriptor = grpc.loadPackageDefinition(packageDefinition); var protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
var echo = protoDescriptor.grpc.gateway.testing; var echo = protoDescriptor.grpc.gateway.testing;
/**
* @param {!Object} call
* @return {!Object} metadata
*/
function copyMetadata(call) { function copyMetadata(call) {
var metadata = call.metadata.getMap(); var metadata = call.metadata.getMap();
var response_metadata = new grpc.Metadata(); var response_metadata = new grpc.Metadata();
@ -43,12 +46,20 @@ function copyMetadata(call) {
return response_metadata; return response_metadata;
} }
/**
* @param {!Object} call
* @param {function():?} callback
*/
function doEcho(call, callback) { function doEcho(call, callback) {
callback(null, { callback(null, {
message: call.request.message message: call.request.message
}, copyMetadata(call)); }, copyMetadata(call));
} }
/**
* @param {!Object} call
* @param {function():?} callback
*/
function doEchoAbort(call, callback) { function doEchoAbort(call, callback) {
callback({ callback({
code: grpc.status.ABORTED, code: grpc.status.ABORTED,
@ -56,6 +67,9 @@ function doEchoAbort(call, callback) {
}); });
} }
/**
* @param {!Object} call
*/
function doServerStreamingEcho(call) { function doServerStreamingEcho(call) {
var senders = []; var senders = [];
function sender(message, interval) { function sender(message, interval) {
@ -77,7 +91,7 @@ function doServerStreamingEcho(call) {
/** /**
* Get a new server with the handler functions in this file bound to the methods * Get a new server with the handler functions in this file bound to the methods
* it serves. * it serves.
* @return {Server} The new server object * @return {!Server} The new server object
*/ */
function getServer() { function getServer() {
var server = new grpc.Server(); var server = new grpc.Server();

View File

@ -254,9 +254,9 @@ in the current directory:
These are also the 2 files that our `client.js` file imported earlier in the These are also the 2 files that our `client.js` file imported earlier in the
example. example.
## Compile the Client Javascript Code ## Compile the Client JavaScript Code
Next, we need to compile the client side Javascript code into something that Next, we need to compile the client side JavaScript code into something that
can be consumed by the browser. can be consumed by the browser.
```sh ```sh

View File

@ -17,8 +17,11 @@ syntax = "proto3";
package helloworld; package helloworld;
service Greeter { service Greeter {
// unary call
rpc SayHello (HelloRequest) returns (HelloReply); rpc SayHello (HelloRequest) returns (HelloReply);
// server streaming call
rpc SayRepeatHello (RepeatHelloRequest) returns (stream HelloReply); rpc SayRepeatHello (RepeatHelloRequest) returns (stream HelloReply);
// unary call - response after a length delay
rpc SayHelloAfterDelay (HelloRequest) returns (HelloReply); rpc SayHelloAfterDelay (HelloRequest) returns (HelloReply);
} }

View File

@ -11,11 +11,14 @@ var packageDefinition = protoLoader.loadSync(
defaults: true, defaults: true,
oneofs: true oneofs: true
}); });
var protoDescriptor = grpc.loadPackageDefinition(packageDefinition) var protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
var helloworld = protoDescriptor.helloworld; var helloworld = protoDescriptor.helloworld;
var client = new helloworld.Greeter('localhost:9090', var client = new helloworld.Greeter('localhost:9090',
grpc.credentials.createInsecure()); grpc.credentials.createInsecure());
/**
* @param {function():?} callback
*/
function runSayHello(callback) { function runSayHello(callback) {
client.sayHello({name: 'John'}, {}, (err, response) => { client.sayHello({name: 'John'}, {}, (err, response) => {
console.log(response.message); console.log(response.message);
@ -23,6 +26,9 @@ function runSayHello(callback) {
}); });
} }
/**
* @param {function():?} callback
*/
function runSayRepeatHello(callback) { function runSayRepeatHello(callback) {
var stream = client.sayRepeatHello({name: 'John', count: 5}, {}); var stream = client.sayRepeatHello({name: 'John', count: 5}, {});
stream.on('data', (response) => { stream.on('data', (response) => {
@ -33,6 +39,9 @@ function runSayRepeatHello(callback) {
}); });
} }
/**
* @param {function():?} callback
*/
function runSayHelloAfterDelay(callback) { function runSayHelloAfterDelay(callback) {
var deadline = new Date(); var deadline = new Date();
deadline.setSeconds(deadline.getSeconds() + 1); deadline.setSeconds(deadline.getSeconds() + 1);

View File

@ -33,10 +33,17 @@ var packageDefinition = protoLoader.loadSync(
var protoDescriptor = grpc.loadPackageDefinition(packageDefinition); var protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
var helloworld = protoDescriptor.helloworld; var helloworld = protoDescriptor.helloworld;
/**
* @param {!Object} call
* @param {function():?} callback
*/
function doSayHello(call, callback) { function doSayHello(call, callback) {
callback(null, {message: 'Hello! '+ call.request.name}); callback(null, {message: 'Hello! '+ call.request.name});
} }
/**
* @param {!Object} call
*/
function doSayRepeatHello(call) { function doSayRepeatHello(call) {
var senders = []; var senders = [];
function sender(name) { function sender(name) {
@ -55,6 +62,10 @@ function doSayRepeatHello(call) {
}); });
} }
/**
* @param {!Object} call
* @param {function():?} callback
*/
function doSayHelloAfterDelay(call, callback) { function doSayHelloAfterDelay(call, callback) {
function dummy() { function dummy() {
return (cb) => { return (cb) => {
@ -68,6 +79,9 @@ function doSayHelloAfterDelay(call, callback) {
}); });
} }
/**
* @return {!Object} gRPC server
*/
function getServer() { function getServer() {
var server = new grpc.Server(); var server = new grpc.Server();
server.addProtoService(helloworld.Greeter.service, { server.addProtoService(helloworld.Greeter.service, {