mirror of https://github.com/grpc/grpc.git
commit
d5bb9a3c93
851 changed files with 17469 additions and 6738 deletions
@ -1,95 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2014, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef __GRPCPP_ASYNC_SERVER_CONTEXT_H__ |
||||
#define __GRPCPP_ASYNC_SERVER_CONTEXT_H__ |
||||
|
||||
#include <chrono> |
||||
|
||||
#include <grpc++/config.h> |
||||
|
||||
struct grpc_byte_buffer; |
||||
struct grpc_call; |
||||
struct grpc_completion_queue; |
||||
|
||||
namespace google { |
||||
namespace protobuf { |
||||
class Message; |
||||
} |
||||
} |
||||
|
||||
using std::chrono::system_clock; |
||||
|
||||
namespace grpc { |
||||
class Status; |
||||
|
||||
// TODO(rocking): wrap grpc c structures.
|
||||
class AsyncServerContext { |
||||
public: |
||||
AsyncServerContext(grpc_call* call, const grpc::string& method, |
||||
const grpc::string& host, |
||||
system_clock::time_point absolute_deadline); |
||||
~AsyncServerContext(); |
||||
|
||||
// Accept this rpc, bind it to a completion queue.
|
||||
void Accept(grpc_completion_queue* cq); |
||||
|
||||
// Read and write calls, all async. Return true for success.
|
||||
bool StartRead(google::protobuf::Message* request); |
||||
bool StartWrite(const google::protobuf::Message& response, int flags); |
||||
bool StartWriteStatus(const Status& status); |
||||
|
||||
bool ParseRead(grpc_byte_buffer* read_buffer); |
||||
|
||||
grpc::string method() const { return method_; } |
||||
grpc::string host() const { return host_; } |
||||
system_clock::time_point absolute_deadline() { return absolute_deadline_; } |
||||
|
||||
grpc_call* call() { return call_; } |
||||
|
||||
private: |
||||
AsyncServerContext(const AsyncServerContext&); |
||||
AsyncServerContext& operator=(const AsyncServerContext&); |
||||
|
||||
// These properties may be moved to a ServerContext class.
|
||||
const grpc::string method_; |
||||
const grpc::string host_; |
||||
system_clock::time_point absolute_deadline_; |
||||
|
||||
google::protobuf::Message* request_; // not owned
|
||||
grpc_call* call_; // owned
|
||||
}; |
||||
|
||||
} // namespace grpc
|
||||
|
||||
#endif // __GRPCPP_ASYNC_SERVER_CONTEXT_H__
|
@ -0,0 +1,140 @@ |
||||
/*
|
||||
* |
||||
* 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. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef __GRPCPP_ASYNC_UNARY_CALL_H__ |
||||
#define __GRPCPP_ASYNC_UNARY_CALL_H__ |
||||
|
||||
#include <grpc++/channel_interface.h> |
||||
#include <grpc++/client_context.h> |
||||
#include <grpc++/completion_queue.h> |
||||
#include <grpc++/server_context.h> |
||||
#include <grpc++/impl/call.h> |
||||
#include <grpc++/impl/service_type.h> |
||||
#include <grpc++/status.h> |
||||
#include <grpc/support/log.h> |
||||
|
||||
namespace grpc { |
||||
template <class R> |
||||
class ClientAsyncResponseReader final { |
||||
public: |
||||
ClientAsyncResponseReader(ChannelInterface* channel, CompletionQueue* cq, |
||||
const RpcMethod& method, ClientContext* context, |
||||
const google::protobuf::Message& request, void* tag) |
||||
: context_(context), |
||||
call_(channel->CreateCall(method, context, cq)) { |
||||
init_buf_.Reset(tag); |
||||
init_buf_.AddSendInitialMetadata(&context->send_initial_metadata_); |
||||
init_buf_.AddSendMessage(request); |
||||
init_buf_.AddClientSendClose(); |
||||
call_.PerformOps(&init_buf_); |
||||
} |
||||
|
||||
void ReadInitialMetadata(void* tag) { |
||||
GPR_ASSERT(!context_->initial_metadata_received_); |
||||
|
||||
meta_buf_.Reset(tag); |
||||
meta_buf_.AddRecvInitialMetadata(context_); |
||||
call_.PerformOps(&meta_buf_); |
||||
} |
||||
|
||||
void Finish(R* msg, Status* status, void* tag) { |
||||
finish_buf_.Reset(tag); |
||||
if (!context_->initial_metadata_received_) { |
||||
finish_buf_.AddRecvInitialMetadata(context_); |
||||
} |
||||
finish_buf_.AddRecvMessage(msg); |
||||
finish_buf_.AddClientRecvStatus(context_, status); |
||||
call_.PerformOps(&finish_buf_); |
||||
} |
||||
|
||||
|
||||
private: |
||||
ClientContext* context_ = nullptr; |
||||
Call call_; |
||||
CallOpBuffer init_buf_; |
||||
CallOpBuffer meta_buf_; |
||||
CallOpBuffer finish_buf_; |
||||
}; |
||||
|
||||
template <class W> |
||||
class ServerAsyncResponseWriter final : public ServerAsyncStreamingInterface { |
||||
public: |
||||
explicit ServerAsyncResponseWriter(ServerContext* ctx) |
||||
: call_(nullptr, nullptr, nullptr), ctx_(ctx) {} |
||||
|
||||
void SendInitialMetadata(void* tag) { |
||||
GPR_ASSERT(!ctx_->sent_initial_metadata_); |
||||
|
||||
meta_buf_.Reset(tag); |
||||
meta_buf_.AddSendInitialMetadata(&ctx_->initial_metadata_); |
||||
ctx_->sent_initial_metadata_ = true; |
||||
call_.PerformOps(&meta_buf_); |
||||
} |
||||
|
||||
void Finish(const W& msg, const Status& status, void* tag) { |
||||
finish_buf_.Reset(tag); |
||||
if (!ctx_->sent_initial_metadata_) { |
||||
finish_buf_.AddSendInitialMetadata(&ctx_->initial_metadata_); |
||||
ctx_->sent_initial_metadata_ = true; |
||||
} |
||||
// The response is dropped if the status is not OK.
|
||||
if (status.IsOk()) { |
||||
finish_buf_.AddSendMessage(msg); |
||||
} |
||||
finish_buf_.AddServerSendStatus(&ctx_->trailing_metadata_, status); |
||||
call_.PerformOps(&finish_buf_); |
||||
} |
||||
|
||||
void FinishWithError(const Status& status, void* tag) { |
||||
GPR_ASSERT(!status.IsOk()); |
||||
finish_buf_.Reset(tag); |
||||
if (!ctx_->sent_initial_metadata_) { |
||||
finish_buf_.AddSendInitialMetadata(&ctx_->initial_metadata_); |
||||
ctx_->sent_initial_metadata_ = true; |
||||
} |
||||
finish_buf_.AddServerSendStatus(&ctx_->trailing_metadata_, status); |
||||
call_.PerformOps(&finish_buf_); |
||||
} |
||||
|
||||
private: |
||||
void BindCall(Call* call) override { call_ = *call; } |
||||
|
||||
Call call_; |
||||
ServerContext* ctx_; |
||||
CallOpBuffer meta_buf_; |
||||
CallOpBuffer finish_buf_; |
||||
}; |
||||
|
||||
} // namespace grpc
|
||||
|
||||
#endif // __GRPCPP_ASYNC_UNARY_CALL_H__
|
@ -0,0 +1,145 @@ |
||||
/*
|
||||
* |
||||
* 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. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef __GRPCPP_CALL_H__ |
||||
#define __GRPCPP_CALL_H__ |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <grpc++/status.h> |
||||
#include <grpc++/completion_queue.h> |
||||
|
||||
#include <memory> |
||||
#include <map> |
||||
|
||||
namespace google { |
||||
namespace protobuf { |
||||
class Message; |
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
|
||||
struct grpc_call; |
||||
struct grpc_op; |
||||
|
||||
namespace grpc { |
||||
|
||||
class Call; |
||||
|
||||
class CallOpBuffer : public CompletionQueueTag { |
||||
public: |
||||
CallOpBuffer() : return_tag_(this) {} |
||||
~CallOpBuffer(); |
||||
|
||||
void Reset(void *next_return_tag); |
||||
|
||||
// Does not take ownership.
|
||||
void AddSendInitialMetadata( |
||||
std::multimap<grpc::string, grpc::string> *metadata); |
||||
void AddSendInitialMetadata(ClientContext *ctx); |
||||
void AddRecvInitialMetadata(ClientContext *ctx); |
||||
void AddSendMessage(const google::protobuf::Message &message); |
||||
void AddRecvMessage(google::protobuf::Message *message); |
||||
void AddClientSendClose(); |
||||
void AddClientRecvStatus(ClientContext *ctx, Status *status); |
||||
void AddServerSendStatus(std::multimap<grpc::string, grpc::string> *metadata, |
||||
const Status &status); |
||||
void AddServerRecvClose(bool *cancelled); |
||||
|
||||
// INTERNAL API:
|
||||
|
||||
// Convert to an array of grpc_op elements
|
||||
void FillOps(grpc_op *ops, size_t *nops); |
||||
|
||||
// Called by completion queue just prior to returning from Next() or Pluck()
|
||||
bool FinalizeResult(void **tag, bool *status) override; |
||||
|
||||
bool got_message = false; |
||||
|
||||
private: |
||||
void *return_tag_ = nullptr; |
||||
// Send initial metadata
|
||||
bool send_initial_metadata_ = false; |
||||
size_t initial_metadata_count_ = 0; |
||||
grpc_metadata *initial_metadata_ = nullptr; |
||||
// Recv initial metadta
|
||||
std::multimap<grpc::string, grpc::string> *recv_initial_metadata_ = nullptr; |
||||
grpc_metadata_array recv_initial_metadata_arr_ = {0, 0, nullptr}; |
||||
// Send message
|
||||
const google::protobuf::Message *send_message_ = nullptr; |
||||
grpc_byte_buffer *send_message_buf_ = nullptr; |
||||
// Recv message
|
||||
google::protobuf::Message *recv_message_ = nullptr; |
||||
grpc_byte_buffer *recv_message_buf_ = nullptr; |
||||
// Client send close
|
||||
bool client_send_close_ = false; |
||||
// Client recv status
|
||||
std::multimap<grpc::string, grpc::string> *recv_trailing_metadata_ = nullptr; |
||||
Status *recv_status_ = nullptr; |
||||
grpc_metadata_array recv_trailing_metadata_arr_ = {0, 0, nullptr}; |
||||
grpc_status_code status_code_ = GRPC_STATUS_OK; |
||||
char *status_details_ = nullptr; |
||||
size_t status_details_capacity_ = 0; |
||||
// Server send status
|
||||
const Status *send_status_ = nullptr; |
||||
size_t trailing_metadata_count_ = 0; |
||||
grpc_metadata *trailing_metadata_ = nullptr; |
||||
int cancelled_buf_; |
||||
bool *recv_closed_ = nullptr; |
||||
}; |
||||
|
||||
// Channel and Server implement this to allow them to hook performing ops
|
||||
class CallHook { |
||||
public: |
||||
virtual ~CallHook() {} |
||||
virtual void PerformOpsOnCall(CallOpBuffer *ops, Call *call) = 0; |
||||
}; |
||||
|
||||
// Straightforward wrapping of the C call object
|
||||
class Call final { |
||||
public: |
||||
/* call is owned by the caller */ |
||||
Call(grpc_call *call, CallHook *call_hook_, CompletionQueue *cq); |
||||
|
||||
void PerformOps(CallOpBuffer *buffer); |
||||
|
||||
grpc_call *call() { return call_; } |
||||
CompletionQueue *cq() { return cq_; } |
||||
|
||||
private: |
||||
CallHook *call_hook_; |
||||
CompletionQueue *cq_; |
||||
grpc_call *call_; |
||||
}; |
||||
|
||||
} // namespace grpc
|
||||
|
||||
#endif // __GRPCPP_CALL_INTERFACE_H__
|
@ -0,0 +1,127 @@ |
||||
/*
|
||||
* |
||||
* 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. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef __GRPCPP_IMPL_SERVICE_TYPE_H__ |
||||
#define __GRPCPP_IMPL_SERVICE_TYPE_H__ |
||||
|
||||
namespace google { |
||||
namespace protobuf { |
||||
class Message; |
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
|
||||
namespace grpc { |
||||
|
||||
class Call; |
||||
class RpcService; |
||||
class Server; |
||||
class ServerContext; |
||||
class Status; |
||||
|
||||
class SynchronousService { |
||||
public: |
||||
virtual ~SynchronousService() {} |
||||
virtual RpcService* service() = 0; |
||||
}; |
||||
|
||||
class ServerAsyncStreamingInterface { |
||||
public: |
||||
virtual ~ServerAsyncStreamingInterface() {} |
||||
|
||||
virtual void SendInitialMetadata(void* tag) = 0; |
||||
|
||||
private: |
||||
friend class Server; |
||||
virtual void BindCall(Call* call) = 0; |
||||
}; |
||||
|
||||
class AsynchronousService { |
||||
public: |
||||
// this is Server, but in disguise to avoid a link dependency
|
||||
class DispatchImpl { |
||||
public: |
||||
virtual void RequestAsyncCall(void* registered_method, |
||||
ServerContext* context, |
||||
::google::protobuf::Message* request, |
||||
ServerAsyncStreamingInterface* stream, |
||||
CompletionQueue* cq, void* tag) = 0; |
||||
}; |
||||
|
||||
AsynchronousService(CompletionQueue* cq, const char** method_names, |
||||
size_t method_count) |
||||
: cq_(cq), method_names_(method_names), method_count_(method_count) {} |
||||
|
||||
~AsynchronousService() { delete[] request_args_; } |
||||
|
||||
CompletionQueue* completion_queue() const { return cq_; } |
||||
|
||||
protected: |
||||
void RequestAsyncUnary(int index, ServerContext* context, |
||||
::google::protobuf::Message* request, |
||||
ServerAsyncStreamingInterface* stream, |
||||
CompletionQueue* cq, void* tag) { |
||||
dispatch_impl_->RequestAsyncCall(request_args_[index], context, request, |
||||
stream, cq, tag); |
||||
} |
||||
void RequestClientStreaming(int index, ServerContext* context, |
||||
ServerAsyncStreamingInterface* stream, |
||||
CompletionQueue* cq, void* tag) { |
||||
dispatch_impl_->RequestAsyncCall(request_args_[index], context, nullptr, |
||||
stream, cq, tag); |
||||
} |
||||
void RequestServerStreaming(int index, ServerContext* context, |
||||
::google::protobuf::Message* request, |
||||
ServerAsyncStreamingInterface* stream, |
||||
CompletionQueue* cq, void* tag) { |
||||
dispatch_impl_->RequestAsyncCall(request_args_[index], context, request, |
||||
stream, cq, tag); |
||||
} |
||||
void RequestBidiStreaming(int index, ServerContext* context, |
||||
ServerAsyncStreamingInterface* stream, |
||||
CompletionQueue* cq, void* tag) { |
||||
dispatch_impl_->RequestAsyncCall(request_args_[index], context, nullptr, |
||||
stream, cq, tag); |
||||
} |
||||
|
||||
private: |
||||
friend class Server; |
||||
CompletionQueue* const cq_; |
||||
DispatchImpl* dispatch_impl_ = nullptr; |
||||
const char** const method_names_; |
||||
size_t method_count_; |
||||
void** request_args_ = nullptr; |
||||
}; |
||||
|
||||
} // namespace grpc
|
||||
|
||||
#endif // __GRPCPP_IMPL_SERVICE_TYPE_H__
|
@ -0,0 +1,330 @@ |
||||
/*
|
||||
* |
||||
* 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 <cassert> |
||||
#include <cctype> |
||||
#include <map> |
||||
#include <ostream> |
||||
#include <sstream> |
||||
|
||||
#include "src/compiler/python_generator.h" |
||||
#include <google/protobuf/io/printer.h> |
||||
#include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
||||
#include <google/protobuf/descriptor.pb.h> |
||||
#include <google/protobuf/descriptor.h> |
||||
|
||||
using google::protobuf::FileDescriptor; |
||||
using google::protobuf::ServiceDescriptor; |
||||
using google::protobuf::MethodDescriptor; |
||||
using google::protobuf::io::Printer; |
||||
using google::protobuf::io::StringOutputStream; |
||||
using std::initializer_list; |
||||
using std::map; |
||||
using std::string; |
||||
|
||||
namespace grpc_python_generator { |
||||
namespace { |
||||
//////////////////////////////////
|
||||
// BEGIN FORMATTING BOILERPLATE //
|
||||
//////////////////////////////////
|
||||
|
||||
// Converts an initializer list of the form { key0, value0, key1, value1, ... }
|
||||
// into a map of key* to value*. Is merely a readability helper for later code.
|
||||
map<string, string> ListToDict(const initializer_list<string>& values) { |
||||
assert(values.size() % 2 == 0); |
||||
map<string, string> value_map; |
||||
auto value_iter = values.begin(); |
||||
for (unsigned i = 0; i < values.size()/2; ++i) { |
||||
string key = *value_iter; |
||||
++value_iter; |
||||
string value = *value_iter; |
||||
value_map[key] = value; |
||||
++value_iter; |
||||
} |
||||
return value_map; |
||||
} |
||||
|
||||
// Provides RAII indentation handling. Use as:
|
||||
// {
|
||||
// IndentScope raii_my_indent_var_name_here(my_py_printer);
|
||||
// // constructor indented my_py_printer
|
||||
// ...
|
||||
// // destructor called at end of scope, un-indenting my_py_printer
|
||||
// }
|
||||
class IndentScope { |
||||
public: |
||||
explicit IndentScope(Printer* printer) : printer_(printer) { |
||||
printer_->Indent(); |
||||
} |
||||
|
||||
~IndentScope() { |
||||
printer_->Outdent(); |
||||
} |
||||
|
||||
private: |
||||
Printer* printer_; |
||||
}; |
||||
|
||||
////////////////////////////////
|
||||
// END FORMATTING BOILERPLATE //
|
||||
////////////////////////////////
|
||||
|
||||
void PrintService(const ServiceDescriptor* service, |
||||
Printer* out) { |
||||
string doc = "<fill me in later!>"; |
||||
map<string, string> dict = ListToDict({ |
||||
"Service", service->name(), |
||||
"Documentation", doc, |
||||
}); |
||||
out->Print(dict, "class $Service$Service(object):\n"); |
||||
{ |
||||
IndentScope raii_class_indent(out); |
||||
out->Print(dict, "\"\"\"$Documentation$\"\"\"\n"); |
||||
out->Print("def __init__(self):\n"); |
||||
{ |
||||
IndentScope raii_method_indent(out); |
||||
out->Print("pass\n"); |
||||
} |
||||
} |
||||
} |
||||
|
||||
void PrintServicer(const ServiceDescriptor* service, |
||||
Printer* out) { |
||||
string doc = "<fill me in later!>"; |
||||
map<string, string> dict = ListToDict({ |
||||
"Service", service->name(), |
||||
"Documentation", doc, |
||||
}); |
||||
out->Print(dict, "class $Service$Servicer(object):\n"); |
||||
{ |
||||
IndentScope raii_class_indent(out); |
||||
out->Print(dict, "\"\"\"$Documentation$\"\"\"\n"); |
||||
for (int i = 0; i < service->method_count(); ++i) { |
||||
auto meth = service->method(i); |
||||
out->Print("def $Method$(self, arg):\n", "Method", meth->name()); |
||||
{ |
||||
IndentScope raii_method_indent(out); |
||||
out->Print("raise NotImplementedError()\n"); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
void PrintStub(const ServiceDescriptor* service, |
||||
Printer* out) { |
||||
string doc = "<fill me in later!>"; |
||||
map<string, string> dict = ListToDict({ |
||||
"Service", service->name(), |
||||
"Documentation", doc, |
||||
}); |
||||
out->Print(dict, "class $Service$Stub(object):\n"); |
||||
{ |
||||
IndentScope raii_class_indent(out); |
||||
out->Print(dict, "\"\"\"$Documentation$\"\"\"\n"); |
||||
for (int i = 0; i < service->method_count(); ++i) { |
||||
const MethodDescriptor* meth = service->method(i); |
||||
auto methdict = ListToDict({"Method", meth->name()}); |
||||
out->Print(methdict, "def $Method$(self, arg):\n"); |
||||
{ |
||||
IndentScope raii_method_indent(out); |
||||
out->Print("raise NotImplementedError()\n"); |
||||
} |
||||
out->Print(methdict, "$Method$.async = None\n"); |
||||
} |
||||
} |
||||
} |
||||
|
||||
void PrintStubImpl(const ServiceDescriptor* service, |
||||
Printer* out) { |
||||
map<string, string> dict = ListToDict({ |
||||
"Service", service->name(), |
||||
}); |
||||
out->Print(dict, "class _$Service$Stub($Service$Stub):\n"); |
||||
{ |
||||
IndentScope raii_class_indent(out); |
||||
out->Print("def __init__(self, face_stub, default_timeout):\n"); |
||||
{ |
||||
IndentScope raii_method_indent(out); |
||||
out->Print("self._face_stub = face_stub\n" |
||||
"self._default_timeout = default_timeout\n" |
||||
"stub_self = self\n"); |
||||
|
||||
for (int i = 0; i < service->method_count(); ++i) { |
||||
const MethodDescriptor* meth = service->method(i); |
||||
bool server_streaming = meth->server_streaming(); |
||||
bool client_streaming = meth->client_streaming(); |
||||
std::string blocking_call, future_call; |
||||
if (server_streaming) { |
||||
if (client_streaming) { |
||||
blocking_call = "stub_self._face_stub.inline_stream_in_stream_out"; |
||||
future_call = blocking_call; |
||||
} else { |
||||
blocking_call = "stub_self._face_stub.inline_value_in_stream_out"; |
||||
future_call = blocking_call; |
||||
} |
||||
} else { |
||||
if (client_streaming) { |
||||
blocking_call = "stub_self._face_stub.blocking_stream_in_value_out"; |
||||
future_call = "stub_self._face_stub.future_stream_in_value_out"; |
||||
} else { |
||||
blocking_call = "stub_self._face_stub.blocking_value_in_value_out"; |
||||
future_call = "stub_self._face_stub.future_value_in_value_out"; |
||||
} |
||||
} |
||||
// TODO(atash): use the solution described at
|
||||
// http://stackoverflow.com/a/2982 to bind 'async' attribute
|
||||
// functions to def'd functions instead of using callable attributes.
|
||||
auto methdict = ListToDict({ |
||||
"Method", meth->name(), |
||||
"BlockingCall", blocking_call, |
||||
"FutureCall", future_call |
||||
}); |
||||
out->Print(methdict, "class $Method$(object):\n"); |
||||
{ |
||||
IndentScope raii_callable_indent(out); |
||||
out->Print("def __call__(self, arg):\n"); |
||||
{ |
||||
IndentScope raii_callable_call_indent(out); |
||||
out->Print(methdict, |
||||
"return $BlockingCall$(\"$Method$\", arg, " |
||||
"stub_self._default_timeout)\n"); |
||||
} |
||||
out->Print("def async(self, arg):\n"); |
||||
{ |
||||
IndentScope raii_callable_async_indent(out); |
||||
out->Print(methdict, |
||||
"return $FutureCall$(\"$Method$\", arg, " |
||||
"stub_self._default_timeout)\n"); |
||||
} |
||||
} |
||||
out->Print(methdict, "self.$Method$ = $Method$()\n"); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
void PrintStubGenerators(const ServiceDescriptor* service, Printer* out) { |
||||
map<string, string> dict = ListToDict({ |
||||
"Service", service->name(), |
||||
}); |
||||
// Write out a generator of linked pairs of Server/Stub
|
||||
out->Print(dict, "def mock_$Service$(servicer, default_timeout):\n"); |
||||
{ |
||||
IndentScope raii_mock_indent(out); |
||||
out->Print("value_in_value_out = {}\n" |
||||
"value_in_stream_out = {}\n" |
||||
"stream_in_value_out = {}\n" |
||||
"stream_in_stream_out = {}\n"); |
||||
for (int i = 0; i < service->method_count(); ++i) { |
||||
const MethodDescriptor* meth = service->method(i); |
||||
std::string super_interface, meth_dict; |
||||
bool server_streaming = meth->server_streaming(); |
||||
bool client_streaming = meth->client_streaming(); |
||||
if (server_streaming) { |
||||
if (client_streaming) { |
||||
super_interface = "InlineStreamInStreamOutMethod"; |
||||
meth_dict = "stream_in_stream_out"; |
||||
} else { |
||||
super_interface = "InlineValueInStreamOutMethod"; |
||||
meth_dict = "value_in_stream_out"; |
||||
} |
||||
} else { |
||||
if (client_streaming) { |
||||
super_interface = "InlineStreamInValueOutMethod"; |
||||
meth_dict = "stream_in_value_out"; |
||||
} else { |
||||
super_interface = "InlineValueInValueOutMethod"; |
||||
meth_dict = "value_in_value_out"; |
||||
} |
||||
} |
||||
map<string, string> methdict = ListToDict({ |
||||
"Method", meth->name(), |
||||
"SuperInterface", super_interface, |
||||
"MethodDict", meth_dict |
||||
}); |
||||
out->Print( |
||||
methdict, "class $Method$(_face_interfaces.$SuperInterface$):\n"); |
||||
{ |
||||
IndentScope raii_inline_class_indent(out); |
||||
out->Print("def service(self, request, context):\n"); |
||||
{ |
||||
IndentScope raii_inline_class_fn_indent(out); |
||||
out->Print(methdict, "return servicer.$Method$(request)\n"); |
||||
} |
||||
} |
||||
out->Print(methdict, "$MethodDict$['$Method$'] = $Method$()\n"); |
||||
} |
||||
out->Print( |
||||
"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)\n"); |
||||
out->Print("class LinkedPair(object):\n"); |
||||
{ |
||||
IndentScope raii_linked_pair(out); |
||||
out->Print("def __init__(self, server, stub):\n"); |
||||
{ |
||||
IndentScope raii_linked_pair_init(out); |
||||
out->Print("self.server = server\n" |
||||
"self.stub = stub\n"); |
||||
} |
||||
} |
||||
out->Print( |
||||
dict, |
||||
"stub = _$Service$Stub(face_linked_pair.stub, default_timeout)\n"); |
||||
out->Print("return LinkedPair(None, stub)\n"); |
||||
} |
||||
} |
||||
|
||||
} // namespace
|
||||
|
||||
string GetServices(const FileDescriptor* file) { |
||||
string output; |
||||
StringOutputStream output_stream(&output); |
||||
Printer out(&output_stream, '$'); |
||||
out.Print("from grpc.framework.face import demonstration as _face_testing\n"); |
||||
out.Print("from grpc.framework.face import interfaces as _face_interfaces\n"); |
||||
|
||||
for (int i = 0; i < file->service_count(); ++i) { |
||||
auto service = file->service(i); |
||||
PrintService(service, &out); |
||||
PrintServicer(service, &out); |
||||
PrintStub(service, &out); |
||||
PrintStubImpl(service, &out); |
||||
PrintStubGenerators(service, &out); |
||||
} |
||||
return output; |
||||
} |
||||
|
||||
} // namespace grpc_python_generator
|
@ -0,0 +1,87 @@ |
||||
/*
|
||||
* |
||||
* 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 a Python gRPC service interface out of Protobuf IDL.
|
||||
|
||||
#include <memory> |
||||
#include <string> |
||||
|
||||
#include "src/compiler/python_generator.h" |
||||
#include <google/protobuf/compiler/code_generator.h> |
||||
#include <google/protobuf/compiler/plugin.h> |
||||
#include <google/protobuf/io/coded_stream.h> |
||||
#include <google/protobuf/io/zero_copy_stream.h> |
||||
#include <google/protobuf/descriptor.h> |
||||
|
||||
using google::protobuf::FileDescriptor; |
||||
using google::protobuf::compiler::CodeGenerator; |
||||
using google::protobuf::compiler::GeneratorContext; |
||||
using google::protobuf::compiler::PluginMain; |
||||
using google::protobuf::io::CodedOutputStream; |
||||
using google::protobuf::io::ZeroCopyOutputStream; |
||||
using std::string; |
||||
|
||||
class PythonGrpcGenerator : public CodeGenerator { |
||||
public: |
||||
PythonGrpcGenerator() {} |
||||
~PythonGrpcGenerator() override {} |
||||
|
||||
bool Generate(const FileDescriptor* file, |
||||
const string& parameter, |
||||
GeneratorContext* context, |
||||
string* error) const override { |
||||
// Get output file name.
|
||||
string file_name; |
||||
static const int proto_suffix_length = 6; // length of ".proto"
|
||||
if (file->name().size() > proto_suffix_length && |
||||
file->name().find_last_of(".proto") == file->name().size() - 1) { |
||||
file_name = file->name().substr( |
||||
0, file->name().size() - proto_suffix_length) + "_pb2.py"; |
||||
} else { |
||||
*error = "Invalid proto file name. Proto file must end with .proto"; |
||||
return false; |
||||
} |
||||
|
||||
std::unique_ptr<ZeroCopyOutputStream> output( |
||||
context->OpenForInsert(file_name, "module_scope")); |
||||
CodedOutputStream coded_out(output.get()); |
||||
string code = grpc_python_generator::GetServices(file); |
||||
coded_out.WriteRaw(code.data(), code.size()); |
||||
return true; |
||||
} |
||||
}; |
||||
|
||||
int main(int argc, char* argv[]) { |
||||
PythonGrpcGenerator generator; |
||||
return PluginMain(argc, argv, &generator); |
||||
} |
@ -0,0 +1,9 @@ |
||||
#Overview |
||||
|
||||
This directory contains source code for shared C library. Libraries in other languages in this repository (C++, Ruby, |
||||
Python, PHP, NodeJS, Objective-C) are layered on top of this library. |
||||
|
||||
#Status |
||||
|
||||
Alpha : Ready for early adopters |
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue