diff options
author | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2015-04-10 04:46:03 +0200 |
---|---|---|
committer | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2015-04-10 18:28:13 +0200 |
commit | a9ef765bfcd2fbc8c588ea1cdcb5681547fc364f (patch) | |
tree | 994980c251f49f07973212a736fc1a6b88cfbd66 /src | |
parent | 9f72864caf46f292d0f0ce34baa5ec0e1beb7739 (diff) | |
parent | e2ecc508415b95526563eb7b70510e015b9a004e (diff) |
Merge branch 'master' of github.com:grpc/grpc into sanity
Conflicts:
tools/run_tests/run_tests.py
Diffstat (limited to 'src')
148 files changed, 3424 insertions, 3557 deletions
diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc index d5004624d4..c78f0333d8 100644 --- a/src/compiler/cpp_generator.cc +++ b/src/compiler/cpp_generator.cc @@ -109,9 +109,49 @@ bool HasBidiStreaming(const grpc::protobuf::FileDescriptor *file) { } return false; } + +grpc::string FilenameIdentifier(const grpc::string& filename) { + grpc::string result; + for (unsigned i = 0; i < filename.size(); i++) { + char c = filename[i]; + if (isalnum(c)) { + result.push_back(c); + } else { + static char hex[] = "0123456789abcdef"; + result.push_back('_'); + result.push_back(hex[(c >> 4) & 0xf]); + result.push_back(hex[c & 0xf]); + } + } + return result; +} } // namespace -grpc::string GetHeaderIncludes(const grpc::protobuf::FileDescriptor *file) { +grpc::string GetHeaderPrologue(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms) { + grpc::string output; + grpc::protobuf::io::StringOutputStream output_stream(&output); + grpc::protobuf::io::Printer printer(&output_stream, '$'); + std::map<grpc::string, grpc::string> vars; + + vars["filename"] = file->name(); + vars["filename_identifier"] = FilenameIdentifier(file->name()); + vars["filename_base"] = grpc_generator::StripProto(file->name()); + + printer.Print(vars, "// Generated by the gRPC protobuf plugin.\n"); + printer.Print(vars, "// If you make any local change, they will be lost.\n"); + printer.Print(vars, "// source: $filename$\n"); + printer.Print(vars, "#ifndef GRPC_$filename_identifier$__INCLUDED\n"); + printer.Print(vars, "#define GRPC_$filename_identifier$__INCLUDED\n"); + printer.Print(vars, "\n"); + printer.Print(vars, "#include \"$filename_base$.pb.h\"\n"); + printer.Print(vars, "\n"); + + return output; +} + +grpc::string GetHeaderIncludes(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms) { grpc::string temp = "#include <grpc++/impl/internal_stub.h>\n" "#include <grpc++/impl/service_type.h>\n" @@ -155,17 +195,21 @@ grpc::string GetHeaderIncludes(const grpc::protobuf::FileDescriptor *file) { "class ServerAsyncReaderWriter;\n"); } temp.append("} // namespace grpc\n"); - return temp; -} -grpc::string GetSourceIncludes() { - return "#include <grpc++/async_unary_call.h>\n" - "#include <grpc++/channel_interface.h>\n" - "#include <grpc++/impl/client_unary_call.h>\n" - "#include <grpc++/impl/rpc_method.h>\n" - "#include <grpc++/impl/rpc_service_method.h>\n" - "#include <grpc++/impl/service_type.h>\n" - "#include <grpc++/stream.h>\n"; + temp.append("\n"); + + std::vector<grpc::string> parts = + grpc_generator::tokenize(file->package(), "."); + + for (auto part = parts.begin(); part != parts.end(); part++) { + temp.append("namespace "); + temp.append(*part); + temp.append(" {\n"); + } + + temp.append("\n"); + + return temp; } void PrintHeaderClientMethod(grpc::protobuf::io::Printer *printer, @@ -353,16 +397,99 @@ void PrintHeaderService(grpc::protobuf::io::Printer *printer, printer->Print("};\n"); } -grpc::string GetHeaderServices(const grpc::protobuf::FileDescriptor *file) { +grpc::string GetHeaderServices(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms) { grpc::string output; grpc::protobuf::io::StringOutputStream output_stream(&output); grpc::protobuf::io::Printer printer(&output_stream, '$'); std::map<grpc::string, grpc::string> vars; + if (!params.services_namespace.empty()) { + vars["services_namespace"] = params.services_namespace; + printer.Print(vars, "\nnamespace $services_namespace$ {\n\n"); + } + for (int i = 0; i < file->service_count(); ++i) { PrintHeaderService(&printer, file->service(i), &vars); printer.Print("\n"); } + + if (!params.services_namespace.empty()) { + printer.Print(vars, "} // namespace $services_namespace$\n\n"); + } + + return output; +} + +grpc::string GetHeaderEpilogue(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms) { + grpc::string output; + grpc::protobuf::io::StringOutputStream output_stream(&output); + grpc::protobuf::io::Printer printer(&output_stream, '$'); + std::map<grpc::string, grpc::string> vars; + + vars["filename"] = file->name(); + vars["filename_identifier"] = FilenameIdentifier(file->name()); + + std::vector<grpc::string> parts = + grpc_generator::tokenize(file->package(), "."); + + for (auto part = parts.rbegin(); part != parts.rend(); part++) { + vars["part"] = *part; + printer.Print(vars, "} // namespace $part$\n"); + } + + printer.Print(vars, "\n\n"); + printer.Print(vars, "#endif // GRPC_$filename_identifier$__INCLUDED\n"); + + return output; +} + +grpc::string GetSourcePrologue(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms) { + grpc::string output; + grpc::protobuf::io::StringOutputStream output_stream(&output); + grpc::protobuf::io::Printer printer(&output_stream, '$'); + std::map<grpc::string, grpc::string> vars; + + vars["filename"] = file->name(); + vars["filename_base"] = grpc_generator::StripProto(file->name()); + + printer.Print(vars, "// Generated by the gRPC protobuf plugin.\n"); + printer.Print(vars, "// If you make any local change, they will be lost.\n"); + printer.Print(vars, "// source: $filename$\n\n"); + printer.Print(vars, "#include \"$filename_base$.pb.h\"\n"); + printer.Print(vars, "#include \"$filename_base$.grpc.pb.h\"\n"); + printer.Print(vars, "\n"); + + return output; +} + +grpc::string GetSourceIncludes(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶m) { + grpc::string output; + grpc::protobuf::io::StringOutputStream output_stream(&output); + grpc::protobuf::io::Printer printer(&output_stream, '$'); + std::map<grpc::string, grpc::string> vars; + + printer.Print(vars, "#include <grpc++/async_unary_call.h>\n"); + printer.Print(vars, "#include <grpc++/channel_interface.h>\n"); + printer.Print(vars, "#include <grpc++/impl/client_unary_call.h>\n"); + printer.Print(vars, "#include <grpc++/impl/rpc_method.h>\n"); + printer.Print(vars, "#include <grpc++/impl/rpc_service_method.h>\n"); + printer.Print(vars, "#include <grpc++/impl/service_type.h>\n"); + printer.Print(vars, "#include <grpc++/stream.h>\n"); + + std::vector<grpc::string> parts = + grpc_generator::tokenize(file->package(), "."); + + for (auto part = parts.begin(); part != parts.end(); part++) { + vars["part"] = *part; + printer.Print(vars, "namespace $part$ {\n"); + } + + printer.Print(vars, "\n"); + return output; } @@ -376,18 +503,18 @@ void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer, grpc_cpp_generator::ClassName(method->output_type(), true); if (NoStreaming(method)) { printer->Print(*vars, - "::grpc::Status $Service$::Stub::$Method$(" + "::grpc::Status $ns$$Service$::Stub::$Method$(" "::grpc::ClientContext* context, " "const $Request$& request, $Response$* response) {\n"); printer->Print(*vars, " return ::grpc::BlockingUnaryCall(channel()," - "::grpc::RpcMethod($Service$_method_names[$Idx$]), " + "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$]), " "context, request, response);\n" "}\n\n"); printer->Print( *vars, "std::unique_ptr< ::grpc::ClientAsyncResponseReader< $Response$>> " - "$Service$::Stub::Async$Method$(::grpc::ClientContext* context, " + "$ns$$Service$::Stub::Async$Method$(::grpc::ClientContext* context, " "const $Request$& request, " "::grpc::CompletionQueue* cq, void* tag) {\n"); printer->Print(*vars, @@ -395,32 +522,32 @@ void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer, "::grpc::ClientAsyncResponseReader< $Response$>>(new " "::grpc::ClientAsyncResponseReader< $Response$>(" "channel(), cq, " - "::grpc::RpcMethod($Service$_method_names[$Idx$]), " + "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$]), " "context, request, tag));\n" "}\n\n"); } else if (ClientOnlyStreaming(method)) { printer->Print(*vars, "std::unique_ptr< ::grpc::ClientWriter< $Request$>> " - "$Service$::Stub::$Method$(" + "$ns$$Service$::Stub::$Method$(" "::grpc::ClientContext* context, $Response$* response) {\n"); printer->Print(*vars, " return std::unique_ptr< ::grpc::ClientWriter< " "$Request$>>(new ::grpc::ClientWriter< $Request$>(" "channel()," - "::grpc::RpcMethod($Service$_method_names[$Idx$], " + "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], " "::grpc::RpcMethod::RpcType::CLIENT_STREAMING), " "context, response));\n" "}\n\n"); printer->Print(*vars, "std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>> " - "$Service$::Stub::Async$Method$(" + "$ns$$Service$::Stub::Async$Method$(" "::grpc::ClientContext* context, $Response$* response, " "::grpc::CompletionQueue* cq, void* tag) {\n"); printer->Print(*vars, " return std::unique_ptr< ::grpc::ClientAsyncWriter< " "$Request$>>(new ::grpc::ClientAsyncWriter< $Request$>(" "channel(), cq, " - "::grpc::RpcMethod($Service$_method_names[$Idx$], " + "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], " "::grpc::RpcMethod::RpcType::CLIENT_STREAMING), " "context, response, tag));\n" "}\n\n"); @@ -428,26 +555,26 @@ void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer, printer->Print( *vars, "std::unique_ptr< ::grpc::ClientReader< $Response$>> " - "$Service$::Stub::$Method$(" + "$ns$$Service$::Stub::$Method$(" "::grpc::ClientContext* context, const $Request$& request) {\n"); printer->Print(*vars, " return std::unique_ptr< ::grpc::ClientReader< " "$Response$>>(new ::grpc::ClientReader< $Response$>(" "channel()," - "::grpc::RpcMethod($Service$_method_names[$Idx$], " + "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], " "::grpc::RpcMethod::RpcType::SERVER_STREAMING), " "context, request));\n" "}\n\n"); printer->Print(*vars, "std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> " - "$Service$::Stub::Async$Method$(" + "$ns$$Service$::Stub::Async$Method$(" "::grpc::ClientContext* context, const $Request$& request, " "::grpc::CompletionQueue* cq, void* tag) {\n"); printer->Print(*vars, " return std::unique_ptr< ::grpc::ClientAsyncReader< " "$Response$>>(new ::grpc::ClientAsyncReader< $Response$>(" "channel(), cq, " - "::grpc::RpcMethod($Service$_method_names[$Idx$], " + "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], " "::grpc::RpcMethod::RpcType::SERVER_STREAMING), " "context, request, tag));\n" "}\n\n"); @@ -455,27 +582,27 @@ void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer, printer->Print( *vars, "std::unique_ptr< ::grpc::ClientReaderWriter< $Request$, $Response$>> " - "$Service$::Stub::$Method$(::grpc::ClientContext* context) {\n"); + "$ns$$Service$::Stub::$Method$(::grpc::ClientContext* context) {\n"); printer->Print(*vars, " return std::unique_ptr< ::grpc::ClientReaderWriter< " "$Request$, $Response$>>(new ::grpc::ClientReaderWriter< " "$Request$, $Response$>(" "channel()," - "::grpc::RpcMethod($Service$_method_names[$Idx$], " + "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], " "::grpc::RpcMethod::RpcType::BIDI_STREAMING), " "context));\n" "}\n\n"); printer->Print(*vars, "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< " "$Request$, $Response$>> " - "$Service$::Stub::Async$Method$(::grpc::ClientContext* context, " + "$ns$$Service$::Stub::Async$Method$(::grpc::ClientContext* context, " "::grpc::CompletionQueue* cq, void* tag) {\n"); printer->Print(*vars, " return std::unique_ptr< ::grpc::ClientAsyncReaderWriter< " "$Request$, $Response$>>(new " "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>(" "channel(), cq, " - "::grpc::RpcMethod($Service$_method_names[$Idx$], " + "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], " "::grpc::RpcMethod::RpcType::BIDI_STREAMING), " "context, tag));\n" "}\n\n"); @@ -492,7 +619,7 @@ void PrintSourceServerMethod(grpc::protobuf::io::Printer *printer, grpc_cpp_generator::ClassName(method->output_type(), true); if (NoStreaming(method)) { printer->Print(*vars, - "::grpc::Status $Service$::Service::$Method$(" + "::grpc::Status $ns$$Service$::Service::$Method$(" "::grpc::ServerContext* context, " "const $Request$* request, $Response$* response) {\n"); printer->Print( @@ -501,7 +628,7 @@ void PrintSourceServerMethod(grpc::protobuf::io::Printer *printer, printer->Print("}\n\n"); } else if (ClientOnlyStreaming(method)) { printer->Print(*vars, - "::grpc::Status $Service$::Service::$Method$(" + "::grpc::Status $ns$$Service$::Service::$Method$(" "::grpc::ServerContext* context, " "::grpc::ServerReader< $Request$>* reader, " "$Response$* response) {\n"); @@ -511,7 +638,7 @@ void PrintSourceServerMethod(grpc::protobuf::io::Printer *printer, printer->Print("}\n\n"); } else if (ServerOnlyStreaming(method)) { printer->Print(*vars, - "::grpc::Status $Service$::Service::$Method$(" + "::grpc::Status $ns$$Service$::Service::$Method$(" "::grpc::ServerContext* context, " "const $Request$* request, " "::grpc::ServerWriter< $Response$>* writer) {\n"); @@ -521,7 +648,7 @@ void PrintSourceServerMethod(grpc::protobuf::io::Printer *printer, printer->Print("}\n\n"); } else if (BidiStreaming(method)) { printer->Print(*vars, - "::grpc::Status $Service$::Service::$Method$(" + "::grpc::Status $ns$$Service$::Service::$Method$(" "::grpc::ServerContext* context, " "::grpc::ServerReaderWriter< $Response$, $Request$>* " "stream) {\n"); @@ -543,7 +670,7 @@ void PrintSourceServerAsyncMethod( grpc_cpp_generator::ClassName(method->output_type(), true); if (NoStreaming(method)) { printer->Print(*vars, - "void $Service$::AsyncService::Request$Method$(" + "void $ns$$Service$::AsyncService::Request$Method$(" "::grpc::ServerContext* context, " "$Request$* request, " "::grpc::ServerAsyncResponseWriter< $Response$>* response, " @@ -554,7 +681,7 @@ void PrintSourceServerAsyncMethod( printer->Print("}\n\n"); } else if (ClientOnlyStreaming(method)) { printer->Print(*vars, - "void $Service$::AsyncService::Request$Method$(" + "void $ns$$Service$::AsyncService::Request$Method$(" "::grpc::ServerContext* context, " "::grpc::ServerAsyncReader< $Response$, $Request$>* reader, " "::grpc::CompletionQueue* cq, void* tag) {\n"); @@ -564,7 +691,7 @@ void PrintSourceServerAsyncMethod( printer->Print("}\n\n"); } else if (ServerOnlyStreaming(method)) { printer->Print(*vars, - "void $Service$::AsyncService::Request$Method$(" + "void $ns$$Service$::AsyncService::Request$Method$(" "::grpc::ServerContext* context, " "$Request$* request, " "::grpc::ServerAsyncWriter< $Response$>* writer, " @@ -576,7 +703,7 @@ void PrintSourceServerAsyncMethod( } else if (BidiStreaming(method)) { printer->Print( *vars, - "void $Service$::AsyncService::Request$Method$(" + "void $ns$$Service$::AsyncService::Request$Method$(" "::grpc::ServerContext* context, " "::grpc::ServerAsyncReaderWriter< $Response$, $Request$>* stream, " "::grpc::CompletionQueue* cq, void *tag) {\n"); @@ -592,7 +719,7 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, std::map<grpc::string, grpc::string> *vars) { (*vars)["Service"] = service->name(); - printer->Print(*vars, "static const char* $Service$_method_names[] = {\n"); + printer->Print(*vars, "static const char* $prefix$$Service$_method_names[] = {\n"); for (int i = 0; i < service->method_count(); ++i) { (*vars)["Method"] = service->method(i)->name(); printer->Print(*vars, " \"/$Package$$Service$/$Method$\",\n"); @@ -601,9 +728,9 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, printer->Print( *vars, - "std::unique_ptr< $Service$::Stub> $Service$::NewStub(" + "std::unique_ptr< $ns$$Service$::Stub> $ns$$Service$::NewStub(" "const std::shared_ptr< ::grpc::ChannelInterface>& channel) {\n" - " std::unique_ptr< $Service$::Stub> stub(new $Service$::Stub());\n" + " std::unique_ptr< $ns$$Service$::Stub> stub(new $ns$$Service$::Stub());\n" " stub->set_channel(channel);\n" " return stub;\n" "}\n\n"); @@ -615,12 +742,12 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, (*vars)["MethodCount"] = as_string(service->method_count()); printer->Print( *vars, - "$Service$::AsyncService::AsyncService(::grpc::CompletionQueue* cq) : " - "::grpc::AsynchronousService(cq, $Service$_method_names, $MethodCount$) " + "$ns$$Service$::AsyncService::AsyncService(::grpc::CompletionQueue* cq) : " + "::grpc::AsynchronousService(cq, $prefix$$Service$_method_names, $MethodCount$) " "{}\n\n"); printer->Print(*vars, - "$Service$::Service::~Service() {\n" + "$ns$$Service$::Service::~Service() {\n" " delete service_;\n" "}\n\n"); for (int i = 0; i < service->method_count(); ++i) { @@ -629,7 +756,7 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, PrintSourceServerAsyncMethod(printer, service->method(i), vars); } printer->Print(*vars, - "::grpc::RpcService* $Service$::Service::service() {\n"); + "::grpc::RpcService* $ns$$Service$::Service::service() {\n"); printer->Indent(); printer->Print( "if (service_ != nullptr) {\n" @@ -648,52 +775,52 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, printer->Print( *vars, "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" - " $Service$_method_names[$Idx$],\n" + " $prefix$$Service$_method_names[$Idx$],\n" " ::grpc::RpcMethod::NORMAL_RPC,\n" - " new ::grpc::RpcMethodHandler< $Service$::Service, $Request$, " + " new ::grpc::RpcMethodHandler< $ns$$Service$::Service, $Request$, " "$Response$>(\n" - " std::function< ::grpc::Status($Service$::Service*, " + " std::function< ::grpc::Status($ns$$Service$::Service*, " "::grpc::ServerContext*, const $Request$*, $Response$*)>(" - "&$Service$::Service::$Method$), this),\n" + "&$ns$$Service$::Service::$Method$), this),\n" " new $Request$, new $Response$));\n"); } else if (ClientOnlyStreaming(method)) { printer->Print( *vars, "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" - " $Service$_method_names[$Idx$],\n" + " $prefix$$Service$_method_names[$Idx$],\n" " ::grpc::RpcMethod::CLIENT_STREAMING,\n" " new ::grpc::ClientStreamingHandler< " - "$Service$::Service, $Request$, $Response$>(\n" - " std::function< ::grpc::Status($Service$::Service*, " + "$ns$$Service$::Service, $Request$, $Response$>(\n" + " std::function< ::grpc::Status($ns$$Service$::Service*, " "::grpc::ServerContext*, " "::grpc::ServerReader< $Request$>*, $Response$*)>(" - "&$Service$::Service::$Method$), this),\n" + "&$ns$$Service$::Service::$Method$), this),\n" " new $Request$, new $Response$));\n"); } else if (ServerOnlyStreaming(method)) { printer->Print( *vars, "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" - " $Service$_method_names[$Idx$],\n" + " $prefix$$Service$_method_names[$Idx$],\n" " ::grpc::RpcMethod::SERVER_STREAMING,\n" " new ::grpc::ServerStreamingHandler< " - "$Service$::Service, $Request$, $Response$>(\n" - " std::function< ::grpc::Status($Service$::Service*, " + "$ns$$Service$::Service, $Request$, $Response$>(\n" + " std::function< ::grpc::Status($ns$$Service$::Service*, " "::grpc::ServerContext*, " "const $Request$*, ::grpc::ServerWriter< $Response$>*)>(" - "&$Service$::Service::$Method$), this),\n" + "&$ns$$Service$::Service::$Method$), this),\n" " new $Request$, new $Response$));\n"); } else if (BidiStreaming(method)) { printer->Print( *vars, "service_->AddMethod(new ::grpc::RpcServiceMethod(\n" - " $Service$_method_names[$Idx$],\n" + " $prefix$$Service$_method_names[$Idx$],\n" " ::grpc::RpcMethod::BIDI_STREAMING,\n" " new ::grpc::BidiStreamingHandler< " - "$Service$::Service, $Request$, $Response$>(\n" - " std::function< ::grpc::Status($Service$::Service*, " + "$ns$$Service$::Service, $Request$, $Response$>(\n" + " std::function< ::grpc::Status($ns$$Service$::Service*, " "::grpc::ServerContext*, " "::grpc::ServerReaderWriter< $Response$, $Request$>*)>(" - "&$Service$::Service::$Method$), this),\n" + "&$ns$$Service$::Service::$Method$), this),\n" " new $Request$, new $Response$));\n"); } } @@ -702,7 +829,8 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, printer->Print("}\n\n"); } -grpc::string GetSourceServices(const grpc::protobuf::FileDescriptor *file) { +grpc::string GetSourceServices(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms) { grpc::string output; grpc::protobuf::io::StringOutputStream output_stream(&output); grpc::protobuf::io::Printer printer(&output_stream, '$'); @@ -713,6 +841,13 @@ grpc::string GetSourceServices(const grpc::protobuf::FileDescriptor *file) { if (!file->package().empty()) { vars["Package"].append("."); } + if (!params.services_namespace.empty()) { + vars["ns"] = params.services_namespace + "::"; + vars["prefix"] = params.services_namespace; + } else { + vars["ns"] = ""; + vars["prefix"] = ""; + } for (int i = 0; i < file->service_count(); ++i) { PrintSourceService(&printer, file->service(i), &vars); @@ -721,4 +856,22 @@ grpc::string GetSourceServices(const grpc::protobuf::FileDescriptor *file) { return output; } +grpc::string GetSourceEpilogue(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms) { + grpc::string temp; + + std::vector<grpc::string> parts = + grpc_generator::tokenize(file->package(), "."); + + for (auto part = parts.begin(); part != parts.end(); part++) { + temp.append("} // namespace "); + temp.append(*part); + temp.append("\n"); + } + + temp.append("\n"); + + return temp; +} + } // namespace grpc_cpp_generator diff --git a/src/compiler/cpp_generator.h b/src/compiler/cpp_generator.h index 2ecdb5c47e..70c2e985f6 100644 --- a/src/compiler/cpp_generator.h +++ b/src/compiler/cpp_generator.h @@ -38,17 +38,43 @@ namespace grpc_cpp_generator { +// Contains all the parameters that are parsed from the command line. +struct Parameters { + // Puts the service into a namespace + grpc::string services_namespace; +}; + +// Return the prologue of the generated header file. +grpc::string GetHeaderPrologue(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms); + // Return the includes needed for generated header file. -grpc::string GetHeaderIncludes(const grpc::protobuf::FileDescriptor *file); +grpc::string GetHeaderIncludes(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms); // Return the includes needed for generated source file. -grpc::string GetSourceIncludes(); +grpc::string GetSourceIncludes(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms); + +// Return the epilogue of the generated header file. +grpc::string GetHeaderEpilogue(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms); + +// Return the prologue of the generated source file. +grpc::string GetSourcePrologue(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms); // Return the services for generated header file. -grpc::string GetHeaderServices(const grpc::protobuf::FileDescriptor *file); +grpc::string GetHeaderServices(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms); // Return the services for generated source file. -grpc::string GetSourceServices(const grpc::protobuf::FileDescriptor *file); +grpc::string GetSourceServices(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms); + +// Return the epilogue of the generated source file. +grpc::string GetSourceEpilogue(const grpc::protobuf::FileDescriptor *file, + const Parameters ¶ms); } // namespace grpc_cpp_generator diff --git a/src/compiler/cpp_plugin.cc b/src/compiler/cpp_plugin.cc index 5b83aa85cf..88c704948e 100644 --- a/src/compiler/cpp_plugin.cc +++ b/src/compiler/cpp_plugin.cc @@ -58,18 +58,48 @@ class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { return false; } + grpc_cpp_generator::Parameters generator_parameters; + + if (!parameter.empty()) { + std::vector<grpc::string> parameters_list = + grpc_generator::tokenize(parameter, ","); + for (auto parameter_string = parameters_list.begin(); + parameter_string != parameters_list.end(); + parameter_string++) { + std::vector<grpc::string> param = + grpc_generator::tokenize(*parameter_string, "="); + if (param[0] == "services_namespace") { + generator_parameters.services_namespace = param[1]; + } else { + *error = grpc::string("Unknown parameter: ") + *parameter_string; + return false; + } + } + } + grpc::string file_name = grpc_generator::StripProto(file->name()); - // Generate .pb.h - Insert(context, file_name + ".pb.h", "includes", - grpc_cpp_generator::GetHeaderIncludes(file)); - Insert(context, file_name + ".pb.h", "namespace_scope", - grpc_cpp_generator::GetHeaderServices(file)); - // Generate .pb.cc - Insert(context, file_name + ".pb.cc", "includes", - grpc_cpp_generator::GetSourceIncludes()); - Insert(context, file_name + ".pb.cc", "namespace_scope", - grpc_cpp_generator::GetSourceServices(file)); + grpc::string header_code = + grpc_cpp_generator::GetHeaderPrologue(file, generator_parameters) + + grpc_cpp_generator::GetHeaderIncludes(file, generator_parameters) + + grpc_cpp_generator::GetHeaderServices(file, generator_parameters) + + grpc_cpp_generator::GetHeaderEpilogue(file, generator_parameters); + std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> header_output( + context->Open(file_name + ".grpc.pb.h")); + grpc::protobuf::io::CodedOutputStream header_coded_out( + header_output.get()); + header_coded_out.WriteRaw(header_code.data(), header_code.size()); + + grpc::string source_code = + grpc_cpp_generator::GetSourcePrologue(file, generator_parameters) + + grpc_cpp_generator::GetSourceIncludes(file, generator_parameters) + + grpc_cpp_generator::GetSourceServices(file, generator_parameters) + + grpc_cpp_generator::GetSourceEpilogue(file, generator_parameters); + std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> source_output( + context->Open(file_name + ".grpc.pb.cc")); + grpc::protobuf::io::CodedOutputStream source_coded_out( + source_output.get()); + source_coded_out.WriteRaw(source_code.data(), source_code.size()); return true; } diff --git a/src/compiler/generator_helpers.h b/src/compiler/generator_helpers.h index 1e6727dd4c..374e1374cf 100644 --- a/src/compiler/generator_helpers.h +++ b/src/compiler/generator_helpers.h @@ -75,6 +75,47 @@ inline grpc::string StringReplace(grpc::string str, const grpc::string &from, return str; } +inline std::vector<grpc::string> tokenize(const grpc::string &input, + const grpc::string &delimiters) { + std::vector<grpc::string> tokens; + size_t pos, last_pos = 0; + + for (;;) { + bool done = false; + pos = input.find_first_of(delimiters, last_pos); + if (pos == grpc::string::npos) { + done = true; + pos = input.length(); + } + + tokens.push_back(input.substr(last_pos, pos - last_pos)); + if (done) return tokens; + + last_pos = pos + 1; + } +} + +inline grpc::string CapitalizeFirstLetter(grpc::string s) { + if (s.empty()) { + return s; + } + s[0] = ::toupper(s[0]); + return s; +} + +inline grpc::string LowerUnderscoreToUpperCamel(grpc::string str) { + std::vector<grpc::string> tokens = tokenize(str, "_"); + grpc::string result = ""; + for (unsigned int i = 0; i < tokens.size(); i++) { + result += CapitalizeFirstLetter(tokens[i]); + } + return result; +} + +inline grpc::string FileNameInUpperCamel(const grpc::protobuf::FileDescriptor *file) { + return LowerUnderscoreToUpperCamel(StripProto(file->name())); +} + } // namespace grpc_generator #endif // GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H diff --git a/src/compiler/objective_c_generator.cc b/src/compiler/objective_c_generator.cc new file mode 100644 index 0000000000..c68c9c37c2 --- /dev/null +++ b/src/compiler/objective_c_generator.cc @@ -0,0 +1,236 @@ +/* + * + * Copyright 2015, 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 <map> + +#include "src/compiler/objective_c_generator.h" +#include "src/compiler/objective_c_generator_helpers.h" + +#include "src/compiler/config.h" + +#include <sstream> + +namespace grpc_objective_c_generator { +namespace { + +void PrintSimpleBlockSignature(grpc::protobuf::io::Printer *printer, + const grpc::protobuf::MethodDescriptor *method, + std::map<grpc::string, grpc::string> *vars) { + (*vars)["method_name"] = method->name(); + (*vars)["request_type"] = PrefixedName(method->input_type()->name()); + (*vars)["response_type"] = PrefixedName(method->output_type()->name()); + + if (method->server_streaming()) { + printer->Print("// When the response stream finishes, the handler is " + "called with nil for both arguments.\n\n"); + } else { + printer->Print("// The handler is only called once.\n\n"); + } + printer->Print(*vars, "- (id<GRXLiveSource>)$method_name$WithRequest:" + "($request_type$)request completionHandler:(void(^)" + "($response_type$ *, NSError *))handler"); +} + +void PrintSimpleDelegateSignature(grpc::protobuf::io::Printer *printer, + const grpc::protobuf::MethodDescriptor *method, + std::map<grpc::string, grpc::string> *vars) { + (*vars)["method_name"] = method->name(); + (*vars)["request_type"] = PrefixedName(method->input_type()->name()); + + printer->Print(*vars, "- (id<GRXLiveSource>)$method_name$WithRequest:" + "($request_type$)request delegate:(id<GRXSink>)delegate"); +} + +void PrintAdvancedSignature(grpc::protobuf::io::Printer *printer, + const grpc::protobuf::MethodDescriptor *method, + std::map<grpc::string, grpc::string> *vars) { + (*vars)["method_name"] = method->name(); + printer->Print(*vars, "- (GRXSource *)$method_name$WithRequest:" + "(id<GRXSource>)request"); +} + +void PrintSourceMethodSimpleBlock(grpc::protobuf::io::Printer *printer, + const grpc::protobuf::MethodDescriptor *method, + std::map<grpc::string, grpc::string> *vars) { + PrintSimpleBlockSignature(printer, method, vars); + + (*vars)["method_name"] = method->name(); + printer->Print(" {\n"); + printer->Indent(); + printer->Print(*vars, "return [[self $method_name$WithRequest:request] " + "connectHandler:^(id value, NSError *error) {\n"); + printer->Indent(); + printer->Print("handler(value, error);\n"); + printer->Outdent(); + printer->Print("}];\n"); + printer->Outdent(); + printer->Print("}\n"); +} + +void PrintSourceMethodSimpleDelegate(grpc::protobuf::io::Printer *printer, + const grpc::protobuf::MethodDescriptor *method, + std::map<grpc::string, grpc::string> *vars) { + PrintSimpleDelegateSignature(printer, method, vars); + + (*vars)["method_name"] = method->name(); + printer->Print(" {\n"); + printer->Indent(); + printer->Print(*vars, "return [[self $method_name$WithRequest:request]" + "connectToSink:delegate];\n"); + printer->Outdent(); + printer->Print("}\n"); +} + +void PrintSourceMethodAdvanced(grpc::protobuf::io::Printer *printer, + const grpc::protobuf::MethodDescriptor *method, + std::map<grpc::string, grpc::string> *vars) { + PrintAdvancedSignature(printer, method, vars); + + (*vars)["method_name"] = method->name(); + printer->Print(" {\n"); + printer->Indent(); + printer->Print(*vars, "return [self $method_name$WithRequest:request " + "client:[self newClient]];\n"); + printer->Outdent(); + printer->Print("}\n"); +} + +void PrintSourceMethodHandler(grpc::protobuf::io::Printer *printer, + const grpc::protobuf::MethodDescriptor *method, + std::map<grpc::string, grpc::string> *vars) { + (*vars)["method_name"] = method->name(); + (*vars)["response_type"] = PrefixedName(method->output_type()->name()); + (*vars)["caps_name"] = grpc_generator::CapitalizeFirstLetter(method->name()); + + printer->Print(*vars, "- (GRXSource *)$method_name$WithRequest:" + "(id<GRXSource>)request client:(PBgRPCClient *)client {\n"); + printer->Indent(); + printer->Print(*vars, + "return [self responseWithMethod:$@\"$caps_name\"\n"); + printer->Print(*vars, + " class:[$response_type$ class]\n"); + printer->Print(" request:request\n"); + printer->Print(" client:client];\n"); + printer->Outdent(); + printer->Print("}\n"); +} + +} + +grpc::string GetHeader(const grpc::protobuf::ServiceDescriptor *service, + const grpc::string message_header) { + grpc::string output; + grpc::protobuf::io::StringOutputStream output_stream(&output); + grpc::protobuf::io::Printer printer(&output_stream, '$'); + std::map<grpc::string, grpc::string> vars; + printer.Print("#import \"PBgRPCClient.h\"\n"); + printer.Print("#import \"PBStub.h\"\n"); + vars["message_header"] = message_header; + printer.Print(vars, "#import \"$message_header$\"\n\n"); + printer.Print("@protocol GRXSource\n"); + printer.Print("@class GRXSource\n\n"); + vars["service_name"] = service->name(); + printer.Print("@protocol $service_name$Stub <NSObject>\n\n"); + printer.Print("#pragma mark Simple block handlers\n\n"); + for (int i = 0; i < service->method_count(); i++) { + PrintSimpleBlockSignature(&printer, service->method(i), &vars); + printer.Print(";\n"); + } + printer.Print("\n"); + printer.Print("#pragma mark Simple delegate handlers.\n\n"); + printer.Print("# TODO(jcanizales): Use high-level snippets to remove this duplication."); + for (int i = 0; i < service->method_count(); i++) { + PrintSimpleDelegateSignature(&printer, service->method(i), &vars); + printer.Print(";\n"); + } + printer.Print("\n"); + printer.Print("#pragma mark Advanced handlers.\n\n"); + for (int i = 0; i < service->method_count(); i++) { + PrintAdvancedSignature(&printer, service->method(i), &vars); + printer.Print(";\n"); + } + printer.Print("\n"); + printer.Print("@end\n\n"); + printer.Print("// Basic stub that only does marshalling and parsing\n"); + printer.Print(vars, "@interface $service_name$Stub :" + " PBStub<$service_name$Stub>\n"); + printer.Print("- (instancetype)initWithHost:(NSString *)host;\n"); + printer.Print("@end\n"); + return output; +} + +grpc::string GetSource(const grpc::protobuf::ServiceDescriptor *service) { + grpc::string output; + grpc::protobuf::io::StringOutputStream output_stream(&output); + grpc::protobuf::io::Printer printer(&output_stream, '$'); + std::map<grpc::string, grpc::string> vars; + vars["service_name"] = service->name(); + printer.Print(vars, "#import \"$service_name$Stub.pb.h\"\n"); + printer.Print("#import \"PBGeneratedMessage+GRXSource.h\"\n\n"); + vars["full_name"] = service->full_name(); + printer.Print(vars, + "static NSString *const kInterface = @\"$full_name$\";\n"); + printer.Print("@implementation $service_name$Stub\n\n"); + printer.Print("- (instancetype)initWithHost:(NSString *)host {\n"); + printer.Indent(); + printer.Print("if ((self = [super initWithHost:host " + "interface:kInterface])) {\n"); + printer.Print("}\n"); + printer.Print("return self;\n"); + printer.Outdent(); + printer.Print("}\n\n"); + printer.Print("#pragma mark Simple block handlers.\n"); + for (int i = 0; i < service->method_count(); i++) { + PrintSourceMethodSimpleBlock(&printer, service->method(i), &vars); + } + printer.Print("\n"); + printer.Print("#pragma mark Simple delegate handlers.\n"); + for (int i = 0; i < service->method_count(); i++) { + PrintSourceMethodSimpleDelegate(&printer, service->method(i), &vars); + } + printer.Print("\n"); + printer.Print("#pragma mark Advanced handlers.\n"); + for (int i = 0; i < service->method_count(); i++) { + PrintSourceMethodAdvanced(&printer, service->method(i), &vars); + } + printer.Print("\n"); + printer.Print("#pragma mark Handlers for subclasses " + "(stub wrappers) to override.\n"); + for (int i = 0; i < service->method_count(); i++) { + PrintSourceMethodHandler(&printer, service->method(i), &vars); + } + printer.Print("@end\n"); + return output; +} + +} // namespace grpc_objective_c_generator diff --git a/src/php/ext/grpc/event.h b/src/compiler/objective_c_generator.h index ef5846aee1..93c730b34e 100755..100644 --- a/src/php/ext/grpc/event.h +++ b/src/compiler/objective_c_generator.h @@ -31,21 +31,18 @@ * */ -#ifndef NET_GRPC_PHP_GRPC_EVENT_H_ -#define NET_GRPC_PHP_GRPC_EVENT_H_ +#ifndef GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_H +#define GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_H -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif +#include "src/compiler/config.h" -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" -#include "php_grpc.h" +namespace grpc_objective_c_generator { -#include "grpc/grpc.h" +grpc::string GetHeader(const grpc::protobuf::ServiceDescriptor *service, + const grpc::string message_header); -/* Create a new Event object that wraps an existing grpc_event struct */ -zval *grpc_php_convert_event(grpc_event *event); +grpc::string GetSource(const grpc::protobuf::ServiceDescriptor *service); -#endif /* NET_GRPC_PHP_GRPC_COMPLETION_CHANNEL_H */ +} // namespace grpc_objective_c_generator + +#endif // GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_H diff --git a/src/php/tests/unit_tests/CompletionQueueTest.php b/src/compiler/objective_c_generator_helpers.h index 76ee61dfe8..6a7c13991f 100755..100644 --- a/src/php/tests/unit_tests/CompletionQueueTest.php +++ b/src/compiler/objective_c_generator_helpers.h @@ -1,4 +1,3 @@ -<?php /* * * Copyright 2015, Google Inc. @@ -31,16 +30,29 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -class CompletionQueueTest extends PHPUnit_Framework_TestCase{ - public function testNextReturnsNullWithNoCall() { - $cq = new Grpc\CompletionQueue(); - $event = $cq->next(Grpc\Timeval::zero()); - $this->assertNull($event); - } - - public function testPluckReturnsNullWithNoCall() { - $cq = new Grpc\CompletionQueue(); - $event = $cq->pluck(0, Grpc\Timeval::zero()); - $this->assertNull($event); - } + +#ifndef GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_HELPERS_H +#define GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_HELPERS_H + +#include <map> +#include "src/compiler/config.h" +#include "src/compiler/generator_helpers.h" + +namespace grpc_objective_c_generator { + +const grpc::string prefix = "PBG"; + +inline grpc::string MessageHeaderName(const grpc::protobuf::FileDescriptor *file) { + return grpc_generator::FileNameInUpperCamel(file) + ".pb.h"; +} + +inline grpc::string StubFileName(grpc::string service_name) { + return prefix + service_name + "Stub"; +} + +inline grpc::string PrefixedName(grpc::string name) { + return prefix + name; +} + } +#endif // GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_HELPERS_H diff --git a/src/compiler/objective_c_plugin.cc b/src/compiler/objective_c_plugin.cc new file mode 100644 index 0000000000..eebce0cd20 --- /dev/null +++ b/src/compiler/objective_c_plugin.cc @@ -0,0 +1,98 @@ +/* + * + * Copyright 2015, 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 Objective C gRPC service interface out of Protobuf IDL. + +#include <memory> + +#include "src/compiler/config.h" +#include "src/compiler/objective_c_generator.h" +#include "src/compiler/objective_c_generator_helpers.h" + +class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { + public: + ObjectiveCGrpcGenerator() {} + virtual ~ObjectiveCGrpcGenerator() {} + + virtual bool Generate(const grpc::protobuf::FileDescriptor *file, + const grpc::string ¶meter, + grpc::protobuf::compiler::GeneratorContext *context, + grpc::string *error) const { + + if (file->service_count() == 0) { + // No services. Do nothing. + return true; + } + + for (int i = 0; i < file->service_count(); i++) { + const grpc::protobuf::ServiceDescriptor *service = file->service(i); + grpc::string file_name = grpc_objective_c_generator::StubFileName( + service->name()); + + // Generate .pb.h + grpc::string header_code = grpc_objective_c_generator::GetHeader( + service, grpc_objective_c_generator::MessageHeaderName(file)); + std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> header_output( + context->Open(file_name + ".pb.h")); + grpc::protobuf::io::CodedOutputStream header_coded_out( + header_output.get()); + header_coded_out.WriteRaw(header_code.data(), header_code.size()); + + // Generate .pb.m + grpc::string source_code = grpc_objective_c_generator::GetSource(service); + std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> source_output( + context->Open(file_name + ".pb.m")); + grpc::protobuf::io::CodedOutputStream source_coded_out( + source_output.get()); + source_coded_out.WriteRaw(source_code.data(), source_code.size()); + } + + return true; + } + + private: + // Insert the given code into the given file at the given insertion point. + void Insert(grpc::protobuf::compiler::GeneratorContext *context, + const grpc::string &filename, const grpc::string &insertion_point, + const grpc::string &code) const { + std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output( + context->OpenForInsert(filename, insertion_point)); + grpc::protobuf::io::CodedOutputStream coded_out(output.get()); + coded_out.WriteRaw(code.data(), code.size()); + } +}; + +int main(int argc, char *argv[]) { + ObjectiveCGrpcGenerator generator; + return grpc::protobuf::compiler::PluginMain(argc, argv, &generator); +} diff --git a/src/compiler/python_generator.cc b/src/compiler/python_generator.cc index c2d4cda31a..72149bc4e3 100644 --- a/src/compiler/python_generator.cc +++ b/src/compiler/python_generator.cc @@ -271,7 +271,7 @@ bool GetModuleAndMessagePath(const Descriptor* type, bool PrintServerFactory(const grpc::string& package_qualified_service_name, const ServiceDescriptor* service, Printer* out) { out->Print("def early_adopter_create_$Service$_server(servicer, port, " - "root_certificates, key_chain_pairs):\n", + "private_key=None, certificate_chain=None):\n", "Service", service->name()); { IndentScope raii_create_server_indent(out); @@ -309,17 +309,20 @@ bool PrintServerFactory(const grpc::string& package_qualified_service_name, make_pair(method->name(), output_message_module_and_class)); } out->Print("method_service_descriptions = {\n"); - for (auto& name_and_description_constructor : - method_description_constructors) { + for (auto name_and_description_constructor = + method_description_constructors.begin(); + name_and_description_constructor != + method_description_constructors.end(); + name_and_description_constructor++) { IndentScope raii_descriptions_indent(out); - const grpc::string method_name = name_and_description_constructor.first; + const grpc::string method_name = name_and_description_constructor->first; auto input_message_module_and_class = input_message_modules_and_classes.find(method_name); auto output_message_module_and_class = output_message_modules_and_classes.find(method_name); out->Print("\"$Method$\": utilities.$Constructor$(\n", "Method", method_name, "Constructor", - name_and_description_constructor.second); + name_and_description_constructor->second); { IndentScope raii_description_arguments_indent(out); out->Print("servicer.$Method$,\n", "Method", method_name); @@ -336,10 +339,10 @@ bool PrintServerFactory(const grpc::string& package_qualified_service_name, } out->Print("}\n"); out->Print( - "return implementations.secure_server(" + "return implementations.server(" "\"$PackageQualifiedServiceName$\"," - " method_service_descriptions, port, root_certificates," - " key_chain_pairs)\n", + " method_service_descriptions, port, private_key=private_key," + " certificate_chain=certificate_chain)\n", "PackageQualifiedServiceName", package_qualified_service_name); } return true; @@ -350,7 +353,10 @@ bool PrintStubFactory(const grpc::string& package_qualified_service_name, map<grpc::string, grpc::string> dict = ListToDict({ "Service", service->name(), }); - out->Print(dict, "def early_adopter_create_$Service$_stub(host, port):\n"); + out->Print(dict, "def early_adopter_create_$Service$_stub(host, port," + " metadata_transformer=None," + " secure=False, root_certificates=None, private_key=None," + " certificate_chain=None, server_host_override=None):\n"); { IndentScope raii_create_server_indent(out); map<grpc::string, grpc::string> method_description_constructors; @@ -387,17 +393,20 @@ bool PrintStubFactory(const grpc::string& package_qualified_service_name, make_pair(method->name(), output_message_module_and_class)); } out->Print("method_invocation_descriptions = {\n"); - for (auto& name_and_description_constructor : - method_description_constructors) { + for (auto name_and_description_constructor = + method_description_constructors.begin(); + name_and_description_constructor != + method_description_constructors.end(); + name_and_description_constructor++) { IndentScope raii_descriptions_indent(out); - const grpc::string method_name = name_and_description_constructor.first; + const grpc::string method_name = name_and_description_constructor->first; auto input_message_module_and_class = input_message_modules_and_classes.find(method_name); auto output_message_module_and_class = output_message_modules_and_classes.find(method_name); out->Print("\"$Method$\": utilities.$Constructor$(\n", "Method", method_name, "Constructor", - name_and_description_constructor.second); + name_and_description_constructor->second); { IndentScope raii_description_arguments_indent(out); out->Print( @@ -413,9 +422,13 @@ bool PrintStubFactory(const grpc::string& package_qualified_service_name, } out->Print("}\n"); out->Print( - "return implementations.insecure_stub(" + "return implementations.stub(" "\"$PackageQualifiedServiceName$\"," - " method_invocation_descriptions, host, port)\n", + " method_invocation_descriptions, host, port," + " metadata_transformer=metadata_transformer, secure=secure," + " root_certificates=root_certificates, private_key=private_key," + " certificate_chain=certificate_chain," + " server_host_override=server_host_override)\n", "PackageQualifiedServiceName", package_qualified_service_name); } return true; diff --git a/src/core/channel/http_server_filter.c b/src/core/channel/http_server_filter.c index f565cbf3ae..9da8b333ca 100644 --- a/src/core/channel/http_server_filter.c +++ b/src/core/channel/http_server_filter.c @@ -189,7 +189,8 @@ static void call_op(grpc_call_element *elem, grpc_call_element *from_elem, /* translate host to :authority since :authority may be omitted */ grpc_mdelem *authority = grpc_mdelem_from_metadata_strings( - channeld->mdctx, channeld->authority_key, op->data.metadata->value); + channeld->mdctx, grpc_mdstr_ref(channeld->authority_key), + grpc_mdstr_ref(op->data.metadata->value)); grpc_mdelem_unref(op->data.metadata); op->data.metadata = authority; /* pass the event up */ diff --git a/src/core/httpcli/parser.c b/src/core/httpcli/parser.c index f4decda98a..7b2a62060c 100644 --- a/src/core/httpcli/parser.c +++ b/src/core/httpcli/parser.c @@ -177,6 +177,8 @@ static int addbyte(grpc_httpcli_parser *parser, gpr_uint8 byte) { } gpr_log(GPR_ERROR, "should never reach here"); abort(); + + return 0; } void grpc_httpcli_parser_init(grpc_httpcli_parser *parser) { diff --git a/src/core/iomgr/iocp_windows.c b/src/core/iomgr/iocp_windows.c index 8b019e8049..aec626509a 100644 --- a/src/core/iomgr/iocp_windows.c +++ b/src/core/iomgr/iocp_windows.c @@ -52,10 +52,11 @@ static OVERLAPPED g_iocp_custom_overlap; static gpr_event g_shutdown_iocp; static gpr_event g_iocp_done; +static gpr_atm g_orphans = 0; static HANDLE g_iocp; -static int do_iocp_work() { +static void do_iocp_work() { BOOL success; DWORD bytes = 0; DWORD flags = 0; @@ -71,14 +72,14 @@ static int do_iocp_work() { gpr_time_to_millis(wait_time)); if (!success && !overlapped) { /* The deadline got attained. */ - return 0; + return; } GPR_ASSERT(completion_key && overlapped); if (overlapped == &g_iocp_custom_overlap) { if (completion_key == (ULONG_PTR) &g_iocp_kick_token) { /* We were awoken from a kick. */ gpr_log(GPR_DEBUG, "do_iocp_work - got a kick"); - return 1; + return; } gpr_log(GPR_ERROR, "Unknown custom completion key."); abort(); @@ -97,8 +98,13 @@ static int do_iocp_work() { } success = WSAGetOverlappedResult(socket->socket, &info->overlapped, &bytes, FALSE, &flags); - gpr_log(GPR_DEBUG, "bytes: %u, flags: %u - op %s", bytes, flags, - success ? "succeeded" : "failed"); + gpr_log(GPR_DEBUG, "bytes: %u, flags: %u - op %s %s", bytes, flags, + success ? "succeeded" : "failed", socket->orphan ? "orphan" : ""); + if (socket->orphan) { + grpc_winsocket_destroy(socket); + gpr_atm_full_fetch_add(&g_orphans, -1); + return; + } info->bytes_transfered = bytes; info->wsa_error = success ? 0 : WSAGetLastError(); GPR_ASSERT(overlapped == &info->overlapped); @@ -113,12 +119,10 @@ static int do_iocp_work() { } gpr_mu_unlock(&socket->state_mu); if (f) f(opaque, 1); - - return 1; } static void iocp_loop(void *p) { - while (!gpr_event_get(&g_shutdown_iocp)) { + while (gpr_atm_acq_load(&g_orphans) || !gpr_event_get(&g_shutdown_iocp)) { grpc_maybe_call_delayed_callbacks(NULL, 1); do_iocp_work(); } @@ -138,13 +142,19 @@ void grpc_iocp_init(void) { gpr_thd_new(&id, iocp_loop, NULL, NULL); } -void grpc_iocp_shutdown(void) { +void grpc_iocp_kick(void) { BOOL success; - gpr_event_set(&g_shutdown_iocp, (void *)1); + success = PostQueuedCompletionStatus(g_iocp, 0, (ULONG_PTR) &g_iocp_kick_token, &g_iocp_custom_overlap); GPR_ASSERT(success); +} + +void grpc_iocp_shutdown(void) { + BOOL success; + gpr_event_set(&g_shutdown_iocp, (void *)1); + grpc_iocp_kick(); gpr_event_wait(&g_iocp_done, gpr_inf_future); success = CloseHandle(g_iocp); GPR_ASSERT(success); @@ -166,6 +176,10 @@ void grpc_iocp_add_socket(grpc_winsocket *socket) { GPR_ASSERT(ret == g_iocp); } +void grpc_iocp_socket_orphan(grpc_winsocket *socket) { + gpr_atm_full_fetch_add(&g_orphans, 1); +} + static void socket_notify_on_iocp(grpc_winsocket *socket, void(*cb)(void *, int), void *opaque, grpc_winsocket_callback_info *info) { diff --git a/src/core/iomgr/iocp_windows.h b/src/core/iomgr/iocp_windows.h index 33133193a1..fa3f5eee10 100644 --- a/src/core/iomgr/iocp_windows.h +++ b/src/core/iomgr/iocp_windows.h @@ -42,6 +42,7 @@ void grpc_iocp_init(void); void grpc_iocp_shutdown(void); void grpc_iocp_add_socket(grpc_winsocket *); +void grpc_iocp_socket_orphan(grpc_winsocket *); void grpc_socket_notify_on_write(grpc_winsocket *, void(*cb)(void *, int success), void *opaque); diff --git a/src/core/iomgr/iomgr.c b/src/core/iomgr/iomgr.c index 058685b295..d0e6706fbd 100644 --- a/src/core/iomgr/iomgr.c +++ b/src/core/iomgr/iomgr.c @@ -117,7 +117,16 @@ void grpc_iomgr_shutdown(void) { gpr_mu_lock(&g_mu); } if (g_refs) { - if (gpr_cv_wait(&g_rcv, &g_mu, shutdown_deadline) && g_cbs_head == NULL) { + int timeout = 0; + gpr_timespec short_deadline = gpr_time_add(gpr_now(), + gpr_time_from_millis(100)); + while (gpr_cv_wait(&g_rcv, &g_mu, short_deadline) && g_cbs_head == NULL) { + if (gpr_time_cmp(gpr_now(), shutdown_deadline) > 0) { + timeout = 1; + break; + } + } + if (timeout) { gpr_log(GPR_DEBUG, "Failed to free %d iomgr objects before shutdown deadline: " "memory leaks are likely", diff --git a/src/core/iomgr/socket_windows.c b/src/core/iomgr/socket_windows.c index 99f38b0e03..22dad41783 100644 --- a/src/core/iomgr/socket_windows.c +++ b/src/core/iomgr/socket_windows.c @@ -55,7 +55,7 @@ grpc_winsocket *grpc_winsocket_create(SOCKET socket) { return r; } -void shutdown_op(grpc_winsocket_callback_info *info) { +static void shutdown_op(grpc_winsocket_callback_info *info) { if (!info->cb) return; grpc_iomgr_add_delayed_callback(info->cb, info->opaque, 0); } @@ -68,8 +68,13 @@ void grpc_winsocket_shutdown(grpc_winsocket *socket) { void grpc_winsocket_orphan(grpc_winsocket *socket) { gpr_log(GPR_DEBUG, "grpc_winsocket_orphan"); + grpc_iocp_socket_orphan(socket); + socket->orphan = 1; grpc_iomgr_unref(); closesocket(socket->socket); +} + +void grpc_winsocket_destroy(grpc_winsocket *socket) { gpr_mu_destroy(&socket->state_mu); gpr_free(socket); } diff --git a/src/core/iomgr/socket_windows.h b/src/core/iomgr/socket_windows.h index d4776ab10f..cbae91692c 100644 --- a/src/core/iomgr/socket_windows.h +++ b/src/core/iomgr/socket_windows.h @@ -57,12 +57,13 @@ typedef struct grpc_winsocket_callback_info { typedef struct grpc_winsocket { SOCKET socket; - int added_to_iocp; - grpc_winsocket_callback_info write_info; grpc_winsocket_callback_info read_info; gpr_mu state_mu; + + int added_to_iocp; + int orphan; } grpc_winsocket; /* Create a wrapped windows handle. @@ -71,5 +72,6 @@ grpc_winsocket *grpc_winsocket_create(SOCKET socket); void grpc_winsocket_shutdown(grpc_winsocket *socket); void grpc_winsocket_orphan(grpc_winsocket *socket); +void grpc_winsocket_destroy(grpc_winsocket *socket); #endif /* GRPC_INTERNAL_CORE_IOMGR_SOCKET_WINDOWS_H */ diff --git a/src/core/iomgr/tcp_server.h b/src/core/iomgr/tcp_server.h index 68ee85c5a7..66bb3ef701 100644 --- a/src/core/iomgr/tcp_server.h +++ b/src/core/iomgr/tcp_server.h @@ -71,6 +71,8 @@ int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr, up when grpc_tcp_server_destroy is called. */ int grpc_tcp_server_get_fd(grpc_tcp_server *s, unsigned index); -void grpc_tcp_server_destroy(grpc_tcp_server *server); +void grpc_tcp_server_destroy(grpc_tcp_server *server, + void (*shutdown_done)(void *shutdown_done_arg), + void *shutdown_done_arg); -#endif /* GRPC_INTERNAL_CORE_IOMGR_TCP_SERVER_H */ +#endif /* GRPC_INTERNAL_CORE_IOMGR_TCP_SERVER_H */ diff --git a/src/core/iomgr/tcp_server_posix.c b/src/core/iomgr/tcp_server_posix.c index 90b7eb451d..895f85fc68 100644 --- a/src/core/iomgr/tcp_server_posix.c +++ b/src/core/iomgr/tcp_server_posix.c @@ -102,12 +102,18 @@ struct grpc_tcp_server { gpr_cv cv; /* active port count: how many ports are actually still listening */ - int active_ports; + size_t active_ports; + /* destroyed port count: how many ports are completely destroyed */ + size_t destroyed_ports; /* all listening ports */ server_port *ports; size_t nports; size_t port_capacity; + + /* shutdown callback */ + void (*shutdown_complete)(void *); + void *shutdown_complete_arg; }; grpc_tcp_server *grpc_tcp_server_create(void) { @@ -115,6 +121,7 @@ grpc_tcp_server *grpc_tcp_server_create(void) { gpr_mu_init(&s->mu); gpr_cv_init(&s->cv); s->active_ports = 0; + s->destroyed_ports = 0; s->cb = NULL; s->cb_arg = NULL; s->ports = gpr_malloc(sizeof(server_port) * INIT_PORT_CAP); @@ -123,29 +130,64 @@ grpc_tcp_server *grpc_tcp_server_create(void) { return s; } -void grpc_tcp_server_destroy(grpc_tcp_server *s) { +static void finish_shutdown(grpc_tcp_server *s) { + s->shutdown_complete(s->shutdown_complete_arg); + + gpr_mu_destroy(&s->mu); + gpr_cv_destroy(&s->cv); + + gpr_free(s->ports); + gpr_free(s); +} + +static void destroyed_port(void *server, int success) { + grpc_tcp_server *s = server; + gpr_mu_lock(&s->mu); + s->destroyed_ports++; + if (s->destroyed_ports == s->nports) { + gpr_mu_unlock(&s->mu); + finish_shutdown(s); + } else { + gpr_mu_unlock(&s->mu); + } +} + +static void dont_care_about_shutdown_completion(void *ignored) {} + +void grpc_tcp_server_destroy( + grpc_tcp_server *s, void (*shutdown_complete)(void *shutdown_complete_arg), + void *shutdown_complete_arg) { size_t i; gpr_mu_lock(&s->mu); + + s->shutdown_complete = shutdown_complete + ? shutdown_complete + : dont_care_about_shutdown_completion; + s->shutdown_complete_arg = shutdown_complete_arg; + /* shutdown all fd's */ for (i = 0; i < s->nports; i++) { grpc_fd_shutdown(s->ports[i].emfd); } /* wait while that happens */ + /* TODO(ctiller): make this asynchronous also */ while (s->active_ports) { gpr_cv_wait(&s->cv, &s->mu, gpr_inf_future); } gpr_mu_unlock(&s->mu); /* delete ALL the things */ - for (i = 0; i < s->nports; i++) { - server_port *sp = &s->ports[i]; - if (sp->addr.sockaddr.sa_family == AF_UNIX) { - unlink_if_unix_domain_socket(&sp->addr.un); + if (s->nports) { + for (i = 0; i < s->nports; i++) { + server_port *sp = &s->ports[i]; + if (sp->addr.sockaddr.sa_family == AF_UNIX) { + unlink_if_unix_domain_socket(&sp->addr.un); + } + grpc_fd_orphan(sp->emfd, destroyed_port, s); } - grpc_fd_orphan(sp->emfd, NULL, NULL); + } else { + finish_shutdown(s); } - gpr_free(s->ports); - gpr_free(s); } /* get max listen queue size on linux */ diff --git a/src/core/iomgr/tcp_server_windows.c b/src/core/iomgr/tcp_server_windows.c index 0c3ab1dc91..a43d5670a4 100644 --- a/src/core/iomgr/tcp_server_windows.c +++ b/src/core/iomgr/tcp_server_windows.c @@ -92,7 +92,9 @@ grpc_tcp_server *grpc_tcp_server_create(void) { return s; } -void grpc_tcp_server_destroy(grpc_tcp_server *s) { +void grpc_tcp_server_destroy(grpc_tcp_server *s, + void (*shutdown_done)(void *shutdown_done_arg), + void *shutdown_done_arg) { size_t i; gpr_mu_lock(&s->mu); /* shutdown all fd's */ @@ -112,11 +114,15 @@ void grpc_tcp_server_destroy(grpc_tcp_server *s) { } gpr_free(s->ports); gpr_free(s); + + if (shutdown_done) { + shutdown_done(shutdown_done_arg); + } } /* Prepare a recently-created socket for listening. */ -static int prepare_socket(SOCKET sock, - const struct sockaddr *addr, int addr_len) { +static int prepare_socket(SOCKET sock, const struct sockaddr *addr, + int addr_len) { struct sockaddr_storage sockname_temp; socklen_t sockname_len; @@ -147,15 +153,15 @@ static int prepare_socket(SOCKET sock, } sockname_len = sizeof(sockname_temp); - if (getsockname(sock, (struct sockaddr *) &sockname_temp, &sockname_len) - == SOCKET_ERROR) { + if (getsockname(sock, (struct sockaddr *)&sockname_temp, &sockname_len) == + SOCKET_ERROR) { char *utf8_message = gpr_format_message(WSAGetLastError()); gpr_log(GPR_ERROR, "getsockname: %s", utf8_message); gpr_free(utf8_message); goto error; } - return grpc_sockaddr_get_port((struct sockaddr *) &sockname_temp); + return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); error: if (sock != INVALID_SOCKET) closesocket(sock); @@ -221,8 +227,7 @@ static void on_accept(void *arg, int success) { DWORD transfered_bytes = 0; DWORD flags; BOOL wsa_success = WSAGetOverlappedResult(sock, &info->overlapped, - &transfered_bytes, FALSE, - &flags); + &transfered_bytes, FALSE, &flags); if (!wsa_success) { char *utf8_message = gpr_format_message(WSAGetLastError()); gpr_log(GPR_ERROR, "on_accept error: %s", utf8_message); @@ -257,9 +262,9 @@ static int add_socket_to_server(grpc_tcp_server *s, SOCKET sock, if (sock == INVALID_SOCKET) return -1; - status = WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER, - &guid, sizeof(guid), &AcceptEx, sizeof(AcceptEx), - &ioctl_num_bytes, NULL, NULL); + status = + WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid), + &AcceptEx, sizeof(AcceptEx), &ioctl_num_bytes, NULL, NULL); if (status != 0) { char *utf8_message = gpr_format_message(WSAGetLastError()); @@ -307,9 +312,8 @@ int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr, for (i = 0; i < s->nports; i++) { sockname_len = sizeof(sockname_temp); if (0 == getsockname(s->ports[i].socket->socket, - (struct sockaddr *) &sockname_temp, - &sockname_len)) { - port = grpc_sockaddr_get_port((struct sockaddr *) &sockname_temp); + (struct sockaddr *)&sockname_temp, &sockname_len)) { + port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); if (port > 0) { allocated_addr = malloc(addr_len); memcpy(allocated_addr, addr, addr_len); @@ -330,7 +334,7 @@ int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr, if (grpc_sockaddr_is_wildcard(addr, &port)) { grpc_sockaddr_make_wildcard6(port, &wildcard); - addr = (struct sockaddr *) &wildcard; + addr = (struct sockaddr *)&wildcard; addr_len = sizeof(wildcard); } @@ -369,4 +373,4 @@ void grpc_tcp_server_start(grpc_tcp_server *s, grpc_pollset **pollset, gpr_mu_unlock(&s->mu); } -#endif /* GPR_WINSOCK_SOCKET */ +#endif /* GPR_WINSOCK_SOCKET */ diff --git a/src/core/security/server_secure_chttp2.c b/src/core/security/server_secure_chttp2.c index c155b80b7e..081272724c 100644 --- a/src/core/security/server_secure_chttp2.c +++ b/src/core/security/server_secure_chttp2.c @@ -85,10 +85,10 @@ static void on_secure_transport_setup_done(void *statep, if (status == GRPC_SECURITY_OK) { gpr_mu_lock(&state->mu); if (!state->is_shutdown) { - grpc_create_chttp2_transport( - setup_transport, state->server, - grpc_server_get_channel_args(state->server), - secure_endpoint, NULL, 0, grpc_mdctx_create(), 0); + grpc_create_chttp2_transport(setup_transport, state->server, + grpc_server_get_channel_args(state->server), + secure_endpoint, NULL, 0, + grpc_mdctx_create(), 0); } else { /* We need to consume this here, because the server may already have gone * away. */ @@ -104,7 +104,8 @@ static void on_secure_transport_setup_done(void *statep, static void on_accept(void *statep, grpc_endpoint *tcp) { grpc_server_secure_state *state = statep; state_ref(state); - grpc_setup_secure_transport(state->ctx, tcp, on_secure_transport_setup_done, state); + grpc_setup_secure_transport(state->ctx, tcp, on_secure_transport_setup_done, + state); } /* Server callback: start listening on our ports */ @@ -120,12 +121,14 @@ static void destroy(grpc_server *server, void *statep) { grpc_server_secure_state *state = statep; gpr_mu_lock(&state->mu); state->is_shutdown = 1; - grpc_tcp_server_destroy(state->tcp); + grpc_tcp_server_destroy(state->tcp, grpc_server_listener_destroy_done, + server); gpr_mu_unlock(&state->mu); state_unref(state); } -int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, grpc_server_credentials *creds) { +int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, + grpc_server_credentials *creds) { grpc_resolved_addresses *resolved = NULL; grpc_tcp_server *tcp = NULL; grpc_server_secure_state *state = NULL; @@ -213,7 +216,7 @@ error: grpc_resolved_addresses_destroy(resolved); } if (tcp) { - grpc_tcp_server_destroy(tcp); + grpc_tcp_server_destroy(tcp, NULL, NULL); } if (state) { gpr_free(state); diff --git a/src/php/ext/grpc/completion_queue.h b/src/core/support/thd.c index 1d386cc58f..ec308f3119 100755..100644 --- a/src/php/ext/grpc/completion_queue.h +++ b/src/core/support/thd.c @@ -31,32 +31,36 @@ * */ -#ifndef NET_GRPC_PHP_GRPC_COMPLETION_QUEUE_H_ -#define NET_GRPC_PHP_GRPC_COMPLETION_QUEUE_H_ +/* Posix implementation for gpr threads. */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif +#include <memory.h> -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" -#include "php_grpc.h" +#include <grpc/support/thd.h> -#include "grpc/grpc.h" +enum { + GPR_THD_JOINABLE = 1 +}; -/* Class entry for the PHP CompletionQueue class */ -extern zend_class_entry *grpc_ce_completion_queue; +gpr_thd_options gpr_thd_options_default(void) { + gpr_thd_options options; + memset(&options, 0, sizeof(options)); + return options; +} -/* Wrapper class for grpc_completion_queue that can be associated with a - PHP object */ -typedef struct wrapped_grpc_completion_queue { - zend_object std; +void gpr_thd_options_set_detached(gpr_thd_options *options) { + options->flags &= ~GPR_THD_JOINABLE; +} - grpc_completion_queue *wrapped; -} wrapped_grpc_completion_queue; +void gpr_thd_options_set_joinable(gpr_thd_options *options) { + options->flags |= GPR_THD_JOINABLE; +} -/* Initialize the CompletionQueue class */ -void grpc_init_completion_queue(TSRMLS_D); +int gpr_thd_options_is_detached(const gpr_thd_options *options) { + if (!options) return 1; + return (options->flags & GPR_THD_JOINABLE) == 0; +} -#endif /* NET_GRPC_PHP_GRPC_COMPLETION_QUEUE_H_ */ +int gpr_thd_options_is_joinable(const gpr_thd_options *options) { + if (!options) return 0; + return (options->flags & GPR_THD_JOINABLE) == GPR_THD_JOINABLE; +} diff --git a/src/core/support/thd_posix.c b/src/core/support/thd_posix.c index f50ea58335..fa4eb50556 100644 --- a/src/core/support/thd_posix.c +++ b/src/core/support/thd_posix.c @@ -68,7 +68,11 @@ int gpr_thd_new(gpr_thd_id *t, void (*thd_body)(void *arg), void *arg, a->arg = arg; GPR_ASSERT(pthread_attr_init(&attr) == 0); - GPR_ASSERT(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == 0); + if (gpr_thd_options_is_detached(options)) { + GPR_ASSERT(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == 0); + } else { + GPR_ASSERT(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE) == 0); + } thread_started = (pthread_create(&p, &attr, &thread_body, a) == 0); GPR_ASSERT(pthread_attr_destroy(&attr) == 0); if (!thread_started) { @@ -78,14 +82,12 @@ int gpr_thd_new(gpr_thd_id *t, void (*thd_body)(void *arg), void *arg, return thread_started; } -gpr_thd_options gpr_thd_options_default(void) { - gpr_thd_options options; - memset(&options, 0, sizeof(options)); - return options; -} - gpr_thd_id gpr_thd_currentid(void) { return (gpr_thd_id)pthread_self(); } +void gpr_thd_join(gpr_thd_id t) { + pthread_join((pthread_t)t, NULL); +} + #endif /* GPR_POSIX_SYNC */ diff --git a/src/core/support/thd_win32.c b/src/core/support/thd_win32.c index 347cad57e3..3cc798293a 100644 --- a/src/core/support/thd_win32.c +++ b/src/core/support/thd_win32.c @@ -31,7 +31,7 @@ * */ -/* Posix implementation for gpr threads. */ +/* Windows implementation for gpr threads. */ #include <grpc/support/port_platform.h> @@ -40,47 +40,81 @@ #include <windows.h> #include <string.h> #include <grpc/support/alloc.h> +#include <grpc/support/log.h> #include <grpc/support/thd.h> -struct thd_arg { +#if defined(_MSC_VER) +#define thread_local __declspec(thread) +#elif defined(__GNUC__) +#define thread_local __thread +#else +#error "Unknown compiler - please file a bug report" +#endif + +struct thd_info { void (*body)(void *arg); /* body of a thread */ void *arg; /* argument to a thread */ + HANDLE join_event; /* if joinable, the join event */ + int joinable; /* true if not detached */ }; +static thread_local struct thd_info *g_thd_info; + +/* Destroys a thread info */ +static void destroy_thread(struct thd_info *t) { + if (t->joinable) CloseHandle(t->join_event); + gpr_free(t); +} + /* Body of every thread started via gpr_thd_new. */ static DWORD WINAPI thread_body(void *v) { - struct thd_arg a = *(struct thd_arg *)v; - gpr_free(v); - (*a.body)(a.arg); + g_thd_info = (struct thd_info *)v; + g_thd_info->body(g_thd_info->arg); + if (g_thd_info->joinable) { + BOOL ret = SetEvent(g_thd_info->join_event); + GPR_ASSERT(ret); + } else { + destroy_thread(g_thd_info); + } return 0; } int gpr_thd_new(gpr_thd_id *t, void (*thd_body)(void *arg), void *arg, const gpr_thd_options *options) { HANDLE handle; - DWORD thread_id; - struct thd_arg *a = gpr_malloc(sizeof(*a)); - a->body = thd_body; - a->arg = arg; + struct thd_info *info = gpr_malloc(sizeof(*info)); + info->body = thd_body; + info->arg = arg; *t = 0; - handle = CreateThread(NULL, 64 * 1024, thread_body, a, 0, &thread_id); + if (gpr_thd_options_is_joinable(options)) { + info->joinable = 1; + info->join_event = CreateEvent(NULL, FALSE, FALSE, NULL); + if (info->join_event == NULL) { + gpr_free(info); + return 0; + } + } else { + info->joinable = 0; + } + handle = CreateThread(NULL, 64 * 1024, thread_body, info, 0, NULL); if (handle == NULL) { - gpr_free(a); + destroy_thread(info); } else { - CloseHandle(handle); /* threads are "detached" */ + *t = (gpr_thd_id)info; + CloseHandle(handle); } - *t = (gpr_thd_id)thread_id; return handle != NULL; } -gpr_thd_options gpr_thd_options_default(void) { - gpr_thd_options options; - memset(&options, 0, sizeof(options)); - return options; +gpr_thd_id gpr_thd_currentid(void) { + return (gpr_thd_id)g_thd_info; } -gpr_thd_id gpr_thd_currentid(void) { - return (gpr_thd_id)GetCurrentThreadId(); +void gpr_thd_join(gpr_thd_id t) { + struct thd_info *info = (struct thd_info *)t; + DWORD ret = WaitForSingleObject(info->join_event, INFINITE); + GPR_ASSERT(ret == WAIT_OBJECT_0); + destroy_thread(info); } #endif /* GPR_WIN32 */ diff --git a/src/core/surface/call.c b/src/core/surface/call.c index cfce943794..dba63058b8 100644 --- a/src/core/surface/call.c +++ b/src/core/surface/call.c @@ -1006,6 +1006,8 @@ grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, const grpc_op *op; grpc_ioreq *req; + GRPC_CALL_LOG_BATCH(GPR_INFO, call, ops, nops, tag); + if (nops == 0) { grpc_cq_begin_op(call->cq, call, GRPC_OP_COMPLETE); grpc_cq_end_op_complete(call->cq, tag, call, do_nothing, NULL, GRPC_OP_OK); diff --git a/src/core/surface/call.h b/src/core/surface/call.h index cb81cb52c2..06434f87ac 100644 --- a/src/core/surface/call.h +++ b/src/core/surface/call.h @@ -119,4 +119,13 @@ grpc_call_stack *grpc_call_get_call_stack(grpc_call *call); /* Given the top call_element, get the call object. */ grpc_call *grpc_call_from_top_element(grpc_call_element *surface_element); +extern int grpc_trace_batch; + +void grpc_call_log_batch(char *file, int line, gpr_log_severity severity, + grpc_call *call, const grpc_op *ops, size_t nops, + void *tag); + +#define GRPC_CALL_LOG_BATCH(sev, call, ops, nops, tag) \ + if (grpc_trace_batch) grpc_call_log_batch(sev, call, ops, nops, tag) + #endif /* GRPC_INTERNAL_CORE_SURFACE_CALL_H */ diff --git a/src/core/surface/call_log_batch.c b/src/core/surface/call_log_batch.c new file mode 100644 index 0000000000..a33583a12d --- /dev/null +++ b/src/core/surface/call_log_batch.c @@ -0,0 +1,121 @@ +/* + * + * Copyright 2015, 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 "src/core/surface/call.h" + +#include "src/core/support/string.h" +#include <grpc/support/alloc.h> + +int grpc_trace_batch = 0; + +static void add_metadata(gpr_strvec *b, const grpc_metadata *md, size_t count) { + size_t i; + for(i = 0; i < count; i++) { + gpr_strvec_add(b, gpr_strdup("\nkey=")); + gpr_strvec_add(b, gpr_strdup(md[i].key)); + + gpr_strvec_add(b, gpr_strdup(" value=")); + gpr_strvec_add(b, gpr_hexdump(md[i].value, md[i].value_length, + GPR_HEXDUMP_PLAINTEXT)); + } +} + +char *grpc_op_string(const grpc_op *op) { + char *tmp; + char *out; + + gpr_strvec b; + gpr_strvec_init(&b); + + switch (op->op) { + case GRPC_OP_SEND_INITIAL_METADATA: + gpr_strvec_add(&b, gpr_strdup("SEND_INITIAL_METADATA")); + add_metadata(&b, op->data.send_initial_metadata.metadata, + op->data.send_initial_metadata.count); + break; + case GRPC_OP_SEND_MESSAGE: + gpr_asprintf(&tmp, "SEND_MESSAGE ptr=%p", op->data.send_message); + gpr_strvec_add(&b, tmp); + break; + case GRPC_OP_SEND_CLOSE_FROM_CLIENT: + gpr_strvec_add(&b, gpr_strdup("SEND_CLOSE_FROM_CLIENT")); + break; + case GRPC_OP_SEND_STATUS_FROM_SERVER: + gpr_asprintf(&tmp, "SEND_STATUS_FROM_SERVER status=%d details=%s", + op->data.send_status_from_server.status, + op->data.send_status_from_server.status_details); + gpr_strvec_add(&b, tmp); + add_metadata(&b, op->data.send_status_from_server.trailing_metadata, + op->data.send_status_from_server.trailing_metadata_count); + break; + case GRPC_OP_RECV_INITIAL_METADATA: + gpr_asprintf(&tmp, "RECV_INITIAL_METADATA ptr=%p", + op->data.recv_initial_metadata); + gpr_strvec_add(&b, tmp); + break; + case GRPC_OP_RECV_MESSAGE: + gpr_asprintf(&tmp, "RECV_MESSAGE ptr=%p", op->data.recv_message); + gpr_strvec_add(&b, tmp); + break; + case GRPC_OP_RECV_STATUS_ON_CLIENT: + gpr_asprintf(&tmp, + "RECV_STATUS_ON_CLIENT metadata=%p status=%p details=%p", + op->data.recv_status_on_client.trailing_metadata, + op->data.recv_status_on_client.status, + op->data.recv_status_on_client.status_details); + gpr_strvec_add(&b, tmp); + break; + case GRPC_OP_RECV_CLOSE_ON_SERVER: + gpr_asprintf(&tmp, "RECV_CLOSE_ON_SERVER cancelled=%p", + op->data.recv_close_on_server.cancelled); + gpr_strvec_add(&b, tmp); + } + out = gpr_strvec_flatten(&b, NULL); + gpr_strvec_destroy(&b); + + return out; +} + +void grpc_call_log_batch(char *file, int line, gpr_log_severity severity, + grpc_call *call, const grpc_op *ops, size_t nops, + void *tag) { + char *tmp; + size_t i; + gpr_log(file, line, severity, + "grpc_call_start_batch(%p, %p, %d, 0x%x)", call, ops, nops, tag); + for(i = 0; i < nops; i++) { + tmp = grpc_op_string(&ops[i]); + gpr_log(file, line, severity, "ops[%d]: %s", i, tmp); + gpr_free(tmp); + } +} diff --git a/src/core/surface/completion_queue.c b/src/core/surface/completion_queue.c index 6a1d83ce5d..24f4a05071 100644 --- a/src/core/surface/completion_queue.c +++ b/src/core/surface/completion_queue.c @@ -432,3 +432,11 @@ void grpc_cq_dump_pending_ops(grpc_completion_queue *cc) { grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc) { return &cc->pollset; } + +void grpc_cq_hack_spin_pollset(grpc_completion_queue *cc) { + gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset)); + grpc_pollset_kick(&cc->pollset); + grpc_pollset_work(&cc->pollset, + gpr_time_add(gpr_now(), gpr_time_from_millis(100))); + gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset)); +} diff --git a/src/core/surface/completion_queue.h b/src/core/surface/completion_queue.h index 3054264cad..3a7cc99dda 100644 --- a/src/core/surface/completion_queue.h +++ b/src/core/surface/completion_queue.h @@ -114,4 +114,6 @@ void grpc_cq_dump_pending_ops(grpc_completion_queue *cc); grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc); -#endif /* GRPC_INTERNAL_CORE_SURFACE_COMPLETION_QUEUE_H */ +void grpc_cq_hack_spin_pollset(grpc_completion_queue *cc); + +#endif /* GRPC_INTERNAL_CORE_SURFACE_COMPLETION_QUEUE_H */ diff --git a/src/core/surface/init.c b/src/core/surface/init.c index e48c4202e5..d4f0eb40e8 100644 --- a/src/core/surface/init.c +++ b/src/core/surface/init.c @@ -36,6 +36,7 @@ #include "src/core/debug/trace.h" #include "src/core/statistics/census_interface.h" #include "src/core/channel/channel_stack.h" +#include "src/core/surface/call.h" #include "src/core/surface/init.h" #include "src/core/surface/surface_trace.h" #include "src/core/transport/chttp2_transport.h" @@ -57,6 +58,7 @@ void grpc_init(void) { grpc_register_tracer("channel", &grpc_trace_channel); grpc_register_tracer("surface", &grpc_surface_trace); grpc_register_tracer("http", &grpc_http_trace); + grpc_register_tracer("batch", &grpc_trace_batch); grpc_security_pre_init(); grpc_tracer_init("GRPC_TRACE"); grpc_iomgr_init(); @@ -82,4 +84,3 @@ int grpc_is_initialized(void) { gpr_mu_unlock(&g_init_mu); return r; } - diff --git a/src/core/surface/server.c b/src/core/surface/server.c index 424734c54c..17cba9a505 100644 --- a/src/core/surface/server.c +++ b/src/core/surface/server.c @@ -137,6 +137,7 @@ struct grpc_server { size_t cq_count; gpr_mu mu; + gpr_cv cv; registered_method *registered_methods; requested_call_array requested_calls; @@ -149,6 +150,7 @@ struct grpc_server { channel_data root_channel_data; listener *listeners; + int listeners_destroyed; gpr_refcount internal_refcount; }; @@ -263,6 +265,7 @@ static void server_unref(grpc_server *server) { if (gpr_unref(&server->internal_refcount)) { grpc_channel_args_destroy(server->channel_args); gpr_mu_destroy(&server->mu); + gpr_cv_destroy(&server->cv); gpr_free(server->channel_filters); requested_call_array_destroy(&server->requested_calls); while ((rm = server->registered_methods) != NULL) { @@ -589,9 +592,8 @@ static void destroy_channel_elem(grpc_channel_element *elem) { } static const grpc_channel_filter server_surface_filter = { - call_op, channel_op, sizeof(call_data), - init_call_elem, destroy_call_elem, sizeof(channel_data), - init_channel_elem, destroy_channel_elem, "server", + call_op, channel_op, sizeof(call_data), init_call_elem, destroy_call_elem, + sizeof(channel_data), init_channel_elem, destroy_channel_elem, "server", }; static void addcq(grpc_server *server, grpc_completion_queue *cq) { @@ -620,6 +622,7 @@ grpc_server *grpc_server_create_from_filters(grpc_completion_queue *cq, if (cq) addcq(server, cq); gpr_mu_init(&server->mu); + gpr_cv_init(&server->cv); server->unregistered_cq = cq; /* decremented by grpc_server_destroy */ @@ -733,7 +736,8 @@ grpc_transport_setup_result grpc_server_setup_transport( channel = grpc_channel_create_from_filters(filters, num_filters, s->channel_args, mdctx, 0); chand = (channel_data *)grpc_channel_stack_element( - grpc_channel_get_channel_stack(channel), 0)->channel_data; + grpc_channel_get_channel_stack(channel), 0) + ->channel_data; chand->server = s; server_ref(s); chand->channel = channel; @@ -754,7 +758,7 @@ grpc_transport_setup_result grpc_server_setup_transport( method = grpc_mdstr_from_string(mdctx, rm->method); hash = GRPC_MDSTR_KV_HASH(host ? host->hash : 0, method->hash); for (probes = 0; chand->registered_methods[(hash + probes) % slots] - .server_registered_method != NULL; + .server_registered_method != NULL; probes++) ; if (probes > max_probes) max_probes = probes; @@ -781,6 +785,15 @@ grpc_transport_setup_result grpc_server_setup_transport( return result; } +static int num_listeners(grpc_server *server) { + listener *l; + int n = 0; + for (l = server->listeners; l; l = l->next) { + n++; + } + return n; +} + static void shutdown_internal(grpc_server *server, gpr_uint8 have_shutdown_tag, void *shutdown_tag) { listener *l; @@ -878,11 +891,6 @@ static void shutdown_internal(grpc_server *server, gpr_uint8 have_shutdown_tag, for (l = server->listeners; l; l = l->next) { l->destroy(server, l->arg); } - while (server->listeners) { - l = server->listeners; - server->listeners = l->next; - gpr_free(l); - } } void grpc_server_shutdown(grpc_server *server) { @@ -893,8 +901,18 @@ void grpc_server_shutdown_and_notify(grpc_server *server, void *tag) { shutdown_internal(server, 1, tag); } +void grpc_server_listener_destroy_done(void *s) { + grpc_server *server = s; + gpr_mu_lock(&server->mu); + server->listeners_destroyed++; + gpr_cv_signal(&server->cv); + gpr_mu_unlock(&server->mu); +} + void grpc_server_destroy(grpc_server *server) { channel_data *c; + listener *l; + size_t i; gpr_mu_lock(&server->mu); if (!server->shutdown) { gpr_mu_unlock(&server->mu); @@ -902,6 +920,23 @@ void grpc_server_destroy(grpc_server *server) { gpr_mu_lock(&server->mu); } + while (server->listeners_destroyed != num_listeners(server)) { + for (i = 0; i < server->cq_count; i++) { + gpr_mu_unlock(&server->mu); + grpc_cq_hack_spin_pollset(server->cqs[i]); + gpr_mu_lock(&server->mu); + } + + gpr_cv_wait(&server->cv, &server->mu, + gpr_time_add(gpr_now(), gpr_time_from_millis(100))); + } + + while (server->listeners) { + l = server->listeners; + server->listeners = l->next; + gpr_free(l); + } + for (c = server->root_channel_data.next; c != &server->root_channel_data; c = c->next) { shutdown_channel(c); diff --git a/src/core/surface/server.h b/src/core/surface/server.h index e33f69b8c7..2cfa38fa43 100644 --- a/src/core/surface/server.h +++ b/src/core/surface/server.h @@ -48,9 +48,12 @@ grpc_server *grpc_server_create_from_filters(grpc_completion_queue *cq, and when it shuts down, it will call destroy */ void grpc_server_add_listener(grpc_server *server, void *listener, void (*start)(grpc_server *server, void *arg, - grpc_pollset **pollsets, size_t npollsets), + grpc_pollset **pollsets, + size_t npollsets), void (*destroy)(grpc_server *server, void *arg)); +void grpc_server_listener_destroy_done(void *server); + /* Setup a transport - creates a channel stack, binds the transport to the server */ grpc_transport_setup_result grpc_server_setup_transport( @@ -60,4 +63,4 @@ grpc_transport_setup_result grpc_server_setup_transport( const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server); -#endif /* GRPC_INTERNAL_CORE_SURFACE_SERVER_H */ +#endif /* GRPC_INTERNAL_CORE_SURFACE_SERVER_H */ diff --git a/src/core/surface/server_chttp2.c b/src/core/surface/server_chttp2.c index 27434b39e2..f3b9219f8b 100644 --- a/src/core/surface/server_chttp2.c +++ b/src/core/surface/server_chttp2.c @@ -66,7 +66,8 @@ static void new_transport(void *server, grpc_endpoint *tcp) { } /* Server callback: start listening on our ports */ -static void start(grpc_server *server, void *tcpp, grpc_pollset **pollsets, size_t pollset_count) { +static void start(grpc_server *server, void *tcpp, grpc_pollset **pollsets, + size_t pollset_count) { grpc_tcp_server *tcp = tcpp; grpc_tcp_server_start(tcp, pollsets, pollset_count, new_transport, server); } @@ -75,7 +76,7 @@ static void start(grpc_server *server, void *tcpp, grpc_pollset **pollsets, size callbacks) */ static void destroy(grpc_server *server, void *tcpp) { grpc_tcp_server *tcp = tcpp; - grpc_tcp_server_destroy(tcp); + grpc_tcp_server_destroy(tcp, grpc_server_listener_destroy_done, server); } int grpc_server_add_http2_port(grpc_server *server, const char *addr) { @@ -131,7 +132,7 @@ error: grpc_resolved_addresses_destroy(resolved); } if (tcp) { - grpc_tcp_server_destroy(tcp); + grpc_tcp_server_destroy(tcp, NULL, NULL); } return 0; } diff --git a/src/core/transport/chttp2_transport.c b/src/core/transport/chttp2_transport.c index 2b15b2a812..4c0394d46f 100644 --- a/src/core/transport/chttp2_transport.c +++ b/src/core/transport/chttp2_transport.c @@ -1710,6 +1710,8 @@ static int process_read(transport *t, gpr_slice slice) { gpr_log(GPR_ERROR, "should never reach here"); abort(); + + return 0; } /* tcp read callback */ diff --git a/src/core/transport/metadata.c b/src/core/transport/metadata.c index 1c15716fad..066cc263a1 100644 --- a/src/core/transport/metadata.c +++ b/src/core/transport/metadata.c @@ -97,7 +97,7 @@ static void internal_string_ref(internal_string *s); static void internal_string_unref(internal_string *s); static void discard_metadata(grpc_mdctx *ctx); static void gc_mdtab(grpc_mdctx *ctx); -static void metadata_context_destroy(grpc_mdctx *ctx); +static void metadata_context_destroy_locked(grpc_mdctx *ctx); static void lock(grpc_mdctx *ctx) { gpr_mu_lock(&ctx->mu); } @@ -122,8 +122,7 @@ static void unlock(grpc_mdctx *ctx) { discard_metadata(ctx); } if (ctx->strtab_count == 0) { - gpr_mu_unlock(&ctx->mu); - metadata_context_destroy(ctx); + metadata_context_destroy_locked(ctx); return; } } @@ -185,8 +184,7 @@ static void discard_metadata(grpc_mdctx *ctx) { } } -static void metadata_context_destroy(grpc_mdctx *ctx) { - gpr_mu_lock(&ctx->mu); +static void metadata_context_destroy_locked(grpc_mdctx *ctx) { GPR_ASSERT(ctx->strtab_count == 0); GPR_ASSERT(ctx->mdtab_count == 0); GPR_ASSERT(ctx->mdtab_free == 0); diff --git a/src/core/tsi/ssl_transport_security.c b/src/core/tsi/ssl_transport_security.c index 33645ca8b8..018ddc4456 100644 --- a/src/core/tsi/ssl_transport_security.c +++ b/src/core/tsi/ssl_transport_security.c @@ -567,7 +567,8 @@ static tsi_result populate_ssl_context( EC_KEY* ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); if (!SSL_CTX_set_tmp_ecdh(context, ecdh)) { gpr_log(GPR_ERROR, "Could not set ephemeral ECDH key."); - result = TSI_INTERNAL_ERROR; + EC_KEY_free(ecdh); + return TSI_INTERNAL_ERROR; } SSL_CTX_set_options(context, SSL_OP_SINGLE_ECDH_USE); EC_KEY_free(ecdh); @@ -604,6 +605,7 @@ static tsi_result build_alpn_protocol_name_list( unsigned char* current; *protocol_name_list = NULL; *protocol_name_list_length = 0; + if (num_alpn_protocols == 0) return TSI_INVALID_ARGUMENT; for (i = 0; i < num_alpn_protocols; i++) { if (alpn_protocols_lengths[i] == 0) { gpr_log(GPR_ERROR, "Invalid 0-length protocol name."); diff --git a/src/php/lib/autoload.php b/src/cpp/client/generic_stub.cc index 42eb33d65b..3bf7bdf45f 100755..100644 --- a/src/php/lib/autoload.php +++ b/src/cpp/client/generic_stub.cc @@ -1,4 +1,3 @@ -<?php /* * * Copyright 2015, Google Inc. @@ -31,23 +30,22 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -function grpcAutoloader($class) { - $prefix = 'Grpc\\'; - $base_dir = __DIR__ . '/Grpc/'; +#include <grpc++/generic_stub.h> - $len = strlen($prefix); - if (strncmp($prefix, $class, $len) !== 0) { - return; - } +#include <grpc++/impl/rpc_method.h> - $relative_class = substr($class, $len); +namespace grpc { - $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; - - if (file_exists($file)) { - include $file; - } +// begin a call to a named method +std::unique_ptr<GenericClientAsyncReaderWriter> GenericStub::Call( + ClientContext* context, const grpc::string& method, + CompletionQueue* cq, void* tag) { + return std::unique_ptr<GenericClientAsyncReaderWriter>( + new GenericClientAsyncReaderWriter( + channel_.get(), cq, RpcMethod(method.c_str()), context, tag)); } -spl_autoload_register('grpcAutoloader'); + +} // namespace grpc + diff --git a/src/cpp/client/insecure_credentials.cc b/src/cpp/client/insecure_credentials.cc index f3ca430bd4..8945b038de 100644 --- a/src/cpp/client/insecure_credentials.cc +++ b/src/cpp/client/insecure_credentials.cc @@ -31,8 +31,6 @@ * */ -#include <string> - #include <grpc/grpc.h> #include <grpc/support/log.h> diff --git a/src/cpp/client/secure_credentials.cc b/src/cpp/client/secure_credentials.cc index e3c6637623..d6f9acc675 100644 --- a/src/cpp/client/secure_credentials.cc +++ b/src/cpp/client/secure_credentials.cc @@ -31,8 +31,6 @@ * */ -#include <string> - #include <grpc/grpc_security.h> #include <grpc/support/log.h> diff --git a/src/cpp/common/call.cc b/src/cpp/common/call.cc index 5c26a1ad7c..e75e77e0b5 100644 --- a/src/cpp/common/call.cc +++ b/src/cpp/common/call.cc @@ -60,7 +60,8 @@ CallOpBuffer::CallOpBuffer() status_code_(GRPC_STATUS_OK), status_details_(nullptr), status_details_capacity_(0), - send_status_(nullptr), + send_status_available_(false), + send_status_code_(GRPC_STATUS_OK), trailing_metadata_count_(0), trailing_metadata_(nullptr), cancelled_buf_(0), @@ -104,7 +105,9 @@ void CallOpBuffer::Reset(void* next_return_tag) { status_code_ = GRPC_STATUS_OK; - send_status_ = nullptr; + send_status_available_ = false; + send_status_code_ = GRPC_STATUS_OK; + send_status_details_.clear(); trailing_metadata_count_ = 0; trailing_metadata_ = nullptr; @@ -148,7 +151,7 @@ void FillMetadataMap(grpc_metadata_array* arr, // TODO(yangg) handle duplicates? metadata->insert(std::pair<grpc::string, grpc::string>( arr->metadata[i].key, - {arr->metadata[i].value, arr->metadata[i].value_length})); + grpc::string(arr->metadata[i].value, arr->metadata[i].value_length))); } grpc_metadata_array_destroy(arr); grpc_metadata_array_init(arr); @@ -186,6 +189,7 @@ void CallOpBuffer::AddRecvMessage(grpc::protobuf::Message* message) { void CallOpBuffer::AddRecvMessage(ByteBuffer* message) { recv_message_buffer_ = message; + recv_message_buffer_->Clear(); } void CallOpBuffer::AddClientSendClose() { client_send_close_ = true; } @@ -207,7 +211,9 @@ void CallOpBuffer::AddServerSendStatus( } else { trailing_metadata_count_ = 0; } - send_status_ = &status; + send_status_available_ = true; + send_status_code_ = static_cast<grpc_status_code>(status.code()); + send_status_details_ = status.details(); } void CallOpBuffer::FillOps(grpc_op* ops, size_t* nops) { @@ -256,16 +262,15 @@ void CallOpBuffer::FillOps(grpc_op* ops, size_t* nops) { &status_details_capacity_; (*nops)++; } - if (send_status_) { + if (send_status_available_) { ops[*nops].op = GRPC_OP_SEND_STATUS_FROM_SERVER; ops[*nops].data.send_status_from_server.trailing_metadata_count = trailing_metadata_count_; ops[*nops].data.send_status_from_server.trailing_metadata = trailing_metadata_; - ops[*nops].data.send_status_from_server.status = - static_cast<grpc_status_code>(send_status_->code()); + ops[*nops].data.send_status_from_server.status = send_status_code_; ops[*nops].data.send_status_from_server.status_details = - send_status_->details().c_str(); + send_status_details_.empty() ? nullptr : send_status_details_.c_str(); (*nops)++; } if (recv_closed_) { diff --git a/src/cpp/server/secure_server_credentials.cc b/src/cpp/server/secure_server_credentials.cc index 88f7a9b1a9..49d69a3fb9 100644 --- a/src/cpp/server/secure_server_credentials.cc +++ b/src/cpp/server/secure_server_credentials.cc @@ -59,9 +59,12 @@ class SecureServerCredentials GRPC_FINAL : public ServerCredentials { std::shared_ptr<ServerCredentials> SslServerCredentials( const SslServerCredentialsOptions& options) { std::vector<grpc_ssl_pem_key_cert_pair> pem_key_cert_pairs; - for (const auto& key_cert_pair : options.pem_key_cert_pairs) { - pem_key_cert_pairs.push_back( - {key_cert_pair.private_key.c_str(), key_cert_pair.cert_chain.c_str()}); + for (auto key_cert_pair = options.pem_key_cert_pairs.begin(); + key_cert_pair != options.pem_key_cert_pairs.end(); + key_cert_pair++) { + grpc_ssl_pem_key_cert_pair p = {key_cert_pair->private_key.c_str(), + key_cert_pair->cert_chain.c_str()}; + pem_key_cert_pairs.push_back(p); } grpc_server_credentials* c_creds = grpc_ssl_server_credentials_create( options.pem_root_certs.empty() ? nullptr : options.pem_root_certs.c_str(), diff --git a/src/cpp/server/server.cc b/src/cpp/server/server.cc index 5a4ca6915a..046133c5eb 100644 --- a/src/cpp/server/server.cc +++ b/src/cpp/server/server.cc @@ -107,6 +107,7 @@ class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag { request_payload_(mrd->request_payload_), method_(mrd->method_) { ctx_.call_ = mrd->call_; + ctx_.cq_ = &cq_; GPR_ASSERT(mrd->in_flight_); mrd->in_flight_ = false; mrd->request_metadata_.count = 0; @@ -182,7 +183,7 @@ Server::Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned) Server::~Server() { { - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); if (started_ && !shutdown_) { lock.unlock(); Shutdown(); @@ -247,8 +248,8 @@ bool Server::Start() { // Start processing rpcs. if (!sync_methods_.empty()) { - for (auto& m : sync_methods_) { - m.Request(server_); + for (auto m = sync_methods_.begin(); m != sync_methods_.end(); m++) { + m->Request(server_); } ScheduleCallback(); @@ -258,7 +259,7 @@ bool Server::Start() { } void Server::Shutdown() { - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); if (started_ && !shutdown_) { shutdown_ = true; grpc_server_shutdown(server_); @@ -272,7 +273,7 @@ void Server::Shutdown() { } void Server::Wait() { - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); while (num_running_cb_ != 0) { callback_cv_.wait(lock); } @@ -364,6 +365,7 @@ class Server::AsyncRequest GRPC_FINAL : public CompletionQueueTag { } } ctx->call_ = call_; + ctx->cq_ = cq_; Call call(call_, server_, cq_); if (orig_status && call_) { ctx->BeginCompletionOp(&call); @@ -403,7 +405,7 @@ void Server::RequestAsyncGenericCall(GenericServerContext* context, void Server::ScheduleCallback() { { - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); num_running_cb_++; } thread_pool_->ScheduleCallback(std::bind(&Server::RunRpc, this)); @@ -424,7 +426,7 @@ void Server::RunRpc() { } { - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); num_running_cb_--; if (shutdown_) { callback_cv_.notify_all(); diff --git a/src/cpp/server/server_builder.cc b/src/cpp/server/server_builder.cc index 58bf9d937f..c5e115f396 100644 --- a/src/cpp/server/server_builder.cc +++ b/src/cpp/server/server_builder.cc @@ -86,24 +86,26 @@ std::unique_ptr<Server> ServerBuilder::BuildAndStart() { thread_pool_owned = true; } std::unique_ptr<Server> server(new Server(thread_pool_, thread_pool_owned)); - for (auto* service : services_) { - if (!server->RegisterService(service)) { + for (auto service = services_.begin(); service != services_.end(); + service++) { + if (!server->RegisterService(*service)) { return nullptr; } } - for (auto* service : async_services_) { - if (!server->RegisterAsyncService(service)) { + for (auto service = async_services_.begin(); + service != async_services_.end(); service++) { + if (!server->RegisterAsyncService(*service)) { return nullptr; } } if (generic_service_) { server->RegisterAsyncGenericService(generic_service_); } - for (auto& port : ports_) { - int r = server->AddListeningPort(port.addr, port.creds.get()); + for (auto port = ports_.begin(); port != ports_.end(); port++) { + int r = server->AddListeningPort(port->addr, port->creds.get()); if (!r) return nullptr; - if (port.selected_port != nullptr) { - *port.selected_port = r; + if (port->selected_port != nullptr) { + *port->selected_port = r; } } if (!server->Start()) { diff --git a/src/cpp/server/server_context.cc b/src/cpp/server/server_context.cc index bb3c2d1405..ffd6d30d5d 100644 --- a/src/cpp/server/server_context.cc +++ b/src/cpp/server/server_context.cc @@ -33,9 +33,8 @@ #include <grpc++/server_context.h> -#include <mutex> - #include <grpc++/impl/call.h> +#include <grpc++/impl/sync.h> #include <grpc/grpc.h> #include <grpc/support/log.h> #include "src/cpp/util/time.h" @@ -57,14 +56,14 @@ class ServerContext::CompletionOp GRPC_FINAL : public CallOpBuffer { void Unref(); private: - std::mutex mu_; + grpc::mutex mu_; int refs_; bool finalized_; bool cancelled_; }; void ServerContext::CompletionOp::Unref() { - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); if (--refs_ == 0) { lock.unlock(); delete this; @@ -73,13 +72,13 @@ void ServerContext::CompletionOp::Unref() { bool ServerContext::CompletionOp::CheckCancelled(CompletionQueue* cq) { cq->TryPluck(this); - std::lock_guard<std::mutex> g(mu_); + grpc::lock_guard<grpc::mutex> g(mu_); return finalized_ ? cancelled_ : false; } bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) { GPR_ASSERT(CallOpBuffer::FinalizeResult(tag, status)); - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); finalized_ = true; if (!*status) cancelled_ = true; if (--refs_ == 0) { diff --git a/src/cpp/server/thread_pool.cc b/src/cpp/server/thread_pool.cc index d3013b806c..e8d0e89ed2 100644 --- a/src/cpp/server/thread_pool.cc +++ b/src/cpp/server/thread_pool.cc @@ -31,48 +31,52 @@ * */ +#include <grpc++/impl/sync.h> +#include <grpc++/impl/thd.h> + #include "src/cpp/server/thread_pool.h" namespace grpc { +void ThreadPool::ThreadFunc() { + for (;;) { + // Wait until work is available or we are shutting down. + grpc::unique_lock<grpc::mutex> lock(mu_); + if (!shutdown_ && callbacks_.empty()) { + cv_.wait(lock); + } + // Drain callbacks before considering shutdown to ensure all work + // gets completed. + if (!callbacks_.empty()) { + auto cb = callbacks_.front(); + callbacks_.pop(); + lock.unlock(); + cb(); + } else if (shutdown_) { + return; + } + } +} + ThreadPool::ThreadPool(int num_threads) : shutdown_(false) { for (int i = 0; i < num_threads; i++) { - threads_.push_back(std::thread([this]() { - for (;;) { - // Wait until work is available or we are shutting down. - auto have_work = [this]() { return shutdown_ || !callbacks_.empty(); }; - std::unique_lock<std::mutex> lock(mu_); - if (!have_work()) { - cv_.wait(lock, have_work); - } - // Drain callbacks before considering shutdown to ensure all work - // gets completed. - if (!callbacks_.empty()) { - auto cb = callbacks_.front(); - callbacks_.pop(); - lock.unlock(); - cb(); - } else if (shutdown_) { - return; - } - } - })); + threads_.push_back(grpc::thread(&ThreadPool::ThreadFunc, this)); } } ThreadPool::~ThreadPool() { { - std::lock_guard<std::mutex> lock(mu_); + grpc::lock_guard<grpc::mutex> lock(mu_); shutdown_ = true; cv_.notify_all(); } - for (auto& t : threads_) { - t.join(); + for (auto t = threads_.begin(); t != threads_.end(); t++) { + t->join(); } } void ThreadPool::ScheduleCallback(const std::function<void()>& callback) { - std::lock_guard<std::mutex> lock(mu_); + grpc::lock_guard<grpc::mutex> lock(mu_); callbacks_.push(callback); cv_.notify_one(); } diff --git a/src/cpp/server/thread_pool.h b/src/cpp/server/thread_pool.h index 6225d82a0b..0f24d6e9b3 100644 --- a/src/cpp/server/thread_pool.h +++ b/src/cpp/server/thread_pool.h @@ -35,11 +35,11 @@ #define GRPC_INTERNAL_CPP_SERVER_THREAD_POOL_H #include <grpc++/config.h> + +#include <grpc++/impl/sync.h> +#include <grpc++/impl/thd.h> #include <grpc++/thread_pool_interface.h> -#include <condition_variable> -#include <thread> -#include <mutex> #include <queue> #include <vector> @@ -53,11 +53,13 @@ class ThreadPool GRPC_FINAL : public ThreadPoolInterface { void ScheduleCallback(const std::function<void()>& callback) GRPC_OVERRIDE; private: - std::mutex mu_; - std::condition_variable cv_; + grpc::mutex mu_; + grpc::condition_variable cv_; bool shutdown_; std::queue<std::function<void()>> callbacks_; - std::vector<std::thread> threads_; + std::vector<grpc::thread> threads_; + + void ThreadFunc(); }; } // namespace grpc diff --git a/src/csharp/Grpc.Core.Tests/Properties/AssemblyInfo.cs b/src/csharp/Grpc.Core.Tests/Properties/AssemblyInfo.cs index e4328806ad..e0f0474245 100644 --- a/src/csharp/Grpc.Core.Tests/Properties/AssemblyInfo.cs +++ b/src/csharp/Grpc.Core.Tests/Properties/AssemblyInfo.cs @@ -9,4 +9,4 @@ using System.Runtime.CompilerServices; [assembly: AssemblyCopyright("Google Inc. All rights reserved.")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("0.1.*")] +[assembly: AssemblyVersion("0.2.*")] diff --git a/src/csharp/Grpc.Core/Grpc.Core.nuspec b/src/csharp/Grpc.Core/Grpc.Core.nuspec index af8a8869ca..f2032522c9 100644 --- a/src/csharp/Grpc.Core/Grpc.Core.nuspec +++ b/src/csharp/Grpc.Core/Grpc.Core.nuspec @@ -7,7 +7,7 @@ <description>Core C# implementation of gRPC - an RPC library and framework. See project site for more info. This is an experimental release, not ready to use. </description> - <version>0.1.0</version> + <version>0.2.0</version> <authors>Google Inc.</authors> <owners>jtattermusch</owners> <licenseUrl>https://github.com/grpc/grpc/blob/master/LICENSE</licenseUrl> diff --git a/src/csharp/Grpc.Core/Properties/AssemblyInfo.cs b/src/csharp/Grpc.Core/Properties/AssemblyInfo.cs index 168939cf8c..81218cb67e 100644 --- a/src/csharp/Grpc.Core/Properties/AssemblyInfo.cs +++ b/src/csharp/Grpc.Core/Properties/AssemblyInfo.cs @@ -9,6 +9,6 @@ using System.Runtime.CompilerServices; [assembly: AssemblyCopyright("Google Inc. All rights reserved.")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("0.1.*")] +[assembly: AssemblyVersion("0.2.*")] [assembly: InternalsVisibleTo("Grpc.Core.Tests")] diff --git a/src/csharp/Grpc.Examples.MathClient/MathClient.cs b/src/csharp/Grpc.Examples.MathClient/MathClient.cs index f5956bd33e..ca7683d399 100644 --- a/src/csharp/Grpc.Examples.MathClient/MathClient.cs +++ b/src/csharp/Grpc.Examples.MathClient/MathClient.cs @@ -46,11 +46,15 @@ namespace math MathGrpc.IMathServiceClient stub = new MathGrpc.MathServiceClientStub(channel); MathExamples.DivExample(stub); - MathExamples.FibExample(stub); + MathExamples.DivAsyncExample(stub).Wait(); - MathExamples.SumExample(stub); + MathExamples.FibExample(stub).Wait(); - MathExamples.DivManyExample(stub); + MathExamples.SumExample(stub).Wait(); + + MathExamples.DivManyExample(stub).Wait(); + + MathExamples.DependendRequestsExample(stub).Wait(); } GrpcEnvironment.Shutdown(); diff --git a/src/csharp/Grpc.Examples.MathClient/Properties/AssemblyInfo.cs b/src/csharp/Grpc.Examples.MathClient/Properties/AssemblyInfo.cs index 11fc099a95..1989ca8430 100644 --- a/src/csharp/Grpc.Examples.MathClient/Properties/AssemblyInfo.cs +++ b/src/csharp/Grpc.Examples.MathClient/Properties/AssemblyInfo.cs @@ -9,4 +9,4 @@ using System.Runtime.CompilerServices; [assembly: AssemblyCopyright("Google Inc. All rights reserved.")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("0.1.*")] +[assembly: AssemblyVersion("0.2.*")] diff --git a/src/csharp/Grpc.Examples.MathServer/.gitignore b/src/csharp/Grpc.Examples.MathServer/.gitignore new file mode 100644 index 0000000000..1746e3269e --- /dev/null +++ b/src/csharp/Grpc.Examples.MathServer/.gitignore @@ -0,0 +1,2 @@ +bin +obj diff --git a/src/csharp/Grpc.Examples.MathServer/Grpc.Examples.MathServer.csproj b/src/csharp/Grpc.Examples.MathServer/Grpc.Examples.MathServer.csproj new file mode 100644 index 0000000000..3f7e6c0768 --- /dev/null +++ b/src/csharp/Grpc.Examples.MathServer/Grpc.Examples.MathServer.csproj @@ -0,0 +1,52 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">x86</Platform> + <ProductVersion>10.0.0</ProductVersion> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{BF62FE08-373A-43D6-9D73-41CAA38B7011}</ProjectGuid> + <OutputType>Exe</OutputType> + <RootNamespace>Grpc.Examples.MathServer</RootNamespace> + <AssemblyName>Grpc.Examples.MathServer</AssemblyName> + <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug</OutputPath> + <DefineConstants>DEBUG;</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <Externalconsole>true</Externalconsole> + <PlatformTarget>x86</PlatformTarget> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> + <DebugType>full</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release</OutputPath> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <Externalconsole>true</Externalconsole> + <PlatformTarget>x86</PlatformTarget> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="MathServer.cs" /> + </ItemGroup> + <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> + <ItemGroup> + <ProjectReference Include="..\Grpc.Core\Grpc.Core.csproj"> + <Project>{CCC4440E-49F7-4790-B0AF-FEABB0837AE7}</Project> + <Name>Grpc.Core</Name> + </ProjectReference> + <ProjectReference Include="..\Grpc.Examples\Grpc.Examples.csproj"> + <Project>{7DC1433E-3225-42C7-B7EA-546D56E27A4B}</Project> + <Name>Grpc.Examples</Name> + </ProjectReference> + </ItemGroup> +</Project>
\ No newline at end of file diff --git a/src/csharp/Grpc.Examples.MathServer/MathServer.cs b/src/csharp/Grpc.Examples.MathServer/MathServer.cs new file mode 100644 index 0000000000..884a84d0a6 --- /dev/null +++ b/src/csharp/Grpc.Examples.MathServer/MathServer.cs @@ -0,0 +1,61 @@ +#region Copyright notice and license +// Copyright 2015, 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. +#endregion + +using System; +using System.Runtime.InteropServices; +using System.Threading; +using Grpc.Core; + +namespace math +{ + class MainClass + { + public static void Main(string[] args) + { + String host = "0.0.0.0"; + + GrpcEnvironment.Initialize(); + + Server server = new Server(); + server.AddServiceDefinition(MathGrpc.BindService(new MathServiceImpl())); + int port = server.AddListeningPort(host + ":0"); + server.Start(); + + Console.WriteLine("MathServer listening on port " + port); + + Console.WriteLine("Press any key to stop the server..."); + Console.ReadKey(); + + server.ShutdownAsync().Wait(); + GrpcEnvironment.Shutdown(); + } + } +} diff --git a/src/csharp/Grpc.Examples.MathServer/Properties/AssemblyInfo.cs b/src/csharp/Grpc.Examples.MathServer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..6b3d0516b9 --- /dev/null +++ b/src/csharp/Grpc.Examples.MathServer/Properties/AssemblyInfo.cs @@ -0,0 +1,12 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +[assembly: AssemblyTitle("Grpc.Examples.MathServer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("Google Inc. All rights reserved.")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] +[assembly: AssemblyVersion("0.1.*")] diff --git a/src/csharp/Grpc.Examples.Tests/Grpc.Examples.Tests.csproj b/src/csharp/Grpc.Examples.Tests/Grpc.Examples.Tests.csproj index cf5a640079..f9c1caf700 100644 --- a/src/csharp/Grpc.Examples.Tests/Grpc.Examples.Tests.csproj +++ b/src/csharp/Grpc.Examples.Tests/Grpc.Examples.Tests.csproj @@ -37,6 +37,18 @@ <Reference Include="Google.ProtocolBuffers"> <HintPath>..\packages\Google.ProtocolBuffers.2.4.1.521\lib\net40\Google.ProtocolBuffers.dll</HintPath> </Reference> + <Reference Include="System.Reactive.Interfaces"> + <HintPath>..\packages\Rx-Interfaces.2.2.5\lib\net45\System.Reactive.Interfaces.dll</HintPath> + </Reference> + <Reference Include="System.Reactive.Core"> + <HintPath>..\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll</HintPath> + </Reference> + <Reference Include="System.Reactive.Linq"> + <HintPath>..\packages\Rx-Linq.2.2.5\lib\net45\System.Reactive.Linq.dll</HintPath> + </Reference> + <Reference Include="System.Reactive.PlatformServices"> + <HintPath>..\packages\Rx-PlatformServices.2.2.5\lib\net45\System.Reactive.PlatformServices.dll</HintPath> + </Reference> </ItemGroup> <ItemGroup> <Compile Include="Properties\AssemblyInfo.cs" /> diff --git a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs index 85f213cb39..fa5d6688a6 100644 --- a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs +++ b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs @@ -33,6 +33,7 @@ using System; using System.Collections.Generic; +using System.Reactive.Linq; using System.Threading; using System.Threading.Tasks; using Grpc.Core; @@ -120,14 +121,12 @@ namespace math.Tests [Test] public void Sum() { - var res = client.Sum(); - foreach (var num in new long[] { 10, 20, 30 }) - { - res.Inputs.OnNext(Num.CreateBuilder().SetNum_(num).Build()); - } - res.Inputs.OnCompleted(); + var clientStreamingResult = client.Sum(); + var numList = new List<long> { 10, 20, 30 }.ConvertAll( + n => Num.CreateBuilder().SetNum_(n).Build()); + numList.Subscribe(clientStreamingResult.Inputs); - Assert.AreEqual(60, res.Task.Result.Num_); + Assert.AreEqual(60, clientStreamingResult.Task.Result.Num_); } [Test] @@ -142,13 +141,7 @@ namespace math.Tests var recorder = new RecordingObserver<DivReply>(); var requestObserver = client.DivMany(recorder); - - foreach (var arg in divArgsList) - { - requestObserver.OnNext(arg); - } - requestObserver.OnCompleted(); - + divArgsList.Subscribe(requestObserver); var result = recorder.ToList().Result; CollectionAssert.AreEqual(new long[] { 3, 4, 3 }, result.ConvertAll((divReply) => divReply.Quotient)); diff --git a/src/csharp/Grpc.Examples.Tests/Properties/AssemblyInfo.cs b/src/csharp/Grpc.Examples.Tests/Properties/AssemblyInfo.cs index 43c7616ac3..d78e9210c0 100644 --- a/src/csharp/Grpc.Examples.Tests/Properties/AssemblyInfo.cs +++ b/src/csharp/Grpc.Examples.Tests/Properties/AssemblyInfo.cs @@ -9,4 +9,4 @@ using System.Runtime.CompilerServices; [assembly: AssemblyCopyright("Google Inc. All rights reserved.")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("0.1.*")] +[assembly: AssemblyVersion("0.2.*")] diff --git a/src/csharp/Grpc.Examples.Tests/packages.config b/src/csharp/Grpc.Examples.Tests/packages.config index 51c17bcd5e..06c5e6a4eb 100644 --- a/src/csharp/Grpc.Examples.Tests/packages.config +++ b/src/csharp/Grpc.Examples.Tests/packages.config @@ -1,5 +1,10 @@ -<?xml version="1.0" encoding="utf-8"?> -<packages> - <package id="Google.ProtocolBuffers" version="2.4.1.521" targetFramework="net45" /> - <package id="NUnit" version="2.6.4" targetFramework="net45" /> +<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="Google.ProtocolBuffers" version="2.4.1.521" targetFramework="net45" />
+ <package id="NUnit" version="2.6.4" targetFramework="net45" />
+ <package id="Rx-Core" version="2.2.5" targetFramework="net45" />
+ <package id="Rx-Interfaces" version="2.2.5" targetFramework="net45" />
+ <package id="Rx-Linq" version="2.2.5" targetFramework="net45" />
+ <package id="Rx-Main" version="2.2.5" targetFramework="net45" />
+ <package id="Rx-PlatformServices" version="2.2.5" targetFramework="net45" />
</packages>
\ No newline at end of file diff --git a/src/csharp/Grpc.Examples/MathExamples.cs b/src/csharp/Grpc.Examples/MathExamples.cs index b8bb7eacbd..032372b2a1 100644 --- a/src/csharp/Grpc.Examples/MathExamples.cs +++ b/src/csharp/Grpc.Examples/MathExamples.cs @@ -45,51 +45,45 @@ namespace math Console.WriteLine("Div Result: " + result); } - public static void DivAsyncExample(MathGrpc.IMathServiceClient stub) + public static async Task DivAsyncExample(MathGrpc.IMathServiceClient stub) { - Task<DivReply> call = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); - DivReply result = call.Result; - Console.WriteLine(result); + Task<DivReply> resultTask = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); + DivReply result = await resultTask; + Console.WriteLine("DivAsync Result: " + result); } - public static void DivAsyncWithCancellationExample(MathGrpc.IMathServiceClient stub) + public static async Task DivAsyncWithCancellationExample(MathGrpc.IMathServiceClient stub) { - Task<DivReply> call = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); - DivReply result = call.Result; + Task<DivReply> resultTask = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); + DivReply result = await resultTask; Console.WriteLine(result); } - public static void FibExample(MathGrpc.IMathServiceClient stub) + public static async Task FibExample(MathGrpc.IMathServiceClient stub) { var recorder = new RecordingObserver<Num>(); stub.Fib(new FibArgs.Builder { Limit = 5 }.Build(), recorder); - - List<Num> numbers = recorder.ToList().Result; - Console.WriteLine("Fib Result: " + string.Join("|", recorder.ToList().Result)); + List<Num> result = await recorder.ToList(); + Console.WriteLine("Fib Result: " + string.Join("|", result)); } - public static void SumExample(MathGrpc.IMathServiceClient stub) + public static async Task SumExample(MathGrpc.IMathServiceClient stub) { - List<Num> numbers = new List<Num> + var numbers = new List<Num> { new Num.Builder { Num_ = 1 }.Build(), new Num.Builder { Num_ = 2 }.Build(), new Num.Builder { Num_ = 3 }.Build() }; - var res = stub.Sum(); - foreach (var num in numbers) - { - res.Inputs.OnNext(num); - } - res.Inputs.OnCompleted(); - - Console.WriteLine("Sum Result: " + res.Task.Result); + var clientStreamingResult = stub.Sum(); + numbers.Subscribe(clientStreamingResult.Inputs); + Console.WriteLine("Sum Result: " + await clientStreamingResult.Task); } - public static void DivManyExample(MathGrpc.IMathServiceClient stub) + public static async Task DivManyExample(MathGrpc.IMathServiceClient stub) { - List<DivArgs> divArgsList = new List<DivArgs> + var divArgsList = new List<DivArgs> { new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(), new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(), @@ -97,26 +91,27 @@ namespace math }; var recorder = new RecordingObserver<DivReply>(); - var inputs = stub.DivMany(recorder); - foreach (var input in divArgsList) - { - inputs.OnNext(input); - } - inputs.OnCompleted(); - - Console.WriteLine("DivMany Result: " + string.Join("|", recorder.ToList().Result)); + divArgsList.Subscribe(inputs); + var result = await recorder.ToList(); + Console.WriteLine("DivMany Result: " + string.Join("|", result)); } - public static void DependendRequestsExample(MathGrpc.IMathServiceClient stub) + public static async Task DependendRequestsExample(MathGrpc.IMathServiceClient stub) { - var numberList = new List<Num> + var numbers = new List<Num> { - new Num.Builder { Num_ = 1 }.Build(), - new Num.Builder { Num_ = 2 }.Build(), new Num.Builder { Num_ = 3 }.Build() + new Num.Builder { Num_ = 1 }.Build(), + new Num.Builder { Num_ = 2 }.Build(), + new Num.Builder { Num_ = 3 }.Build() }; - numberList.ToObservable(); + var clientStreamingResult = stub.Sum(); + numbers.Subscribe(clientStreamingResult.Inputs); + Num sum = await clientStreamingResult.Task; + + DivReply result = await stub.DivAsync(new DivArgs.Builder { Dividend = sum.Num_, Divisor = numbers.Count }.Build()); + Console.WriteLine("Avg Result: " + result); } } } diff --git a/src/csharp/Grpc.Examples/Properties/AssemblyInfo.cs b/src/csharp/Grpc.Examples/Properties/AssemblyInfo.cs index b55d24166c..fd1cdbbc1c 100644 --- a/src/csharp/Grpc.Examples/Properties/AssemblyInfo.cs +++ b/src/csharp/Grpc.Examples/Properties/AssemblyInfo.cs @@ -9,4 +9,4 @@ using System.Runtime.CompilerServices; [assembly: AssemblyCopyright("Google Inc. All rights reserved.")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("0.1.*")] +[assembly: AssemblyVersion("0.2.*")] diff --git a/src/csharp/Grpc.IntegrationTesting.Client/Properties/AssemblyInfo.cs b/src/csharp/Grpc.IntegrationTesting.Client/Properties/AssemblyInfo.cs index c93dd1eb2f..d9d36f03e4 100644 --- a/src/csharp/Grpc.IntegrationTesting.Client/Properties/AssemblyInfo.cs +++ b/src/csharp/Grpc.IntegrationTesting.Client/Properties/AssemblyInfo.cs @@ -9,4 +9,4 @@ using System.Runtime.CompilerServices; [assembly: AssemblyCopyright("Google Inc. All rights reserved.")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("0.1.*")] +[assembly: AssemblyVersion("0.2.*")] diff --git a/src/csharp/Grpc.IntegrationTesting.Server/Properties/AssemblyInfo.cs b/src/csharp/Grpc.IntegrationTesting.Server/Properties/AssemblyInfo.cs index f3def1aea4..b0b163b883 100644 --- a/src/csharp/Grpc.IntegrationTesting.Server/Properties/AssemblyInfo.cs +++ b/src/csharp/Grpc.IntegrationTesting.Server/Properties/AssemblyInfo.cs @@ -9,4 +9,4 @@ using System.Runtime.CompilerServices; [assembly: AssemblyCopyright("Google Inc. All rights reserved.")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("0.1.*")] +[assembly: AssemblyVersion("0.2.*")] diff --git a/src/csharp/Grpc.IntegrationTesting/Properties/AssemblyInfo.cs b/src/csharp/Grpc.IntegrationTesting/Properties/AssemblyInfo.cs index f09a448e9e..fe6c8a8aed 100644 --- a/src/csharp/Grpc.IntegrationTesting/Properties/AssemblyInfo.cs +++ b/src/csharp/Grpc.IntegrationTesting/Properties/AssemblyInfo.cs @@ -9,4 +9,4 @@ using System.Runtime.CompilerServices; [assembly: AssemblyCopyright("Google Inc. All rights reserved.")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("0.1.*")] +[assembly: AssemblyVersion("0.2.*")] diff --git a/src/csharp/Grpc.nuspec b/src/csharp/Grpc.nuspec index 96a6aaf6b7..4c106a2ca2 100644 --- a/src/csharp/Grpc.nuspec +++ b/src/csharp/Grpc.nuspec @@ -7,7 +7,7 @@ <description>C# implementation of gRPC - an RPC library and framework. See project site for more info. This is an experimental release, not ready to use. </description> - <version>0.1.0</version> + <version>0.2.0</version> <authors>Google Inc.</authors> <owners>jtattermusch</owners> <licenseUrl>https://github.com/grpc/grpc/blob/master/LICENSE</licenseUrl> @@ -17,7 +17,7 @@ <copyright>Copyright 2015, Google Inc.</copyright> <tags>gRPC RPC Protocol HTTP/2</tags> <dependencies> - <dependency id="Grpc.Core" version="0.1.0" /> + <dependency id="Grpc.Core" version="0.2.0" /> </dependencies> </metadata> </package> diff --git a/src/csharp/Grpc.sln b/src/csharp/Grpc.sln index 2e6d288699..2f8c2e1719 100644 --- a/src/csharp/Grpc.sln +++ b/src/csharp/Grpc.sln @@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Grpc.IntegrationTesting.Cli EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Grpc.IntegrationTesting.Server", "Grpc.IntegrationTesting.Server\Grpc.IntegrationTesting.Server.csproj", "{A654F3B8-E859-4E6A-B30D-227527DBEF0D}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Grpc.Examples.MathServer", "Grpc.Examples.MathServer\Grpc.Examples.MathServer.csproj", "{BF62FE08-373A-43D6-9D73-41CAA38B7011}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
@@ -47,6 +49,10 @@ Global {A654F3B8-E859-4E6A-B30D-227527DBEF0D}.Debug|x86.Build.0 = Debug|x86
{A654F3B8-E859-4E6A-B30D-227527DBEF0D}.Release|x86.ActiveCfg = Release|x86
{A654F3B8-E859-4E6A-B30D-227527DBEF0D}.Release|x86.Build.0 = Release|x86
+ {BF62FE08-373A-43D6-9D73-41CAA38B7011}.Debug|x86.ActiveCfg = Debug|x86
+ {BF62FE08-373A-43D6-9D73-41CAA38B7011}.Debug|x86.Build.0 = Debug|x86
+ {BF62FE08-373A-43D6-9D73-41CAA38B7011}.Release|x86.ActiveCfg = Release|x86
+ {BF62FE08-373A-43D6-9D73-41CAA38B7011}.Release|x86.Build.0 = Release|x86
{C61154BA-DD4A-4838-8420-0162A28925E0}.Debug|x86.ActiveCfg = Debug|x86
{C61154BA-DD4A-4838-8420-0162A28925E0}.Debug|x86.Build.0 = Debug|x86
{C61154BA-DD4A-4838-8420-0162A28925E0}.Release|x86.ActiveCfg = Release|x86
diff --git a/src/csharp/ext/grpc_csharp_ext.c b/src/csharp/ext/grpc_csharp_ext.c index 9a1c908d11..e182468d9b 100644 --- a/src/csharp/ext/grpc_csharp_ext.c +++ b/src/csharp/ext/grpc_csharp_ext.c @@ -731,7 +731,7 @@ grpcsharp_ssl_credentials_create(const char *pem_root_certs, } } -GPR_EXPORT void grpcsharp_credentials_release(grpc_credentials *creds) { +GPR_EXPORT void GPR_CALLTYPE grpcsharp_credentials_release(grpc_credentials *creds) { grpc_credentials_release(creds); } @@ -765,7 +765,7 @@ grpcsharp_ssl_server_credentials_create( return creds; } -GPR_EXPORT void grpcsharp_server_credentials_release( +GPR_EXPORT void GPR_CALLTYPE grpcsharp_server_credentials_release( grpc_server_credentials *creds) { grpc_server_credentials_release(creds); } diff --git a/src/node/binding.gyp b/src/node/binding.gyp index 7ef3bdf4bd..83f72fabca 100644 --- a/src/node/binding.gyp +++ b/src/node/binding.gyp @@ -18,12 +18,29 @@ ], 'link_settings': { 'libraries': [ - '-lrt', '-lpthread', '-lgrpc', '-lgpr' - ], + ] }, + "conditions": [ + ['OS == "mac"', { + 'xcode_settings': { + 'MACOSX_DEPLOYMENT_TARGET': '10.9', + 'OTHER_CFLAGS': [ + '-std=c++11', + '-stdlib=libc++' + ] + } + }], + ['OS != "mac"', { + 'link_settings': { + 'libraries': [ + '-lrt' + ] + } + }] + ], "target_name": "grpc", "sources": [ "ext/byte_buffer.cc", diff --git a/src/node/ext/byte_buffer.cc b/src/node/ext/byte_buffer.cc index 82b54b518c..01bd92ea52 100644 --- a/src/node/ext/byte_buffer.cc +++ b/src/node/ext/byte_buffer.cc @@ -32,7 +32,6 @@ */ #include <string.h> -#include <malloc.h> #include <node.h> #include <nan.h> diff --git a/src/node/ext/channel.cc b/src/node/ext/channel.cc index 787e274973..d37bf763dd 100644 --- a/src/node/ext/channel.cc +++ b/src/node/ext/channel.cc @@ -31,8 +31,6 @@ * */ -#include <malloc.h> - #include <vector> #include <node.h> diff --git a/src/node/ext/server.cc b/src/node/ext/server.cc index e47bac833b..3c2396b810 100644 --- a/src/node/ext/server.cc +++ b/src/node/ext/server.cc @@ -38,8 +38,6 @@ #include <node.h> #include <nan.h> -#include <malloc.h> - #include <vector> #include "grpc/grpc.h" #include "grpc/grpc_security.h" diff --git a/src/node/interop/interop_client.js b/src/node/interop/interop_client.js index 77804cf595..3341486b9e 100644 --- a/src/node/interop/interop_client.js +++ b/src/node/interop/interop_client.js @@ -318,8 +318,8 @@ var test_cases = { empty_stream: emptyStream, cancel_after_begin: cancelAfterBegin, cancel_after_first_response: cancelAfterFirstResponse, - compute_engine_creds: _.partial(authTest, AUTH_USER), - service_account_creds: _.partial(authTest, COMPUTE_ENGINE_USER) + compute_engine_creds: _.partial(authTest, COMPUTE_ENGINE_USER), + service_account_creds: _.partial(authTest, AUTH_USER) }; /** diff --git a/src/node/src/client.js b/src/node/src/client.js index c46f7d0526..fad369c2f8 100644 --- a/src/node/src/client.js +++ b/src/node/src/client.js @@ -241,13 +241,13 @@ function makeUnaryRequestFunction(method, serialize, deserialize) { callback(err); return; } + emitter.emit('status', response.status); if (response.status.code !== grpc.status.OK) { var error = new Error(response.status.details); error.code = response.status.code; callback(error); return; } - emitter.emit('status', response.status); emitter.emit('metadata', response.metadata); callback(null, deserialize(response.read)); }); @@ -312,13 +312,13 @@ function makeClientStreamRequestFunction(method, serialize, deserialize) { callback(err); return; } + stream.emit('status', response.status); if (response.status.code !== grpc.status.OK) { var error = new Error(response.status.details); error.code = response.status.code; callback(error); return; } - stream.emit('status', response.status); callback(null, deserialize(response.read)); }); }); diff --git a/src/node/src/server.js b/src/node/src/server.js index 8a26a43606..05de16294d 100644 --- a/src/node/src/server.js +++ b/src/node/src/server.js @@ -70,6 +70,9 @@ function handleError(call, error) { status.details = error.details; } } + if (error.hasOwnProperty('metadata')) { + status.metadata = error.metadata; + } var error_batch = {}; error_batch[grpc.opType.SEND_STATUS_FROM_SERVER] = status; call.startBatch(error_batch, function(){}); @@ -102,15 +105,20 @@ function waitForCancel(call, emitter) { * @param {*} value The value to respond with * @param {function(*):Buffer=} serialize Serialization function for the * response + * @param {Object=} metadata Optional trailing metadata to send with status */ -function sendUnaryResponse(call, value, serialize) { +function sendUnaryResponse(call, value, serialize, metadata) { var end_batch = {}; - end_batch[grpc.opType.SEND_MESSAGE] = serialize(value); - end_batch[grpc.opType.SEND_STATUS_FROM_SERVER] = { + var status = { code: grpc.status.OK, details: 'OK', metadata: {} }; + if (metadata) { + status.metadata = metadata; + } + end_batch[grpc.opType.SEND_MESSAGE] = serialize(value); + end_batch[grpc.opType.SEND_STATUS_FROM_SERVER] = status; call.startBatch(end_batch, function (){}); } @@ -143,6 +151,7 @@ function setUpWritable(stream, serialize) { function setStatus(err) { var code = grpc.status.INTERNAL; var details = 'Unknown Error'; + var metadata = {}; if (err.hasOwnProperty('message')) { details = err.message; } @@ -152,7 +161,10 @@ function setUpWritable(stream, serialize) { details = err.details; } } - stream.status = {code: code, details: details, metadata: {}}; + if (err.hasOwnProperty('metadata')) { + metadata = err.metadata; + } + stream.status = {code: code, details: details, metadata: metadata}; } /** * Terminate the call. This includes indicating that reads are done, draining @@ -166,6 +178,17 @@ function setUpWritable(stream, serialize) { stream.end(); } stream.on('error', terminateCall); + /** + * Override of Writable#end method that allows for sending metadata with a + * success status. + * @param {Object=} metadata Metadata to send with the status + */ + stream.end = function(metadata) { + if (metadata) { + stream.status.metadata = metadata; + } + Writable.prototype.end.call(this); + }; } /** @@ -335,11 +358,13 @@ function handleUnary(call, handler, metadata) { if (emitter.cancelled) { return; } - handler.func(emitter, function sendUnaryData(err, value) { + handler.func(emitter, function sendUnaryData(err, value, trailer) { if (err) { + err.metadata = trailer; handleError(call, err); + } else { + sendUnaryResponse(call, value, handler.serialize, trailer); } - sendUnaryResponse(call, value, handler.serialize); }); }); } @@ -378,12 +403,14 @@ function handleClientStreaming(call, handler, metadata) { var metadata_batch = {}; metadata_batch[grpc.opType.SEND_INITIAL_METADATA] = metadata; call.startBatch(metadata_batch, function() {}); - handler.func(stream, function(err, value) { + handler.func(stream, function(err, value, trailer) { stream.terminate(); if (err) { + err.metadata = trailer; handleError(call, err); + } else { + sendUnaryResponse(call, value, handler.serialize, trailer); } - sendUnaryResponse(call, value, handler.serialize); }); } diff --git a/src/node/test/surface_test.js b/src/node/test/surface_test.js index 96b47815e1..590c644c71 100644 --- a/src/node/test/surface_test.js +++ b/src/node/test/surface_test.js @@ -126,6 +126,167 @@ describe('Generic client and server', function() { }); }); }); +describe('Trailing metadata', function() { + var client; + var server; + before(function() { + var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto'); + var test_service = test_proto.lookup('TestService'); + var Server = grpc.buildServer([test_service]); + server = new Server({ + TestService: { + unary: function(call, cb) { + var req = call.request; + if (req.error) { + cb(new Error('Requested error'), null, {metadata: ['yes']}); + } else { + cb(null, {count: 1}, {metadata: ['yes']}); + } + }, + clientStream: function(stream, cb){ + var count = 0; + var errored; + stream.on('data', function(data) { + if (data.error) { + errored = true; + cb(new Error('Requested error'), null, {metadata: ['yes']}); + } else { + count += 1; + } + }); + stream.on('end', function() { + if (!errored) { + cb(null, {count: count}, {metadata: ['yes']}); + } + }); + }, + serverStream: function(stream) { + var req = stream.request; + if (req.error) { + var err = new Error('Requested error'); + err.metadata = {metadata: ['yes']}; + stream.emit('error', err); + } else { + for (var i = 0; i < 5; i++) { + stream.write({count: i}); + } + stream.end({metadata: ['yes']}); + } + }, + bidiStream: function(stream) { + var count = 0; + stream.on('data', function(data) { + if (data.error) { + var err = new Error('Requested error'); + err.metadata = { + metadata: ['yes'], + count: ['' + count] + }; + stream.emit('error', err); + } else { + stream.write({count: count}); + count += 1; + } + }); + stream.on('end', function() { + stream.end({metadata: ['yes']}); + }); + } + } + }); + var port = server.bind('localhost:0'); + var Client = surface_client.makeProtobufClientConstructor(test_service); + client = new Client('localhost:' + port); + server.listen(); + }); + after(function() { + server.shutdown(); + }); + it('should be present when a unary call succeeds', function(done) { + var call = client.unary({error: false}, function(err, data) { + assert.ifError(err); + }); + call.on('status', function(status) { + assert.deepEqual(status.metadata.metadata, ['yes']); + done(); + }); + }); + it('should be present when a unary call fails', function(done) { + var call = client.unary({error: true}, function(err, data) { + assert(err); + }); + call.on('status', function(status) { + assert.deepEqual(status.metadata.metadata, ['yes']); + done(); + }); + }); + it('should be present when a client stream call succeeds', function(done) { + var call = client.clientStream(function(err, data) { + assert.ifError(err); + }); + call.write({error: false}); + call.write({error: false}); + call.end(); + call.on('status', function(status) { + assert.deepEqual(status.metadata.metadata, ['yes']); + done(); + }); + }); + it('should be present when a client stream call fails', function(done) { + var call = client.clientStream(function(err, data) { + assert(err); + }); + call.write({error: false}); + call.write({error: true}); + call.end(); + call.on('status', function(status) { + assert.deepEqual(status.metadata.metadata, ['yes']); + done(); + }); + }); + it('should be present when a server stream call succeeds', function(done) { + var call = client.serverStream({error: false}); + call.on('data', function(){}); + call.on('status', function(status) { + assert.strictEqual(status.code, grpc.status.OK); + assert.deepEqual(status.metadata.metadata, ['yes']); + done(); + }); + }); + it('should be present when a server stream call fails', function(done) { + var call = client.serverStream({error: true}); + call.on('data', function(){}); + call.on('status', function(status) { + assert.notStrictEqual(status.code, grpc.status.OK); + assert.deepEqual(status.metadata.metadata, ['yes']); + done(); + }); + }); + it('should be present when a bidi stream succeeds', function(done) { + var call = client.bidiStream(); + call.write({error: false}); + call.write({error: false}); + call.end(); + call.on('data', function(){}); + call.on('status', function(status) { + assert.strictEqual(status.code, grpc.status.OK); + assert.deepEqual(status.metadata.metadata, ['yes']); + done(); + }); + }); + it('should be present when a bidi stream fails', function(done) { + var call = client.bidiStream(); + call.write({error: false}); + call.write({error: true}); + call.end(); + call.on('data', function(){}); + call.on('status', function(status) { + assert.notStrictEqual(status.code, grpc.status.OK); + assert.deepEqual(status.metadata.metadata, ['yes']); + done(); + }); + }); +}); describe('Cancelling surface client', function() { var client; var server; diff --git a/src/node/test/test_service.proto b/src/node/test/test_service.proto new file mode 100644 index 0000000000..5d3d891841 --- /dev/null +++ b/src/node/test/test_service.proto @@ -0,0 +1,52 @@ +// Copyright 2015, 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. + +syntax = "proto2"; + +message Request { + optional bool error = 1; +} + +message Response { + optional int32 count = 1; +} + +service TestService { + rpc Unary (Request) returns (Response) { + } + + rpc ClientStream (stream Request) returns (Response) { + } + + rpc ServerStream (Request) returns (stream Response) { + } + + rpc BidiStream (stream Request) returns (stream Response) { + } +}
\ No newline at end of file diff --git a/src/php/bin/generate_proto_php.sh b/src/php/bin/generate_proto_php.sh new file mode 100755 index 0000000000..16f93747ab --- /dev/null +++ b/src/php/bin/generate_proto_php.sh @@ -0,0 +1,40 @@ +#!/bin/sh +# Copyright 2015, 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. + + +set +e +cd $(dirname $0) + +gen_code='../tests/generated_code' +interop='../tests/interop' + +protoc-gen-php -i $gen_code -o $gen_code $gen_code/math.proto + +protoc-gen-php -i $interop -o $interop $interop/test.proto diff --git a/src/php/bin/interop_client.sh b/src/php/bin/interop_client.sh index 2c61ea8aa0..22e4a493c4 100755 --- a/src/php/bin/interop_client.sh +++ b/src/php/bin/interop_client.sh @@ -31,5 +31,8 @@ set +e cd $(dirname $0) -php -d extension_dir=../ext/grpc/modules/ -d extension=grpc.so \ + +module_dir=`php --version | grep -q 'PHP 5.6' && echo '../ext/grpc' || echo '../ext/grpc/modules'` + +php -d extension_dir=$module_dir -d extension=grpc.so \ ../tests/interop/interop_client.php $@ 1>&2 diff --git a/src/php/bin/run_tests.sh b/src/php/bin/run_tests.sh index c3358ed899..1335672e9e 100755 --- a/src/php/bin/run_tests.sh +++ b/src/php/bin/run_tests.sh @@ -34,13 +34,15 @@ set -e cd $(dirname $0) default_extension_dir=`php -i | grep extension_dir | sed 's/.*=> //g'` +module_dir=`php --version | grep -q 'PHP 5.6' && echo '../ext/grpc' || echo '../ext/grpc/modules'` + # sym-link in system supplied extensions for f in $default_extension_dir/*.so do - ln -s $f ../ext/grpc/modules/$(basename $f) &> /dev/null || true + ln -s $f $module_dir/$(basename $f) &> /dev/null || true done php \ - -d extension_dir=../ext/grpc/modules/ \ + -d extension_dir=$module_dir \ -d extension=grpc.so \ `which phpunit` -v --debug --strict ../tests/unit_tests diff --git a/src/php/composer.json b/src/php/composer.json new file mode 100644 index 0000000000..3c0cb37231 --- /dev/null +++ b/src/php/composer.json @@ -0,0 +1,15 @@ +{ + "name": "grpc/grpc", + "description": "gRPC library for PHP", + "version": "0.5.0", + "homepage": "http://grpc.io", + "license": "BSD-3-Clause", + "require": { + "php": ">=5.5.0" + }, + "autoload": { + "psr-4": { + "Grpc\\": "lib/Grpc/" + } + } +} diff --git a/src/php/composer.lock b/src/php/composer.lock new file mode 100644 index 0000000000..47fc70fd2d --- /dev/null +++ b/src/php/composer.lock @@ -0,0 +1,19 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "65467a098f5fd8b8fe5f7f6e10226f8a", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.5.0" + }, + "platform-dev": [] +} diff --git a/src/php/ext/grpc/byte_buffer.c b/src/php/ext/grpc/byte_buffer.c index 1ced1bf3f0..4f3e6b67af 100644 --- a/src/php/ext/grpc/byte_buffer.c +++ b/src/php/ext/grpc/byte_buffer.c @@ -35,18 +35,18 @@ #include "config.h" #endif -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" -#include "ext/spl/spl_exceptions.h" +#include <php.h> +#include <php_ini.h> +#include <ext/standard/info.h> +#include <ext/spl/spl_exceptions.h> #include "php_grpc.h" #include <string.h> #include "byte_buffer.h" -#include "grpc/grpc.h" -#include "grpc/support/slice.h" +#include <grpc/grpc.h> +#include <grpc/support/slice.h> grpc_byte_buffer *string_to_byte_buffer(char *string, size_t length) { gpr_slice slice = gpr_slice_from_copied_buffer(string, length); @@ -57,6 +57,11 @@ grpc_byte_buffer *string_to_byte_buffer(char *string, size_t length) { void byte_buffer_to_string(grpc_byte_buffer *buffer, char **out_string, size_t *out_length) { + if (buffer == NULL) { + *out_string = NULL; + *out_length = 0; + return; + } size_t length = grpc_byte_buffer_length(buffer); char *string = ecalloc(length + 1, sizeof(char)); size_t offset = 0; diff --git a/src/php/ext/grpc/byte_buffer.h b/src/php/ext/grpc/byte_buffer.h index 7a40638591..0e9d1e7145 100644 --- a/src/php/ext/grpc/byte_buffer.h +++ b/src/php/ext/grpc/byte_buffer.h @@ -34,7 +34,7 @@ #ifndef NET_GRPC_PHP_GRPC_BYTE_BUFFER_H_ #define NET_GRPC_PHP_GRPC_BYTE_BUFFER_H_ -#include "grpc/grpc.h" +#include <grpc/grpc.h> grpc_byte_buffer *string_to_byte_buffer(char *string, size_t length); diff --git a/src/php/ext/grpc/call.c b/src/php/ext/grpc/call.c index 798747109a..b1525e9246 100644 --- a/src/php/ext/grpc/call.c +++ b/src/php/ext/grpc/call.c @@ -37,23 +37,23 @@ #include "config.h" #endif -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" -#include "ext/spl/spl_exceptions.h" +#include <php.h> +#include <php_ini.h> +#include <ext/standard/info.h> +#include <ext/spl/spl_exceptions.h> #include "php_grpc.h" -#include "zend_exceptions.h" -#include "zend_hash.h" +#include <zend_exceptions.h> +#include <zend_hash.h> #include <stdbool.h> -#include "grpc/support/log.h" -#include "grpc/grpc.h" +#include <grpc/support/log.h> +#include <grpc/support/alloc.h> +#include <grpc/grpc.h> #include "timeval.h" #include "channel.h" -#include "completion_queue.h" #include "byte_buffer.h" zend_class_entry *grpc_ce_call; @@ -61,7 +61,19 @@ zend_class_entry *grpc_ce_call; /* Frees and destroys an instance of wrapped_grpc_call */ void free_wrapped_grpc_call(void *object TSRMLS_DC) { wrapped_grpc_call *call = (wrapped_grpc_call *)object; + grpc_event *event; if (call->owned && call->wrapped != NULL) { + if (call->queue != NULL) { + grpc_completion_queue_shutdown(call->queue); + event = grpc_completion_queue_next(call->queue, gpr_inf_future); + while (event != NULL) { + if (event->type == GRPC_QUEUE_SHUTDOWN) { + break; + } + event = grpc_completion_queue_next(call->queue, gpr_inf_future); + } + grpc_completion_queue_destroy(call->queue); + } grpc_call_destroy(call->wrapped); } efree(call); @@ -88,17 +100,23 @@ zend_object_value create_wrapped_grpc_call(zend_class_entry *class_type /* Wraps a grpc_call struct in a PHP object. Owned indicates whether the struct should be destroyed at the end of the object's lifecycle */ -zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned) { +zval *grpc_php_wrap_call(grpc_call *wrapped, grpc_completion_queue *queue, + bool owned) { zval *call_object; MAKE_STD_ZVAL(call_object); object_init_ex(call_object, grpc_ce_call); wrapped_grpc_call *call = (wrapped_grpc_call *)zend_object_store_get_object(call_object TSRMLS_CC); call->wrapped = wrapped; + call->queue = queue; return call_object; } -zval *grpc_call_create_metadata_array(int count, grpc_metadata *elements) { +/* Creates and returns a PHP array object with the data in a + * grpc_metadata_array. Returns NULL on failure */ +zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array) { + int count = metadata_array->count; + grpc_metadata *elements = metadata_array->metadata; int i; zval *array; zval **data = NULL; @@ -139,6 +157,64 @@ zval *grpc_call_create_metadata_array(int count, grpc_metadata *elements) { return array; } +/* Populates a grpc_metadata_array with the data in a PHP array object. + Returns true on success and false on failure */ +bool create_metadata_array(zval *array, grpc_metadata_array *metadata) { + zval **inner_array; + zval **value; + HashTable *array_hash; + HashPosition array_pointer; + HashTable *inner_array_hash; + HashPosition inner_array_pointer; + char *key; + uint key_len; + ulong index; + if (Z_TYPE_P(array) != IS_ARRAY) { + return false; + } + grpc_metadata_array_init(metadata); + array_hash = Z_ARRVAL_P(array); + for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer); + zend_hash_get_current_data_ex(array_hash, (void**)&inner_array, + &array_pointer) == SUCCESS; + zend_hash_move_forward_ex(array_hash, &array_pointer)) { + if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0, + &array_pointer) != HASH_KEY_IS_STRING) { + return false; + } + if (Z_TYPE_P(*inner_array) != IS_ARRAY) { + return false; + } + inner_array_hash = Z_ARRVAL_P(*inner_array); + metadata->capacity += zend_hash_num_elements(inner_array_hash); + } + metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata)); + for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer); + zend_hash_get_current_data_ex(array_hash, (void**)&inner_array, + &array_pointer) == SUCCESS; + zend_hash_move_forward_ex(array_hash, &array_pointer)) { + if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0, + &array_pointer) != HASH_KEY_IS_STRING) { + return false; + } + inner_array_hash = Z_ARRVAL_P(*inner_array); + for (zend_hash_internal_pointer_reset_ex(inner_array_hash, + &inner_array_pointer); + zend_hash_get_current_data_ex(inner_array_hash, (void**)&value, + &inner_array_pointer) == SUCCESS; + zend_hash_move_forward_ex(inner_array_hash, &inner_array_pointer)) { + if (Z_TYPE_P(*value) != IS_STRING) { + return false; + } + metadata->metadata[metadata->count].key = key; + metadata->metadata[metadata->count].value = Z_STRVAL_P(*value); + metadata->metadata[metadata->count].value_length = Z_STRLEN_P(*value); + metadata->count += 1; + } + } + return true; +} + /** * Constructs a new instance of the Call class. * @param Channel $channel The channel to associate the call with. Must not be @@ -157,9 +233,10 @@ PHP_METHOD(Call, __construct) { if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OsO", &channel_obj, grpc_ce_channel, &method, &method_len, &deadline_obj, grpc_ce_timeval) == FAILURE) { - zend_throw_exception(spl_ce_InvalidArgumentException, - "Call expects a Channel, a String, and a Timeval", - 1 TSRMLS_CC); + zend_throw_exception( + spl_ce_InvalidArgumentException, + "Call expects a Channel, a String, and a Timeval", + 1 TSRMLS_CC); return; } wrapped_grpc_channel *channel = @@ -175,289 +252,253 @@ PHP_METHOD(Call, __construct) { wrapped_grpc_timeval *deadline = (wrapped_grpc_timeval *)zend_object_store_get_object( deadline_obj TSRMLS_CC); - call->wrapped = grpc_channel_create_call_old( - channel->wrapped, method, channel->target, deadline->wrapped); + call->queue = grpc_completion_queue_create(); + call->wrapped = grpc_channel_create_call( + channel->wrapped, call->queue, method, channel->target, + deadline->wrapped); } /** - * Add metadata to the call. All array keys must be strings. If the value is a - * string, it is added as a key/value pair. If it is an array, each value is - * added paired with the same string - * @param array $metadata The metadata to add - * @param long $flags A bitwise combination of the Grpc\WRITE_* constants - * (optional) - * @return Void + * Start a batch of RPC actions. + * @param array batch Array of actions to take + * @return object Object with results of all actions */ -PHP_METHOD(Call, add_metadata) { +PHP_METHOD(Call, startBatch) { wrapped_grpc_call *call = (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); - grpc_metadata metadata; - grpc_call_error error_code; + grpc_op ops[8]; + size_t op_num = 0; zval *array; - zval **inner_array; zval **value; + zval **inner_value; HashTable *array_hash; HashPosition array_pointer; - HashTable *inner_array_hash; - HashPosition inner_array_pointer; + HashTable *status_hash; char *key; uint key_len; ulong index; - long flags = 0; - /* "a|l" == 1 array, 1 optional long */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|l", &array, &flags) == + grpc_metadata_array metadata; + grpc_metadata_array trailing_metadata; + grpc_metadata_array recv_metadata; + grpc_metadata_array recv_trailing_metadata; + grpc_status_code status; + char *status_details = NULL; + size_t status_details_capacity = 0; + grpc_byte_buffer *message; + int cancelled; + grpc_call_error error; + grpc_event *event; + zval *result; + char *message_str; + size_t message_len; + zval *recv_status; + grpc_metadata_array_init(&metadata); + grpc_metadata_array_init(&trailing_metadata); + grpc_metadata_array_init(&recv_metadata); + grpc_metadata_array_init(&recv_trailing_metadata); + MAKE_STD_ZVAL(result); + object_init(result); + /* "a" == 1 array */ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) == FAILURE) { zend_throw_exception(spl_ce_InvalidArgumentException, - "add_metadata expects an array and an optional long", - 1 TSRMLS_CC); - return; + "start_batch expects an array", 1 TSRMLS_CC); + goto cleanup; } array_hash = Z_ARRVAL_P(array); for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer); - zend_hash_get_current_data_ex(array_hash, (void**)&inner_array, + zend_hash_get_current_data_ex(array_hash, (void**)&value, &array_pointer) == SUCCESS; zend_hash_move_forward_ex(array_hash, &array_pointer)) { if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0, - &array_pointer) != HASH_KEY_IS_STRING) { + &array_pointer) != HASH_KEY_IS_LONG) { zend_throw_exception(spl_ce_InvalidArgumentException, - "metadata keys must be strings", 1 TSRMLS_CC); - return; + "batch keys must be integers", 1 TSRMLS_CC); + goto cleanup; } - if (Z_TYPE_P(*inner_array) != IS_ARRAY) { - zend_throw_exception(spl_ce_InvalidArgumentException, - "metadata values must be arrays", - 1 TSRMLS_CC); - return; - } - inner_array_hash = Z_ARRVAL_P(*inner_array); - for (zend_hash_internal_pointer_reset_ex(inner_array_hash, - &inner_array_pointer); - zend_hash_get_current_data_ex(inner_array_hash, (void**)&value, - &inner_array_pointer) == SUCCESS; - zend_hash_move_forward_ex(inner_array_hash, &inner_array_pointer)) { - if (Z_TYPE_P(*value) != IS_STRING) { + switch(index) { + case GRPC_OP_SEND_INITIAL_METADATA: + if (!create_metadata_array(*value, &metadata)) { + zend_throw_exception(spl_ce_InvalidArgumentException, + "Bad metadata value given", 1 TSRMLS_CC); + goto cleanup; + } + ops[op_num].data.send_initial_metadata.count = + metadata.count; + ops[op_num].data.send_initial_metadata.metadata = + metadata.metadata; + break; + case GRPC_OP_SEND_MESSAGE: + if (Z_TYPE_PP(value) != IS_STRING) { + zend_throw_exception(spl_ce_InvalidArgumentException, + "Expected a string for send message", + 1 TSRMLS_CC); + } + ops[op_num].data.send_message = + string_to_byte_buffer(Z_STRVAL_PP(value), Z_STRLEN_PP(value)); + break; + case GRPC_OP_SEND_CLOSE_FROM_CLIENT: + break; + case GRPC_OP_SEND_STATUS_FROM_SERVER: + status_hash = Z_ARRVAL_PP(value); + if (zend_hash_find(status_hash, "metadata", sizeof("metadata"), + (void **)&inner_value) == SUCCESS) { + if (!create_metadata_array(*inner_value, &trailing_metadata)) { + zend_throw_exception(spl_ce_InvalidArgumentException, + "Bad trailing metadata value given", + 1 TSRMLS_CC); + goto cleanup; + } + ops[op_num].data.send_status_from_server.trailing_metadata = + trailing_metadata.metadata; + ops[op_num].data.send_status_from_server.trailing_metadata_count = + trailing_metadata.count; + } + if (zend_hash_find(status_hash, "code", sizeof("code"), + (void**)&inner_value) == SUCCESS) { + if (Z_TYPE_PP(inner_value) != IS_LONG) { + zend_throw_exception(spl_ce_InvalidArgumentException, + "Status code must be an integer", + 1 TSRMLS_CC); + goto cleanup; + } + ops[op_num].data.send_status_from_server.status = + Z_LVAL_PP(inner_value); + } else { + zend_throw_exception(spl_ce_InvalidArgumentException, + "Integer status code is required", + 1 TSRMLS_CC); + goto cleanup; + } + if (zend_hash_find(status_hash, "details", sizeof("details"), + (void**)&inner_value) == SUCCESS) { + if (Z_TYPE_PP(inner_value) != IS_STRING) { + zend_throw_exception(spl_ce_InvalidArgumentException, + "Status details must be a string", + 1 TSRMLS_CC); + goto cleanup; + } + ops[op_num].data.send_status_from_server.status_details = + Z_STRVAL_PP(inner_value); + } else { + zend_throw_exception(spl_ce_InvalidArgumentException, + "String status details is required", + 1 TSRMLS_CC); + goto cleanup; + } + break; + case GRPC_OP_RECV_INITIAL_METADATA: + ops[op_num].data.recv_initial_metadata = &recv_metadata; + break; + case GRPC_OP_RECV_MESSAGE: + ops[op_num].data.recv_message = &message; + break; + case GRPC_OP_RECV_STATUS_ON_CLIENT: + ops[op_num].data.recv_status_on_client.trailing_metadata = + &recv_trailing_metadata; + ops[op_num].data.recv_status_on_client.status = &status; + ops[op_num].data.recv_status_on_client.status_details = + &status_details; + ops[op_num].data.recv_status_on_client.status_details_capacity = + &status_details_capacity; + break; + case GRPC_OP_RECV_CLOSE_ON_SERVER: + ops[op_num].data.recv_close_on_server.cancelled = &cancelled; + break; + default: zend_throw_exception(spl_ce_InvalidArgumentException, - "metadata values must be arrays of strings", - 1 TSRMLS_CC); - return; - } - metadata.key = key; - metadata.value = Z_STRVAL_P(*value); - metadata.value_length = Z_STRLEN_P(*value); - error_code = grpc_call_add_metadata_old(call->wrapped, &metadata, 0u); - MAYBE_THROW_CALL_ERROR(add_metadata, error_code); + "Unrecognized key in batch", 1 TSRMLS_CC); + goto cleanup; } + ops[op_num].op = (grpc_op_type)index; + op_num++; } -} - -/** - * Invoke the RPC. Starts sending metadata and request headers over the wire - * @param CompletionQueue $queue The completion queue to use with this call - * @param long $metadata_tag The tag to associate with returned metadata - * @param long $finished_tag The tag to associate with the finished event - * @param long $flags A bitwise combination of the Grpc\WRITE_* constants - * (optional) - * @return Void - */ -PHP_METHOD(Call, invoke) { - grpc_call_error error_code; - long tag1; - long tag2; - zval *queue_obj; - long flags = 0; - /* "Oll|l" == 1 Object, 3 mandatory longs, 1 optional long */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oll|l", &queue_obj, - grpc_ce_completion_queue, &tag1, &tag2, - &flags) == FAILURE) { - zend_throw_exception( - spl_ce_InvalidArgumentException, - "invoke needs a CompletionQueue, 2 longs, and an optional long", - 1 TSRMLS_CC); - return; + error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped); + if (error != GRPC_CALL_OK) { + zend_throw_exception(spl_ce_LogicException, + "start_batch was called incorrectly", + (long)error TSRMLS_CC); + goto cleanup; } - add_property_zval(getThis(), "completion_queue", queue_obj); - wrapped_grpc_call *call = - (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); - wrapped_grpc_completion_queue *queue = - (wrapped_grpc_completion_queue *)zend_object_store_get_object( - queue_obj TSRMLS_CC); - error_code = grpc_call_invoke_old(call->wrapped, queue->wrapped, (void *)tag1, - (void *)tag2, (gpr_uint32)flags); - MAYBE_THROW_CALL_ERROR(invoke, error_code); -} - -/** - * Accept an incoming RPC, binding a completion queue to it. To be called after - * adding metadata to the call, but before sending messages. Can only be called - * on the server - * @param CompletionQueue $queue The completion queue to use with this call - * @param long $finished_tag The tag to associate with the finished event - * @param long $flags A bitwise combination of the Grpc\WRITE_* constants - * (optional) - * @return Void - */ -PHP_METHOD(Call, server_accept) { - long tag; - zval *queue_obj; - grpc_call_error error_code; - /* "Ol|l" == 1 Object, 1 long */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ol", &queue_obj, - grpc_ce_completion_queue, &tag) == FAILURE) { - zend_throw_exception( - spl_ce_InvalidArgumentException, - "server_accept expects a CompletionQueue, a long, and an optional long", - 1 TSRMLS_CC); - return; - } - add_property_zval(getThis(), "completion_queue", queue_obj); - wrapped_grpc_call *call = - (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); - wrapped_grpc_completion_queue *queue = - (wrapped_grpc_completion_queue *)zend_object_store_get_object( - queue_obj TSRMLS_CC); - error_code = - grpc_call_server_accept_old(call->wrapped, queue->wrapped, (void *)tag); - MAYBE_THROW_CALL_ERROR(server_accept, error_code); -} - -PHP_METHOD(Call, server_end_initial_metadata) { - grpc_call_error error_code; - long flags = 0; - /* "|l" == 1 optional long */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags) == - FAILURE) { - zend_throw_exception(spl_ce_InvalidArgumentException, - "server_end_initial_metadata expects an optional long", - 1 TSRMLS_CC); - } - wrapped_grpc_call *call = - (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); - error_code = grpc_call_server_end_initial_metadata_old(call->wrapped, flags); - MAYBE_THROW_CALL_ERROR(server_end_initial_metadata, error_code); -} - -/** - * Called by clients to cancel an RPC on the server. - * @return Void - */ -PHP_METHOD(Call, cancel) { - wrapped_grpc_call *call = - (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); - grpc_call_error error_code = grpc_call_cancel(call->wrapped); - MAYBE_THROW_CALL_ERROR(cancel, error_code); -} - -/** - * Queue a byte buffer for writing - * @param string $buffer The buffer to queue for writing - * @param long $tag The tag to associate with this write - * @param long $flags A bitwise combination of the Grpc\WRITE_* constants - * (optional) - * @return Void - */ -PHP_METHOD(Call, start_write) { - grpc_call_error error_code; - wrapped_grpc_call *call = - (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); - char *buffer; - int buffer_len; - long tag; - long flags = 0; - /* "Ol|l" == 1 Object, 1 mandatory long, 1 optional long */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l", &buffer, - &buffer_len, &tag, &flags) == FAILURE) { - zend_throw_exception(spl_ce_InvalidArgumentException, - "start_write expects a string and an optional long", + event = grpc_completion_queue_pluck(call->queue, call->wrapped, + gpr_inf_future); + if (event->data.op_complete != GRPC_OP_OK) { + zend_throw_exception(spl_ce_LogicException, + "The batch failed for some reason", 1 TSRMLS_CC); - return; + goto cleanup; } - error_code = grpc_call_start_write_old( - call->wrapped, string_to_byte_buffer(buffer, buffer_len), (void *)tag, - (gpr_uint32)flags); - MAYBE_THROW_CALL_ERROR(start_write, error_code); -} - -/** - * Queue a status for writing - * @param long $status_code The status code to send - * @param string $status_details The status details to send - * @param long $tag The tag to associate with this status - * @return Void - */ -PHP_METHOD(Call, start_write_status) { - grpc_call_error error_code; - wrapped_grpc_call *call = - (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); - long status_code; - int status_details_length; - long tag; - char *status_details; - /* "lsl" == 1 long, 1 string, 1 long */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lsl", &status_code, - &status_details, &status_details_length, - &tag) == FAILURE) { - zend_throw_exception( - spl_ce_InvalidArgumentException, - "start_write_status expects a long, a string, and a long", 1 TSRMLS_CC); - return; + for (int i = 0; i < op_num; i++) { + switch(ops[i].op) { + case GRPC_OP_SEND_INITIAL_METADATA: + add_property_bool(result, "send_metadata", true); + break; + case GRPC_OP_SEND_MESSAGE: + add_property_bool(result, "send_message", true); + break; + case GRPC_OP_SEND_CLOSE_FROM_CLIENT: + add_property_bool(result, "send_close", true); + break; + case GRPC_OP_SEND_STATUS_FROM_SERVER: + add_property_bool(result, "send_status", true); + break; + case GRPC_OP_RECV_INITIAL_METADATA: + array = grpc_parse_metadata_array(&recv_metadata); + add_property_zval(result, "metadata", array); + Z_DELREF_P(array); + break; + case GRPC_OP_RECV_MESSAGE: + byte_buffer_to_string(message, &message_str, &message_len); + if (message_str == NULL) { + add_property_null(result, "message"); + } else { + add_property_stringl(result, "message", message_str, message_len, + false); + } + break; + case GRPC_OP_RECV_STATUS_ON_CLIENT: + MAKE_STD_ZVAL(recv_status); + object_init(recv_status); + array = grpc_parse_metadata_array(&recv_trailing_metadata); + add_property_zval(recv_status, "metadata", array); + Z_DELREF_P(array); + add_property_long(recv_status, "code", status); + add_property_string(recv_status, "details", status_details, true); + add_property_zval(result, "status", recv_status); + Z_DELREF_P(recv_status); + break; + case GRPC_OP_RECV_CLOSE_ON_SERVER: + add_property_bool(result, "cancelled", cancelled); + break; + default: + break; + } } - error_code = grpc_call_start_write_status_old(call->wrapped, - (grpc_status_code)status_code, - status_details, (void *)tag); - MAYBE_THROW_CALL_ERROR(start_write_status, error_code); -} - -/** - * Indicate that there are no more messages to send - * @return Void - */ -PHP_METHOD(Call, writes_done) { - grpc_call_error error_code; - wrapped_grpc_call *call = - (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); - long tag; - /* "l" == 1 long */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &tag) == FAILURE) { - zend_throw_exception(spl_ce_InvalidArgumentException, - "writes_done expects a long", 1 TSRMLS_CC); - return; +cleanup: + grpc_metadata_array_destroy(&metadata); + grpc_metadata_array_destroy(&trailing_metadata); + grpc_metadata_array_destroy(&recv_metadata); + grpc_metadata_array_destroy(&recv_trailing_metadata); + if (status_details != NULL) { + gpr_free(status_details); } - error_code = grpc_call_writes_done_old(call->wrapped, (void *)tag); - MAYBE_THROW_CALL_ERROR(writes_done, error_code); + RETURN_DESTROY_ZVAL(result); } /** - * Initiate a read on a call. Output event contains a byte buffer with the - * result of the read - * @param long $tag The tag to associate with this read - * @return Void + * Cancel the call. This will cause the call to end with STATUS_CANCELLED if it + * has not already ended with another status. */ -PHP_METHOD(Call, start_read) { - grpc_call_error error_code; +PHP_METHOD(Call, cancel) { wrapped_grpc_call *call = (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); - long tag; - /* "l" == 1 long */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &tag) == FAILURE) { - zend_throw_exception(spl_ce_InvalidArgumentException, - "start_read expects a long", 1 TSRMLS_CC); - return; - } - error_code = grpc_call_start_read_old(call->wrapped, (void *)tag); - MAYBE_THROW_CALL_ERROR(start_read, error_code); + grpc_call_cancel(call->wrapped); } static zend_function_entry call_methods[] = { PHP_ME(Call, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) - PHP_ME(Call, server_accept, NULL, ZEND_ACC_PUBLIC) - PHP_ME(Call, server_end_initial_metadata, NULL, ZEND_ACC_PUBLIC) - PHP_ME(Call, add_metadata, NULL, ZEND_ACC_PUBLIC) - PHP_ME(Call, cancel, NULL, ZEND_ACC_PUBLIC) - PHP_ME(Call, invoke, NULL, ZEND_ACC_PUBLIC) - PHP_ME(Call, start_read, NULL, ZEND_ACC_PUBLIC) - PHP_ME(Call, start_write, NULL, ZEND_ACC_PUBLIC) - PHP_ME(Call, start_write_status, NULL, ZEND_ACC_PUBLIC) - PHP_ME(Call, writes_done, NULL, ZEND_ACC_PUBLIC) PHP_FE_END}; + PHP_ME(Call, startBatch, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Call, cancel, NULL, ZEND_ACC_PUBLIC) PHP_FE_END}; void grpc_init_call(TSRMLS_D) { zend_class_entry ce; diff --git a/src/php/ext/grpc/call.h b/src/php/ext/grpc/call.h index bce5d82974..e7eb9a75e2 100644 --- a/src/php/ext/grpc/call.h +++ b/src/php/ext/grpc/call.h @@ -38,23 +38,12 @@ #include "config.h" #endif -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" +#include <php.h> +#include <php_ini.h> +#include <ext/standard/info.h> #include "php_grpc.h" -#include "grpc/grpc.h" - -// Throw an exception if error_code is not OK -#define MAYBE_THROW_CALL_ERROR(func_name, error_code) \ - do { \ - if (error_code != GRPC_CALL_OK) { \ - zend_throw_exception(spl_ce_LogicException, \ - #func_name " was called incorrectly", \ - (long)error_code TSRMLS_CC); \ - return; \ - } \ - } while (0) +#include <grpc/grpc.h> /* Class entry for the Call PHP class */ extern zend_class_entry *grpc_ce_call; @@ -65,16 +54,18 @@ typedef struct wrapped_grpc_call { bool owned; grpc_call *wrapped; + grpc_completion_queue *queue; } wrapped_grpc_call; /* Initializes the Call PHP class */ void grpc_init_call(TSRMLS_D); /* Creates a Call object that wraps the given grpc_call struct */ -zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned); +zval *grpc_php_wrap_call(grpc_call *wrapped, grpc_completion_queue *queue, + bool owned); /* Creates and returns a PHP associative array of metadata from a C array of * call metadata */ -zval *grpc_call_create_metadata_array(int count, grpc_metadata *elements); +zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array); #endif /* NET_GRPC_PHP_GRPC_CHANNEL_H_ */ diff --git a/src/php/ext/grpc/channel.c b/src/php/ext/grpc/channel.c index 5e99332fab..b8262db162 100644 --- a/src/php/ext/grpc/channel.c +++ b/src/php/ext/grpc/channel.c @@ -37,21 +37,20 @@ #include "config.h" #endif -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" -#include "ext/spl/spl_exceptions.h" +#include <php.h> +#include <php_ini.h> +#include <ext/standard/info.h> +#include <ext/spl/spl_exceptions.h> #include "php_grpc.h" -#include "zend_exceptions.h" +#include <zend_exceptions.h> #include <stdbool.h> -#include "grpc/grpc.h" -#include "grpc/support/log.h" -#include "grpc/grpc_security.h" +#include <grpc/grpc.h> +#include <grpc/support/log.h> +#include <grpc/grpc_security.h> -#include "completion_queue.h" #include "server.h" #include "credentials.h" @@ -63,6 +62,7 @@ void free_wrapped_grpc_channel(void *object TSRMLS_DC) { if (channel->wrapped != NULL) { grpc_channel_destroy(channel->wrapped); } + efree(channel->target); efree(channel); } @@ -139,6 +139,9 @@ PHP_METHOD(Channel, __construct) { HashTable *array_hash; zval **creds_obj = NULL; wrapped_grpc_credentials *creds = NULL; + zval **override_obj; + char *override; + int override_len; /* "s|a" == 1 string, 1 optional array */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &target, &target_length, &args_array) == FAILURE) { @@ -146,6 +149,8 @@ PHP_METHOD(Channel, __construct) { "Channel expects a string and an array", 1 TSRMLS_CC); return; } + override = target; + override_len = target_length; if (args_array == NULL) { channel->wrapped = grpc_channel_create(target, NULL); } else { @@ -162,6 +167,19 @@ PHP_METHOD(Channel, __construct) { *creds_obj TSRMLS_CC); zend_hash_del(array_hash, "credentials", 12); } + if (zend_hash_find(array_hash, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, + sizeof(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG), + (void **)&override_obj) == SUCCESS) { + if (Z_TYPE_PP(override_obj) != IS_STRING) { + zend_throw_exception(spl_ce_InvalidArgumentException, + GRPC_SSL_TARGET_NAME_OVERRIDE_ARG + " must be a string", + 1 TSRMLS_CC); + return; + } + override = Z_STRVAL_PP(override_obj); + override_len = Z_STRLEN_PP(override_obj); + } php_grpc_read_args_array(args_array, &args); if (creds == NULL) { channel->wrapped = grpc_channel_create(target, &args); @@ -172,8 +190,8 @@ PHP_METHOD(Channel, __construct) { } efree(args.args); } - channel->target = ecalloc(target_length + 1, sizeof(char)); - memcpy(channel->target, target, target_length); + channel->target = ecalloc(override_len + 1, sizeof(char)); + memcpy(channel->target, override, override_len); } /** diff --git a/src/php/ext/grpc/channel.h b/src/php/ext/grpc/channel.h index 2c79668a4d..c13fa4c6d7 100755 --- a/src/php/ext/grpc/channel.h +++ b/src/php/ext/grpc/channel.h @@ -38,12 +38,12 @@ #include "config.h" #endif -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" +#include <php.h> +#include <php_ini.h> +#include <ext/standard/info.h> #include "php_grpc.h" -#include "grpc/grpc.h" +#include <grpc/grpc.h> /* Class entry for the PHP Channel class */ extern zend_class_entry *grpc_ce_channel; diff --git a/src/php/ext/grpc/completion_queue.c b/src/php/ext/grpc/completion_queue.c deleted file mode 100644 index 93abf5df36..0000000000 --- a/src/php/ext/grpc/completion_queue.c +++ /dev/null @@ -1,170 +0,0 @@ -/* - * - * Copyright 2015, 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 "completion_queue.h" - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" -#include "ext/spl/spl_exceptions.h" -#include "php_grpc.h" - -#include "zend_exceptions.h" - -#include <stdbool.h> - -#include "grpc/grpc.h" - -#include "event.h" -#include "timeval.h" - -zend_class_entry *grpc_ce_completion_queue; - -/* Frees and destroys a wrapped instance of grpc_completion_queue */ -void free_wrapped_grpc_completion_queue(void *object TSRMLS_DC) { - wrapped_grpc_completion_queue *queue = NULL; - grpc_event *event; - queue = (wrapped_grpc_completion_queue *)object; - if (queue->wrapped != NULL) { - grpc_completion_queue_shutdown(queue->wrapped); - event = grpc_completion_queue_next(queue->wrapped, gpr_inf_future); - while (event != NULL) { - if (event->type == GRPC_QUEUE_SHUTDOWN) { - break; - } - event = grpc_completion_queue_next(queue->wrapped, gpr_inf_future); - } - grpc_completion_queue_destroy(queue->wrapped); - } - efree(queue); -} - -/* Initializes an instance of wrapped_grpc_channel to be associated with an - * object of a class specified by class_type */ -zend_object_value create_wrapped_grpc_completion_queue( - zend_class_entry *class_type TSRMLS_DC) { - zend_object_value retval; - wrapped_grpc_completion_queue *intern; - - intern = (wrapped_grpc_completion_queue *)emalloc( - sizeof(wrapped_grpc_completion_queue)); - memset(intern, 0, sizeof(wrapped_grpc_completion_queue)); - - zend_object_std_init(&intern->std, class_type TSRMLS_CC); - object_properties_init(&intern->std, class_type); - retval.handle = zend_objects_store_put( - intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, - free_wrapped_grpc_completion_queue, NULL TSRMLS_CC); - retval.handlers = zend_get_std_object_handlers(); - return retval; -} - -/** - * Construct an instance of CompletionQueue - */ -PHP_METHOD(CompletionQueue, __construct) { - wrapped_grpc_completion_queue *queue = - (wrapped_grpc_completion_queue *)zend_object_store_get_object(getThis() - TSRMLS_CC); - queue->wrapped = grpc_completion_queue_create(); -} - -/** - * Blocks until an event is available, the completion queue is being shutdown, - * or timeout is reached. Returns NULL on timeout, otherwise the event that - * occurred. Callers should call event.finish once they have processed the - * event. - * @param Timeval $timeout The timeout for the event - * @return Event The event that occurred - */ -PHP_METHOD(CompletionQueue, next) { - zval *timeout; - /* "O" == 1 Object */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &timeout, - grpc_ce_timeval) == FAILURE) { - zend_throw_exception(spl_ce_InvalidArgumentException, - "next needs a Timeval", 1 TSRMLS_CC); - return; - } - wrapped_grpc_completion_queue *completion_queue = - (wrapped_grpc_completion_queue *)zend_object_store_get_object(getThis() - TSRMLS_CC); - wrapped_grpc_timeval *wrapped_timeout = - (wrapped_grpc_timeval *)zend_object_store_get_object(timeout TSRMLS_CC); - grpc_event *event = grpc_completion_queue_next(completion_queue->wrapped, - wrapped_timeout->wrapped); - if (event == NULL) { - RETURN_NULL(); - } - zval *wrapped_event = grpc_php_convert_event(event); - RETURN_DESTROY_ZVAL(wrapped_event); -} - -PHP_METHOD(CompletionQueue, pluck) { - long tag; - zval *timeout; - /* "lO" == 1 long, 1 Object */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO", &tag, &timeout, - grpc_ce_timeval) == FAILURE) { - zend_throw_exception(spl_ce_InvalidArgumentException, - "pluck needs a long and a Timeval", 1 TSRMLS_CC); - } - wrapped_grpc_completion_queue *completion_queue = - (wrapped_grpc_completion_queue *)zend_object_store_get_object(getThis() - TSRMLS_CC); - wrapped_grpc_timeval *wrapped_timeout = - (wrapped_grpc_timeval *)zend_object_store_get_object(timeout TSRMLS_CC); - grpc_event *event = grpc_completion_queue_pluck( - completion_queue->wrapped, (void *)tag, wrapped_timeout->wrapped); - if (event == NULL) { - RETURN_NULL(); - } - zval *wrapped_event = grpc_php_convert_event(event); - RETURN_DESTROY_ZVAL(wrapped_event); -} - -static zend_function_entry completion_queue_methods[] = { - PHP_ME(CompletionQueue, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) - PHP_ME(CompletionQueue, next, NULL, ZEND_ACC_PUBLIC) - PHP_ME(CompletionQueue, pluck, NULL, ZEND_ACC_PUBLIC) PHP_FE_END}; - -void grpc_init_completion_queue(TSRMLS_D) { - zend_class_entry ce; - INIT_CLASS_ENTRY(ce, "Grpc\\CompletionQueue", completion_queue_methods); - ce.create_object = create_wrapped_grpc_completion_queue; - grpc_ce_completion_queue = zend_register_internal_class(&ce TSRMLS_CC); -} diff --git a/src/php/ext/grpc/config.m4 b/src/php/ext/grpc/config.m4 index 27c67781e7..11778e3bb6 100755 --- a/src/php/ext/grpc/config.m4 +++ b/src/php/ext/grpc/config.m4 @@ -66,5 +66,5 @@ if test "$PHP_GRPC" != "no"; then PHP_SUBST(GRPC_SHARED_LIBADD) - PHP_NEW_EXTENSION(grpc, byte_buffer.c call.c channel.c completion_queue.c credentials.c event.c timeval.c server.c server_credentials.c php_grpc.c, $ext_shared, , -Wall -Werror -pedantic -std=c99) + PHP_NEW_EXTENSION(grpc, byte_buffer.c call.c channel.c credentials.c timeval.c server.c server_credentials.c php_grpc.c, $ext_shared, , -Wall -Werror -std=c11) fi diff --git a/src/php/ext/grpc/credentials.c b/src/php/ext/grpc/credentials.c index a94b0eac2d..a262b9981f 100644 --- a/src/php/ext/grpc/credentials.c +++ b/src/php/ext/grpc/credentials.c @@ -37,17 +37,17 @@ #include "config.h" #endif -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" -#include "ext/spl/spl_exceptions.h" +#include <php.h> +#include <php_ini.h> +#include <ext/standard/info.h> +#include <ext/spl/spl_exceptions.h> #include "php_grpc.h" -#include "zend_exceptions.h" -#include "zend_hash.h" +#include <zend_exceptions.h> +#include <zend_hash.h> -#include "grpc/grpc.h" -#include "grpc/grpc_security.h" +#include <grpc/grpc.h> +#include <grpc/grpc_security.h> zend_class_entry *grpc_ce_credentials; diff --git a/src/php/ext/grpc/event.c b/src/php/ext/grpc/event.c deleted file mode 100644 index 452c4b8bcb..0000000000 --- a/src/php/ext/grpc/event.c +++ /dev/null @@ -1,150 +0,0 @@ -/* - * - * Copyright 2015, 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 "event.h" - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" -#include "php_grpc.h" - -#include <stdbool.h> - -#include "grpc/grpc.h" - -#include "byte_buffer.h" -#include "call.h" -#include "timeval.h" - -/* Create a new PHP object containing the event data in the event struct. - event must not be used after this function is called */ -zval *grpc_php_convert_event(grpc_event *event) { - zval *data_object; - char *detail_string; - size_t detail_len; - char *method_string; - size_t method_len; - char *host_string; - size_t host_len; - char *read_string; - size_t read_len; - - zval *event_object; - - if (event == NULL) { - return NULL; - } - - MAKE_STD_ZVAL(event_object); - object_init(event_object); - - add_property_zval( - event_object, "call", - grpc_php_wrap_call(event->call, event->type == GRPC_SERVER_RPC_NEW)); - add_property_long(event_object, "type", event->type); - add_property_long(event_object, "tag", (long)event->tag); - - switch (event->type) { - case GRPC_QUEUE_SHUTDOWN: - add_property_null(event_object, "data"); - break; - case GRPC_READ: - if (event->data.read == NULL) { - add_property_null(event_object, "data"); - } else { - byte_buffer_to_string(event->data.read, &read_string, &read_len); - add_property_stringl(event_object, "data", read_string, read_len, true); - } - break; - case GRPC_WRITE_ACCEPTED: - add_property_long(event_object, "data", (long)event->data.write_accepted); - break; - case GRPC_FINISH_ACCEPTED: - add_property_long(event_object, "data", - (long)event->data.finish_accepted); - break; - case GRPC_CLIENT_METADATA_READ: - data_object = grpc_call_create_metadata_array( - event->data.client_metadata_read.count, - event->data.client_metadata_read.elements); - add_property_zval(event_object, "data", data_object); - break; - case GRPC_FINISHED: - MAKE_STD_ZVAL(data_object); - object_init(data_object); - add_property_long(data_object, "code", event->data.finished.status); - if (event->data.finished.details == NULL) { - add_property_null(data_object, "details"); - } else { - detail_len = strlen(event->data.finished.details); - detail_string = ecalloc(detail_len + 1, sizeof(char)); - memcpy(detail_string, event->data.finished.details, detail_len); - add_property_string(data_object, "details", detail_string, true); - } - add_property_zval(data_object, "metadata", - grpc_call_create_metadata_array( - event->data.finished.metadata_count, - event->data.finished.metadata_elements)); - add_property_zval(event_object, "data", data_object); - break; - case GRPC_SERVER_RPC_NEW: - MAKE_STD_ZVAL(data_object); - object_init(data_object); - method_len = strlen(event->data.server_rpc_new.method); - method_string = ecalloc(method_len + 1, sizeof(char)); - memcpy(method_string, event->data.server_rpc_new.method, method_len); - add_property_string(data_object, "method", method_string, false); - host_len = strlen(event->data.server_rpc_new.host); - host_string = ecalloc(host_len + 1, sizeof(char)); - memcpy(host_string, event->data.server_rpc_new.host, host_len); - add_property_string(data_object, "host", host_string, false); - add_property_zval( - data_object, "absolute_timeout", - grpc_php_wrap_timeval(event->data.server_rpc_new.deadline)); - add_property_zval(data_object, "metadata", - grpc_call_create_metadata_array( - event->data.server_rpc_new.metadata_count, - event->data.server_rpc_new.metadata_elements)); - add_property_zval(event_object, "data", data_object); - break; - default: - add_property_null(event_object, "data"); - break; - } - grpc_event_finish(event); - return event_object; -} diff --git a/src/php/ext/grpc/php_grpc.c b/src/php/ext/grpc/php_grpc.c index 67e366c385..3e669ecd17 100644 --- a/src/php/ext/grpc/php_grpc.c +++ b/src/php/ext/grpc/php_grpc.c @@ -34,8 +34,6 @@ #include "call.h" #include "channel.h" #include "server.h" -#include "completion_queue.h" -#include "event.h" #include "timeval.h" #include "credentials.h" #include "server_credentials.h" @@ -44,9 +42,9 @@ #include "config.h" #endif -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" +#include <php.h> +#include <php_ini.h> +#include <ext/standard/info.h> #include "php_grpc.h" // ZEND_DECLARE_MODULE_GLOBALS(grpc) @@ -127,27 +125,12 @@ PHP_MINIT_FUNCTION(grpc) { REGISTER_LONG_CONSTANT("Grpc\\CALL_ERROR_INVALID_FLAGS", GRPC_CALL_ERROR_INVALID_FLAGS, CONST_CS); - /* Register op error constants */ - REGISTER_LONG_CONSTANT("Grpc\\OP_OK", GRPC_OP_OK, CONST_CS); - REGISTER_LONG_CONSTANT("Grpc\\OP_ERROR", GRPC_OP_ERROR, CONST_CS); - /* Register flag constants */ REGISTER_LONG_CONSTANT("Grpc\\WRITE_BUFFER_HINT", GRPC_WRITE_BUFFER_HINT, CONST_CS); REGISTER_LONG_CONSTANT("Grpc\\WRITE_NO_COMPRESS", GRPC_WRITE_NO_COMPRESS, CONST_CS); - /* Register completion type constants */ - REGISTER_LONG_CONSTANT("Grpc\\QUEUE_SHUTDOWN", GRPC_QUEUE_SHUTDOWN, CONST_CS); - REGISTER_LONG_CONSTANT("Grpc\\READ", GRPC_READ, CONST_CS); - REGISTER_LONG_CONSTANT("Grpc\\FINISH_ACCEPTED", GRPC_FINISH_ACCEPTED, - CONST_CS); - REGISTER_LONG_CONSTANT("Grpc\\WRITE_ACCEPTED", GRPC_WRITE_ACCEPTED, CONST_CS); - REGISTER_LONG_CONSTANT("Grpc\\CLIENT_METADATA_READ", - GRPC_CLIENT_METADATA_READ, CONST_CS); - REGISTER_LONG_CONSTANT("Grpc\\FINISHED", GRPC_FINISHED, CONST_CS); - REGISTER_LONG_CONSTANT("Grpc\\SERVER_RPC_NEW", GRPC_SERVER_RPC_NEW, CONST_CS); - /* Register status constants */ REGISTER_LONG_CONSTANT("Grpc\\STATUS_OK", GRPC_STATUS_OK, CONST_CS); REGISTER_LONG_CONSTANT("Grpc\\STATUS_CANCELLED", GRPC_STATUS_CANCELLED, @@ -181,10 +164,27 @@ PHP_MINIT_FUNCTION(grpc) { REGISTER_LONG_CONSTANT("Grpc\\STATUS_DATA_LOSS", GRPC_STATUS_DATA_LOSS, CONST_CS); + /* Register op type constants */ + REGISTER_LONG_CONSTANT("Grpc\\OP_SEND_INITIAL_METADATA", + GRPC_OP_SEND_INITIAL_METADATA, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\OP_SEND_MESSAGE", + GRPC_OP_SEND_MESSAGE, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\OP_SEND_CLOSE_FROM_CLIENT", + GRPC_OP_SEND_CLOSE_FROM_CLIENT, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\OP_SEND_STATUS_FROM_SERVER", + GRPC_OP_SEND_STATUS_FROM_SERVER, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\OP_RECV_INITIAL_METADATA", + GRPC_OP_RECV_INITIAL_METADATA, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\OP_RECV_MESSAGE", + GRPC_OP_RECV_MESSAGE, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\OP_RECV_STATUS_ON_CLIENT", + GRPC_OP_RECV_STATUS_ON_CLIENT, CONST_CS); + REGISTER_LONG_CONSTANT("Grpc\\OP_RECV_CLOSE_ON_SERVER", + GRPC_OP_RECV_CLOSE_ON_SERVER, CONST_CS); + grpc_init_call(TSRMLS_C); grpc_init_channel(TSRMLS_C); grpc_init_server(TSRMLS_C); - grpc_init_completion_queue(TSRMLS_C); grpc_init_timeval(TSRMLS_C); grpc_init_credentials(TSRMLS_C); grpc_init_server_credentials(TSRMLS_C); diff --git a/src/php/ext/grpc/server.c b/src/php/ext/grpc/server.c index a5cfd95287..dbb9425619 100644 --- a/src/php/ext/grpc/server.c +++ b/src/php/ext/grpc/server.c @@ -37,30 +37,42 @@ #include "config.h" #endif -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" -#include "ext/spl/spl_exceptions.h" +#include <php.h> +#include <php_ini.h> +#include <ext/standard/info.h> +#include <ext/spl/spl_exceptions.h> #include "php_grpc.h" -#include "zend_exceptions.h" +#include <zend_exceptions.h> #include <stdbool.h> -#include "grpc/grpc.h" -#include "grpc/support/log.h" -#include "grpc/grpc_security.h" +#include <grpc/grpc.h> +#include <grpc/support/log.h> +#include <grpc/grpc_security.h> #include "server.h" -#include "completion_queue.h" #include "channel.h" #include "server_credentials.h" +#include "timeval.h" zend_class_entry *grpc_ce_server; /* Frees and destroys an instance of wrapped_grpc_server */ void free_wrapped_grpc_server(void *object TSRMLS_DC) { wrapped_grpc_server *server = (wrapped_grpc_server *)object; + grpc_event *event; + if (server->queue != NULL) { + grpc_completion_queue_shutdown(server->queue); + event = grpc_completion_queue_next(server->queue, gpr_inf_future); + while (event != NULL) { + if (event->type == GRPC_QUEUE_SHUTDOWN) { + break; + } + event = grpc_completion_queue_next(server->queue, gpr_inf_future); + } + grpc_completion_queue_destroy(server->queue); + } if (server->wrapped != NULL) { grpc_server_shutdown(server->wrapped); grpc_server_destroy(server->wrapped); @@ -95,26 +107,22 @@ zend_object_value create_wrapped_grpc_server(zend_class_entry *class_type PHP_METHOD(Server, __construct) { wrapped_grpc_server *server = (wrapped_grpc_server *)zend_object_store_get_object(getThis() TSRMLS_CC); - zval *queue_obj; zval *args_array = NULL; grpc_channel_args args; - /* "O|a" == 1 Object, 1 optional array */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|a", &queue_obj, - grpc_ce_completion_queue, &args_array) == FAILURE) { + /* "|a" == 1 optional array */ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a", &args_array) == + FAILURE) { zend_throw_exception(spl_ce_InvalidArgumentException, - "Server expects a CompletionQueue and an array", + "Server expects an array", 1 TSRMLS_CC); return; } - add_property_zval(getThis(), "completion_queue", queue_obj); - wrapped_grpc_completion_queue *queue = - (wrapped_grpc_completion_queue *)zend_object_store_get_object( - queue_obj TSRMLS_CC); + server->queue = grpc_completion_queue_create(); if (args_array == NULL) { - server->wrapped = grpc_server_create(queue->wrapped, NULL); + server->wrapped = grpc_server_create(server->queue, NULL); } else { php_grpc_read_args_array(args_array, &args); - server->wrapped = grpc_server_create(queue->wrapped, &args); + server->wrapped = grpc_server_create(server->queue, &args); efree(args.args); } } @@ -125,20 +133,44 @@ PHP_METHOD(Server, __construct) { * @param long $tag_cancel The tag to use if the call is cancelled * @return Void */ -PHP_METHOD(Server, request_call) { +PHP_METHOD(Server, requestCall) { grpc_call_error error_code; wrapped_grpc_server *server = (wrapped_grpc_server *)zend_object_store_get_object(getThis() TSRMLS_CC); - long tag_new; - /* "l" == 1 long */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &tag_new) == - FAILURE) { - zend_throw_exception(spl_ce_InvalidArgumentException, - "request_call expects a long", 1 TSRMLS_CC); - return; + grpc_call *call; + grpc_call_details details; + grpc_metadata_array metadata; + zval *result; + grpc_event *event; + MAKE_STD_ZVAL(result); + object_init(result); + grpc_call_details_init(&details); + grpc_metadata_array_init(&metadata); + error_code = grpc_server_request_call(server->wrapped, &call, &details, + &metadata, server->queue, NULL); + if (error_code != GRPC_CALL_OK) { + zend_throw_exception(spl_ce_LogicException, "request_call failed", + (long)error_code TSRMLS_CC); + goto cleanup; + } + event = grpc_completion_queue_pluck(server->queue, NULL, gpr_inf_future); + if (event->data.op_complete != GRPC_OP_OK) { + zend_throw_exception(spl_ce_LogicException, + "Failed to request a call for some reason", + 1 TSRMLS_CC); + goto cleanup; } - error_code = grpc_server_request_call_old(server->wrapped, (void *)tag_new); - MAYBE_THROW_CALL_ERROR(request_call, error_code); + add_property_zval(result, "call", grpc_php_wrap_call(call, server->queue, + true)); + add_property_string(result, "method", details.method, true); + add_property_string(result, "host", details.host, true); + add_property_zval(result, "absolute_deadline", + grpc_php_wrap_timeval(details.deadline)); + add_property_zval(result, "metadata", grpc_parse_metadata_array(&metadata)); +cleanup: + grpc_call_details_destroy(&details); + grpc_metadata_array_destroy(&metadata); + RETURN_DESTROY_ZVAL(result); } /** @@ -146,7 +178,7 @@ PHP_METHOD(Server, request_call) { * @param string $addr The address to add * @return true on success, false on failure */ -PHP_METHOD(Server, add_http2_port) { +PHP_METHOD(Server, addHttp2Port) { wrapped_grpc_server *server = (wrapped_grpc_server *)zend_object_store_get_object(getThis() TSRMLS_CC); const char *addr; @@ -161,14 +193,14 @@ PHP_METHOD(Server, add_http2_port) { RETURN_LONG(grpc_server_add_http2_port(server->wrapped, addr)); } -PHP_METHOD(Server, add_secure_http2_port) { +PHP_METHOD(Server, addSecureHttp2Port) { wrapped_grpc_server *server = (wrapped_grpc_server *)zend_object_store_get_object(getThis() TSRMLS_CC); const char *addr; int addr_len; zval *creds_obj; /* "sO" == 1 string, 1 object */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &addr, &addr_len, + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sO", &addr, &addr_len, &creds_obj, grpc_ce_server_credentials) == FAILURE) { zend_throw_exception( @@ -195,9 +227,9 @@ PHP_METHOD(Server, start) { static zend_function_entry server_methods[] = { PHP_ME(Server, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) - PHP_ME(Server, request_call, NULL, ZEND_ACC_PUBLIC) - PHP_ME(Server, add_http2_port, NULL, ZEND_ACC_PUBLIC) - PHP_ME(Server, add_secure_http2_port, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Server, requestCall, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Server, addHttp2Port, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Server, addSecureHttp2Port, NULL, ZEND_ACC_PUBLIC) PHP_ME(Server, start, NULL, ZEND_ACC_PUBLIC) PHP_FE_END}; void grpc_init_server(TSRMLS_D) { diff --git a/src/php/ext/grpc/server.h b/src/php/ext/grpc/server.h index b55689c581..a2ee2ff5a9 100755 --- a/src/php/ext/grpc/server.h +++ b/src/php/ext/grpc/server.h @@ -38,12 +38,12 @@ #include "config.h" #endif -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" +#include <php.h> +#include <php_ini.h> +#include <ext/standard/info.h> #include "php_grpc.h" -#include "grpc/grpc.h" +#include <grpc/grpc.h> /* Class entry for the Server PHP class */ extern zend_class_entry *grpc_ce_server; @@ -53,6 +53,7 @@ typedef struct wrapped_grpc_server { zend_object std; grpc_server *wrapped; + grpc_completion_queue *queue; } wrapped_grpc_server; /* Initializes the Server class */ diff --git a/src/php/ext/grpc/server_credentials.c b/src/php/ext/grpc/server_credentials.c index df64e65986..c4c1fabb1a 100644 --- a/src/php/ext/grpc/server_credentials.c +++ b/src/php/ext/grpc/server_credentials.c @@ -37,17 +37,17 @@ #include "config.h" #endif -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" -#include "ext/spl/spl_exceptions.h" +#include <php.h> +#include <php_ini.h> +#include <ext/standard/info.h> +#include <ext/spl/spl_exceptions.h> #include "php_grpc.h" -#include "zend_exceptions.h" -#include "zend_hash.h" +#include <zend_exceptions.h> +#include <zend_hash.h> -#include "grpc/grpc.h" -#include "grpc/grpc_security.h" +#include <grpc/grpc.h> +#include <grpc/grpc_security.h> zend_class_entry *grpc_ce_server_credentials; diff --git a/src/php/ext/grpc/server_credentials.h b/src/php/ext/grpc/server_credentials.h index 8ed3697150..7101d65000 100755 --- a/src/php/ext/grpc/server_credentials.h +++ b/src/php/ext/grpc/server_credentials.h @@ -38,13 +38,13 @@ #include "config.h" #endif -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" +#include <php.h> +#include <php_ini.h> +#include <ext/standard/info.h> #include "php_grpc.h" -#include "grpc/grpc.h" -#include "grpc/grpc_security.h" +#include <grpc/grpc.h> +#include <grpc/grpc_security.h> /* Class entry for the Server_Credentials PHP class */ extern zend_class_entry *grpc_ce_server_credentials; diff --git a/src/php/ext/grpc/timeval.c b/src/php/ext/grpc/timeval.c index f90f0062ba..8a278d6760 100644 --- a/src/php/ext/grpc/timeval.c +++ b/src/php/ext/grpc/timeval.c @@ -37,18 +37,18 @@ #include "config.h" #endif -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" -#include "ext/spl/spl_exceptions.h" +#include <php.h> +#include <php_ini.h> +#include <ext/standard/info.h> +#include <ext/spl/spl_exceptions.h> #include "php_grpc.h" -#include "zend_exceptions.h" +#include <zend_exceptions.h> #include <stdbool.h> -#include "grpc/grpc.h" -#include "grpc/support/time.h" +#include <grpc/grpc.h> +#include <grpc/support/time.h> zend_class_entry *grpc_ce_timeval; @@ -227,7 +227,7 @@ PHP_METHOD(Timeval, zero) { * Returns the infinite future time value as a timeval object * @return Timeval Infinite future time value */ -PHP_METHOD(Timeval, inf_future) { +PHP_METHOD(Timeval, infFuture) { zval *grpc_php_timeval_inf_future = grpc_php_wrap_timeval(gpr_inf_future); RETURN_DESTROY_ZVAL(grpc_php_timeval_inf_future); } @@ -236,7 +236,7 @@ PHP_METHOD(Timeval, inf_future) { * Returns the infinite past time value as a timeval object * @return Timeval Infinite past time value */ -PHP_METHOD(Timeval, inf_past) { +PHP_METHOD(Timeval, infPast) { zval *grpc_php_timeval_inf_past = grpc_php_wrap_timeval(gpr_inf_past); RETURN_DESTROY_ZVAL(grpc_php_timeval_inf_past); } @@ -245,7 +245,7 @@ PHP_METHOD(Timeval, inf_past) { * Sleep until this time, interpreted as an absolute timeout * @return void */ -PHP_METHOD(Timeval, sleep_until) { +PHP_METHOD(Timeval, sleepUntil) { wrapped_grpc_timeval *this = (wrapped_grpc_timeval *)zend_object_store_get_object(getThis() TSRMLS_CC); gpr_sleep_until(this->wrapped); @@ -255,11 +255,11 @@ static zend_function_entry timeval_methods[] = { PHP_ME(Timeval, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) PHP_ME(Timeval, add, NULL, ZEND_ACC_PUBLIC) PHP_ME(Timeval, compare, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - PHP_ME(Timeval, inf_future, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - PHP_ME(Timeval, inf_past, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ME(Timeval, infFuture, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ME(Timeval, infPast, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ME(Timeval, now, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ME(Timeval, similar, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - PHP_ME(Timeval, sleep_until, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Timeval, sleepUntil, NULL, ZEND_ACC_PUBLIC) PHP_ME(Timeval, subtract, NULL, ZEND_ACC_PUBLIC) PHP_ME(Timeval, zero, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_FE_END}; diff --git a/src/php/ext/grpc/timeval.h b/src/php/ext/grpc/timeval.h index e3183f691d..07cef037cb 100755 --- a/src/php/ext/grpc/timeval.h +++ b/src/php/ext/grpc/timeval.h @@ -38,13 +38,13 @@ #include "config.h" #endif -#include "php.h" -#include "php_ini.h" -#include "ext/standard/info.h" +#include <php.h> +#include <php_ini.h> +#include <ext/standard/info.h> #include "php_grpc.h" -#include "grpc/grpc.h" -#include "grpc/support/time.h" +#include <grpc/grpc.h> +#include <grpc/support/time.h> /* Class entry for the Timeval PHP Class */ extern zend_class_entry *grpc_ce_timeval; diff --git a/src/php/lib/Grpc/BidiStreamingSurfaceActiveCall.php b/src/php/lib/Grpc/AbstractCall.php index 0459f21e27..1add972589 100755..100644 --- a/src/php/lib/Grpc/BidiStreamingSurfaceActiveCall.php +++ b/src/php/lib/Grpc/AbstractCall.php @@ -32,44 +32,47 @@ * */ namespace Grpc; -require_once realpath(dirname(__FILE__) . '/../autoload.php'); -/** - * Represents an active call that allows for sending and recieving messages in - * streams in any order. - */ -class BidiStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall { +abstract class AbstractCall { + + protected $call; + protected $deserialize; + protected $metadata; /** - * Reads the next value from the server. - * @return The next value from the server, or null if there is none + * Create a new Call wrapper object. + * @param Channel $channel The channel to communicate on + * @param string $method The method to call on the remote server */ - public function read() { - return $this->_read(); + public function __construct(Channel $channel, $method, $deserialize) { + $this->call = new Call($channel, $method, Timeval::infFuture()); + $this->deserialize = $deserialize; + $this->metadata = null; } /** - * Writes a single message to the server. This cannot be called after - * writesDone is called. - * @param $value The message to send + * @return The metadata sent by the server. */ - public function write($value) { - $this->_write($value); + public function getMetadata() { + return $this->metadata; } /** - * Indicate that no more writes will be sent + * Cancels the call */ - public function writesDone() { - $this->_writesDone(); + public function cancel() { + $this->call->cancel(); } /** - * Wait for the server to send the status, and return it. - * @return object The status object, with integer $code and string $details - * members + * Deserialize a response value to an object. + * @param string $value The binary value to deserialize + * @return The deserialized value */ - public function getStatus() { - return $this->_getStatus(); + protected function deserializeResponse($value) { + if ($value === null) { + return null; + } + return call_user_func($this->deserialize, $value); } -} +}
\ No newline at end of file diff --git a/src/php/lib/Grpc/ActiveCall.php b/src/php/lib/Grpc/ActiveCall.php deleted file mode 100755 index f0d0d55582..0000000000 --- a/src/php/lib/Grpc/ActiveCall.php +++ /dev/null @@ -1,123 +0,0 @@ -<?php -/* - * - * Copyright 2015, 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. - * - */ -namespace Grpc; -require_once realpath(dirname(__FILE__) . '/../autoload.php'); - -/** - * Represents an active call that allows sending and recieving binary data - */ -class ActiveCall { - private $completion_queue; - private $call; - private $flags; - private $metadata; - - /** - * Create a new active call. - * @param Channel $channel The channel to communicate on - * @param string $method The method to call on the remote server - * @param array $metadata Metadata to send with the call, if applicable - * @param long $flags Write flags to use with this call - */ - public function __construct(Channel $channel, - $method, - $metadata = array(), - $flags = 0) { - $this->completion_queue = new CompletionQueue(); - $this->call = new Call($channel, $method, Timeval::inf_future()); - $this->call->add_metadata($metadata, 0); - $this->flags = $flags; - - // Invoke the call. - $this->call->invoke($this->completion_queue, - CLIENT_METADATA_READ, - FINISHED, 0); - $metadata_event = $this->completion_queue->pluck(CLIENT_METADATA_READ, - Timeval::inf_future()); - $this->metadata = $metadata_event->data; - } - - /** - * @return The metadata sent by the server. - */ - public function getMetadata() { - return $this->metadata; - } - - /** - * Cancels the call - */ - public function cancel() { - $this->call->cancel(); - } - - /** - * Read a single message from the server. - * @return The next message from the server, or null if there is none. - */ - public function read() { - $this->call->start_read(READ); - $read_event = $this->completion_queue->pluck(READ, Timeval::inf_future()); - return $read_event->data; - } - - /** - * Write a single message to the server. This cannot be called after - * writesDone is called. - * @param ByteBuffer $data The data to write - */ - public function write($data) { - $this->call->start_write($data, WRITE_ACCEPTED, $this->flags); - $this->completion_queue->pluck(WRITE_ACCEPTED, Timeval::inf_future()); - } - - /** - * Indicate that no more writes will be sent. - */ - public function writesDone() { - $this->call->writes_done(FINISH_ACCEPTED); - $this->completion_queue->pluck(FINISH_ACCEPTED, Timeval::inf_future()); - } - - /** - * Wait for the server to send the status, and return it. - * @return object The status object, with integer $code, string $details, - * and array $metadata members - */ - public function getStatus() { - $status_event = $this->completion_queue->pluck(FINISHED, - Timeval::inf_future()); - return $status_event->data; - } -} diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php index fde055a3b3..fc83dace20 100755 --- a/src/php/lib/Grpc/BaseStub.php +++ b/src/php/lib/Grpc/BaseStub.php @@ -32,7 +32,6 @@ * */ namespace Grpc; -require_once realpath(dirname(__FILE__) . '/../autoload.php'); /** * Base class for generated client stubs. Stub methods are expected to call @@ -69,11 +68,9 @@ class BaseStub { $argument, callable $deserialize, $metadata = array()) { - return new SimpleSurfaceActiveCall($this->channel, - $method, - $deserialize, - $argument, - $metadata); + $call = new UnaryCall($this->channel, $method, $deserialize); + $call->start($argument, $metadata); + return $call; } /** @@ -91,11 +88,9 @@ class BaseStub { $arguments, callable $deserialize, $metadata = array()) { - return new ClientStreamingSurfaceActiveCall($this->channel, - $method, - $deserialize, - $arguments, - $metadata); + $call = new ClientStreamingCall($this->channel, $method, $deserialize); + $call->start($arguments, $metadata); + return $call; } /** @@ -112,11 +107,9 @@ class BaseStub { $argument, callable $deserialize, $metadata = array()) { - return new ServerStreamingSurfaceActiveCall($this->channel, - $method, - $deserialize, - $argument, - $metadata); + $call = new ServerStreamingCall($this->channel, $method, $deserialize); + $call->start($argument, $metadata); + return $call; } /** @@ -130,9 +123,8 @@ class BaseStub { public function _bidiRequest($method, callable $deserialize, $metadata = array()) { - return new BidiStreamingSurfaceActiveCall($this->channel, - $method, - $deserialize, - $metadata); + $call = new BidiStreamingCall($this->channel, $method, $deserialize); + $call->start($metadata); + return $call; } } diff --git a/src/php/lib/Grpc/AbstractSurfaceActiveCall.php b/src/php/lib/Grpc/BidiStreamingCall.php index 9d0af090ce..76c642bef4 100755..100644 --- a/src/php/lib/Grpc/AbstractSurfaceActiveCall.php +++ b/src/php/lib/Grpc/BidiStreamingCall.php @@ -1,5 +1,4 @@ <?php - /* * * Copyright 2015, Google Inc. @@ -32,67 +31,62 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ - namespace Grpc; -require_once realpath(dirname(__FILE__) . '/../autoload.php'); - /** - * Represents an active call that allows sending and recieving messages. - * Subclasses restrict how data can be sent and recieved. + * Represents an active call that allows for sending and recieving messages in + * streams in any order. */ -abstract class AbstractSurfaceActiveCall { - private $active_call; - private $deserialize; - +class BidiStreamingCall extends AbstractCall { /** - * Create a new surface active call. - * @param Channel $channel The channel to communicate on - * @param string $method The method to call on the remote server - * @param callable $deserialize The function to deserialize a value + * Start the call * @param array $metadata Metadata to send with the call, if applicable - * @param long $flags Write flags to use with this call */ - public function __construct(Channel $channel, - $method, - callable $deserialize, - $metadata = array(), - $flags = 0) { - $this->active_call = new ActiveCall($channel, $method, $metadata, $flags); - $this->deserialize = $deserialize; + public function start($metadata) { + $this->call->startBatch([OP_SEND_INITIAL_METADATA => $metadata]); } /** - * @return The metadata sent by the server + * Reads the next value from the server. + * @return The next value from the server, or null if there is none */ - public function getMetadata() { - return $this->metadata(); + public function read() { + $batch = [OP_RECV_MESSAGE => true]; + if ($this->metadata === null) { + $batch[OP_RECV_INITIAL_METADATA] = true; + } + $read_event = $this->call->startBatch($batch); + if ($this->metadata === null) { + $this->metadata = $read_event->metadata; + } + return $this->deserializeResponse($read_event->message); } /** - * Cancels the call + * Write a single message to the server. This cannot be called after + * writesDone is called. + * @param ByteBuffer $data The data to write */ - public function cancel() { - $this->active_call->cancel(); + public function write($data) { + $this->call->startBatch([OP_SEND_MESSAGE => $data->serialize()]); } - protected function _read() { - $response = $this->active_call->read(); - if ($response === null) { - return null; - } - return call_user_func($this->deserialize, $response); - } - - protected function _write($value) { - return $this->active_call->write($value->serialize()); - } - - protected function _writesDone() { - $this->active_call->writesDone(); + /** + * Indicate that no more writes will be sent. + */ + public function writesDone() { + $this->call->startBatch([OP_SEND_CLOSE_FROM_CLIENT => true]); } - protected function _getStatus() { - return $this->active_call->getStatus(); + /** + * Wait for the server to send the status, and return it. + * @return object The status object, with integer $code, string $details, + * and array $metadata members + */ + public function getStatus() { + $status_event = $this->call->startBatch([ + OP_RECV_STATUS_ON_CLIENT => true + ]); + return $status_event->status; } -} +}
\ No newline at end of file diff --git a/src/php/lib/Grpc/ClientStreamingSurfaceActiveCall.php b/src/php/lib/Grpc/ClientStreamingCall.php index d33f09fbe4..61439d3f47 100755..100644 --- a/src/php/lib/Grpc/ClientStreamingSurfaceActiveCall.php +++ b/src/php/lib/Grpc/ClientStreamingCall.php @@ -32,31 +32,23 @@ * */ namespace Grpc; -require_once realpath(dirname(__FILE__) . '/../autoload.php'); /** * Represents an active call that sends a stream of messages and then gets a * single response. */ -class ClientStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall { +class ClientStreamingCall extends AbstractCall { /** - * Create a new simple (single request/single response) active call. - * @param Channel $channel The channel to communicate on - * @param string $method The method to call on the remote server - * @param callable $deserialize The function to deserialize a value + * Start the call. * @param Traversable $arg_iter The iterator of arguments to send * @param array $metadata Metadata to send with the call, if applicable */ - public function __construct(Channel $channel, - $method, - callable $deserialize, - $arg_iter, - $metadata = array()) { - parent::__construct($channel, $method, $deserialize, $metadata, 0); + public function start($arg_iter, $metadata = array()) { + $event = $this->call->startBatch([OP_SEND_INITIAL_METADATA => $metadata]); foreach($arg_iter as $arg) { - $this->_write($arg); + $this->call->startBatch([OP_SEND_MESSAGE => $arg->serialize()]); } - $this->_writesDone(); + $this->call->startBatch([OP_SEND_CLOSE_FROM_CLIENT => true]); } /** @@ -64,8 +56,11 @@ class ClientStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall { * @return [response data, status] */ public function wait() { - $response = $this->_read(); - $status = $this->_getStatus(); - return array($response, $status); + $event = $this->call->startBatch([ + OP_RECV_INITIAL_METADATA => true, + OP_RECV_MESSAGE => true, + OP_RECV_STATUS_ON_CLIENT => true]); + $this->metadata = $event->metadata; + return array($this->deserializeResponse($event->message), $event->status); } -} +}
\ No newline at end of file diff --git a/src/php/lib/Grpc/ServerStreamingSurfaceActiveCall.php b/src/php/lib/Grpc/ServerStreamingCall.php index fd08e86e51..631c863345 100755..100644 --- a/src/php/lib/Grpc/ServerStreamingSurfaceActiveCall.php +++ b/src/php/lib/Grpc/ServerStreamingCall.php @@ -33,42 +33,45 @@ */ namespace Grpc; -require_once realpath(dirname(__FILE__) . '/../autoload.php'); - /** * Represents an active call that sends a single message and then gets a stream * of reponses */ -class ServerStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall { +class ServerStreamingCall extends AbstractCall { /** - * Create a new simple (single request/single response) active call. - * @param Channel $channel The channel to communicate on - * @param string $method The method to call on the remote server - * @param callable $deserialize The function to deserialize a value + * Start the call * @param $arg The argument to send * @param array $metadata Metadata to send with the call, if applicable */ - public function __construct(Channel $channel, - $method, - callable $deserialize, - $arg, - $metadata = array()) { - parent::__construct($channel, $method, $deserialize, $metadata, - \Grpc\WRITE_BUFFER_HINT); - $this->_write($arg); - $this->_writesDone(); + public function start($arg, $metadata = array()) { + $event = $this->call->startBatch([ + OP_SEND_INITIAL_METADATA => $metadata, + OP_RECV_INITIAL_METADATA => true, + OP_SEND_MESSAGE => $arg->serialize(), + OP_SEND_CLOSE_FROM_CLIENT => true]); + $this->metadata = $event->metadata; } /** * @return An iterator of response values */ public function responses() { - while(($response = $this->_read()) !== null) { - yield $response; + $response = $this->call->startBatch([OP_RECV_MESSAGE => true])->message; + while($response !== null) { + yield $this->deserializeResponse($response); + $response = $this->call->startBatch([OP_RECV_MESSAGE => true])->message; } } + /** + * Wait for the server to send the status, and return it. + * @return object The status object, with integer $code, string $details, + * and array $metadata members + */ public function getStatus() { - return $this->_getStatus(); + $status_event = $this->call->startBatch([ + OP_RECV_STATUS_ON_CLIENT => true + ]); + return $status_event->status; } -} +}
\ No newline at end of file diff --git a/src/php/lib/Grpc/SimpleSurfaceActiveCall.php b/src/php/lib/Grpc/UnaryCall.php index ba82f5704f..97a10a40f4 100755..100644 --- a/src/php/lib/Grpc/SimpleSurfaceActiveCall.php +++ b/src/php/lib/Grpc/UnaryCall.php @@ -33,30 +33,23 @@ */ namespace Grpc; -require_once realpath(dirname(__FILE__) . '/../autoload.php'); - /** * Represents an active call that sends a single message and then gets a single * response. */ -class SimpleSurfaceActiveCall extends AbstractSurfaceActiveCall { +class UnaryCall extends AbstractCall { /** - * Create a new simple (single request/single response) active call. - * @param Channel $channel The channel to communicate on - * @param string $method The method to call on the remote server - * @param callable $deserialize The function to deserialize a value + * Start the call * @param $arg The argument to send * @param array $metadata Metadata to send with the call, if applicable */ - public function __construct(Channel $channel, - $method, - callable $deserialize, - $arg, - $metadata = array()) { - parent::__construct($channel, $method, $deserialize, $metadata, - \Grpc\WRITE_BUFFER_HINT); - $this->_write($arg); - $this->_writesDone(); + public function start($arg, $metadata = array()) { + $event = $this->call->startBatch([ + OP_SEND_INITIAL_METADATA => $metadata, + OP_RECV_INITIAL_METADATA => true, + OP_SEND_MESSAGE => $arg->serialize(), + OP_SEND_CLOSE_FROM_CLIENT => true]); + $this->metadata = $event->metadata; } /** @@ -64,8 +57,9 @@ class SimpleSurfaceActiveCall extends AbstractSurfaceActiveCall { * @return [response data, status] */ public function wait() { - $response = $this->_read(); - $status = $this->_getStatus(); - return array($response, $status); + $event = $this->call->startBatch([ + OP_RECV_MESSAGE => true, + OP_RECV_STATUS_ON_CLIENT => true]); + return array($this->deserializeResponse($event->message), $event->status); } -} +}
\ No newline at end of file diff --git a/src/php/tests/generated_code/GeneratedCodeTest.php b/src/php/tests/generated_code/GeneratedCodeTest.php index cb2c0e6d10..927d24ca63 100755 --- a/src/php/tests/generated_code/GeneratedCodeTest.php +++ b/src/php/tests/generated_code/GeneratedCodeTest.php @@ -31,7 +31,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -require_once realpath(dirname(__FILE__) . '/../../lib/autoload.php'); +require_once realpath(dirname(__FILE__) . '/../../vendor/autoload.php'); require 'DrSlump/Protobuf.php'; \DrSlump\Protobuf::autoload(); require 'math.php'; @@ -41,7 +41,8 @@ class GeneratedCodeTest extends PHPUnit_Framework_TestCase { protected static $client; protected static $timeout; public static function setUpBeforeClass() { - self::$client = new math\MathClient(getenv('GRPC_TEST_HOST')); + self::$client = new math\MathClient(new Grpc\BaseStub( + getenv('GRPC_TEST_HOST'), [])); } public function testSimpleRequest() { diff --git a/src/php/tests/generated_code/math.php b/src/php/tests/generated_code/math.php deleted file mode 100755 index e97a5cf97e..0000000000 --- a/src/php/tests/generated_code/math.php +++ /dev/null @@ -1,479 +0,0 @@ -<?php -// DO NOT EDIT! Generated by Protobuf-PHP protoc plugin 1.0 -// Source: math.proto -// Date: 2014-11-14 00:00:41 - -namespace math { - - class DivArgs extends \DrSlump\Protobuf\Message { - - /** @var int */ - public $dividend = null; - - /** @var int */ - public $divisor = null; - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'math.DivArgs'); - - // REQUIRED INT64 dividend = 1 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 1; - $f->name = "dividend"; - $f->type = \DrSlump\Protobuf::TYPE_INT64; - $f->rule = \DrSlump\Protobuf::RULE_REQUIRED; - $descriptor->addField($f); - - // REQUIRED INT64 divisor = 2 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 2; - $f->name = "divisor"; - $f->type = \DrSlump\Protobuf::TYPE_INT64; - $f->rule = \DrSlump\Protobuf::RULE_REQUIRED; - $descriptor->addField($f); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - - /** - * Check if <dividend> has a value - * - * @return boolean - */ - public function hasDividend(){ - return $this->_has(1); - } - - /** - * Clear <dividend> value - * - * @return \math\DivArgs - */ - public function clearDividend(){ - return $this->_clear(1); - } - - /** - * Get <dividend> value - * - * @return int - */ - public function getDividend(){ - return $this->_get(1); - } - - /** - * Set <dividend> value - * - * @param int $value - * @return \math\DivArgs - */ - public function setDividend( $value){ - return $this->_set(1, $value); - } - - /** - * Check if <divisor> has a value - * - * @return boolean - */ - public function hasDivisor(){ - return $this->_has(2); - } - - /** - * Clear <divisor> value - * - * @return \math\DivArgs - */ - public function clearDivisor(){ - return $this->_clear(2); - } - - /** - * Get <divisor> value - * - * @return int - */ - public function getDivisor(){ - return $this->_get(2); - } - - /** - * Set <divisor> value - * - * @param int $value - * @return \math\DivArgs - */ - public function setDivisor( $value){ - return $this->_set(2, $value); - } - } -} - -namespace math { - - class DivReply extends \DrSlump\Protobuf\Message { - - /** @var int */ - public $quotient = null; - - /** @var int */ - public $remainder = null; - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'math.DivReply'); - - // REQUIRED INT64 quotient = 1 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 1; - $f->name = "quotient"; - $f->type = \DrSlump\Protobuf::TYPE_INT64; - $f->rule = \DrSlump\Protobuf::RULE_REQUIRED; - $descriptor->addField($f); - - // REQUIRED INT64 remainder = 2 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 2; - $f->name = "remainder"; - $f->type = \DrSlump\Protobuf::TYPE_INT64; - $f->rule = \DrSlump\Protobuf::RULE_REQUIRED; - $descriptor->addField($f); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - - /** - * Check if <quotient> has a value - * - * @return boolean - */ - public function hasQuotient(){ - return $this->_has(1); - } - - /** - * Clear <quotient> value - * - * @return \math\DivReply - */ - public function clearQuotient(){ - return $this->_clear(1); - } - - /** - * Get <quotient> value - * - * @return int - */ - public function getQuotient(){ - return $this->_get(1); - } - - /** - * Set <quotient> value - * - * @param int $value - * @return \math\DivReply - */ - public function setQuotient( $value){ - return $this->_set(1, $value); - } - - /** - * Check if <remainder> has a value - * - * @return boolean - */ - public function hasRemainder(){ - return $this->_has(2); - } - - /** - * Clear <remainder> value - * - * @return \math\DivReply - */ - public function clearRemainder(){ - return $this->_clear(2); - } - - /** - * Get <remainder> value - * - * @return int - */ - public function getRemainder(){ - return $this->_get(2); - } - - /** - * Set <remainder> value - * - * @param int $value - * @return \math\DivReply - */ - public function setRemainder( $value){ - return $this->_set(2, $value); - } - } -} - -namespace math { - - class FibArgs extends \DrSlump\Protobuf\Message { - - /** @var int */ - public $limit = null; - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'math.FibArgs'); - - // OPTIONAL INT64 limit = 1 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 1; - $f->name = "limit"; - $f->type = \DrSlump\Protobuf::TYPE_INT64; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $descriptor->addField($f); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - - /** - * Check if <limit> has a value - * - * @return boolean - */ - public function hasLimit(){ - return $this->_has(1); - } - - /** - * Clear <limit> value - * - * @return \math\FibArgs - */ - public function clearLimit(){ - return $this->_clear(1); - } - - /** - * Get <limit> value - * - * @return int - */ - public function getLimit(){ - return $this->_get(1); - } - - /** - * Set <limit> value - * - * @param int $value - * @return \math\FibArgs - */ - public function setLimit( $value){ - return $this->_set(1, $value); - } - } -} - -namespace math { - - class Num extends \DrSlump\Protobuf\Message { - - /** @var int */ - public $num = null; - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'math.Num'); - - // REQUIRED INT64 num = 1 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 1; - $f->name = "num"; - $f->type = \DrSlump\Protobuf::TYPE_INT64; - $f->rule = \DrSlump\Protobuf::RULE_REQUIRED; - $descriptor->addField($f); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - - /** - * Check if <num> has a value - * - * @return boolean - */ - public function hasNum(){ - return $this->_has(1); - } - - /** - * Clear <num> value - * - * @return \math\Num - */ - public function clearNum(){ - return $this->_clear(1); - } - - /** - * Get <num> value - * - * @return int - */ - public function getNum(){ - return $this->_get(1); - } - - /** - * Set <num> value - * - * @param int $value - * @return \math\Num - */ - public function setNum( $value){ - return $this->_set(1, $value); - } - } -} - -namespace math { - - class FibReply extends \DrSlump\Protobuf\Message { - - /** @var int */ - public $count = null; - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'math.FibReply'); - - // REQUIRED INT64 count = 1 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 1; - $f->name = "count"; - $f->type = \DrSlump\Protobuf::TYPE_INT64; - $f->rule = \DrSlump\Protobuf::RULE_REQUIRED; - $descriptor->addField($f); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - - /** - * Check if <count> has a value - * - * @return boolean - */ - public function hasCount(){ - return $this->_has(1); - } - - /** - * Clear <count> value - * - * @return \math\FibReply - */ - public function clearCount(){ - return $this->_clear(1); - } - - /** - * Get <count> value - * - * @return int - */ - public function getCount(){ - return $this->_get(1); - } - - /** - * Set <count> value - * - * @param int $value - * @return \math\FibReply - */ - public function setCount( $value){ - return $this->_set(1, $value); - } - } -} - -namespace math { - - class MathClient extends \Grpc\BaseStub { - /** - * @param math\DivArgs $input - * @return math\DivReply - */ - public function Div(\math\DivArgs $argument, $metadata = array()) { - return $this->_simpleRequest('/Math/Div', $argument, '\math\DivReply::deserialize', $metadata); - } - /** - * @param math\DivArgs $input - * @return math\DivReply - */ - public function DivMany($metadata = array()) { - return $this->_bidiRequest('/Math/DivMany', '\math\DivReply::deserialize', $metadata); - } - /** - * @param math\FibArgs $input - * @return math\Num - */ - public function Fib($argument, $metadata = array()) { - return $this->_serverStreamRequest('/Math/Fib', $argument, '\math\Num::deserialize', $metadata); - } - /** - * @param math\Num $input - * @return math\Num - */ - public function Sum($arguments, $metadata = array()) { - return $this->_clientStreamRequest('/Math/Sum', $arguments, '\math\Num::deserialize', $metadata); - } - } -} diff --git a/src/php/tests/generated_code/math.proto b/src/php/tests/generated_code/math.proto new file mode 100644 index 0000000000..e34ad5e967 --- /dev/null +++ b/src/php/tests/generated_code/math.proto @@ -0,0 +1,80 @@ + +// Copyright 2015, 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. + +syntax = "proto3"; + +package math; + +message DivArgs { + optional int64 dividend = 1; + optional int64 divisor = 2; +} + +message DivReply { + optional int64 quotient = 1; + optional int64 remainder = 2; +} + +message FibArgs { + optional int64 limit = 1; +} + +message Num { + optional int64 num = 1; +} + +message FibReply { + 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/php/tests/interop/empty.php b/src/php/tests/interop/empty.php deleted file mode 100755 index 22b11803b6..0000000000 --- a/src/php/tests/interop/empty.php +++ /dev/null @@ -1,25 +0,0 @@ -<?php -// DO NOT EDIT! Generated by Protobuf-PHP protoc plugin 1.0 -// Source: test/cpp/interop/empty.proto -// Date: 2015-01-30 23:30:46 - -namespace grpc\testing { - - class EmptyMessage extends \DrSlump\Protobuf\Message { - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'grpc.testing.EmptyMessage'); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - } -} diff --git a/src/php/tests/interop/empty.proto b/src/php/tests/interop/empty.proto new file mode 100644 index 0000000000..4200d7b519 --- /dev/null +++ b/src/php/tests/interop/empty.proto @@ -0,0 +1,43 @@ + +// Copyright 2015, 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. + +syntax = "proto2"; + +package grpc.testing; + +// An empty message that you can re-use to avoid defining duplicated empty +// messages in your project. A typical example is to use it as argument or the +// return value of a service API. For instance: +// +// service Foo { +// rpc Bar (grpc.testing.EmptyMessage) returns (grpc.testing.EmptyMessage) { }; +// }; +// +message EmptyMessage {} diff --git a/src/php/tests/interop/interop_client.php b/src/php/tests/interop/interop_client.php index 82ca438169..6f81bfa6cd 100755 --- a/src/php/tests/interop/interop_client.php +++ b/src/php/tests/interop/interop_client.php @@ -31,7 +31,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -require_once realpath(dirname(__FILE__) . '/../../lib/autoload.php'); +require_once realpath(dirname(__FILE__) . '/../../vendor/autoload.php'); require 'DrSlump/Protobuf.php'; \DrSlump\Protobuf::autoload(); require 'empty.php'; @@ -132,8 +132,6 @@ function serverStreaming($stub) { } $call = $stub->StreamingOutputCall($request); - hardAssert($call->getStatus()->code === Grpc\STATUS_OK, - 'Call did not complete successfully'); $i = 0; foreach($call->responses() as $value) { hardAssert($i < 4, 'Too many responses'); @@ -142,7 +140,10 @@ function serverStreaming($stub) { 'Payload ' . $i . ' had the wrong type'); hardAssert(strlen($payload->getBody()) === $sizes[$i], 'Response ' . $i . ' had the wrong length'); + $i += 1; } + hardAssert($call->getStatus()->code === Grpc\STATUS_OK, + 'Call did not complete successfully'); } /** @@ -240,4 +241,7 @@ switch($args['test_case']) { break; case 'cancel_after_first_response': cancelAfterFirstResponse($stub); + break; + default: + exit(1); } diff --git a/src/php/tests/interop/messages.php b/src/php/tests/interop/messages.php deleted file mode 100755 index a626a17ab3..0000000000 --- a/src/php/tests/interop/messages.php +++ /dev/null @@ -1,1074 +0,0 @@ -<?php -// DO NOT EDIT! Generated by Protobuf-PHP protoc plugin 1.0 -// Source: test/cpp/interop/messages.proto -// Date: 2015-01-30 23:30:46 - -namespace grpc\testing { - - class PayloadType extends \DrSlump\Protobuf\Enum { - const COMPRESSABLE = 0; - const UNCOMPRESSABLE = 1; - const RANDOM = 2; - } -} -namespace grpc\testing { - - class Payload extends \DrSlump\Protobuf\Message { - - /** @var int - \grpc\testing\PayloadType */ - public $type = null; - - /** @var string */ - public $body = null; - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'grpc.testing.Payload'); - - // OPTIONAL ENUM type = 1 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 1; - $f->name = "type"; - $f->type = \DrSlump\Protobuf::TYPE_ENUM; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $f->reference = '\grpc\testing\PayloadType'; - $descriptor->addField($f); - - // OPTIONAL BYTES body = 2 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 2; - $f->name = "body"; - $f->type = \DrSlump\Protobuf::TYPE_BYTES; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $descriptor->addField($f); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - - /** - * Check if <type> has a value - * - * @return boolean - */ - public function hasType(){ - return $this->_has(1); - } - - /** - * Clear <type> value - * - * @return \grpc\testing\Payload - */ - public function clearType(){ - return $this->_clear(1); - } - - /** - * Get <type> value - * - * @return int - \grpc\testing\PayloadType - */ - public function getType(){ - return $this->_get(1); - } - - /** - * Set <type> value - * - * @param int - \grpc\testing\PayloadType $value - * @return \grpc\testing\Payload - */ - public function setType( $value){ - return $this->_set(1, $value); - } - - /** - * Check if <body> has a value - * - * @return boolean - */ - public function hasBody(){ - return $this->_has(2); - } - - /** - * Clear <body> value - * - * @return \grpc\testing\Payload - */ - public function clearBody(){ - return $this->_clear(2); - } - - /** - * Get <body> value - * - * @return string - */ - public function getBody(){ - return $this->_get(2); - } - - /** - * Set <body> value - * - * @param string $value - * @return \grpc\testing\Payload - */ - public function setBody( $value){ - return $this->_set(2, $value); - } - } -} - -namespace grpc\testing { - - class SimpleRequest extends \DrSlump\Protobuf\Message { - - /** @var int - \grpc\testing\PayloadType */ - public $response_type = null; - - /** @var int */ - public $response_size = null; - - /** @var \grpc\testing\Payload */ - public $payload = null; - - /** @var boolean */ - public $fill_username = null; - - /** @var boolean */ - public $fill_oauth_scope = null; - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'grpc.testing.SimpleRequest'); - - // OPTIONAL ENUM response_type = 1 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 1; - $f->name = "response_type"; - $f->type = \DrSlump\Protobuf::TYPE_ENUM; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $f->reference = '\grpc\testing\PayloadType'; - $descriptor->addField($f); - - // OPTIONAL INT32 response_size = 2 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 2; - $f->name = "response_size"; - $f->type = \DrSlump\Protobuf::TYPE_INT32; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $descriptor->addField($f); - - // OPTIONAL MESSAGE payload = 3 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 3; - $f->name = "payload"; - $f->type = \DrSlump\Protobuf::TYPE_MESSAGE; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $f->reference = '\grpc\testing\Payload'; - $descriptor->addField($f); - - // OPTIONAL BOOL fill_username = 4 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 4; - $f->name = "fill_username"; - $f->type = \DrSlump\Protobuf::TYPE_BOOL; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $descriptor->addField($f); - - // OPTIONAL BOOL fill_oauth_scope = 5 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 5; - $f->name = "fill_oauth_scope"; - $f->type = \DrSlump\Protobuf::TYPE_BOOL; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $descriptor->addField($f); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - - /** - * Check if <response_type> has a value - * - * @return boolean - */ - public function hasResponseType(){ - return $this->_has(1); - } - - /** - * Clear <response_type> value - * - * @return \grpc\testing\SimpleRequest - */ - public function clearResponseType(){ - return $this->_clear(1); - } - - /** - * Get <response_type> value - * - * @return int - \grpc\testing\PayloadType - */ - public function getResponseType(){ - return $this->_get(1); - } - - /** - * Set <response_type> value - * - * @param int - \grpc\testing\PayloadType $value - * @return \grpc\testing\SimpleRequest - */ - public function setResponseType( $value){ - return $this->_set(1, $value); - } - - /** - * Check if <response_size> has a value - * - * @return boolean - */ - public function hasResponseSize(){ - return $this->_has(2); - } - - /** - * Clear <response_size> value - * - * @return \grpc\testing\SimpleRequest - */ - public function clearResponseSize(){ - return $this->_clear(2); - } - - /** - * Get <response_size> value - * - * @return int - */ - public function getResponseSize(){ - return $this->_get(2); - } - - /** - * Set <response_size> value - * - * @param int $value - * @return \grpc\testing\SimpleRequest - */ - public function setResponseSize( $value){ - return $this->_set(2, $value); - } - - /** - * Check if <payload> has a value - * - * @return boolean - */ - public function hasPayload(){ - return $this->_has(3); - } - - /** - * Clear <payload> value - * - * @return \grpc\testing\SimpleRequest - */ - public function clearPayload(){ - return $this->_clear(3); - } - - /** - * Get <payload> value - * - * @return \grpc\testing\Payload - */ - public function getPayload(){ - return $this->_get(3); - } - - /** - * Set <payload> value - * - * @param \grpc\testing\Payload $value - * @return \grpc\testing\SimpleRequest - */ - public function setPayload(\grpc\testing\Payload $value){ - return $this->_set(3, $value); - } - - /** - * Check if <fill_username> has a value - * - * @return boolean - */ - public function hasFillUsername(){ - return $this->_has(4); - } - - /** - * Clear <fill_username> value - * - * @return \grpc\testing\SimpleRequest - */ - public function clearFillUsername(){ - return $this->_clear(4); - } - - /** - * Get <fill_username> value - * - * @return boolean - */ - public function getFillUsername(){ - return $this->_get(4); - } - - /** - * Set <fill_username> value - * - * @param boolean $value - * @return \grpc\testing\SimpleRequest - */ - public function setFillUsername( $value){ - return $this->_set(4, $value); - } - - /** - * Check if <fill_oauth_scope> has a value - * - * @return boolean - */ - public function hasFillOauthScope(){ - return $this->_has(5); - } - - /** - * Clear <fill_oauth_scope> value - * - * @return \grpc\testing\SimpleRequest - */ - public function clearFillOauthScope(){ - return $this->_clear(5); - } - - /** - * Get <fill_oauth_scope> value - * - * @return boolean - */ - public function getFillOauthScope(){ - return $this->_get(5); - } - - /** - * Set <fill_oauth_scope> value - * - * @param boolean $value - * @return \grpc\testing\SimpleRequest - */ - public function setFillOauthScope( $value){ - return $this->_set(5, $value); - } - } -} - -namespace grpc\testing { - - class SimpleResponse extends \DrSlump\Protobuf\Message { - - /** @var \grpc\testing\Payload */ - public $payload = null; - - /** @var string */ - public $username = null; - - /** @var string */ - public $oauth_scope = null; - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'grpc.testing.SimpleResponse'); - - // OPTIONAL MESSAGE payload = 1 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 1; - $f->name = "payload"; - $f->type = \DrSlump\Protobuf::TYPE_MESSAGE; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $f->reference = '\grpc\testing\Payload'; - $descriptor->addField($f); - - // OPTIONAL STRING username = 2 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 2; - $f->name = "username"; - $f->type = \DrSlump\Protobuf::TYPE_STRING; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $descriptor->addField($f); - - // OPTIONAL STRING oauth_scope = 3 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 3; - $f->name = "oauth_scope"; - $f->type = \DrSlump\Protobuf::TYPE_STRING; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $descriptor->addField($f); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - - /** - * Check if <payload> has a value - * - * @return boolean - */ - public function hasPayload(){ - return $this->_has(1); - } - - /** - * Clear <payload> value - * - * @return \grpc\testing\SimpleResponse - */ - public function clearPayload(){ - return $this->_clear(1); - } - - /** - * Get <payload> value - * - * @return \grpc\testing\Payload - */ - public function getPayload(){ - return $this->_get(1); - } - - /** - * Set <payload> value - * - * @param \grpc\testing\Payload $value - * @return \grpc\testing\SimpleResponse - */ - public function setPayload(\grpc\testing\Payload $value){ - return $this->_set(1, $value); - } - - /** - * Check if <username> has a value - * - * @return boolean - */ - public function hasUsername(){ - return $this->_has(2); - } - - /** - * Clear <username> value - * - * @return \grpc\testing\SimpleResponse - */ - public function clearUsername(){ - return $this->_clear(2); - } - - /** - * Get <username> value - * - * @return string - */ - public function getUsername(){ - return $this->_get(2); - } - - /** - * Set <username> value - * - * @param string $value - * @return \grpc\testing\SimpleResponse - */ - public function setUsername( $value){ - return $this->_set(2, $value); - } - - /** - * Check if <oauth_scope> has a value - * - * @return boolean - */ - public function hasOauthScope(){ - return $this->_has(3); - } - - /** - * Clear <oauth_scope> value - * - * @return \grpc\testing\SimpleResponse - */ - public function clearOauthScope(){ - return $this->_clear(3); - } - - /** - * Get <oauth_scope> value - * - * @return string - */ - public function getOauthScope(){ - return $this->_get(3); - } - - /** - * Set <oauth_scope> value - * - * @param string $value - * @return \grpc\testing\SimpleResponse - */ - public function setOauthScope( $value){ - return $this->_set(3, $value); - } - } -} - -namespace grpc\testing { - - class StreamingInputCallRequest extends \DrSlump\Protobuf\Message { - - /** @var \grpc\testing\Payload */ - public $payload = null; - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'grpc.testing.StreamingInputCallRequest'); - - // OPTIONAL MESSAGE payload = 1 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 1; - $f->name = "payload"; - $f->type = \DrSlump\Protobuf::TYPE_MESSAGE; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $f->reference = '\grpc\testing\Payload'; - $descriptor->addField($f); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - - /** - * Check if <payload> has a value - * - * @return boolean - */ - public function hasPayload(){ - return $this->_has(1); - } - - /** - * Clear <payload> value - * - * @return \grpc\testing\StreamingInputCallRequest - */ - public function clearPayload(){ - return $this->_clear(1); - } - - /** - * Get <payload> value - * - * @return \grpc\testing\Payload - */ - public function getPayload(){ - return $this->_get(1); - } - - /** - * Set <payload> value - * - * @param \grpc\testing\Payload $value - * @return \grpc\testing\StreamingInputCallRequest - */ - public function setPayload(\grpc\testing\Payload $value){ - return $this->_set(1, $value); - } - } -} - -namespace grpc\testing { - - class StreamingInputCallResponse extends \DrSlump\Protobuf\Message { - - /** @var int */ - public $aggregated_payload_size = null; - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'grpc.testing.StreamingInputCallResponse'); - - // OPTIONAL INT32 aggregated_payload_size = 1 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 1; - $f->name = "aggregated_payload_size"; - $f->type = \DrSlump\Protobuf::TYPE_INT32; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $descriptor->addField($f); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - - /** - * Check if <aggregated_payload_size> has a value - * - * @return boolean - */ - public function hasAggregatedPayloadSize(){ - return $this->_has(1); - } - - /** - * Clear <aggregated_payload_size> value - * - * @return \grpc\testing\StreamingInputCallResponse - */ - public function clearAggregatedPayloadSize(){ - return $this->_clear(1); - } - - /** - * Get <aggregated_payload_size> value - * - * @return int - */ - public function getAggregatedPayloadSize(){ - return $this->_get(1); - } - - /** - * Set <aggregated_payload_size> value - * - * @param int $value - * @return \grpc\testing\StreamingInputCallResponse - */ - public function setAggregatedPayloadSize( $value){ - return $this->_set(1, $value); - } - } -} - -namespace grpc\testing { - - class ResponseParameters extends \DrSlump\Protobuf\Message { - - /** @var int */ - public $size = null; - - /** @var int */ - public $interval_us = null; - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'grpc.testing.ResponseParameters'); - - // OPTIONAL INT32 size = 1 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 1; - $f->name = "size"; - $f->type = \DrSlump\Protobuf::TYPE_INT32; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $descriptor->addField($f); - - // OPTIONAL INT32 interval_us = 2 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 2; - $f->name = "interval_us"; - $f->type = \DrSlump\Protobuf::TYPE_INT32; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $descriptor->addField($f); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - - /** - * Check if <size> has a value - * - * @return boolean - */ - public function hasSize(){ - return $this->_has(1); - } - - /** - * Clear <size> value - * - * @return \grpc\testing\ResponseParameters - */ - public function clearSize(){ - return $this->_clear(1); - } - - /** - * Get <size> value - * - * @return int - */ - public function getSize(){ - return $this->_get(1); - } - - /** - * Set <size> value - * - * @param int $value - * @return \grpc\testing\ResponseParameters - */ - public function setSize( $value){ - return $this->_set(1, $value); - } - - /** - * Check if <interval_us> has a value - * - * @return boolean - */ - public function hasIntervalUs(){ - return $this->_has(2); - } - - /** - * Clear <interval_us> value - * - * @return \grpc\testing\ResponseParameters - */ - public function clearIntervalUs(){ - return $this->_clear(2); - } - - /** - * Get <interval_us> value - * - * @return int - */ - public function getIntervalUs(){ - return $this->_get(2); - } - - /** - * Set <interval_us> value - * - * @param int $value - * @return \grpc\testing\ResponseParameters - */ - public function setIntervalUs( $value){ - return $this->_set(2, $value); - } - } -} - -namespace grpc\testing { - - class StreamingOutputCallRequest extends \DrSlump\Protobuf\Message { - - /** @var int - \grpc\testing\PayloadType */ - public $response_type = null; - - /** @var \grpc\testing\ResponseParameters[] */ - public $response_parameters = array(); - - /** @var \grpc\testing\Payload */ - public $payload = null; - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'grpc.testing.StreamingOutputCallRequest'); - - // OPTIONAL ENUM response_type = 1 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 1; - $f->name = "response_type"; - $f->type = \DrSlump\Protobuf::TYPE_ENUM; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $f->reference = '\grpc\testing\PayloadType'; - $descriptor->addField($f); - - // REPEATED MESSAGE response_parameters = 2 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 2; - $f->name = "response_parameters"; - $f->type = \DrSlump\Protobuf::TYPE_MESSAGE; - $f->rule = \DrSlump\Protobuf::RULE_REPEATED; - $f->reference = '\grpc\testing\ResponseParameters'; - $descriptor->addField($f); - - // OPTIONAL MESSAGE payload = 3 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 3; - $f->name = "payload"; - $f->type = \DrSlump\Protobuf::TYPE_MESSAGE; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $f->reference = '\grpc\testing\Payload'; - $descriptor->addField($f); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - - /** - * Check if <response_type> has a value - * - * @return boolean - */ - public function hasResponseType(){ - return $this->_has(1); - } - - /** - * Clear <response_type> value - * - * @return \grpc\testing\StreamingOutputCallRequest - */ - public function clearResponseType(){ - return $this->_clear(1); - } - - /** - * Get <response_type> value - * - * @return int - \grpc\testing\PayloadType - */ - public function getResponseType(){ - return $this->_get(1); - } - - /** - * Set <response_type> value - * - * @param int - \grpc\testing\PayloadType $value - * @return \grpc\testing\StreamingOutputCallRequest - */ - public function setResponseType( $value){ - return $this->_set(1, $value); - } - - /** - * Check if <response_parameters> has a value - * - * @return boolean - */ - public function hasResponseParameters(){ - return $this->_has(2); - } - - /** - * Clear <response_parameters> value - * - * @return \grpc\testing\StreamingOutputCallRequest - */ - public function clearResponseParameters(){ - return $this->_clear(2); - } - - /** - * Get <response_parameters> value - * - * @param int $idx - * @return \grpc\testing\ResponseParameters - */ - public function getResponseParameters($idx = NULL){ - return $this->_get(2, $idx); - } - - /** - * Set <response_parameters> value - * - * @param \grpc\testing\ResponseParameters $value - * @return \grpc\testing\StreamingOutputCallRequest - */ - public function setResponseParameters(\grpc\testing\ResponseParameters $value, $idx = NULL){ - return $this->_set(2, $value, $idx); - } - - /** - * Get all elements of <response_parameters> - * - * @return \grpc\testing\ResponseParameters[] - */ - public function getResponseParametersList(){ - return $this->_get(2); - } - - /** - * Add a new element to <response_parameters> - * - * @param \grpc\testing\ResponseParameters $value - * @return \grpc\testing\StreamingOutputCallRequest - */ - public function addResponseParameters(\grpc\testing\ResponseParameters $value){ - return $this->_add(2, $value); - } - - /** - * Check if <payload> has a value - * - * @return boolean - */ - public function hasPayload(){ - return $this->_has(3); - } - - /** - * Clear <payload> value - * - * @return \grpc\testing\StreamingOutputCallRequest - */ - public function clearPayload(){ - return $this->_clear(3); - } - - /** - * Get <payload> value - * - * @return \grpc\testing\Payload - */ - public function getPayload(){ - return $this->_get(3); - } - - /** - * Set <payload> value - * - * @param \grpc\testing\Payload $value - * @return \grpc\testing\StreamingOutputCallRequest - */ - public function setPayload(\grpc\testing\Payload $value){ - return $this->_set(3, $value); - } - } -} - -namespace grpc\testing { - - class StreamingOutputCallResponse extends \DrSlump\Protobuf\Message { - - /** @var \grpc\testing\Payload */ - public $payload = null; - - - /** @var \Closure[] */ - protected static $__extensions = array(); - - public static function descriptor() - { - $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'grpc.testing.StreamingOutputCallResponse'); - - // OPTIONAL MESSAGE payload = 1 - $f = new \DrSlump\Protobuf\Field(); - $f->number = 1; - $f->name = "payload"; - $f->type = \DrSlump\Protobuf::TYPE_MESSAGE; - $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; - $f->reference = '\grpc\testing\Payload'; - $descriptor->addField($f); - - foreach (self::$__extensions as $cb) { - $descriptor->addField($cb(), true); - } - - return $descriptor; - } - - /** - * Check if <payload> has a value - * - * @return boolean - */ - public function hasPayload(){ - return $this->_has(1); - } - - /** - * Clear <payload> value - * - * @return \grpc\testing\StreamingOutputCallResponse - */ - public function clearPayload(){ - return $this->_clear(1); - } - - /** - * Get <payload> value - * - * @return \grpc\testing\Payload - */ - public function getPayload(){ - return $this->_get(1); - } - - /** - * Set <payload> value - * - * @param \grpc\testing\Payload $value - * @return \grpc\testing\StreamingOutputCallResponse - */ - public function setPayload(\grpc\testing\Payload $value){ - return $this->_set(1, $value); - } - } -} - diff --git a/src/php/tests/interop/messages.proto b/src/php/tests/interop/messages.proto new file mode 100644 index 0000000000..de0b1a2320 --- /dev/null +++ b/src/php/tests/interop/messages.proto @@ -0,0 +1,132 @@ + +// Copyright 2015, 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. + +// Message definitions to be used by integration test service definitions. + +syntax = "proto2"; + +package grpc.testing; + +// The type of payload that should be returned. +enum PayloadType { + // Compressable text format. + COMPRESSABLE = 0; + + // Uncompressable binary format. + UNCOMPRESSABLE = 1; + + // Randomly chosen from all other formats defined in this enum. + RANDOM = 2; +} + +// A block of data, to simply increase gRPC message size. +message Payload { + // The type of data in body. + optional PayloadType type = 1 [default = COMPRESSABLE]; + // Primary contents of payload. + optional bytes body = 2; +} + +// Unary request. +message SimpleRequest { + // Desired payload type in the response from the server. + // If response_type is RANDOM, server randomly chooses one from other formats. + optional PayloadType response_type = 1 [default = COMPRESSABLE]; + + // Desired payload size in the response from the server. + // If response_type is COMPRESSABLE, this denotes the size before compression. + optional int32 response_size = 2; + + // Optional input payload sent along with the request. + optional Payload payload = 3; + + // Whether SimpleResponse should include username. + optional bool fill_username = 4; + + // Whether SimpleResponse should include OAuth scope. + optional bool fill_oauth_scope = 5; +} + +// Unary response, as configured by the request. +message SimpleResponse { + // Payload to increase message size. + optional Payload payload = 1; + // The user the request came from, for verifying authentication was + // successful when the client expected it. + optional string username = 2; + // OAuth scope. + optional string oauth_scope = 3; +} + +// Client-streaming request. +message StreamingInputCallRequest { + // Optional input payload sent along with the request. + optional Payload payload = 1; + + // Not expecting any payload from the response. +} + +// Client-streaming response. +message StreamingInputCallResponse { + // Aggregated size of payloads received from the client. + optional int32 aggregated_payload_size = 1; +} + +// Configuration for a particular response. +message ResponseParameters { + // Desired payload sizes in responses from the server. + // If response_type is COMPRESSABLE, this denotes the size before compression. + optional int32 size = 1; + + // Desired interval between consecutive responses in the response stream in + // microseconds. + optional int32 interval_us = 2; +} + +// Server-streaming request. +message StreamingOutputCallRequest { + // Desired payload type in the response from the server. + // If response_type is RANDOM, the payload from each response in the stream + // might be of different types. This is to simulate a mixed type of payload + // stream. + optional PayloadType response_type = 1 [default = COMPRESSABLE]; + + // Configuration for each expected response message. + repeated ResponseParameters response_parameters = 2; + + // Optional input payload sent along with the request. + optional Payload payload = 3; +} + +// Server-streaming response, as configured by the request and parameters. +message StreamingOutputCallResponse { + // Payload to increase response size. + optional Payload payload = 1; +} diff --git a/src/php/tests/interop/test.php b/src/php/tests/interop/test.php deleted file mode 100755 index 014bbc9517..0000000000 --- a/src/php/tests/interop/test.php +++ /dev/null @@ -1,52 +0,0 @@ -<?php -// DO NOT EDIT! Generated by Protobuf-PHP protoc plugin 1.0 -// Source: test/cpp/interop/test.proto -// Date: 2015-01-30 23:30:46 - -namespace grpc\testing { - - class TestServiceClient{ - - private $rpc_impl; - - public function __construct($rpc_impl) { - $this->rpc_impl = $rpc_impl; - } - /** - * @param grpc\testing\EmptyMessage $input - */ - public function EmptyCall(\grpc\testing\EmptyMessage $argument, $metadata = array()) { - return $this->rpc_impl->_simpleRequest('/grpc.testing.TestService/EmptyCall', $argument, '\grpc\testing\EmptyMessage::deserialize', $metadata); - } - /** - * @param grpc\testing\SimpleRequest $input - */ - public function UnaryCall(\grpc\testing\SimpleRequest $argument, $metadata = array()) { - return $this->rpc_impl->_simpleRequest('/grpc.testing.TestService/UnaryCall', $argument, '\grpc\testing\SimpleResponse::deserialize', $metadata); - } - /** - * @param grpc\testing\StreamingOutputCallRequest $input - */ - public function StreamingOutputCall($argument, $metadata = array()) { - return $this->rpc_impl->_serverStreamRequest('/grpc.testing.TestService/StreamingOutputCall', $argument, '\grpc\testing\StreamingOutputCallResponse::deserialize', $metadata); - } - /** - * @param grpc\testing\StreamingInputCallRequest $input - */ - public function StreamingInputCall($arguments, $metadata = array()) { - return $this->rpc_impl->_clientStreamRequest('/grpc.testing.TestService/StreamingInputCall', $arguments, '\grpc\testing\StreamingInputCallResponse::deserialize', $metadata); - } - /** - * @param grpc\testing\StreamingOutputCallRequest $input - */ - public function FullDuplexCall($metadata = array()) { - return $this->rpc_impl->_bidiRequest('/grpc.testing.TestService/FullDuplexCall', '\grpc\testing\StreamingOutputCallResponse::deserialize', $metadata); - } - /** - * @param grpc\testing\StreamingOutputCallRequest $input - */ - public function HalfDuplexCall($metadata = array()) { - return $this->rpc_impl->_bidiRequest('/grpc.testing.TestService/HalfDuplexCall', '\grpc\testing\StreamingOutputCallResponse::deserialize', $metadata); - } - } -} diff --git a/src/php/tests/interop/test.proto b/src/php/tests/interop/test.proto new file mode 100644 index 0000000000..39c08f3544 --- /dev/null +++ b/src/php/tests/interop/test.proto @@ -0,0 +1,72 @@ + +// Copyright 2015, 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. + +// An integration test service that covers all the method signature permutations +// of unary/streaming requests/responses. +syntax = "proto2"; + +import "empty.proto"; +import "messages.proto"; + +package grpc.testing; + +// A simple service to test the various types of RPCs and experiment with +// performance with various types of payload. +service TestService { + // One empty request followed by one empty response. + rpc EmptyCall(grpc.testing.EmptyMessage) returns (grpc.testing.EmptyMessage); + + // One request followed by one response. + // TODO(Issue 527): Describe required server behavior. + rpc UnaryCall(SimpleRequest) returns (SimpleResponse); + + // One request followed by a sequence of responses (streamed download). + // The server returns the payload with client desired type and sizes. + rpc StreamingOutputCall(StreamingOutputCallRequest) + returns (stream StreamingOutputCallResponse); + + // A sequence of requests followed by one response (streamed upload). + // The server returns the aggregated size of client payload as the result. + rpc StreamingInputCall(stream StreamingInputCallRequest) + returns (StreamingInputCallResponse); + + // A sequence of requests with each request served by the server immediately. + // As one request could lead to multiple responses, this interface + // demonstrates the idea of full duplexing. + rpc FullDuplexCall(stream StreamingOutputCallRequest) + returns (stream StreamingOutputCallResponse); + + // A sequence of requests followed by a sequence of responses. + // The server buffers all the client requests and then serves them in order. A + // stream of responses are returned to the client when the server starts with + // first request. + rpc HalfDuplexCall(stream StreamingOutputCallRequest) + returns (stream StreamingOutputCallResponse); +} diff --git a/src/php/tests/unit_tests/CallTest.php b/src/php/tests/unit_tests/CallTest.php index 8bb0927f21..77a2d86ce4 100755 --- a/src/php/tests/unit_tests/CallTest.php +++ b/src/php/tests/unit_tests/CallTest.php @@ -36,65 +36,47 @@ class CallTest extends PHPUnit_Framework_TestCase{ static $port; public static function setUpBeforeClass() { - $cq = new Grpc\CompletionQueue(); - self::$server = new Grpc\Server($cq, []); - self::$port = self::$server->add_http2_port('0.0.0.0:0'); + self::$server = new Grpc\Server([]); + self::$port = self::$server->addHttp2Port('0.0.0.0:0'); } public function setUp() { - $this->cq = new Grpc\CompletionQueue(); $this->channel = new Grpc\Channel('localhost:' . self::$port, []); $this->call = new Grpc\Call($this->channel, '/foo', - Grpc\Timeval::inf_future()); + Grpc\Timeval::infFuture()); } - /** - * @expectedException LogicException - * @expectedExceptionCode Grpc\CALL_ERROR_INVALID_FLAGS - * @expectedExceptionMessage invoke - */ - public function testInvokeRejectsBadFlags() { - $this->call->invoke($this->cq, 0, 0, 0xDEADBEEF); - } - - /** - * @expectedException LogicException - * @expectedExceptionCode Grpc\CALL_ERROR_NOT_ON_CLIENT - * @expectedExceptionMessage server_accept - */ - public function testServerAcceptFailsCorrectly() { - $this->call->server_accept($this->cq, 0); - } - - /* These test methods with assertTrue(true) at the end just check that the - method calls completed without errors. PHPUnit warns for tests with no - asserts, and this avoids that warning without changing the meaning of the - tests */ - public function testAddEmptyMetadata() { - $this->call->add_metadata([], 0); - /* Dummy assert: Checks that the previous call completed without error */ - $this->assertTrue(true); + $batch = [ + Grpc\OP_SEND_INITIAL_METADATA => [] + ]; + $result = $this->call->startBatch($batch); + $this->assertTrue($result->send_metadata); } public function testAddSingleMetadata() { - $this->call->add_metadata(['key' => ['value']], 0); - /* Dummy assert: Checks that the previous call completed without error */ - $this->assertTrue(true); + $batch = [ + Grpc\OP_SEND_INITIAL_METADATA => ['key' => ['value']] + ]; + $result = $this->call->startBatch($batch); + $this->assertTrue($result->send_metadata); } public function testAddMultiValueMetadata() { - $this->call->add_metadata(['key' => ['value1', 'value2']], 0); - /* Dummy assert: Checks that the previous call completed without error */ - $this->assertTrue(true); + $batch = [ + Grpc\OP_SEND_INITIAL_METADATA => ['key' => ['value1', 'value2']] + ]; + $result = $this->call->startBatch($batch); + $this->assertTrue($result->send_metadata); } public function testAddSingleAndMultiValueMetadata() { - $this->call->add_metadata( - ['key1' => ['value1'], - 'key2' => ['value2', 'value3']], 0); - /* Dummy assert: Checks that the previous call completed without error */ - $this->assertTrue(true); + $batch = [ + Grpc\OP_SEND_INITIAL_METADATA => ['key1' => ['value1'], + 'key2' => ['value2', 'value3']] + ]; + $result = $this->call->startBatch($batch); + $this->assertTrue($result->send_metadata); } } diff --git a/src/php/tests/unit_tests/EndToEndTest.php b/src/php/tests/unit_tests/EndToEndTest.php index 0cbc506c8e..296873fa8f 100755 --- a/src/php/tests/unit_tests/EndToEndTest.php +++ b/src/php/tests/unit_tests/EndToEndTest.php @@ -33,81 +33,68 @@ */ class EndToEndTest extends PHPUnit_Framework_TestCase{ public function setUp() { - $this->client_queue = new Grpc\CompletionQueue(); - $this->server_queue = new Grpc\CompletionQueue(); - $this->server = new Grpc\Server($this->server_queue, []); - $port = $this->server->add_http2_port('0.0.0.0:0'); + $this->server = new Grpc\Server([]); + $port = $this->server->addHttp2Port('0.0.0.0:0'); $this->channel = new Grpc\Channel('localhost:' . $port, []); + $this->server->start(); } public function tearDown() { unset($this->channel); unset($this->server); - unset($this->client_queue); - unset($this->server_queue); } public function testSimpleRequestBody() { - $deadline = Grpc\Timeval::inf_future(); + $deadline = Grpc\Timeval::infFuture(); $status_text = 'xyz'; $call = new Grpc\Call($this->channel, 'dummy_method', $deadline); - $tag = 1; - $call->invoke($this->client_queue, $tag, $tag); - $server_tag = 2; - - $call->writes_done($tag); - $event = $this->client_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\FINISH_ACCEPTED, $event->type); - $this->assertSame(Grpc\OP_OK, $event->data); - - // check that a server rpc new was received - $this->server->start(); - $this->server->request_call($server_tag); - $event = $this->server_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\SERVER_RPC_NEW, $event->type); - $server_call = $event->call; - $this->assertNotNull($server_call); - $server_call->server_accept($this->server_queue, $server_tag); - - $server_call->server_end_initial_metadata(); + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true + ]); - // the server sends the status - $server_call->start_write_status(Grpc\STATUS_OK, $status_text, $server_tag); - $event = $this->server_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\FINISH_ACCEPTED, $event->type); - $this->assertSame(Grpc\OP_OK, $event->data); + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); - // the client gets CLIENT_METADATA_READ - $event = $this->client_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\CLIENT_METADATA_READ, $event->type); + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); + $this->assertSame([], $event->metadata); + $server_call = $event->call; - // the client gets FINISHED - $event = $this->client_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\FINISHED, $event->type); - $status = $event->data; + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => Grpc\STATUS_OK, + 'details' => $status_text + ], + Grpc\OP_RECV_CLOSE_ON_SERVER => true + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_status); + $this->assertFalse($event->cancelled); + + $event = $call->startBatch([ + Grpc\OP_RECV_INITIAL_METADATA => true, + Grpc\OP_RECV_STATUS_ON_CLIENT => true + ]); + + $this->assertSame([], $event->metadata); + $status = $event->status; + $this->assertSame([], $status->metadata); $this->assertSame(Grpc\STATUS_OK, $status->code); $this->assertSame($status_text, $status->details); - // and the server gets FINISHED - $event = $this->server_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\FINISHED, $event->type); - $status = $event->data; - unset($call); unset($server_call); } public function testClientServerFullRequestResponse() { - $deadline = Grpc\Timeval::inf_future(); + $deadline = Grpc\Timeval::infFuture(); $req_text = 'client_server_full_request_response'; $reply_text = 'reply:client_server_full_request_response'; $status_text = 'status:client_server_full_response_text'; @@ -115,79 +102,52 @@ class EndToEndTest extends PHPUnit_Framework_TestCase{ $call = new Grpc\Call($this->channel, 'dummy_method', $deadline); - $tag = 1; - $call->invoke($this->client_queue, $tag, $tag); - $server_tag = 2; + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + Grpc\OP_SEND_MESSAGE => $req_text + ]); - // the client writes - $call->start_write($req_text, $tag); - $event = $this->client_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\WRITE_ACCEPTED, $event->type); + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + $this->assertTrue($event->send_message); - // check that a server rpc new was received - $this->server->start(); - $this->server->request_call($server_tag); - $event = $this->server_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\SERVER_RPC_NEW, $event->type); + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); $server_call = $event->call; - $this->assertNotNull($server_call); - $server_call->server_accept($this->server_queue, $server_tag); - - $server_call->server_end_initial_metadata(); - - // start the server read - $server_call->start_read($server_tag); - $event = $this->server_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\READ, $event->type); - $this->assertSame($req_text, $event->data); - - // the server replies - $server_call->start_write($reply_text, $server_tag); - $event = $this->server_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\WRITE_ACCEPTED, $event->type); - - // the client reads the metadata - $event = $this->client_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\CLIENT_METADATA_READ, $event->type); - - // the client reads the reply - $call->start_read($tag); - $event = $this->client_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\READ, $event->type); - $this->assertSame($reply_text, $event->data); - - // the client sends writes done - $call->writes_done($tag); - $event = $this->client_queue->next($deadline); - $this->assertSame(Grpc\FINISH_ACCEPTED, $event->type); - $this->assertSame(Grpc\OP_OK, $event->data); - - // the server sends the status - $server_call->start_write_status(GRPC\STATUS_OK, $status_text, $server_tag); - $event = $this->server_queue->next($deadline); - $this->assertSame(Grpc\FINISH_ACCEPTED, $event->type); - $this->assertSame(Grpc\OP_OK, $event->data); - - // the client gets FINISHED - $event = $this->client_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\FINISHED, $event->type); - $status = $event->data; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_MESSAGE => $reply_text, + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => Grpc\STATUS_OK, + 'details' => $status_text + ], + Grpc\OP_RECV_MESSAGE => true, + Grpc\OP_RECV_CLOSE_ON_SERVER => true, + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_status); + $this->assertTrue($event->send_message); + $this->assertFalse($event->cancelled); + $this->assertSame($req_text, $event->message); + + $event = $call->startBatch([ + Grpc\OP_RECV_INITIAL_METADATA => true, + Grpc\OP_RECV_MESSAGE => true, + Grpc\OP_RECV_STATUS_ON_CLIENT => true, + ]); + + $this->assertSame([], $event->metadata); + $this->assertSame($reply_text, $event->message); + $status = $event->status; + $this->assertSame([], $status->metadata); $this->assertSame(Grpc\STATUS_OK, $status->code); $this->assertSame($status_text, $status->details); - // and the server gets FINISHED - $event = $this->server_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\FINISHED, $event->type); - unset($call); unset($server_call); } diff --git a/src/php/tests/unit_tests/SecureEndToEndTest.php b/src/php/tests/unit_tests/SecureEndToEndTest.php index 896afeac49..0c18cd3e91 100755 --- a/src/php/tests/unit_tests/SecureEndToEndTest.php +++ b/src/php/tests/unit_tests/SecureEndToEndTest.php @@ -33,17 +33,16 @@ */ class SecureEndToEndTest extends PHPUnit_Framework_TestCase{ public function setUp() { - $this->client_queue = new Grpc\CompletionQueue(); - $this->server_queue = new Grpc\CompletionQueue(); $credentials = Grpc\Credentials::createSsl( file_get_contents(dirname(__FILE__) . '/../data/ca.pem')); $server_credentials = Grpc\ServerCredentials::createSsl( null, file_get_contents(dirname(__FILE__) . '/../data/server1.key'), file_get_contents(dirname(__FILE__) . '/../data/server1.pem')); - $this->server = new Grpc\Server($this->server_queue); - $port = $this->server->add_secure_http2_port('0.0.0.0:0', - $server_credentials); + $this->server = new Grpc\Server(); + $port = $this->server->addSecureHttp2Port('0.0.0.0:0', + $server_credentials); + $this->server->start(); $this->channel = new Grpc\Channel( 'localhost:' . $port, [ @@ -55,71 +54,59 @@ class SecureEndToEndTest extends PHPUnit_Framework_TestCase{ public function tearDown() { unset($this->channel); unset($this->server); - unset($this->client_queue); - unset($this->server_queue); } public function testSimpleRequestBody() { - $this->server->start(); - $deadline = Grpc\Timeval::inf_future(); + $deadline = Grpc\Timeval::infFuture(); $status_text = 'xyz'; $call = new Grpc\Call($this->channel, 'dummy_method', $deadline); - $tag = 1; - $call->invoke($this->client_queue, $tag, $tag); - $server_tag = 2; - - $call->writes_done($tag); - $event = $this->client_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\FINISH_ACCEPTED, $event->type); - $this->assertSame(Grpc\OP_OK, $event->data); - - // check that a server rpc new was received - $this->server->request_call($server_tag); - $event = $this->server_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\SERVER_RPC_NEW, $event->type); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); + $this->assertSame([], $event->metadata); $server_call = $event->call; - $this->assertNotNull($server_call); - $server_call->server_accept($this->server_queue, $server_tag); - - $server_call->server_end_initial_metadata(); - - // the server sends the status - $server_call->start_write_status(Grpc\STATUS_OK, $status_text, $server_tag); - $event = $this->server_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\FINISH_ACCEPTED, $event->type); - $this->assertSame(Grpc\OP_OK, $event->data); - - // the client gets CLIENT_METADATA_READ - $event = $this->client_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\CLIENT_METADATA_READ, $event->type); - - // the client gets FINISHED - $event = $this->client_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\FINISHED, $event->type); - $status = $event->data; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => Grpc\STATUS_OK, + 'details' => $status_text + ], + Grpc\OP_RECV_CLOSE_ON_SERVER => true + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_status); + $this->assertFalse($event->cancelled); + + $event = $call->startBatch([ + Grpc\OP_RECV_INITIAL_METADATA => true, + Grpc\OP_RECV_STATUS_ON_CLIENT => true + ]); + + $this->assertSame([], $event->metadata); + $status = $event->status; + $this->assertSame([], $status->metadata); $this->assertSame(Grpc\STATUS_OK, $status->code); $this->assertSame($status_text, $status->details); - // and the server gets FINISHED - $event = $this->server_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\FINISHED, $event->type); - $status = $event->data; - unset($call); unset($server_call); } public function testClientServerFullRequestResponse() { - $this->server->start(); - $deadline = Grpc\Timeval::inf_future(); + $deadline = Grpc\Timeval::infFuture(); $req_text = 'client_server_full_request_response'; $reply_text = 'reply:client_server_full_request_response'; $status_text = 'status:client_server_full_response_text'; @@ -127,78 +114,52 @@ class SecureEndToEndTest extends PHPUnit_Framework_TestCase{ $call = new Grpc\Call($this->channel, 'dummy_method', $deadline); - $tag = 1; - $call->invoke($this->client_queue, $tag, $tag); - - $server_tag = 2; - - // the client writes - $call->start_write($req_text, $tag); - $event = $this->client_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\WRITE_ACCEPTED, $event->type); - - // check that a server rpc new was received - $this->server->request_call($server_tag); - $event = $this->server_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\SERVER_RPC_NEW, $event->type); + + $event = $call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_CLOSE_FROM_CLIENT => true, + Grpc\OP_SEND_MESSAGE => $req_text + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_close); + $this->assertTrue($event->send_message); + + $event = $this->server->requestCall(); + $this->assertSame('dummy_method', $event->method); $server_call = $event->call; - $this->assertNotNull($server_call); - $server_call->server_accept($this->server_queue, $server_tag); - - $server_call->server_end_initial_metadata(); - - // start the server read - $server_call->start_read($server_tag); - $event = $this->server_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\READ, $event->type); - $this->assertSame($req_text, $event->data); - - // the server replies - $server_call->start_write($reply_text, $server_tag); - $event = $this->server_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\WRITE_ACCEPTED, $event->type); - - // the client reads the metadata - $event = $this->client_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\CLIENT_METADATA_READ, $event->type); - - // the client reads the reply - $call->start_read($tag); - $event = $this->client_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\READ, $event->type); - $this->assertSame($reply_text, $event->data); - - // the client sends writes done - $call->writes_done($tag); - $event = $this->client_queue->next($deadline); - $this->assertSame(Grpc\FINISH_ACCEPTED, $event->type); - $this->assertSame(Grpc\OP_OK, $event->data); - - // the server sends the status - $server_call->start_write_status(GRPC\STATUS_OK, $status_text, $server_tag); - $event = $this->server_queue->next($deadline); - $this->assertSame(Grpc\FINISH_ACCEPTED, $event->type); - $this->assertSame(Grpc\OP_OK, $event->data); - - // the client gets FINISHED - $event = $this->client_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\FINISHED, $event->type); - $status = $event->data; + + $event = $server_call->startBatch([ + Grpc\OP_SEND_INITIAL_METADATA => [], + Grpc\OP_SEND_MESSAGE => $reply_text, + Grpc\OP_SEND_STATUS_FROM_SERVER => [ + 'metadata' => [], + 'code' => Grpc\STATUS_OK, + 'details' => $status_text + ], + Grpc\OP_RECV_MESSAGE => true, + Grpc\OP_RECV_CLOSE_ON_SERVER => true, + ]); + + $this->assertTrue($event->send_metadata); + $this->assertTrue($event->send_status); + $this->assertTrue($event->send_message); + $this->assertFalse($event->cancelled); + $this->assertSame($req_text, $event->message); + + $event = $call->startBatch([ + Grpc\OP_RECV_INITIAL_METADATA => true, + Grpc\OP_RECV_MESSAGE => true, + Grpc\OP_RECV_STATUS_ON_CLIENT => true, + ]); + + $this->assertSame([], $event->metadata); + $this->assertSame($reply_text, $event->message); + $status = $event->status; + $this->assertSame([], $status->metadata); $this->assertSame(Grpc\STATUS_OK, $status->code); $this->assertSame($status_text, $status->details); - // and the server gets FINISHED - $event = $this->server_queue->next($deadline); - $this->assertNotNull($event); - $this->assertSame(Grpc\FINISHED, $event->type); - unset($call); unset($server_call); } diff --git a/src/php/tests/unit_tests/TimevalTest.php b/src/php/tests/unit_tests/TimevalTest.php index d20069afa1..a8bfcf0ac4 100755 --- a/src/php/tests/unit_tests/TimevalTest.php +++ b/src/php/tests/unit_tests/TimevalTest.php @@ -39,14 +39,14 @@ class TimevalTest extends PHPUnit_Framework_TestCase{ public function testPastIsLessThanZero() { $zero = Grpc\Timeval::zero(); - $past = Grpc\Timeval::inf_past(); + $past = Grpc\Timeval::infPast(); $this->assertLessThan(0, Grpc\Timeval::compare($past, $zero)); $this->assertGreaterThan(0, Grpc\Timeval::compare($zero, $past)); } public function testFutureIsGreaterThanZero() { $zero = Grpc\Timeval::zero(); - $future = Grpc\Timeval::inf_future(); + $future = Grpc\Timeval::infFuture(); $this->assertLessThan(0, Grpc\Timeval::compare($zero, $future)); $this->assertGreaterThan(0, Grpc\Timeval::compare($future, $zero)); } @@ -56,7 +56,7 @@ class TimevalTest extends PHPUnit_Framework_TestCase{ */ public function testNowIsBetweenZeroAndFuture() { $zero = Grpc\Timeval::zero(); - $future = Grpc\Timeval::inf_future(); + $future = Grpc\Timeval::infFuture(); $now = Grpc\Timeval::now(); $this->assertLessThan(0, Grpc\Timeval::compare($zero, $now)); $this->assertLessThan(0, Grpc\Timeval::compare($now, $future)); diff --git a/src/python/README.md b/src/python/README.md index c8057be38b..82bc776732 100644 --- a/src/python/README.md +++ b/src/python/README.md @@ -42,7 +42,14 @@ $ tools/run_tests/run_python.sh Installing ----------------------- -- [Install the gRPC core](https://github.com/grpc/grpc/blob/master/INSTALL) +- Install the gRPC core + - [Debian package](https://github.com/grpc/grpc/releases) + ``` + $ wget https://github.com/grpc/grpc/releases/download/release-0_5_0/libgrpc_0.5.0_amd64.deb + $ wget https://github.com/grpc/grpc/releases/download/release-0_5_0/libgrpc-dev_0.5.0_amd64.deb + $ sudo dpkg -i libgrpc_0.5.0_amd64.deb libgrpc-dev_0.5.0_amd64.deb + ``` + - [From source](https://github.com/grpc/grpc/blob/master/INSTALL) - Install gRPC Python's dependencies ``` @@ -53,3 +60,16 @@ $ pip install -r src/python/requirements.txt ``` $ pip install src/python/src ``` + +Packaging to PyPI +----------------------- + +- Install packaging dependencies +``` +$ pip install setuptools twine +``` + +- Push to PyPI +``` +$ ../../tools/distrib/python/submit.py +``` diff --git a/src/python/interop/interop/_insecure_interop_test.py b/src/python/interop/interop/_insecure_interop_test.py index e4ddff1a0b..42e7a4d5c4 100644 --- a/src/python/interop/interop/_insecure_interop_test.py +++ b/src/python/interop/interop/_insecure_interop_test.py @@ -42,11 +42,11 @@ class InsecureInteropTest( unittest.TestCase): def setUp(self): - self.server = implementations.insecure_server( + self.server = implementations.server( methods.SERVICE_NAME, methods.SERVER_METHODS, 0) self.server.start() port = self.server.port() - self.stub = implementations.insecure_stub( + self.stub = implementations.stub( methods.SERVICE_NAME, methods.CLIENT_METHODS, 'localhost', port) def tearDown(self): diff --git a/src/python/interop/interop/_interop_test_case.py b/src/python/interop/interop/_interop_test_case.py index fec8f1915d..cd6a574e90 100644 --- a/src/python/interop/interop/_interop_test_case.py +++ b/src/python/interop/interop/_interop_test_case.py @@ -40,16 +40,16 @@ class InteropTestCase(object): """ def testEmptyUnary(self): - methods.TestCase.EMPTY_UNARY.test_interoperability(self.stub) + methods.TestCase.EMPTY_UNARY.test_interoperability(self.stub, None) def testLargeUnary(self): - methods.TestCase.LARGE_UNARY.test_interoperability(self.stub) + methods.TestCase.LARGE_UNARY.test_interoperability(self.stub, None) def testServerStreaming(self): - methods.TestCase.SERVER_STREAMING.test_interoperability(self.stub) + methods.TestCase.SERVER_STREAMING.test_interoperability(self.stub, None) def testClientStreaming(self): - methods.TestCase.CLIENT_STREAMING.test_interoperability(self.stub) + methods.TestCase.CLIENT_STREAMING.test_interoperability(self.stub, None) def testPingPong(self): - methods.TestCase.PING_PONG.test_interoperability(self.stub) + methods.TestCase.PING_PONG.test_interoperability(self.stub, None) diff --git a/src/python/interop/interop/_secure_interop_test.py b/src/python/interop/interop/_secure_interop_test.py index 214212dca4..27e76315b6 100644 --- a/src/python/interop/interop/_secure_interop_test.py +++ b/src/python/interop/interop/_secure_interop_test.py @@ -45,14 +45,15 @@ class SecureInteropTest( unittest.TestCase): def setUp(self): - self.server = implementations.secure_server( + self.server = implementations.server( methods.SERVICE_NAME, methods.SERVER_METHODS, 0, - resources.private_key(), resources.certificate_chain()) + private_key=resources.private_key(), + certificate_chain=resources.certificate_chain()) self.server.start() port = self.server.port() - self.stub = implementations.secure_stub( + self.stub = implementations.stub( methods.SERVICE_NAME, methods.CLIENT_METHODS, 'localhost', port, - resources.test_root_certificates(), None, None, + secure=True, root_certificates=resources.test_root_certificates(), server_host_override=_SERVER_HOST_OVERRIDE) def tearDown(self): diff --git a/src/python/interop/interop/client.py b/src/python/interop/interop/client.py index fb7dfb5729..bae5e17460 100644 --- a/src/python/interop/interop/client.py +++ b/src/python/interop/interop/client.py @@ -30,6 +30,7 @@ """The Python implementation of the GRPC interoperability test client.""" import argparse +from oauth2client import client as oauth2client_client from grpc.early_adopter import implementations @@ -44,9 +45,6 @@ def _args(): parser.add_argument( '--server_host', help='the host to which to connect', type=str) parser.add_argument( - '--server_host_override', - help='the server host to which to claim to connect', type=str) - parser.add_argument( '--server_port', help='the port to which to connect', type=int) parser.add_argument( '--test_case', help='the test case to execute', type=str) @@ -56,24 +54,40 @@ def _args(): parser.add_argument( '--use_test_ca', help='replace platform root CAs with ca.pem', action='store_true') + parser.add_argument( + '--server_host_override', + help='the server host to which to claim to connect', type=str) + parser.add_argument('--oauth_scope', help='scope for OAuth tokens', type=str) + parser.add_argument( + '--default_service_account', + help='email address of the default service account', type=str) return parser.parse_args() +def _oauth_access_token(args): + credentials = client.GoogleCredentials.get_application_default() + scoped_credentials = credentials.create_scoped([args.oauth_scope]) + return scoped_credentials.get_access_token().access_token def _stub(args): + if args.oauth_scope: + metadata_transformer = lambda x: [('Authorization', 'Bearer %s' % _oauth_access_token(args))] + else: + metadata_transformer = lambda x: [] if args.use_tls: if args.use_test_ca: root_certificates = resources.test_root_certificates() else: root_certificates = resources.prod_root_certificates() - stub = implementations.secure_stub( + stub = implementations.stub( methods.SERVICE_NAME, methods.CLIENT_METHODS, args.server_host, - args.server_port, root_certificates, None, None, + args.server_port, metadata_transformer=metadata_transformer, + secure=True, root_certificates=root_certificates, server_host_override=args.server_host_override) else: - stub = implementations.insecure_stub( + stub = implementations.stub( methods.SERVICE_NAME, methods.CLIENT_METHODS, args.server_host, - args.server_port) + args.server_port, secure=False) return stub @@ -89,7 +103,7 @@ def _test_interoperability(): args = _args() stub = _stub(args) test_case = _test_case_from_arg(args.test_case) - test_case.test_interoperability(stub) + test_case.test_interoperability(stub, args) if __name__ == '__main__': diff --git a/src/python/interop/interop/empty_pb2.py b/src/python/interop/interop/empty_pb2.py index 732a358a36..8c1ce2f13e 100644 --- a/src/python/interop/interop/empty_pb2.py +++ b/src/python/interop/interop/empty_pb2.py @@ -57,6 +57,7 @@ Empty = _reflection.GeneratedProtocolMessageType('Empty', (_message.Message,), d _sym_db.RegisterMessage(Empty) -from grpc.framework.face import demonstration as _face_testing -from grpc.framework.face import interfaces as _face_interfaces +import abc +from grpc.early_adopter import implementations +from grpc.framework.alpha import utilities # @@protoc_insertion_point(module_scope) diff --git a/src/python/interop/interop/messages_pb2.py b/src/python/interop/interop/messages_pb2.py index d449a99140..0bf3d86a31 100644 --- a/src/python/interop/interop/messages_pb2.py +++ b/src/python/interop/interop/messages_pb2.py @@ -441,6 +441,7 @@ StreamingOutputCallResponse = _reflection.GeneratedProtocolMessageType('Streamin _sym_db.RegisterMessage(StreamingOutputCallResponse) -from grpc.framework.face import demonstration as _face_testing -from grpc.framework.face import interfaces as _face_interfaces +import abc +from grpc.early_adopter import implementations +from grpc.framework.alpha import utilities # @@protoc_insertion_point(module_scope) diff --git a/src/python/interop/interop/methods.py b/src/python/interop/interop/methods.py index 79550a3789..c69771dff1 100644 --- a/src/python/interop/interop/methods.py +++ b/src/python/interop/interop/methods.py @@ -30,8 +30,12 @@ """Implementations of interoperability test methods.""" import enum +import json +import os import threading +from oauth2client import client as oauth2client_client + from grpc.framework.alpha import utilities from interop import empty_pb2 @@ -150,19 +154,12 @@ SERVER_METHODS = { } -def _empty_unary(stub): - with stub: - response = stub.EmptyCall(empty_pb2.Empty(), _TIMEOUT) - if not isinstance(response, empty_pb2.Empty): - raise TypeError( - 'response is of type "%s", not empty_pb2.Empty!', type(response)) - - -def _large_unary(stub): +def _large_unary_common_behavior(stub, fill_username, fill_oauth_scope): with stub: request = messages_pb2.SimpleRequest( response_type=messages_pb2.COMPRESSABLE, response_size=314159, - payload=messages_pb2.Payload(body=b'\x00' * 271828)) + payload=messages_pb2.Payload(body=b'\x00' * 271828), + fill_username=fill_username, fill_oauth_scope=fill_oauth_scope) response_future = stub.UnaryCall.async(request, _TIMEOUT) response = response_future.result() if response.payload.type is not messages_pb2.COMPRESSABLE: @@ -171,6 +168,19 @@ def _large_unary(stub): if len(response.payload.body) != 314159: raise ValueError( 'response body of incorrect size %d!' % len(response.payload.body)) + return response + + +def _empty_unary(stub): + with stub: + response = stub.EmptyCall(empty_pb2.Empty(), _TIMEOUT) + if not isinstance(response, empty_pb2.Empty): + raise TypeError( + 'response is of type "%s", not empty_pb2.Empty!', type(response)) + + +def _large_unary(stub): + _large_unary_common_behavior(stub, False, False) def _client_streaming(stub): @@ -266,6 +276,28 @@ def _ping_pong(stub): pipe.close() +def _compute_engine_creds(stub, args): + response = _large_unary_common_behavior(stub, True, True) + if args.default_service_account != response.username: + raise ValueError( + 'expected username %s, got %s' % (args.default_service_account, + response.username)) + + +def _service_account_creds(stub, args): + json_key_filename = os.environ[ + oauth2client_client.GOOGLE_APPLICATION_CREDENTIALS] + wanted_email = json.load(open(json_key_filename, 'rb'))['client_email'] + response = _large_unary_common_behavior(stub, True, True) + if wanted_email != response.username: + raise ValueError( + 'expected username %s, got %s' % (wanted_email, response.username)) + if response.oauth_scope in args.oauth_scope: + raise ValueError( + 'expected to find oauth scope "%s" in received "%s"' % + (response.oauth_scope, args.oauth_scope)) + + @enum.unique class TestCase(enum.Enum): EMPTY_UNARY = 'empty_unary' @@ -273,8 +305,10 @@ class TestCase(enum.Enum): SERVER_STREAMING = 'server_streaming' CLIENT_STREAMING = 'client_streaming' PING_PONG = 'ping_pong' + COMPUTE_ENGINE_CREDS = 'compute_engine_creds' + SERVICE_ACCOUNT_CREDS = 'service_account_creds' - def test_interoperability(self, stub): + def test_interoperability(self, stub, args): if self is TestCase.EMPTY_UNARY: _empty_unary(stub) elif self is TestCase.LARGE_UNARY: @@ -285,5 +319,9 @@ class TestCase(enum.Enum): _client_streaming(stub) elif self is TestCase.PING_PONG: _ping_pong(stub) + elif self is TestCase.COMPUTE_ENGINE_CREDS: + _compute_engine_creds(stub, args) + elif self is TestCase.SERVICE_ACCOUNT_CREDS: + _service_account_creds(stub, args) else: raise NotImplementedError('Test case "%s" not implemented!' % self.name) diff --git a/src/python/interop/interop/server.py b/src/python/interop/interop/server.py index 5791203743..a67d412038 100644 --- a/src/python/interop/interop/server.py +++ b/src/python/interop/interop/server.py @@ -53,11 +53,11 @@ def serve(): if args.use_tls: private_key = resources.private_key() certificate_chain = resources.certificate_chain() - server = implementations.secure_server( - methods.SERVICE_NAME, methods.SERVER_METHODS, args.port, private_key, - certificate_chain) + server = implementations.server( + methods.SERVICE_NAME, methods.SERVER_METHODS, args.port, + private_key=private_key, certificate_chain=certificate_chain) else: - server = implementations.insecure_server( + server = implementations.server( methods.SERVICE_NAME, methods.SERVER_METHODS, args.port) server.start() diff --git a/src/python/interop/interop/test_pb2.py b/src/python/interop/interop/test_pb2.py index e86094611b..71325d5a9f 100644 --- a/src/python/interop/interop/test_pb2.py +++ b/src/python/interop/interop/test_pb2.py @@ -29,121 +29,150 @@ _sym_db.RegisterFileDescriptor(DESCRIPTOR) -from grpc.framework.face import demonstration as _face_testing -from grpc.framework.face import interfaces as _face_interfaces -class TestServiceService(object): +import abc +from grpc.early_adopter import implementations +from grpc.framework.alpha import utilities +class EarlyAdopterTestServiceServicer(object): """<fill me in later!>""" - def __init__(self): - pass -class TestServiceServicer(object): - """<fill me in later!>""" - def EmptyCall(self, arg): + __metaclass__ = abc.ABCMeta + @abc.abstractmethod + def EmptyCall(self, request, context): + raise NotImplementedError() + @abc.abstractmethod + def UnaryCall(self, request, context): raise NotImplementedError() - def UnaryCall(self, arg): + @abc.abstractmethod + def StreamingOutputCall(self, request, context): raise NotImplementedError() - def StreamingOutputCall(self, arg): + @abc.abstractmethod + def StreamingInputCall(self, request_iterator, context): raise NotImplementedError() - def StreamingInputCall(self, arg): + @abc.abstractmethod + def FullDuplexCall(self, request_iterator, context): raise NotImplementedError() - def FullDuplexCall(self, arg): + @abc.abstractmethod + def HalfDuplexCall(self, request_iterator, context): + raise NotImplementedError() +class EarlyAdopterTestServiceServer(object): + """<fill me in later!>""" + __metaclass__ = abc.ABCMeta + @abc.abstractmethod + def start(self): raise NotImplementedError() - def HalfDuplexCall(self, arg): + @abc.abstractmethod + def stop(self): raise NotImplementedError() -class TestServiceStub(object): +class EarlyAdopterTestServiceStub(object): """<fill me in later!>""" - def EmptyCall(self, arg): + __metaclass__ = abc.ABCMeta + @abc.abstractmethod + def EmptyCall(self, request): raise NotImplementedError() EmptyCall.async = None - def UnaryCall(self, arg): + @abc.abstractmethod + def UnaryCall(self, request): raise NotImplementedError() UnaryCall.async = None - def StreamingOutputCall(self, arg): + @abc.abstractmethod + def StreamingOutputCall(self, request): raise NotImplementedError() StreamingOutputCall.async = None - def StreamingInputCall(self, arg): + @abc.abstractmethod + def StreamingInputCall(self, request_iterator): raise NotImplementedError() StreamingInputCall.async = None - def FullDuplexCall(self, arg): + @abc.abstractmethod + def FullDuplexCall(self, request_iterator): raise NotImplementedError() FullDuplexCall.async = None - def HalfDuplexCall(self, arg): + @abc.abstractmethod + def HalfDuplexCall(self, request_iterator): raise NotImplementedError() HalfDuplexCall.async = None -class _TestServiceStub(TestServiceStub): - def __init__(self, face_stub, default_timeout): - self._face_stub = face_stub - self._default_timeout = default_timeout - stub_self = self - class EmptyCall(object): - def __call__(self, arg): - return stub_self._face_stub.blocking_value_in_value_out("EmptyCall", arg, stub_self._default_timeout) - def async(self, arg): - return stub_self._face_stub.future_value_in_value_out("EmptyCall", arg, stub_self._default_timeout) - self.EmptyCall = EmptyCall() - class UnaryCall(object): - def __call__(self, arg): - return stub_self._face_stub.blocking_value_in_value_out("UnaryCall", arg, stub_self._default_timeout) - def async(self, arg): - return stub_self._face_stub.future_value_in_value_out("UnaryCall", arg, stub_self._default_timeout) - self.UnaryCall = UnaryCall() - class StreamingOutputCall(object): - def __call__(self, arg): - return stub_self._face_stub.inline_value_in_stream_out("StreamingOutputCall", arg, stub_self._default_timeout) - def async(self, arg): - return stub_self._face_stub.inline_value_in_stream_out("StreamingOutputCall", arg, stub_self._default_timeout) - self.StreamingOutputCall = StreamingOutputCall() - class StreamingInputCall(object): - def __call__(self, arg): - return stub_self._face_stub.blocking_stream_in_value_out("StreamingInputCall", arg, stub_self._default_timeout) - def async(self, arg): - return stub_self._face_stub.future_stream_in_value_out("StreamingInputCall", arg, stub_self._default_timeout) - self.StreamingInputCall = StreamingInputCall() - class FullDuplexCall(object): - def __call__(self, arg): - return stub_self._face_stub.inline_stream_in_stream_out("FullDuplexCall", arg, stub_self._default_timeout) - def async(self, arg): - return stub_self._face_stub.inline_stream_in_stream_out("FullDuplexCall", arg, stub_self._default_timeout) - self.FullDuplexCall = FullDuplexCall() - class HalfDuplexCall(object): - def __call__(self, arg): - return stub_self._face_stub.inline_stream_in_stream_out("HalfDuplexCall", arg, stub_self._default_timeout) - def async(self, arg): - return stub_self._face_stub.inline_stream_in_stream_out("HalfDuplexCall", arg, stub_self._default_timeout) - self.HalfDuplexCall = HalfDuplexCall() -def mock_TestService(servicer, default_timeout): - value_in_value_out = {} - value_in_stream_out = {} - stream_in_value_out = {} - stream_in_stream_out = {} - class EmptyCall(_face_interfaces.InlineValueInValueOutMethod): - def service(self, request, context): - return servicer.EmptyCall(request) - value_in_value_out['EmptyCall'] = EmptyCall() - class UnaryCall(_face_interfaces.InlineValueInValueOutMethod): - def service(self, request, context): - return servicer.UnaryCall(request) - value_in_value_out['UnaryCall'] = UnaryCall() - class StreamingOutputCall(_face_interfaces.InlineValueInStreamOutMethod): - def service(self, request, context): - return servicer.StreamingOutputCall(request) - value_in_stream_out['StreamingOutputCall'] = StreamingOutputCall() - class StreamingInputCall(_face_interfaces.InlineStreamInValueOutMethod): - def service(self, request, context): - return servicer.StreamingInputCall(request) - stream_in_value_out['StreamingInputCall'] = StreamingInputCall() - class FullDuplexCall(_face_interfaces.InlineStreamInStreamOutMethod): - def service(self, request, context): - return servicer.FullDuplexCall(request) - stream_in_stream_out['FullDuplexCall'] = FullDuplexCall() - class HalfDuplexCall(_face_interfaces.InlineStreamInStreamOutMethod): - def service(self, request, context): - return servicer.HalfDuplexCall(request) - stream_in_stream_out['HalfDuplexCall'] = HalfDuplexCall() - face_linked_pair = _face_testing.server_and_stub(default_timeout,inline_value_in_value_out_methods=value_in_value_out,inline_value_in_stream_out_methods=value_in_stream_out,inline_stream_in_value_out_methods=stream_in_value_out,inline_stream_in_stream_out_methods=stream_in_stream_out) - class LinkedPair(object): - def __init__(self, server, stub): - self.server = server - self.stub = stub - stub = _TestServiceStub(face_linked_pair.stub, default_timeout) - return LinkedPair(None, stub) +def early_adopter_create_TestService_server(servicer, port, private_key=None, certificate_chain=None): + import test.cpp.interop.empty_pb2 + import test.cpp.interop.empty_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + method_service_descriptions = { + "EmptyCall": utilities.unary_unary_service_description( + servicer.EmptyCall, + test.cpp.interop.empty_pb2.Empty.FromString, + test.cpp.interop.empty_pb2.Empty.SerializeToString, + ), + "FullDuplexCall": utilities.stream_stream_service_description( + servicer.FullDuplexCall, + test.cpp.interop.messages_pb2.StreamingOutputCallRequest.FromString, + test.cpp.interop.messages_pb2.StreamingOutputCallResponse.SerializeToString, + ), + "HalfDuplexCall": utilities.stream_stream_service_description( + servicer.HalfDuplexCall, + test.cpp.interop.messages_pb2.StreamingOutputCallRequest.FromString, + test.cpp.interop.messages_pb2.StreamingOutputCallResponse.SerializeToString, + ), + "StreamingInputCall": utilities.stream_unary_service_description( + servicer.StreamingInputCall, + test.cpp.interop.messages_pb2.StreamingInputCallRequest.FromString, + test.cpp.interop.messages_pb2.StreamingInputCallResponse.SerializeToString, + ), + "StreamingOutputCall": utilities.unary_stream_service_description( + servicer.StreamingOutputCall, + test.cpp.interop.messages_pb2.StreamingOutputCallRequest.FromString, + test.cpp.interop.messages_pb2.StreamingOutputCallResponse.SerializeToString, + ), + "UnaryCall": utilities.unary_unary_service_description( + servicer.UnaryCall, + test.cpp.interop.messages_pb2.SimpleRequest.FromString, + test.cpp.interop.messages_pb2.SimpleResponse.SerializeToString, + ), + } + return implementations.server("grpc.testing.TestService", method_service_descriptions, port, private_key=private_key, certificate_chain=certificate_chain) +def early_adopter_create_TestService_stub(host, port, metadata_transformer=None, secure=False, root_certificates=None, private_key=None, certificate_chain=None, server_host_override=None): + import test.cpp.interop.empty_pb2 + import test.cpp.interop.empty_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + import test.cpp.interop.messages_pb2 + method_invocation_descriptions = { + "EmptyCall": utilities.unary_unary_invocation_description( + test.cpp.interop.empty_pb2.Empty.SerializeToString, + test.cpp.interop.empty_pb2.Empty.FromString, + ), + "FullDuplexCall": utilities.stream_stream_invocation_description( + test.cpp.interop.messages_pb2.StreamingOutputCallRequest.SerializeToString, + test.cpp.interop.messages_pb2.StreamingOutputCallResponse.FromString, + ), + "HalfDuplexCall": utilities.stream_stream_invocation_description( + test.cpp.interop.messages_pb2.StreamingOutputCallRequest.SerializeToString, + test.cpp.interop.messages_pb2.StreamingOutputCallResponse.FromString, + ), + "StreamingInputCall": utilities.stream_unary_invocation_description( + test.cpp.interop.messages_pb2.StreamingInputCallRequest.SerializeToString, + test.cpp.interop.messages_pb2.StreamingInputCallResponse.FromString, + ), + "StreamingOutputCall": utilities.unary_stream_invocation_description( + test.cpp.interop.messages_pb2.StreamingOutputCallRequest.SerializeToString, + test.cpp.interop.messages_pb2.StreamingOutputCallResponse.FromString, + ), + "UnaryCall": utilities.unary_unary_invocation_description( + test.cpp.interop.messages_pb2.SimpleRequest.SerializeToString, + test.cpp.interop.messages_pb2.SimpleResponse.FromString, + ), + } + return implementations.stub("grpc.testing.TestService", method_invocation_descriptions, host, port, metadata_transformer=metadata_transformer, secure=secure, root_certificates=root_certificates, private_key=private_key, certificate_chain=certificate_chain, server_host_override=server_host_override) # @@protoc_insertion_point(module_scope) diff --git a/src/python/interop/setup.py b/src/python/interop/setup.py index 6db5435090..502fcbedd8 100644 --- a/src/python/interop/setup.py +++ b/src/python/interop/setup.py @@ -29,7 +29,7 @@ """A setup module for the GRPC Python interop testing package.""" -from distutils import core as _core +import setuptools _PACKAGES = ( 'interop', @@ -45,9 +45,13 @@ _PACKAGE_DATA = { 'credentials/server1.pem',] } -_INSTALL_REQUIRES = ['grpc-2015>=0.0.1'] +_INSTALL_REQUIRES = ['oauth2client>=1.4.7', 'grpcio>=0.4.0a4'] -_core.setup( - name='interop', version='0.0.1', packages=_PACKAGES, - package_dir=_PACKAGE_DIRECTORIES, package_data=_PACKAGE_DATA, - install_requires=_INSTALL_REQUIRES) +setuptools.setup( + name='interop', + version='0.0.1', + packages=_PACKAGES, + package_dir=_PACKAGE_DIRECTORIES, + package_data=_PACKAGE_DATA, + install_requires=_INSTALL_REQUIRES +) diff --git a/src/python/src/.gitignore b/src/python/src/.gitignore new file mode 100644 index 0000000000..bc15a52cf1 --- /dev/null +++ b/src/python/src/.gitignore @@ -0,0 +1,3 @@ +MANIFEST +grpcio.egg-info/ +dist/ diff --git a/src/python/src/MANIFEST.in b/src/python/src/MANIFEST.in new file mode 100644 index 0000000000..6f32db0548 --- /dev/null +++ b/src/python/src/MANIFEST.in @@ -0,0 +1 @@ +graft grpc diff --git a/src/python/src/README.rst b/src/python/src/README.rst new file mode 100644 index 0000000000..bc1815febc --- /dev/null +++ b/src/python/src/README.rst @@ -0,0 +1,27 @@ +gRPC Python +=========== + +Package for GRPC Python. + +Dependencies +------------ + +Ensure that you have installed GRPC core. + +On debian linux systems, install from our released deb package: + +:: + + $ wget https://github.com/grpc/grpc/releases/download/release-0_5_0/libgrpc_0.5.0_amd64.deb + $ wget https://github.com/grpc/grpc/releases/download/release-0_5_0/libgrpc-dev_0.5.0_amd64.deb + $ sudo dpkg -i libgrpc_0.5.0_amd64.deb libgrpc-dev_0.5.0_amd64.deb + +Otherwise, install from source: + +:: + + git clone https://github.com/grpc/grpc.git + cd grpc + ./configure + make && make install + diff --git a/src/python/src/grpc/_adapter/_call.c b/src/python/src/grpc/_adapter/_call.c index d8806e5680..bf96c1a3fa 100644 --- a/src/python/src/grpc/_adapter/_call.c +++ b/src/python/src/grpc/_adapter/_call.c @@ -160,8 +160,22 @@ static const PyObject *pygrpc_call_accept(Call *self, PyObject *args) { return result; } +static const PyObject *pygrpc_call_add_metadata(Call *self, PyObject *args) { + const char* key = NULL; + const char* value = NULL; + int value_length = 0; + grpc_metadata metadata; + if (!PyArg_ParseTuple(args, "ss#", &key, &value, &value_length)) { + return NULL; + } + metadata.key = key; + metadata.value = value; + metadata.value_length = value_length; + return pygrpc_translate_call_error( + grpc_call_add_metadata_old(self->c_call, &metadata, 0)); +} + static const PyObject *pygrpc_call_premetadata(Call *self) { - /* TODO(nathaniel): Metadata support. */ return pygrpc_translate_call_error( grpc_call_server_end_initial_metadata_old(self->c_call, 0)); } @@ -236,6 +250,11 @@ static PyMethodDef methods[] = { {"complete", (PyCFunction)pygrpc_call_complete, METH_O, "Complete writes to this call."}, {"accept", (PyCFunction)pygrpc_call_accept, METH_VARARGS, "Accept an RPC."}, + {"add_metadata", (PyCFunction)pygrpc_call_add_metadata, METH_VARARGS, + "Add metadata to the call. May not be called after invoke on the client " + "side. On the server side: when called before premetadata it provides " + "'leading' metadata, when called after premetadata but before status it " + "provides 'trailing metadata'; may not be called after status."}, {"premetadata", (PyCFunction)pygrpc_call_premetadata, METH_VARARGS, "Indicate the end of leading metadata in the response."}, {"read", (PyCFunction)pygrpc_call_read, METH_O, diff --git a/src/python/src/grpc/_adapter/_call.h b/src/python/src/grpc/_adapter/_call.h index a936e23023..c04a2285f7 100644 --- a/src/python/src/grpc/_adapter/_call.h +++ b/src/python/src/grpc/_adapter/_call.h @@ -37,7 +37,10 @@ #include <Python.h> #include <grpc/grpc.h> -typedef struct { PyObject_HEAD grpc_call *c_call; } Call; +typedef struct { + PyObject_HEAD + grpc_call *c_call; +} Call; PyTypeObject pygrpc_CallType; diff --git a/src/python/src/grpc/_adapter/_channel.h b/src/python/src/grpc/_adapter/_channel.h index 6241ccd02e..afc0f80359 100644 --- a/src/python/src/grpc/_adapter/_channel.h +++ b/src/python/src/grpc/_adapter/_channel.h @@ -37,7 +37,10 @@ #include <Python.h> #include <grpc/grpc.h> -typedef struct { PyObject_HEAD grpc_channel *c_channel; } Channel; +typedef struct { + PyObject_HEAD + grpc_channel *c_channel; +} Channel; PyTypeObject pygrpc_ChannelType; diff --git a/src/python/src/grpc/_adapter/_client_credentials.h b/src/python/src/grpc/_adapter/_client_credentials.h index 664dc80d75..bb9f7f0c3a 100644 --- a/src/python/src/grpc/_adapter/_client_credentials.h +++ b/src/python/src/grpc/_adapter/_client_credentials.h @@ -38,7 +38,8 @@ #include <grpc/grpc_security.h> typedef struct { - PyObject_HEAD grpc_credentials *c_client_credentials; + PyObject_HEAD + grpc_credentials *c_client_credentials; } ClientCredentials; PyTypeObject pygrpc_ClientCredentialsType; diff --git a/src/python/src/grpc/_adapter/_completion_queue.c b/src/python/src/grpc/_adapter/_completion_queue.c index b56ca1926e..a639eff53e 100644 --- a/src/python/src/grpc/_adapter/_completion_queue.c +++ b/src/python/src/grpc/_adapter/_completion_queue.c @@ -115,35 +115,56 @@ static PyObject *pygrpc_status_code(grpc_status_code c_status_code) { } } +static PyObject *pygrpc_metadata_collection_get( + grpc_metadata *metadata_elements, size_t count) { + PyObject *metadata = PyList_New(count); + size_t i; + for (i = 0; i < count; ++i) { + grpc_metadata elem = metadata_elements[i]; + PyObject *key = PyString_FromString(elem.key); + PyObject *value = PyString_FromStringAndSize(elem.value, elem.value_length); + PyObject* kvp = PyTuple_Pack(2, key, value); + /* n.b. PyList_SetItem *steals* a reference to the set element. */ + PyList_SetItem(metadata, i, kvp); + Py_DECREF(key); + Py_DECREF(value); + } + return metadata; +} + static PyObject *pygrpc_stop_event_args(grpc_event *c_event) { - return PyTuple_Pack(7, stop_event_kind, Py_None, Py_None, Py_None, - Py_None, Py_None, Py_None); + return PyTuple_Pack(8, stop_event_kind, Py_None, Py_None, Py_None, + Py_None, Py_None, Py_None, Py_None); } static PyObject *pygrpc_write_event_args(grpc_event *c_event) { PyObject *write_accepted = c_event->data.write_accepted == GRPC_OP_OK ? Py_True : Py_False; - return PyTuple_Pack(7, write_event_kind, (PyObject *)c_event->tag, - write_accepted, Py_None, Py_None, Py_None, Py_None); + return PyTuple_Pack(8, write_event_kind, (PyObject *)c_event->tag, + write_accepted, Py_None, Py_None, Py_None, Py_None, + Py_None); } static PyObject *pygrpc_complete_event_args(grpc_event *c_event) { PyObject *complete_accepted = c_event->data.finish_accepted == GRPC_OP_OK ? Py_True : Py_False; - return PyTuple_Pack(7, complete_event_kind, (PyObject *)c_event->tag, - Py_None, complete_accepted, Py_None, Py_None, Py_None); + return PyTuple_Pack(8, complete_event_kind, (PyObject *)c_event->tag, + Py_None, complete_accepted, Py_None, Py_None, Py_None, + Py_None); } static PyObject *pygrpc_service_event_args(grpc_event *c_event) { if (c_event->data.server_rpc_new.method == NULL) { - return PyTuple_Pack(7, service_event_kind, c_event->tag, - Py_None, Py_None, Py_None, Py_None, Py_None); + return PyTuple_Pack( + 8, service_event_kind, c_event->tag, Py_None, Py_None, Py_None, Py_None, + Py_None, Py_None); } else { PyObject *method = NULL; PyObject *host = NULL; PyObject *service_deadline = NULL; Call *call = NULL; PyObject *service_acceptance = NULL; + PyObject *metadata = NULL; PyObject *event_args = NULL; method = PyBytes_FromString(c_event->data.server_rpc_new.method); @@ -173,11 +194,16 @@ static PyObject *pygrpc_service_event_args(grpc_event *c_event) { goto error; } - event_args = PyTuple_Pack(7, service_event_kind, + metadata = pygrpc_metadata_collection_get( + c_event->data.server_rpc_new.metadata_elements, + c_event->data.server_rpc_new.metadata_count); + event_args = PyTuple_Pack(8, service_event_kind, (PyObject *)c_event->tag, Py_None, Py_None, - service_acceptance, Py_None, Py_None); + service_acceptance, Py_None, Py_None, + metadata); Py_DECREF(service_acceptance); + Py_DECREF(metadata); error: Py_XDECREF(call); Py_XDECREF(method); @@ -190,8 +216,8 @@ error: static PyObject *pygrpc_read_event_args(grpc_event *c_event) { if (c_event->data.read == NULL) { - return PyTuple_Pack(7, read_event_kind, (PyObject *)c_event->tag, - Py_None, Py_None, Py_None, Py_None, Py_None); + return PyTuple_Pack(8, read_event_kind, (PyObject *)c_event->tag, + Py_None, Py_None, Py_None, Py_None, Py_None, Py_None); } else { size_t length; size_t offset; @@ -216,17 +242,23 @@ static PyObject *pygrpc_read_event_args(grpc_event *c_event) { if (bytes == NULL) { return NULL; } - event_args = PyTuple_Pack(7, read_event_kind, (PyObject *)c_event->tag, - Py_None, Py_None, Py_None, bytes, Py_None); + event_args = PyTuple_Pack(8, read_event_kind, (PyObject *)c_event->tag, + Py_None, Py_None, Py_None, bytes, Py_None, + Py_None); Py_DECREF(bytes); return event_args; } } static PyObject *pygrpc_metadata_event_args(grpc_event *c_event) { - /* TODO(nathaniel): Actual transmission of metadata. */ - return PyTuple_Pack(7, metadata_event_kind, (PyObject *)c_event->tag, - Py_None, Py_None, Py_None, Py_None, Py_None); + PyObject *metadata = pygrpc_metadata_collection_get( + c_event->data.client_metadata_read.elements, + c_event->data.client_metadata_read.count); + PyObject* result = PyTuple_Pack( + 8, metadata_event_kind, (PyObject *)c_event->tag, Py_None, Py_None, + Py_None, Py_None, Py_None, metadata); + Py_DECREF(metadata); + return result; } static PyObject *pygrpc_finished_event_args(grpc_event *c_event) { @@ -234,6 +266,7 @@ static PyObject *pygrpc_finished_event_args(grpc_event *c_event) { PyObject *details; PyObject *status; PyObject *event_args; + PyObject *metadata; code = pygrpc_status_code(c_event->data.finished.status); if (code == NULL) { @@ -253,9 +286,14 @@ static PyObject *pygrpc_finished_event_args(grpc_event *c_event) { if (status == NULL) { return NULL; } - event_args = PyTuple_Pack(7, finish_event_kind, (PyObject *)c_event->tag, - Py_None, Py_None, Py_None, Py_None, status); + metadata = pygrpc_metadata_collection_get( + c_event->data.finished.metadata_elements, + c_event->data.finished.metadata_count); + event_args = PyTuple_Pack(8, finish_event_kind, (PyObject *)c_event->tag, + Py_None, Py_None, Py_None, Py_None, status, + metadata); Py_DECREF(status); + Py_DECREF(metadata); return event_args; } diff --git a/src/python/src/grpc/_adapter/_completion_queue.h b/src/python/src/grpc/_adapter/_completion_queue.h index 8e5ee9f406..9b377d15d9 100644 --- a/src/python/src/grpc/_adapter/_completion_queue.h +++ b/src/python/src/grpc/_adapter/_completion_queue.h @@ -38,7 +38,8 @@ #include <grpc/grpc.h> typedef struct { - PyObject_HEAD grpc_completion_queue *c_completion_queue; + PyObject_HEAD + grpc_completion_queue *c_completion_queue; } CompletionQueue; PyTypeObject pygrpc_CompletionQueueType; diff --git a/src/python/src/grpc/_adapter/_datatypes.py b/src/python/src/grpc/_adapter/_datatypes.py index e271ec83b9..3b22784243 100644 --- a/src/python/src/grpc/_adapter/_datatypes.py +++ b/src/python/src/grpc/_adapter/_datatypes.py @@ -70,7 +70,7 @@ class Event( collections.namedtuple( 'Event', ['kind', 'tag', 'write_accepted', 'complete_accepted', - 'service_acceptance', 'bytes', 'status'])): + 'service_acceptance', 'bytes', 'status', 'metadata'])): """Describes an event emitted from a completion queue.""" @enum.unique diff --git a/src/python/src/grpc/_adapter/_links_test.py b/src/python/src/grpc/_adapter/_links_test.py index cfdcc2c4bc..4987be389a 100644 --- a/src/python/src/grpc/_adapter/_links_test.py +++ b/src/python/src/grpc/_adapter/_links_test.py @@ -43,6 +43,14 @@ _IDENTITY = lambda x: x _TIMEOUT = 2 +# TODO(nathaniel): End-to-end metadata testing. +def _transform_metadata(unused_metadata): + return ( + ('one unused key', 'one unused value'), + ('another unused key', 'another unused value'), +) + + class RoundTripTest(unittest.TestCase): def setUp(self): @@ -76,7 +84,8 @@ class RoundTripTest(unittest.TestCase): rear_link = rear.RearLink( 'localhost', port, self.rear_link_pool, {test_method: None}, - {test_method: None}, False, None, None, None) + {test_method: None}, False, None, None, None, + metadata_transformer=_transform_metadata) rear_link.join_fore_link(test_fore_link) test_fore_link.join_rear_link(rear_link) rear_link.start() diff --git a/src/python/src/grpc/_adapter/_low_test.py b/src/python/src/grpc/_adapter/_low_test.py index b04ac1c950..e88b70969c 100644 --- a/src/python/src/grpc/_adapter/_low_test.py +++ b/src/python/src/grpc/_adapter/_low_test.py @@ -115,6 +115,18 @@ class EchoTest(unittest.TestCase): def _perform_echo_test(self, test_data): method = 'test method' details = 'test details' + server_leading_metadata_key = 'my_server_leading_key' + server_leading_metadata_value = 'my_server_leading_value' + server_trailing_metadata_key = 'my_server_trailing_key' + server_trailing_metadata_value = 'my_server_trailing_value' + client_metadata_key = 'my_client_key' + client_metadata_value = 'my_client_value' + server_leading_binary_metadata_key = 'my_server_leading_key-bin' + server_leading_binary_metadata_value = b'\0'*2047 + server_trailing_binary_metadata_key = 'my_server_trailing_key-bin' + server_trailing_binary_metadata_value = b'\0'*2047 + client_binary_metadata_key = 'my_client_key-bin' + client_binary_metadata_value = b'\0'*2047 deadline = _FUTURE metadata_tag = object() finish_tag = object() @@ -128,6 +140,9 @@ class EchoTest(unittest.TestCase): client_data = [] client_call = _low.Call(self.channel, method, self.host, deadline) + client_call.add_metadata(client_metadata_key, client_metadata_value) + client_call.add_metadata(client_binary_metadata_key, + client_binary_metadata_value) client_call.invoke(self.client_completion_queue, metadata_tag, finish_tag) @@ -139,15 +154,31 @@ class EchoTest(unittest.TestCase): self.assertEqual(method, service_accepted.service_acceptance.method) self.assertEqual(self.host, service_accepted.service_acceptance.host) self.assertIsNotNone(service_accepted.service_acceptance.call) + metadata = dict(service_accepted.metadata) + self.assertIn(client_metadata_key, metadata) + self.assertEqual(client_metadata_value, metadata[client_metadata_key]) + self.assertIn(client_binary_metadata_key, metadata) + self.assertEqual(client_binary_metadata_value, + metadata[client_binary_metadata_key]) server_call = service_accepted.service_acceptance.call server_call.accept(self.server_completion_queue, finish_tag) + server_call.add_metadata(server_leading_metadata_key, + server_leading_metadata_value) + server_call.add_metadata(server_leading_binary_metadata_key, + server_leading_binary_metadata_value) server_call.premetadata() metadata_accepted = self.client_completion_queue.get(_FUTURE) self.assertIsNotNone(metadata_accepted) self.assertEqual(_low.Event.Kind.METADATA_ACCEPTED, metadata_accepted.kind) self.assertEqual(metadata_tag, metadata_accepted.tag) - # TODO(nathaniel): Test transmission and reception of metadata. + metadata = dict(metadata_accepted.metadata) + self.assertIn(server_leading_metadata_key, metadata) + self.assertEqual(server_leading_metadata_value, + metadata[server_leading_metadata_key]) + self.assertIn(server_leading_binary_metadata_key, metadata) + self.assertEqual(server_leading_binary_metadata_value, + metadata[server_leading_binary_metadata_key]) for datum in test_data: client_call.write(datum, write_tag) @@ -194,6 +225,11 @@ class EchoTest(unittest.TestCase): self.assertEqual(read_tag, read_accepted.tag) self.assertIsNone(read_accepted.bytes) + server_call.add_metadata(server_trailing_metadata_key, + server_trailing_metadata_value) + server_call.add_metadata(server_trailing_binary_metadata_key, + server_trailing_binary_metadata_value) + server_call.status(_low.Status(_low.Code.OK, details), status_tag) server_terminal_event_one = self.server_completion_queue.get(_FUTURE) server_terminal_event_two = self.server_completion_queue.get(_FUTURE) @@ -229,6 +265,13 @@ class EchoTest(unittest.TestCase): self.assertEqual(_low.Event.Kind.FINISH, finish_accepted.kind) self.assertEqual(finish_tag, finish_accepted.tag) self.assertEqual(_low.Status(_low.Code.OK, details), finish_accepted.status) + metadata = dict(finish_accepted.metadata) + self.assertIn(server_trailing_metadata_key, metadata) + self.assertEqual(server_trailing_metadata_value, + metadata[server_trailing_metadata_key]) + self.assertIn(server_trailing_binary_metadata_key, metadata) + self.assertEqual(server_trailing_binary_metadata_value, + metadata[server_trailing_binary_metadata_key]) server_timeout_none_event = self.server_completion_queue.get(0) self.assertIsNone(server_timeout_none_event) diff --git a/src/python/src/grpc/_adapter/_server.h b/src/python/src/grpc/_adapter/_server.h index 0c517e3715..4836bb638c 100644 --- a/src/python/src/grpc/_adapter/_server.h +++ b/src/python/src/grpc/_adapter/_server.h @@ -37,7 +37,10 @@ #include <Python.h> #include <grpc/grpc.h> -typedef struct { PyObject_HEAD grpc_server *c_server; } Server; +typedef struct { + PyObject_HEAD + grpc_server *c_server; +} Server; int pygrpc_add_server(PyObject *module); diff --git a/src/python/src/grpc/_adapter/_server_credentials.h b/src/python/src/grpc/_adapter/_server_credentials.h index 2e56efdcd9..6090404bd9 100644 --- a/src/python/src/grpc/_adapter/_server_credentials.h +++ b/src/python/src/grpc/_adapter/_server_credentials.h @@ -38,7 +38,8 @@ #include <grpc/grpc_security.h> typedef struct { - PyObject_HEAD grpc_server_credentials *c_server_credentials; + PyObject_HEAD + grpc_server_credentials *c_server_credentials; } ServerCredentials; PyTypeObject pygrpc_ServerCredentialsType; diff --git a/src/python/src/grpc/_adapter/rear.py b/src/python/src/grpc/_adapter/rear.py index f19321c426..2b93aa6331 100644 --- a/src/python/src/grpc/_adapter/rear.py +++ b/src/python/src/grpc/_adapter/rear.py @@ -93,7 +93,7 @@ class RearLink(base_interfaces.RearLink, activated.Activated): def __init__( self, host, port, pool, request_serializers, response_deserializers, secure, root_certificates, private_key, certificate_chain, - server_host_override=None): + metadata_transformer=None, server_host_override=None): """Constructor. Args: @@ -111,6 +111,9 @@ class RearLink(base_interfaces.RearLink, activated.Activated): key should be used. certificate_chain: The PEM-encoded certificate chain to use or None if no certificate chain should be used. + metadata_transformer: A function that given a metadata object produces + another metadata to be used in the underlying communication on the + wire. server_host_override: (For testing only) the target name used for SSL host name checking. """ @@ -134,6 +137,7 @@ class RearLink(base_interfaces.RearLink, activated.Activated): self._root_certificates = root_certificates self._private_key = private_key self._certificate_chain = certificate_chain + self._metadata_transformer = metadata_transformer self._server_host_override = server_host_override def _on_write_event(self, operation_id, event, rpc_state): @@ -243,6 +247,10 @@ class RearLink(base_interfaces.RearLink, activated.Activated): """ request_serializer = self._request_serializers[name] call = _low.Call(self._channel, name, self._host, time.time() + timeout) + if self._metadata_transformer is not None: + metadata = self._metadata_transformer([]) + for metadata_key, metadata_value in metadata: + call.add_metadata(metadata_key, metadata_value) call.invoke(self._completion_queue, operation_id, operation_id) outstanding = set(_INVOCATION_EVENT_KINDS) diff --git a/src/python/src/grpc/early_adopter/implementations.py b/src/python/src/grpc/early_adopter/implementations.py index cc0b8ec9e8..35456d38c6 100644 --- a/src/python/src/grpc/early_adopter/implementations.py +++ b/src/python/src/grpc/early_adopter/implementations.py @@ -114,7 +114,7 @@ class _Stub(interfaces.Stub): def __init__( self, breakdown, host, port, secure, root_certificates, private_key, - certificate_chain, server_host_override=None): + certificate_chain, metadata_transformer=None, server_host_override=None): self._lock = threading.Lock() self._breakdown = breakdown self._host = host @@ -123,6 +123,7 @@ class _Stub(interfaces.Stub): self._root_certificates = root_certificates self._private_key = private_key self._certificate_chain = certificate_chain + self._metadata_transformer = metadata_transformer self._server_host_override = server_host_override self._pool = None @@ -141,6 +142,7 @@ class _Stub(interfaces.Stub): self._breakdown.request_serializers, self._breakdown.response_deserializers, self._secure, self._root_certificates, self._private_key, self._certificate_chain, + metadata_transformer=self._metadata_transformer, server_host_override=self._server_host_override) self._front.join_rear_link(self._rear_link) self._rear_link.join_fore_link(self._front) @@ -188,43 +190,11 @@ class _Stub(interfaces.Stub): raise AttributeError(attr) -def _build_stub( - service_name, methods, host, port, secure, root_certificates, private_key, - certificate_chain, server_host_override=None): - breakdown = _face_utilities.break_down_invocation(service_name, methods) - return _Stub( - breakdown, host, port, secure, root_certificates, private_key, - certificate_chain, server_host_override=server_host_override) - - -def _build_server(service_name, methods, port, private_key, certificate_chain): - breakdown = _face_utilities.break_down_service(service_name, methods) - return _Server(breakdown, port, private_key, certificate_chain) - - -def insecure_stub(service_name, methods, host, port): - """Constructs an insecure interfaces.Stub. - - Args: - service_name: The package-qualified full name of the service. - methods: A dictionary from RPC method name to - interfaces.RpcMethodInvocationDescription describing the RPCs to be - supported by the created stub. The RPC method names in the dictionary are - not qualified by the service name or decorated in any other way. - host: The host to which to connect for RPC service. - port: The port to which to connect for RPC service. - - Returns: - An interfaces.Stub affording RPC invocation. - """ - return _build_stub( - service_name, methods, host, port, False, None, None, None) - - -def secure_stub( - service_name, methods, host, port, root_certificates, private_key, - certificate_chain, server_host_override=None): - """Constructs an insecure interfaces.Stub. +def stub( + service_name, methods, host, port, metadata_transformer=None, secure=False, + root_certificates=None, private_key=None, certificate_chain=None, + server_host_override=None): + """Constructs an interfaces.Stub. Args: service_name: The package-qualified full name of the service. @@ -234,6 +204,10 @@ def secure_stub( not qualified by the service name or decorated in any other way. host: The host to which to connect for RPC service. port: The port to which to connect for RPC service. + metadata_transformer: A callable that given a metadata object produces + another metadata object to be used in the underlying communication on the + wire. + secure: Whether or not to construct the stub with a secure connection. root_certificates: The PEM-encoded root certificates or None to ask for them to be retrieved from a default location. private_key: The PEM-encoded private key to use or None if no private key @@ -246,32 +220,15 @@ def secure_stub( Returns: An interfaces.Stub affording RPC invocation. """ - return _build_stub( - service_name, methods, host, port, True, root_certificates, private_key, + breakdown = _face_utilities.break_down_invocation(service_name, methods) + return _Stub( + breakdown, host, port, secure, root_certificates, private_key, certificate_chain, server_host_override=server_host_override) -def insecure_server(service_name, methods, port): - """Constructs an insecure interfaces.Server. - - Args: - service_name: The package-qualified full name of the service. - methods: A dictionary from RPC method name to - interfaces.RpcMethodServiceDescription describing the RPCs to - be serviced by the created server. The RPC method names in the dictionary - are not qualified by the service name or decorated in any other way. - port: The desired port on which to serve or zero to ask for a port to - be automatically selected. - - Returns: - An interfaces.Server that will run with no security and - service unsecured raw requests. - """ - return _build_server(service_name, methods, port, None, None) - - -def secure_server(service_name, methods, port, private_key, certificate_chain): - """Constructs a secure interfaces.Server. +def server( + service_name, methods, port, private_key=None, certificate_chain=None): + """Constructs an interfaces.Server. Args: service_name: The package-qualified full name of the service. @@ -281,11 +238,12 @@ def secure_server(service_name, methods, port, private_key, certificate_chain): are not qualified by the service name or decorated in any other way. port: The port on which to serve or zero to ask for a port to be automatically selected. - private_key: A pem-encoded private key. - certificate_chain: A pem-encoded certificate chain. + private_key: A pem-encoded private key, or None for an insecure server. + certificate_chain: A pem-encoded certificate chain, or None for an insecure + server. Returns: An interfaces.Server that will serve secure traffic. """ - return _build_server( - service_name, methods, port, private_key, certificate_chain) + breakdown = _face_utilities.break_down_service(service_name, methods) + return _Server(breakdown, port, private_key, certificate_chain) diff --git a/src/python/src/grpc/early_adopter/implementations_test.py b/src/python/src/grpc/early_adopter/implementations_test.py index ae4adad90f..32b974724c 100644 --- a/src/python/src/grpc/early_adopter/implementations_test.py +++ b/src/python/src/grpc/early_adopter/implementations_test.py @@ -106,11 +106,11 @@ _TIMEOUT = 3 class EarlyAdopterImplementationsTest(unittest.TestCase): def setUp(self): - self.server = implementations.insecure_server( + self.server = implementations.server( SERVICE_NAME, _SERVICE_DESCRIPTIONS, 0) self.server.start() port = self.server.port() - self.stub = implementations.insecure_stub( + self.stub = implementations.stub( SERVICE_NAME, _INVOCATION_DESCRIPTIONS, 'localhost', port) def tearDown(self): diff --git a/src/python/src/setup.py b/src/python/src/setup.py index bd70634b8f..32ac41e285 100644 --- a/src/python/src/setup.py +++ b/src/python/src/setup.py @@ -30,6 +30,8 @@ """A setup module for the GRPC Python package.""" from distutils import core as _core +import setuptools +import sys _EXTENSION_SOURCES = ( 'grpc/_adapter/_c.c', @@ -49,8 +51,9 @@ _EXTENSION_INCLUDE_DIRECTORIES = ( _EXTENSION_LIBRARIES = ( 'grpc', 'gpr', - 'rt', ) +if not "darwin" in sys.platform: + _EXTENSION_LIBRARIES += ('rt',) _EXTENSION_MODULE = _core.Extension( 'grpc._adapter._c', sources=list(_EXTENSION_SOURCES), @@ -80,7 +83,15 @@ _PACKAGE_DIRECTORIES = { 'grpc.framework': 'grpc/framework', } -_core.setup( - name='grpc-2015', version='0.4.0', - ext_modules=[_EXTENSION_MODULE], packages=list(_PACKAGES), - package_dir=_PACKAGE_DIRECTORIES) +setuptools.setup( + name='grpcio', + version='0.5.0a0', + ext_modules=[_EXTENSION_MODULE], + packages=list(_PACKAGES), + package_dir=_PACKAGE_DIRECTORIES, + install_requires=[ + 'enum34==1.0.4', + 'futures==2.2.0', + 'protobuf==3.0.0-alpha-1' + ] +) diff --git a/src/ruby/lib/grpc/version.rb b/src/ruby/lib/grpc/version.rb index 513a53724f..bfd0cbb393 100644 --- a/src/ruby/lib/grpc/version.rb +++ b/src/ruby/lib/grpc/version.rb @@ -29,5 +29,5 @@ # GRPC contains the General RPC module. module GRPC - VERSION = '0.5.0' + VERSION = '0.6.0' end |