commit
225d31fd40
65 changed files with 3504 additions and 443 deletions
@ -0,0 +1,520 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2014, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
using namespace std; |
||||
|
||||
#include "src/compiler/go_generator.h" |
||||
|
||||
#include <cctype> |
||||
|
||||
#include <google/protobuf/io/printer.h> |
||||
#include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
||||
#include <google/protobuf/descriptor.pb.h> |
||||
#include <google/protobuf/descriptor.h> |
||||
|
||||
namespace grpc_go_generator { |
||||
|
||||
bool NoStreaming(const google::protobuf::MethodDescriptor* method) { |
||||
return !method->client_streaming() && |
||||
!method->server_streaming(); |
||||
} |
||||
|
||||
bool ClientOnlyStreaming(const google::protobuf::MethodDescriptor* method) { |
||||
return method->client_streaming() && |
||||
!method->server_streaming(); |
||||
} |
||||
|
||||
bool ServerOnlyStreaming(const google::protobuf::MethodDescriptor* method) { |
||||
return !method->client_streaming() && |
||||
method->server_streaming(); |
||||
} |
||||
|
||||
bool BidiStreaming(const google::protobuf::MethodDescriptor* method) { |
||||
return method->client_streaming() && |
||||
method->server_streaming(); |
||||
} |
||||
|
||||
bool HasClientOnlyStreaming(const google::protobuf::FileDescriptor* file) { |
||||
for (int i = 0; i < file->service_count(); i++) { |
||||
for (int j = 0; j < file->service(i)->method_count(); j++) { |
||||
if (ClientOnlyStreaming(file->service(i)->method(j))) { |
||||
return true; |
||||
} |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
string LowerCaseService(const string& service) { |
||||
string ret = service; |
||||
if (!ret.empty() && ret[0] >= 'A' && ret[0] <= 'Z') { |
||||
ret[0] = ret[0] - 'A' + 'a'; |
||||
} |
||||
return ret; |
||||
} |
||||
|
||||
void PrintClientMethodDef(google::protobuf::io::Printer* printer, |
||||
const google::protobuf::MethodDescriptor* method, |
||||
map<string, string>* vars) { |
||||
(*vars)["Method"] = method->name(); |
||||
(*vars)["Request"] = method->input_type()->name(); |
||||
(*vars)["Response"] = method->output_type()->name(); |
||||
if (NoStreaming(method)) { |
||||
printer->Print(*vars, |
||||
"\t$Method$(ctx context.Context, in *$Request$, opts ...rpc.CallOption) " |
||||
"(*$Response$, error)\n"); |
||||
} else if (BidiStreaming(method)) { |
||||
printer->Print(*vars, |
||||
"\t$Method$(ctx context.Context, opts ...rpc.CallOption) " |
||||
"($Service$_$Method$Client, error)\n"); |
||||
} else if (ServerOnlyStreaming(method)) { |
||||
printer->Print(*vars, |
||||
"\t$Method$(ctx context.Context, m *$Request$, opts ...rpc.CallOption) " |
||||
"($Service$_$Method$Client, error)\n"); |
||||
} else if (ClientOnlyStreaming(method)) { |
||||
printer->Print(*vars, |
||||
"\t$Method$(ctx context.Context, opts ...rpc.CallOption) " |
||||
"($Service$_$Method$Client, error)\n"); |
||||
} |
||||
} |
||||
|
||||
void PrintClientMethodImpl(google::protobuf::io::Printer* printer, |
||||
const google::protobuf::MethodDescriptor* method, |
||||
map<string, string>* vars) { |
||||
(*vars)["Method"] = method->name(); |
||||
(*vars)["Request"] = method->input_type()->name(); |
||||
(*vars)["Response"] = method->output_type()->name(); |
||||
|
||||
if (NoStreaming(method)) { |
||||
printer->Print(*vars, |
||||
"func (c *$ServiceStruct$Client) $Method$(ctx context.Context, " |
||||
"in *$Request$, opts ...rpc.CallOption) (*$Response$, error) {\n"); |
||||
printer->Print(*vars, |
||||
"\tout := new($Response$)\n"); |
||||
printer->Print(*vars, |
||||
"\terr := rpc.Invoke(ctx, \"/$Package$$Service$/$Method$\", " |
||||
"in, out, c.cc, opts...)\n"); |
||||
printer->Print("\tif err != nil {\n"); |
||||
printer->Print("\t\treturn nil, err\n"); |
||||
printer->Print("\t}\n"); |
||||
printer->Print("\treturn out, nil\n"); |
||||
printer->Print("}\n\n"); |
||||
} else if (BidiStreaming(method)) { |
||||
printer->Print( |
||||
*vars, |
||||
"func (c *$ServiceStruct$Client) $Method$(ctx context.Context, opts " |
||||
"...rpc.CallOption) ($Service$_$Method$Client, error) {\n" |
||||
"\tstream, err := rpc.NewClientStream(ctx, c.cc, " |
||||
"\"/$Package$$Service$/$Method$\", opts...)\n" |
||||
"\tif err != nil {\n" |
||||
"\t\treturn nil, err\n" |
||||
"\t}\n" |
||||
"\treturn &$ServiceStruct$$Method$Client{stream}, nil\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"type $Service$_$Method$Client interface {\n" |
||||
"\tSend(*$Request$) error\n" |
||||
"\tRecv() (*$Response$, error)\n" |
||||
"\trpc.ClientStream\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"type $ServiceStruct$$Method$Client struct {\n" |
||||
"\trpc.ClientStream\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"func (x *$ServiceStruct$$Method$Client) Send(m *$Request$) error {\n" |
||||
"\treturn x.ClientStream.SendProto(m)\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"func (x *$ServiceStruct$$Method$Client) Recv() (*$Response$, error) " |
||||
"{\n" |
||||
"\tm := new($Response$)\n" |
||||
"\tif err := x.ClientStream.RecvProto(m); err != nil {\n" |
||||
"\t\treturn nil, err\n" |
||||
"\t}\n" |
||||
"\treturn m, nil\n" |
||||
"}\n\n"); |
||||
} else if (ServerOnlyStreaming(method)) { |
||||
printer->Print( |
||||
*vars, |
||||
"func (c *$ServiceStruct$Client) $Method$(ctx context.Context, m " |
||||
"*$Request$, " |
||||
"opts ...rpc.CallOption) ($Service$_$Method$Client, error) {\n" |
||||
"\tstream, err := rpc.NewClientStream(ctx, c.cc, " |
||||
"\"/$Package$$Service$/$Method$\", opts...)\n" |
||||
"\tif err != nil {\n" |
||||
"\t\treturn nil, err\n" |
||||
"\t}\n" |
||||
"\tx := &$ServiceStruct$$Method$Client{stream}\n" |
||||
"\tif err := x.ClientStream.SendProto(m); err != nil {\n" |
||||
"\t\treturn nil, err\n" |
||||
"\t}\n" |
||||
"\tif err := x.ClientStream.CloseSend(); err != nil {\n" |
||||
"\t\treturn nil, err\n" |
||||
"\t}\n" |
||||
"\treturn x, nil\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"type $Service$_$Method$Client interface {\n" |
||||
"\tRecv() (*$Response$, error)\n" |
||||
"\trpc.ClientStream\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"type $ServiceStruct$$Method$Client struct {\n" |
||||
"\trpc.ClientStream\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"func (x *$ServiceStruct$$Method$Client) Recv() (*$Response$, error) " |
||||
"{\n" |
||||
"\tm := new($Response$)\n" |
||||
"\tif err := x.ClientStream.RecvProto(m); err != nil {\n" |
||||
"\t\treturn nil, err\n" |
||||
"\t}\n" |
||||
"\treturn m, nil\n" |
||||
"}\n\n"); |
||||
} else if (ClientOnlyStreaming(method)) { |
||||
printer->Print( |
||||
*vars, |
||||
"func (c *$ServiceStruct$Client) $Method$(ctx context.Context, opts " |
||||
"...rpc.CallOption) ($Service$_$Method$Client, error) {\n" |
||||
"\tstream, err := rpc.NewClientStream(ctx, c.cc, " |
||||
"\"/$Package$$Service$/$Method$\", opts...)\n" |
||||
"\tif err != nil {\n" |
||||
"\t\treturn nil, err\n" |
||||
"\t}\n" |
||||
"\treturn &$ServiceStruct$$Method$Client{stream}, nil\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"type $Service$_$Method$Client interface {\n" |
||||
"\tSend(*$Request$) error\n" |
||||
"\tCloseAndRecv() (*$Response$, error)\n" |
||||
"\trpc.ClientStream\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"type $ServiceStruct$$Method$Client struct {\n" |
||||
"\trpc.ClientStream\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"func (x *$ServiceStruct$$Method$Client) Send(m *$Request$) error {\n" |
||||
"\treturn x.ClientStream.SendProto(m)\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"func (x *$ServiceStruct$$Method$Client) CloseAndRecv() (*$Response$, " |
||||
"error) {\n" |
||||
"\tif err := x.ClientStream.CloseSend(); err != nil {\n" |
||||
"\t\treturn nil, err\n" |
||||
"\t}\n" |
||||
"\tm := new($Response$)\n" |
||||
"\tif err := x.ClientStream.RecvProto(m); err != nil {\n" |
||||
"\t\treturn nil, err\n" |
||||
"\t}\n" |
||||
"\t// Read EOF.\n" |
||||
"\tif err := x.ClientStream.RecvProto(m); err == io.EOF {\n" |
||||
"\t\treturn m, io.EOF\n" |
||||
"\t}\n" |
||||
"\t// gRPC protocol violation.\n" |
||||
"\treturn m, fmt.Errorf(\"Violate gRPC client streaming protocol: no " |
||||
"EOF after the response.\")\n" |
||||
"}\n\n"); |
||||
} |
||||
} |
||||
|
||||
void PrintClient(google::protobuf::io::Printer* printer, |
||||
const google::protobuf::ServiceDescriptor* service, |
||||
map<string, string>* vars) { |
||||
(*vars)["Service"] = service->name(); |
||||
(*vars)["ServiceStruct"] = LowerCaseService(service->name()); |
||||
printer->Print(*vars, "type $Service$Client interface {\n"); |
||||
for (int i = 0; i < service->method_count(); ++i) { |
||||
PrintClientMethodDef(printer, service->method(i), vars); |
||||
} |
||||
printer->Print("}\n\n"); |
||||
|
||||
printer->Print(*vars, |
||||
"type $ServiceStruct$Client struct {\n" |
||||
"\tcc *rpc.ClientConn\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"func New$Service$Client(cc *rpc.ClientConn) $Service$Client {\n" |
||||
"\treturn &$ServiceStruct$Client{cc}\n" |
||||
"}\n\n"); |
||||
for (int i = 0; i < service->method_count(); ++i) { |
||||
PrintClientMethodImpl(printer, service->method(i), vars); |
||||
} |
||||
} |
||||
|
||||
void PrintServerMethodDef(google::protobuf::io::Printer* printer, |
||||
const google::protobuf::MethodDescriptor* method, |
||||
map<string, string>* vars) { |
||||
(*vars)["Method"] = method->name(); |
||||
(*vars)["Request"] = method->input_type()->name(); |
||||
(*vars)["Response"] = method->output_type()->name(); |
||||
if (NoStreaming(method)) { |
||||
printer->Print(*vars, |
||||
"\t$Method$(context.Context, *$Request$) (*$Response$, error)\n"); |
||||
} else if (BidiStreaming(method)) { |
||||
printer->Print(*vars, |
||||
"\t$Method$($Service$_$Method$Server) error\n"); |
||||
} else if (ServerOnlyStreaming(method)) { |
||||
printer->Print(*vars, |
||||
"\t$Method$(*$Request$, $Service$_$Method$Server) error\n"); |
||||
} else if (ClientOnlyStreaming(method)) { |
||||
printer->Print(*vars, |
||||
"\t$Method$($Service$_$Method$Server) error\n"); |
||||
} |
||||
} |
||||
|
||||
void PrintServerHandler(google::protobuf::io::Printer* printer, |
||||
const google::protobuf::MethodDescriptor* method, |
||||
map<string, string>* vars) { |
||||
(*vars)["Method"] = method->name(); |
||||
(*vars)["Request"] = method->input_type()->name(); |
||||
(*vars)["Response"] = method->output_type()->name(); |
||||
if (NoStreaming(method)) { |
||||
printer->Print(*vars, |
||||
"func _$Service$_$Method$_Handler(srv interface{}, ctx context.Context," |
||||
" buf []byte) (proto.Message, error) {\n"); |
||||
printer->Print(*vars, |
||||
"\tin := new($Request$)\n"); |
||||
printer->Print("\tif err := proto.Unmarshal(buf, in); err != nil {\n"); |
||||
printer->Print("\t\treturn nil, err\n"); |
||||
printer->Print("\t}\n"); |
||||
printer->Print(*vars, |
||||
"\tout, err := srv.($Service$Server).$Method$(ctx, in)\n"); |
||||
printer->Print("\tif err != nil {\n"); |
||||
printer->Print("\t\treturn nil, err\n"); |
||||
printer->Print("\t}\n"); |
||||
printer->Print("\treturn out, nil\n"); |
||||
printer->Print("}\n\n"); |
||||
} else if (BidiStreaming(method)) { |
||||
printer->Print(*vars, |
||||
"func _$Service$_$Method$_Handler(srv interface{}, stream rpc.Stream) " |
||||
"error {\n" |
||||
"\treturn srv.($Service$Server).$Method$(&$ServiceStruct$$Method$Server" |
||||
"{stream})\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"type $Service$_$Method$Server interface {\n" |
||||
"\tSend(*$Response$) error\n" |
||||
"\tRecv() (*$Request$, error)\n" |
||||
"\trpc.Stream\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"type $ServiceStruct$$Method$Server struct {\n" |
||||
"\trpc.Stream\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"func (x *$ServiceStruct$$Method$Server) Send(m *$Response$) error {\n" |
||||
"\treturn x.Stream.SendProto(m)\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"func (x *$ServiceStruct$$Method$Server) Recv() (*$Request$, error) " |
||||
"{\n" |
||||
"\tm := new($Request$)\n" |
||||
"\tif err := x.Stream.RecvProto(m); err != nil {\n" |
||||
"\t\treturn nil, err\n" |
||||
"\t}\n" |
||||
"\treturn m, nil\n" |
||||
"}\n\n"); |
||||
} else if (ServerOnlyStreaming(method)) { |
||||
printer->Print(*vars, |
||||
"func _$Service$_$Method$_Handler(srv interface{}, stream rpc.Stream) " |
||||
"error {\n" |
||||
"\tm := new($Request$)\n" |
||||
"\tif err := stream.RecvProto(m); err != nil {\n" |
||||
"\t\treturn err\n" |
||||
"\t}\n" |
||||
"\treturn srv.($Service$Server).$Method$(m, " |
||||
"&$ServiceStruct$$Method$Server{stream})\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"type $Service$_$Method$Server interface {\n" |
||||
"\tSend(*$Response$) error\n" |
||||
"\trpc.Stream\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"type $ServiceStruct$$Method$Server struct {\n" |
||||
"\trpc.Stream\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"func (x *$ServiceStruct$$Method$Server) Send(m *$Response$) error {\n" |
||||
"\treturn x.Stream.SendProto(m)\n" |
||||
"}\n\n"); |
||||
} else if (ClientOnlyStreaming(method)) { |
||||
printer->Print(*vars, |
||||
"func _$Service$_$Method$_Handler(srv interface{}, stream rpc.Stream) " |
||||
"error {\n" |
||||
"\treturn srv.($Service$Server).$Method$(&$ServiceStruct$$Method$Server" |
||||
"{stream})\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"type $Service$_$Method$Server interface {\n" |
||||
"\tSendAndClose(*$Response$) error\n" |
||||
"\tRecv() (*$Request$, error)\n" |
||||
"\trpc.Stream\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"type $ServiceStruct$$Method$Server struct {\n" |
||||
"\trpc.Stream\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"func (x *$ServiceStruct$$Method$Server) SendAndClose(m *$Response$) " |
||||
"error {\n" |
||||
"\tif err := x.Stream.SendProto(m); err != nil {\n" |
||||
"\t\treturn err\n" |
||||
"\t}\n" |
||||
"\treturn nil\n" |
||||
"}\n\n"); |
||||
printer->Print(*vars, |
||||
"func (x *$ServiceStruct$$Method$Server) Recv() (*$Request$, error) {\n" |
||||
"\tm := new($Request$)\n" |
||||
"\tif err := x.Stream.RecvProto(m); err != nil {\n" |
||||
"\t\treturn nil, err\n" |
||||
"\t}\n" |
||||
"\treturn m, nil\n" |
||||
"}\n\n"); |
||||
} |
||||
} |
||||
|
||||
void PrintServerMethodDesc(google::protobuf::io::Printer* printer, |
||||
const google::protobuf::MethodDescriptor* method, |
||||
map<string, string>* vars) { |
||||
(*vars)["Method"] = method->name(); |
||||
printer->Print("\t\t{\n"); |
||||
printer->Print(*vars, |
||||
"\t\t\tMethodName:\t\"$Method$\",\n"); |
||||
printer->Print(*vars, |
||||
"\t\t\tHandler:\t_$Service$_$Method$_Handler,\n"); |
||||
printer->Print("\t\t},\n"); |
||||
} |
||||
|
||||
void PrintServerStreamingMethodDesc(google::protobuf::io::Printer* printer, |
||||
const google::protobuf::MethodDescriptor* method, |
||||
map<string, string>* vars) { |
||||
(*vars)["Method"] = method->name(); |
||||
printer->Print("\t\t{\n"); |
||||
printer->Print(*vars, |
||||
"\t\t\tStreamName:\t\"$Method$\",\n"); |
||||
printer->Print(*vars, |
||||
"\t\t\tHandler:\t_$Service$_$Method$_Handler,\n"); |
||||
printer->Print("\t\t},\n"); |
||||
} |
||||
|
||||
void PrintServer(google::protobuf::io::Printer* printer, |
||||
const google::protobuf::ServiceDescriptor* service, |
||||
map<string, string>* vars) { |
||||
(*vars)["Service"] = service->name(); |
||||
printer->Print(*vars, "type $Service$Server interface {\n"); |
||||
for (int i = 0; i < service->method_count(); ++i) { |
||||
PrintServerMethodDef(printer, service->method(i), vars); |
||||
} |
||||
printer->Print("}\n\n"); |
||||
|
||||
printer->Print(*vars, |
||||
"func RegisterService(s *rpc.Server, srv $Service$Server) {\n" |
||||
"\ts.RegisterService(&_$Service$_serviceDesc, srv)\n" |
||||
"}\n\n"); |
||||
|
||||
for (int i = 0; i < service->method_count(); ++i) { |
||||
PrintServerHandler(printer, service->method(i), vars); |
||||
} |
||||
|
||||
printer->Print(*vars, |
||||
"var _$Service$_serviceDesc = rpc.ServiceDesc{\n" |
||||
"\tServiceName: \"$Package$$Service$\",\n" |
||||
"\tHandlerType: (*$Service$Server)(nil),\n" |
||||
"\tMethods: []rpc.MethodDesc{\n"); |
||||
for (int i = 0; i < service->method_count(); ++i) { |
||||
if (NoStreaming(service->method(i))) { |
||||
PrintServerMethodDesc(printer, service->method(i), vars); |
||||
} |
||||
} |
||||
printer->Print("\t},\n"); |
||||
|
||||
printer->Print("\tStreams: []rpc.StreamDesc{\n"); |
||||
for (int i = 0; i < service->method_count(); ++i) { |
||||
if (!NoStreaming(service->method(i))) { |
||||
PrintServerStreamingMethodDesc(printer, service->method(i), vars); |
||||
} |
||||
} |
||||
printer->Print("\t},\n" |
||||
"}\n\n"); |
||||
} |
||||
|
||||
std::string BadToUnderscore(std::string str) { |
||||
for (unsigned i = 0; i < str.size(); ++i) { |
||||
if (!std::isalnum(str[i])) { |
||||
str[i] = '_'; |
||||
} |
||||
} |
||||
return str; |
||||
} |
||||
|
||||
string GetServices(const google::protobuf::FileDescriptor* file) { |
||||
string output; |
||||
google::protobuf::io::StringOutputStream output_stream(&output); |
||||
google::protobuf::io::Printer printer(&output_stream, '$'); |
||||
map<string, string> vars; |
||||
|
||||
string package_name = !file->options().go_package().empty() |
||||
? file->options().go_package() |
||||
: file->package(); |
||||
vars["PackageName"] = BadToUnderscore(package_name); |
||||
printer.Print(vars, "package $PackageName$\n\n"); |
||||
printer.Print("import (\n"); |
||||
if (HasClientOnlyStreaming(file)) { |
||||
printer.Print("\t\"fmt\"\n" |
||||
"\t\"io\"\n"); |
||||
} |
||||
printer.Print( |
||||
"\t\"google/net/grpc/go/rpc\"\n" |
||||
"\tcontext \"google/third_party/golang/go_net/context/context\"\n" |
||||
"\tproto \"google/net/proto2/go/proto\"\n" |
||||
")\n\n"); |
||||
|
||||
// $Package$ is used to fully qualify method names.
|
||||
vars["Package"] = file->package(); |
||||
if (!file->package().empty()) { |
||||
vars["Package"].append("."); |
||||
} |
||||
|
||||
for (int i = 0; i < file->service_count(); ++i) { |
||||
PrintClient(&printer, file->service(0), &vars); |
||||
printer.Print("\n"); |
||||
PrintServer(&printer, file->service(0), &vars); |
||||
printer.Print("\n"); |
||||
} |
||||
return output; |
||||
} |
||||
|
||||
} // namespace grpc_go_generator
|
@ -0,0 +1,51 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2014, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef NET_GRPC_COMPILER_GO_GENERATOR_H_ |
||||
#define NET_GRPC_COMPILER_GO_GENERATOR_H_ |
||||
|
||||
#include <string> |
||||
|
||||
namespace google { |
||||
namespace protobuf { |
||||
class FileDescriptor; |
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
|
||||
namespace grpc_go_generator { |
||||
|
||||
string GetServices(const google::protobuf::FileDescriptor* file); |
||||
|
||||
} // namespace grpc_go_generator
|
||||
|
||||
#endif // NET_GRPC_COMPILER_GO_GENERATOR_H_
|
@ -0,0 +1,83 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2014, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
// Generates go gRPC service interface out of Protobuf IDL.
|
||||
//
|
||||
// This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto
|
||||
// and net/proto2/compiler/public/plugin.h for more information on plugins.
|
||||
|
||||
#include <fstream> |
||||
#include <memory> |
||||
|
||||
using namespace std; |
||||
|
||||
#include "src/compiler/go_generator.h" |
||||
#include <google/protobuf/compiler/code_generator.h> |
||||
#include <google/protobuf/compiler/plugin.h> |
||||
#include <google/protobuf/io/coded_stream.h> |
||||
#include <google/protobuf/io/zero_copy_stream.h> |
||||
#include <google/protobuf/descriptor.h> |
||||
|
||||
class GoGrpcGenerator : public google::protobuf::compiler::CodeGenerator { |
||||
public: |
||||
GoGrpcGenerator() {} |
||||
virtual ~GoGrpcGenerator() {} |
||||
|
||||
virtual bool Generate(const google::protobuf::FileDescriptor* file, |
||||
const string& parameter, |
||||
google::protobuf::compiler::GeneratorContext* context, |
||||
string* error) const { |
||||
// Get output file name.
|
||||
string file_name; |
||||
if (file->name().size() > 6 && |
||||
file->name().find_last_of(".proto") == file->name().size() - 1) { |
||||
file_name = file->name().substr(0, file->name().size() - 6) + |
||||
"_grpc.pb.go"; |
||||
} else { |
||||
*error = "Invalid proto file name. Proto file must end with .proto"; |
||||
return false; |
||||
} |
||||
|
||||
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output( |
||||
context->Open(file_name)); |
||||
google::protobuf::io::CodedOutputStream coded_out(output.get()); |
||||
string code = grpc_go_generator::GetServices(file); |
||||
coded_out.WriteRaw(code.data(), code.size()); |
||||
return true; |
||||
} |
||||
}; |
||||
|
||||
int main(int argc, char* argv[]) { |
||||
GoGrpcGenerator generator; |
||||
return google::protobuf::compiler::PluginMain(argc, argv, &generator); |
||||
} |
@ -0,0 +1,59 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2014, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef __GRPC_INTERNAL_STATISTICS_CENSUS_TRACING_H_ |
||||
#define __GRPC_INTERNAL_STATISTICS_CENSUS_TRACING_H_ |
||||
|
||||
/* Opaque structure for trace object */ |
||||
typedef struct trace_obj trace_obj; |
||||
|
||||
/* Initializes trace store. This function is thread safe. */ |
||||
void census_tracing_init(); |
||||
|
||||
/* Shutsdown trace store. This function is thread safe. */ |
||||
void census_tracing_shutdown(); |
||||
|
||||
/* Gets trace obj corresponding to the input op_id. Returns NULL if trace store
|
||||
is not initialized or trace obj is not found. Requires trace store being |
||||
locked before calling this function. */ |
||||
trace_obj* census_get_trace_obj_locked(census_op_id op_id); |
||||
|
||||
/* The following two functions acquire and release the trace store global lock.
|
||||
They are for census internal use only. */ |
||||
void census_internal_lock_trace_store(); |
||||
void census_internal_unlock_trace_store(); |
||||
|
||||
/* Gets method tag name associated with the input trace object. */ |
||||
const char* census_get_trace_method_name(const trace_obj* trace); |
||||
|
||||
#endif /* __GRPC_INTERNAL_STATISTICS_CENSUS_TRACING_H_ */ |
@ -0,0 +1,176 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2014, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include <unistd.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/time.h> |
||||
#include <grpc/support/useful.h> |
||||
#include "test/core/end2end/cq_verifier.h" |
||||
|
||||
static gpr_timespec n_seconds_time(int n) { |
||||
return gpr_time_add(gpr_now(), gpr_time_from_micros(GPR_US_PER_SEC * n)); |
||||
} |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char *test_name, |
||||
grpc_channel_args *client_args, |
||||
grpc_channel_args *server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "%s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_client(&f, client_args); |
||||
config.init_server(&f, server_args); |
||||
return f; |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture *f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown(f->server); |
||||
grpc_server_destroy(f->server); |
||||
f->server = NULL; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture *f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = NULL; |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue *cq) { |
||||
grpc_event *ev; |
||||
grpc_completion_type type; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, n_seconds_time(5)); |
||||
GPR_ASSERT(ev); |
||||
type = ev->type; |
||||
grpc_event_finish(ev); |
||||
} while (type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture *f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->server_cq); |
||||
drain_cq(f->server_cq); |
||||
grpc_completion_queue_destroy(f->server_cq); |
||||
grpc_completion_queue_shutdown(f->client_cq); |
||||
drain_cq(f->client_cq); |
||||
grpc_completion_queue_destroy(f->client_cq); |
||||
} |
||||
|
||||
static void *tag(gpr_intptr t) { return (void *)t; } |
||||
|
||||
static void test_body(grpc_end2end_test_fixture f) { |
||||
grpc_call *c; |
||||
grpc_call *s; |
||||
gpr_timespec deadline = n_seconds_time(10); |
||||
cq_verifier *v_client = cq_verifier_create(f.client_cq); |
||||
cq_verifier *v_server = cq_verifier_create(f.server_cq); |
||||
|
||||
c = grpc_channel_create_call(f.client, "/foo", "test.google.com", deadline); |
||||
GPR_ASSERT(c); |
||||
tag(1); |
||||
GPR_ASSERT(GRPC_CALL_OK == |
||||
grpc_call_start_invoke(c, f.client_cq, tag(1), tag(2), tag(3), 0)); |
||||
cq_expect_invoke_accepted(v_client, tag(1), GRPC_OP_OK); |
||||
cq_verify(v_client); |
||||
|
||||
GPR_ASSERT(GRPC_CALL_OK == grpc_call_writes_done(c, tag(4))); |
||||
cq_expect_finish_accepted(v_client, tag(4), GRPC_OP_OK); |
||||
cq_verify(v_client); |
||||
|
||||
GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, tag(100))); |
||||
cq_expect_server_rpc_new(v_server, &s, tag(100), "/foo", "test.google.com", |
||||
deadline, NULL); |
||||
cq_verify(v_server); |
||||
|
||||
GPR_ASSERT(GRPC_CALL_OK == grpc_call_accept(s, f.server_cq, tag(102), 0)); |
||||
cq_expect_client_metadata_read(v_client, tag(2), NULL); |
||||
cq_verify(v_client); |
||||
|
||||
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_write_status( |
||||
s, GRPC_STATUS_UNIMPLEMENTED, "xyz", tag(5))); |
||||
cq_expect_finished_with_status(v_client, tag(3), GRPC_STATUS_UNIMPLEMENTED, |
||||
"xyz", NULL); |
||||
cq_verify(v_client); |
||||
|
||||
cq_expect_finish_accepted(v_server, tag(5), GRPC_OP_OK); |
||||
cq_verify(v_server); |
||||
cq_expect_finished(v_server, tag(102), NULL); |
||||
cq_verify(v_server); |
||||
grpc_call_destroy(c); |
||||
grpc_call_destroy(s); |
||||
|
||||
cq_verifier_destroy(v_client); |
||||
cq_verifier_destroy(v_server); |
||||
} |
||||
|
||||
static void test_invoke_request_with_census( |
||||
grpc_end2end_test_config config, const char *name, |
||||
void (*body)(grpc_end2end_test_fixture f)) { |
||||
char fullname[64]; |
||||
grpc_end2end_test_fixture f; |
||||
grpc_arg client_arg, server_arg; |
||||
grpc_channel_args client_args, server_args; |
||||
|
||||
client_arg.type = GRPC_ARG_INTEGER; |
||||
client_arg.key = GRPC_ARG_ENABLE_CENSUS; |
||||
client_arg.value.integer = 1; |
||||
|
||||
client_args.num_args = 1; |
||||
client_args.args = &client_arg; |
||||
|
||||
server_arg.type = GRPC_ARG_INTEGER; |
||||
server_arg.key = GRPC_ARG_ENABLE_CENSUS; |
||||
server_arg.value.integer = 1; |
||||
server_args.num_args = 1; |
||||
server_args.args = &server_arg; |
||||
|
||||
sprintf(fullname, "%s/%s", __FUNCTION__, name); |
||||
f = begin_test(config, fullname, &client_args, &server_args); |
||||
body(f); |
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void grpc_end2end_tests(grpc_end2end_test_config config) { |
||||
test_invoke_request_with_census(config, "census_simple_request", test_body); |
||||
} |
@ -0,0 +1,197 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2014, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <string.h> |
||||
|
||||
#include "src/core/statistics/census_interface.h" |
||||
#include "src/core/statistics/census_rpc_stats.h" |
||||
#include "src/core/statistics/census_tracing.h" |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/port_platform.h> |
||||
#include <grpc/support/string.h> |
||||
#include <grpc/support/thd.h> |
||||
#include <grpc/support/time.h> |
||||
#include "test/core/util/test_config.h" |
||||
|
||||
/* Ensure all possible state transitions are called without causing problem */ |
||||
static void test_init_shutdown() { |
||||
census_stats_store_init(); |
||||
census_stats_store_init(); |
||||
census_stats_store_shutdown(); |
||||
census_stats_store_shutdown(); |
||||
census_stats_store_init(); |
||||
} |
||||
|
||||
static void test_create_and_destroy() { |
||||
census_rpc_stats* stats = NULL; |
||||
census_aggregated_rpc_stats agg_stats = {0, NULL}; |
||||
|
||||
stats = census_rpc_stats_create_empty(); |
||||
GPR_ASSERT(stats != NULL); |
||||
GPR_ASSERT(stats->cnt == 0 && stats->rpc_error_cnt == 0 && |
||||
stats->app_error_cnt == 0 && stats->elapsed_time_ms == 0.0 && |
||||
stats->api_request_bytes == 0 && stats->wire_request_bytes == 0 && |
||||
stats->api_response_bytes == 0 && stats->wire_response_bytes == 0); |
||||
gpr_free(stats); |
||||
|
||||
census_aggregated_rpc_stats_set_empty(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 0); |
||||
GPR_ASSERT(agg_stats.stats == NULL); |
||||
agg_stats.num_entries = 1; |
||||
agg_stats.stats = (census_per_method_rpc_stats*)gpr_malloc( |
||||
sizeof(census_per_method_rpc_stats)); |
||||
agg_stats.stats[0].method = gpr_strdup("foo"); |
||||
census_aggregated_rpc_stats_set_empty(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 0); |
||||
GPR_ASSERT(agg_stats.stats == NULL); |
||||
} |
||||
|
||||
#define ASSERT_NEAR(a, b) \ |
||||
GPR_ASSERT((a - b) * (a - b) < 1e-24 * (a + b) * (a + b)) |
||||
|
||||
static void test_record_and_get_stats() { |
||||
census_rpc_stats stats = {1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4}; |
||||
census_op_id id; |
||||
census_aggregated_rpc_stats agg_stats = {0, NULL}; |
||||
|
||||
/* Record client stats twice with the same op_id. */ |
||||
census_init(); |
||||
id = census_tracing_start_op(); |
||||
census_add_method_tag(id, "m1"); |
||||
census_record_rpc_client_stats(id, &stats); |
||||
census_record_rpc_client_stats(id, &stats); |
||||
census_tracing_end_op(id); |
||||
/* Server stats expect to be empty */ |
||||
census_get_server_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 0); |
||||
GPR_ASSERT(agg_stats.stats == NULL); |
||||
/* Client stats expect to have one entry */ |
||||
census_get_client_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 1); |
||||
GPR_ASSERT(agg_stats.stats != NULL); |
||||
GPR_ASSERT(strcmp(agg_stats.stats[0].method, "m1") == 0); |
||||
GPR_ASSERT(agg_stats.stats[0].minute_stats.cnt == 2 && |
||||
agg_stats.stats[0].hour_stats.cnt == 2 && |
||||
agg_stats.stats[0].total_stats.cnt == 2); |
||||
ASSERT_NEAR(agg_stats.stats[0].minute_stats.wire_response_bytes, 16.8); |
||||
ASSERT_NEAR(agg_stats.stats[0].hour_stats.wire_response_bytes, 16.8); |
||||
ASSERT_NEAR(agg_stats.stats[0].total_stats.wire_response_bytes, 16.8); |
||||
/* Get stats again, results should be the same. */ |
||||
census_get_client_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 1); |
||||
census_aggregated_rpc_stats_set_empty(&agg_stats); |
||||
census_shutdown(); |
||||
|
||||
/* Record both server (once) and client (twice) stats with different op_ids.*/ |
||||
census_init(); |
||||
id = census_tracing_start_op(); |
||||
census_add_method_tag(id, "m2"); |
||||
census_record_rpc_client_stats(id, &stats); |
||||
census_tracing_end_op(id); |
||||
id = census_tracing_start_op(); |
||||
census_add_method_tag(id, "m3"); |
||||
census_record_rpc_server_stats(id, &stats); |
||||
census_tracing_end_op(id); |
||||
id = census_tracing_start_op(); |
||||
census_add_method_tag(id, "m4"); |
||||
census_record_rpc_client_stats(id, &stats); |
||||
census_tracing_end_op(id); |
||||
/* Check server stats */ |
||||
census_get_server_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 1); |
||||
GPR_ASSERT(strcmp(agg_stats.stats[0].method, "m3") == 0); |
||||
GPR_ASSERT(agg_stats.stats[0].minute_stats.app_error_cnt == 3 && |
||||
agg_stats.stats[0].hour_stats.app_error_cnt == 3 && |
||||
agg_stats.stats[0].total_stats.app_error_cnt == 3); |
||||
census_aggregated_rpc_stats_set_empty(&agg_stats); |
||||
/* Check client stats */ |
||||
census_get_client_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 2); |
||||
GPR_ASSERT(agg_stats.stats != NULL); |
||||
GPR_ASSERT((strcmp(agg_stats.stats[0].method, "m2") == 0 && |
||||
strcmp(agg_stats.stats[1].method, "m4") == 0) || |
||||
(strcmp(agg_stats.stats[0].method, "m4") == 0 && |
||||
strcmp(agg_stats.stats[1].method, "m2") == 0)); |
||||
GPR_ASSERT(agg_stats.stats[0].minute_stats.cnt == 1 && |
||||
agg_stats.stats[1].minute_stats.cnt == 1); |
||||
census_aggregated_rpc_stats_set_empty(&agg_stats); |
||||
census_shutdown(); |
||||
} |
||||
|
||||
static void test_record_stats_on_unknown_op_id() { |
||||
census_op_id unknown_id = {0xDEAD, 0xBEEF}; |
||||
census_rpc_stats stats = {1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4}; |
||||
census_aggregated_rpc_stats agg_stats = {0, NULL}; |
||||
|
||||
census_init(); |
||||
/* Tests that recording stats against unknown id is noop. */ |
||||
census_record_rpc_client_stats(unknown_id, &stats); |
||||
census_record_rpc_server_stats(unknown_id, &stats); |
||||
census_get_server_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 0); |
||||
GPR_ASSERT(agg_stats.stats == NULL); |
||||
census_get_client_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 0); |
||||
GPR_ASSERT(agg_stats.stats == NULL); |
||||
census_aggregated_rpc_stats_set_empty(&agg_stats); |
||||
census_shutdown(); |
||||
} |
||||
|
||||
/* Test that record stats is noop when trace store is uninitialized. */ |
||||
static void test_record_stats_with_trace_store_uninitialized() { |
||||
census_rpc_stats stats = {1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4}; |
||||
census_op_id id = {0, 0}; |
||||
census_aggregated_rpc_stats agg_stats = {0, NULL}; |
||||
|
||||
census_init(); |
||||
id = census_tracing_start_op(); |
||||
census_add_method_tag(id, "m"); |
||||
census_tracing_end_op(id); |
||||
/* shuts down trace store only. */ |
||||
census_tracing_shutdown(); |
||||
census_record_rpc_client_stats(id, &stats); |
||||
census_get_client_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 0); |
||||
census_stats_store_shutdown(); |
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
grpc_test_init(argc, argv); |
||||
test_init_shutdown(); |
||||
test_create_and_destroy(); |
||||
test_record_and_get_stats(); |
||||
test_record_stats_on_unknown_op_id(); |
||||
test_record_stats_with_trace_store_uninitialized(); |
||||
return 0; |
||||
} |
@ -0,0 +1,184 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2014, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <string.h> |
||||
|
||||
#include "src/core/statistics/census_interface.h" |
||||
#include "src/core/statistics/census_tracing.h" |
||||
#include "src/core/statistics/census_tracing.h" |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/port_platform.h> |
||||
#include <grpc/support/sync.h> |
||||
#include <grpc/support/thd.h> |
||||
#include <grpc/support/time.h> |
||||
#include <grpc/support/useful.h> |
||||
#include "test/core/util/test_config.h" |
||||
|
||||
/* Ensure all possible state transitions are called without causing problem */ |
||||
static void test_init_shutdown() { |
||||
census_tracing_init(); |
||||
census_tracing_init(); |
||||
census_tracing_shutdown(); |
||||
census_tracing_shutdown(); |
||||
census_tracing_init(); |
||||
} |
||||
|
||||
static void test_start_op_generates_locally_unique_ids() { |
||||
/* Check that ids generated within window size of 1000 are unique.
|
||||
TODO(hongyu): Replace O(n^2) duplicate detection algorithm with O(nlogn) |
||||
algorithm. Enhance the test to larger window size (>10^6) */ |
||||
#define WINDOW_SIZE 1000 |
||||
census_op_id ids[WINDOW_SIZE]; |
||||
int i; |
||||
census_init(); |
||||
for (i = 0; i < WINDOW_SIZE; i++) { |
||||
ids[i] = census_tracing_start_op(); |
||||
census_tracing_end_op(ids[i]); |
||||
} |
||||
for (i = 0; i < WINDOW_SIZE - 1; i++) { |
||||
int j; |
||||
for (j = i + 1; j < WINDOW_SIZE; j++) { |
||||
GPR_ASSERT(ids[i].upper != ids[j].upper || ids[i].lower != ids[j].lower); |
||||
} |
||||
} |
||||
#undef WINDOW_SIZE |
||||
census_shutdown(); |
||||
} |
||||
|
||||
static void test_get_trace_method_name() { |
||||
census_op_id id; |
||||
const char write_name[] = "service/method"; |
||||
census_tracing_init(); |
||||
id = census_tracing_start_op(); |
||||
census_add_method_tag(id, write_name); |
||||
census_internal_lock_trace_store(); |
||||
{ |
||||
const char* read_name = |
||||
census_get_trace_method_name(census_get_trace_obj_locked(id)); |
||||
GPR_ASSERT(strcmp(read_name, write_name) == 0); |
||||
} |
||||
census_internal_unlock_trace_store(); |
||||
census_tracing_shutdown(); |
||||
} |
||||
|
||||
typedef struct thd_arg { |
||||
int num_done; |
||||
gpr_cv done; |
||||
gpr_mu mu; |
||||
} thd_arg; |
||||
|
||||
static void mimic_trace_op_sequences(void* arg) { |
||||
census_op_id id; |
||||
const char* method_name = "service_foo/method_bar"; |
||||
int i = 0; |
||||
const int num_iter = 200; |
||||
thd_arg* args = (thd_arg*)arg; |
||||
GPR_ASSERT(args != NULL); |
||||
gpr_log(GPR_INFO, "Start trace op sequence thread."); |
||||
for (i = 0; i < num_iter; i++) { |
||||
id = census_tracing_start_op(); |
||||
census_add_method_tag(id, method_name); |
||||
/* pretend doing 1us work. */ |
||||
gpr_sleep_until(gpr_time_add(gpr_now(), gpr_time_from_micros(1))); |
||||
census_tracing_end_op(id); |
||||
} |
||||
gpr_log(GPR_INFO, "End trace op sequence thread."); |
||||
gpr_mu_lock(&args->mu); |
||||
args->num_done += 1; |
||||
gpr_cv_broadcast(&args->done); |
||||
gpr_mu_unlock(&args->mu); |
||||
} |
||||
|
||||
static void test_concurrency() { |
||||
#define NUM_THREADS 1000 |
||||
gpr_thd_id tid[NUM_THREADS]; |
||||
int i = 0; |
||||
thd_arg arg; |
||||
arg.num_done = 0; |
||||
gpr_mu_init(&arg.mu); |
||||
gpr_cv_init(&arg.done); |
||||
census_tracing_init(); |
||||
for (i = 0; i < NUM_THREADS; ++i) { |
||||
gpr_thd_new(tid + i, mimic_trace_op_sequences, &arg, NULL); |
||||
} |
||||
gpr_mu_lock(&arg.mu); |
||||
while (arg.num_done < NUM_THREADS) { |
||||
gpr_log(GPR_INFO, "num done %d", arg.num_done); |
||||
gpr_cv_wait(&arg.done, &arg.mu, gpr_inf_future); |
||||
} |
||||
gpr_mu_unlock(&arg.mu); |
||||
census_tracing_shutdown(); |
||||
#undef NUM_THREADS |
||||
} |
||||
|
||||
static void test_add_method_tag_to_unknown_op_id() { |
||||
census_op_id unknown_id = {0xDEAD, 0xBEEF}; |
||||
int ret = 0; |
||||
census_tracing_init(); |
||||
ret = census_add_method_tag(unknown_id, "foo"); |
||||
GPR_ASSERT(ret != 0); |
||||
census_tracing_shutdown(); |
||||
} |
||||
|
||||
static void test_trace_print() { |
||||
census_op_id id; |
||||
int i; |
||||
const char* annotation_txt[4] = {"abc", "", "$%^ *()_"}; |
||||
char long_txt[CENSUS_MAX_ANNOTATION_LENGTH + 10]; |
||||
|
||||
memset(long_txt, 'a', GPR_ARRAY_SIZE(long_txt)); |
||||
long_txt[CENSUS_MAX_ANNOTATION_LENGTH + 9] = '\0'; |
||||
annotation_txt[3] = long_txt; |
||||
|
||||
census_tracing_init(); |
||||
id = census_tracing_start_op(); |
||||
/* Adds large number of annotations to each trace */ |
||||
for (i = 0; i < 1000; i++) { |
||||
census_tracing_print(id, |
||||
annotation_txt[i % GPR_ARRAY_SIZE(annotation_txt)]); |
||||
} |
||||
census_tracing_end_op(id); |
||||
|
||||
census_tracing_shutdown(); |
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
grpc_test_init(argc, argv); |
||||
test_init_shutdown(); |
||||
test_start_op_generates_locally_unique_ids(); |
||||
test_get_trace_method_name(); |
||||
test_concurrency(); |
||||
test_add_method_tag_to_unknown_op_id(); |
||||
test_trace_print(); |
||||
return 0; |
||||
} |
@ -0,0 +1,73 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2014, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <grpc++/credentials.h> |
||||
|
||||
#include <memory> |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <gtest/gtest.h> |
||||
|
||||
namespace grpc { |
||||
namespace testing { |
||||
|
||||
class CredentialsTest : public ::testing::Test { |
||||
protected: |
||||
}; |
||||
|
||||
TEST_F(CredentialsTest, InvalidSslCreds) { |
||||
std::unique_ptr<Credentials> bad1 = |
||||
CredentialsFactory::SslCredentials({"", "", ""}); |
||||
EXPECT_EQ(nullptr, bad1.get()); |
||||
std::unique_ptr<Credentials> bad2 = |
||||
CredentialsFactory::SslCredentials({"", "bla", "bla"}); |
||||
EXPECT_EQ(nullptr, bad2.get()); |
||||
} |
||||
|
||||
TEST_F(CredentialsTest, InvalidServiceAccountCreds) { |
||||
std::unique_ptr<Credentials> bad1 = |
||||
CredentialsFactory::ServiceAccountCredentials("", "", |
||||
std::chrono::seconds(1)); |
||||
EXPECT_EQ(nullptr, bad1.get()); |
||||
} |
||||
|
||||
} // namespace testing
|
||||
} // namespace grpc
|
||||
|
||||
int main(int argc, char **argv) { |
||||
|
||||
grpc_init(); |
||||
int ret = RUN_ALL_TESTS(); |
||||
grpc_shutdown(); |
||||
return ret; |
||||
} |
@ -0,0 +1,231 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2014, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <memory> |
||||
#include <sstream> |
||||
#include <thread> |
||||
|
||||
#include <google/gflags.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/log.h> |
||||
#include "test/core/end2end/data/ssl_test_data.h" |
||||
#include <grpc++/config.h> |
||||
#include <grpc++/server.h> |
||||
#include <grpc++/server_builder.h> |
||||
#include <grpc++/server_context.h> |
||||
#include <grpc++/server_credentials.h> |
||||
#include <grpc++/status.h> |
||||
#include <grpc++/stream.h> |
||||
#include "test/cpp/interop/test.pb.h" |
||||
#include "test/cpp/interop/empty.pb.h" |
||||
#include "test/cpp/interop/messages.pb.h" |
||||
|
||||
DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls."); |
||||
DEFINE_int32(port, 0, "Server port."); |
||||
|
||||
using grpc::Server; |
||||
using grpc::ServerBuilder; |
||||
using grpc::ServerContext; |
||||
using grpc::ServerCredentials; |
||||
using grpc::ServerCredentialsFactory; |
||||
using grpc::ServerReader; |
||||
using grpc::ServerReaderWriter; |
||||
using grpc::ServerWriter; |
||||
using grpc::SslServerCredentialsOptions; |
||||
using grpc::testing::Payload; |
||||
using grpc::testing::PayloadType; |
||||
using grpc::testing::SimpleRequest; |
||||
using grpc::testing::SimpleResponse; |
||||
using grpc::testing::StreamingInputCallRequest; |
||||
using grpc::testing::StreamingInputCallResponse; |
||||
using grpc::testing::StreamingOutputCallRequest; |
||||
using grpc::testing::StreamingOutputCallResponse; |
||||
using grpc::testing::TestService; |
||||
using grpc::Status; |
||||
|
||||
bool SetPayload(PayloadType type, int size, Payload* payload) { |
||||
PayloadType response_type = type; |
||||
// TODO(yangg): Support UNCOMPRESSABLE payload.
|
||||
if (type != PayloadType::COMPRESSABLE) { |
||||
return false; |
||||
} |
||||
payload->set_type(response_type); |
||||
std::unique_ptr<char[]> body(new char[size]()); |
||||
payload->set_body(body.get(), size); |
||||
return true; |
||||
} |
||||
|
||||
class TestServiceImpl : public TestService::Service { |
||||
public: |
||||
Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request, |
||||
grpc::testing::Empty* response) { |
||||
return Status::OK; |
||||
} |
||||
|
||||
Status UnaryCall(ServerContext* context, const SimpleRequest* request, |
||||
SimpleResponse* response) { |
||||
if (request->has_response_size() && request->response_size() > 0) { |
||||
if (!SetPayload(request->response_type(), request->response_size(), |
||||
response->mutable_payload())) { |
||||
return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
||||
} |
||||
} |
||||
return Status::OK; |
||||
} |
||||
|
||||
Status StreamingOutputCall( |
||||
ServerContext* context, const StreamingOutputCallRequest* request, |
||||
ServerWriter<StreamingOutputCallResponse>* writer) { |
||||
StreamingOutputCallResponse response; |
||||
bool write_success = true; |
||||
response.mutable_payload()->set_type(request->response_type()); |
||||
for (int i = 0; write_success && i < request->response_parameters_size(); |
||||
i++) { |
||||
response.mutable_payload()->set_body( |
||||
grpc::string(request->response_parameters(i).size(), '\0')); |
||||
write_success = writer->Write(response); |
||||
} |
||||
if (write_success) { |
||||
return Status::OK; |
||||
} else { |
||||
return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
||||
} |
||||
} |
||||
|
||||
Status StreamingInputCall(ServerContext* context, |
||||
ServerReader<StreamingInputCallRequest>* reader, |
||||
StreamingInputCallResponse* response) { |
||||
StreamingInputCallRequest request; |
||||
int aggregated_payload_size = 0; |
||||
while (reader->Read(&request)) { |
||||
if (request.has_payload() && request.payload().has_body()) { |
||||
aggregated_payload_size += request.payload().body().size(); |
||||
} |
||||
} |
||||
response->set_aggregated_payload_size(aggregated_payload_size); |
||||
return Status::OK; |
||||
} |
||||
|
||||
Status FullDuplexCall( |
||||
ServerContext* context, |
||||
ServerReaderWriter<StreamingOutputCallResponse, |
||||
StreamingOutputCallRequest>* stream) { |
||||
StreamingOutputCallRequest request; |
||||
StreamingOutputCallResponse response; |
||||
bool write_success = true; |
||||
while (write_success && stream->Read(&request)) { |
||||
response.mutable_payload()->set_type(request.payload().type()); |
||||
if (request.response_parameters_size() == 0) { |
||||
return Status(grpc::StatusCode::INTERNAL, |
||||
"Request does not have response parameters."); |
||||
} |
||||
response.mutable_payload()->set_body( |
||||
grpc::string(request.response_parameters(0).size(), '\0')); |
||||
write_success = stream->Write(response); |
||||
} |
||||
if (write_success) { |
||||
return Status::OK; |
||||
} else { |
||||
return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
||||
} |
||||
} |
||||
|
||||
Status HalfDuplexCall( |
||||
ServerContext* context, |
||||
ServerReaderWriter<StreamingOutputCallResponse, |
||||
StreamingOutputCallRequest>* stream) { |
||||
std::vector<StreamingOutputCallRequest> requests; |
||||
StreamingOutputCallRequest request; |
||||
while (stream->Read(&request)) { |
||||
requests.push_back(request); |
||||
} |
||||
|
||||
StreamingOutputCallResponse response; |
||||
bool write_success = true; |
||||
for (unsigned int i = 0; write_success && i < requests.size(); i++) { |
||||
response.mutable_payload()->set_type(requests[i].payload().type()); |
||||
if (requests[i].response_parameters_size() == 0) { |
||||
return Status(grpc::StatusCode::INTERNAL, |
||||
"Request does not have response parameters."); |
||||
} |
||||
response.mutable_payload()->set_body( |
||||
grpc::string(requests[i].response_parameters(0).size(), '\0')); |
||||
write_success = stream->Write(response); |
||||
} |
||||
if (write_success) { |
||||
return Status::OK; |
||||
} else { |
||||
return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
||||
} |
||||
} |
||||
}; |
||||
|
||||
void RunServer() { |
||||
std::ostringstream server_address; |
||||
server_address << "localhost:" << FLAGS_port; |
||||
TestServiceImpl service; |
||||
|
||||
SimpleRequest request; |
||||
SimpleResponse response; |
||||
|
||||
ServerBuilder builder; |
||||
builder.AddPort(server_address.str()); |
||||
builder.RegisterService(service.service()); |
||||
if (FLAGS_enable_ssl) { |
||||
SslServerCredentialsOptions ssl_opts = { |
||||
"", |
||||
{reinterpret_cast<const char*>(test_server1_key), |
||||
test_server1_key_size}, |
||||
{reinterpret_cast<const char*>(test_server1_cert), |
||||
test_server1_cert_size}}; |
||||
std::shared_ptr<ServerCredentials> creds = |
||||
ServerCredentialsFactory::SslCredentials(ssl_opts); |
||||
builder.SetCredentials(creds); |
||||
} |
||||
std::unique_ptr<Server> server(builder.BuildAndStart()); |
||||
gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str()); |
||||
while (true) { |
||||
std::this_thread::sleep_for(std::chrono::seconds(5)); |
||||
} |
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
grpc_init(); |
||||
google::ParseCommandLineFlags(&argc, &argv, true); |
||||
|
||||
GPR_ASSERT(FLAGS_port != 0); |
||||
RunServer(); |
||||
|
||||
grpc_shutdown(); |
||||
return 0; |
||||
} |
@ -0,0 +1,12 @@ |
||||
// This is a partial copy of echo.proto with a different package name. |
||||
|
||||
syntax = "proto2"; |
||||
|
||||
import "test/cpp/util/messages.proto"; |
||||
|
||||
package grpc.cpp.test.util.duplicate; |
||||
|
||||
service TestService { |
||||
rpc Echo(grpc.cpp.test.util.EchoRequest) |
||||
returns (grpc.cpp.test.util.EchoResponse); |
||||
} |
@ -0,0 +1,21 @@ |
||||
syntax = "proto2"; |
||||
|
||||
package grpc.cpp.test.util; |
||||
|
||||
message RequestParams { |
||||
optional bool echo_deadline = 1; |
||||
} |
||||
|
||||
message EchoRequest { |
||||
optional string message = 1; |
||||
optional RequestParams param = 2; |
||||
} |
||||
|
||||
message ResponseParams { |
||||
optional int64 request_deadline = 1; |
||||
} |
||||
|
||||
message EchoResponse { |
||||
optional string message = 1; |
||||
optional ResponseParams param = 2; |
||||
} |
Loading…
Reference in new issue