Skip to content

Commit

Permalink
Merge pull request grpc#1003 from yang-g/untypedAPI
Browse files Browse the repository at this point in the history
Anonymous API implementation
  • Loading branch information
vjpai committed Mar 16, 2015
2 parents 1cc9fae + 1ad253d commit d3d55c7
Show file tree
Hide file tree
Showing 18 changed files with 985 additions and 44 deletions.
68 changes: 67 additions & 1 deletion Makefile

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions build.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
{
"name": "grpc++_base",
"public_headers": [
"include/grpc++/async_generic_service.h",
"include/grpc++/async_unary_call.h",
"include/grpc++/byte_buffer.h",
"include/grpc++/channel_arguments.h",
"include/grpc++/channel_interface.h",
"include/grpc++/client_context.h",
Expand All @@ -30,6 +32,7 @@
"include/grpc++/server_builder.h",
"include/grpc++/server_context.h",
"include/grpc++/server_credentials.h",
"include/grpc++/slice.h",
"include/grpc++/status.h",
"include/grpc++/status_code_enum.h",
"include/grpc++/stream.h",
Expand All @@ -54,12 +57,15 @@
"src/cpp/common/completion_queue.cc",
"src/cpp/common/rpc_method.cc",
"src/cpp/proto/proto_utils.cc",
"src/cpp/server/async_generic_service.cc",
"src/cpp/server/insecure_server_credentials.cc",
"src/cpp/server/server.cc",
"src/cpp/server/server_builder.cc",
"src/cpp/server/server_context.cc",
"src/cpp/server/server_credentials.cc",
"src/cpp/server/thread_pool.cc",
"src/cpp/util/byte_buffer.cc",
"src/cpp/util/slice.cc",
"src/cpp/util/status.cc",
"src/cpp/util/time.cc"
]
Expand Down Expand Up @@ -1698,6 +1704,22 @@
"gpr"
]
},
{
"name": "generic_end2end_test",
"build": "test",
"language": "c++",
"src": [
"test/cpp/end2end/generic_end2end_test.cc"
],
"deps": [
"grpc++_test_util",
"grpc_test_util",
"grpc++",
"grpc",
"gpr_test_util",
"gpr"
]
},
{
"name": "grpc_cpp_plugin",
"build": "protoc",
Expand Down
79 changes: 79 additions & 0 deletions include/grpc++/async_generic_service.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
*
* 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 GRPCXX_ASYNC_GENERIC_SERVICE_H
#define GRPCXX_ASYNC_GENERIC_SERVICE_H

#include <grpc++/byte_buffer.h>
#include <grpc++/stream.h>

struct grpc_server;

namespace grpc {

typedef ServerAsyncReaderWriter<ByteBuffer, ByteBuffer> GenericServerAsyncReaderWriter;

class GenericServerContext GRPC_FINAL : public ServerContext {
public:
const grpc::string& method() const { return method_; }
const grpc::string& host() const { return host_; }

private:
friend class Server;

grpc::string method_;
grpc::string host_;
};

class AsyncGenericService GRPC_FINAL {
public:
// TODO(yangg) Once we can add multiple completion queues to the server
// in c core, add a CompletionQueue* argument to the ctor here.
// TODO(yangg) support methods list.
AsyncGenericService(const grpc::string& methods) : server_(nullptr) {}

void RequestCall(GenericServerContext* ctx,
GenericServerAsyncReaderWriter* reader_writer,
CompletionQueue* cq, void* tag);

// The new rpc event should be obtained from this completion queue.
CompletionQueue* completion_queue();

private:
friend class Server;
Server* server_;
};

} // namespace grpc

#endif // GRPCXX_ASYNC_GENERIC_SERVICE_H
84 changes: 84 additions & 0 deletions include/grpc++/byte_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
*
* 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 GRPCXX_BYTE_BUFFER_H
#define GRPCXX_BYTE_BUFFER_H

#include <grpc/grpc.h>
#include <grpc/support/log.h>
#include <grpc++/config.h>
#include <grpc++/slice.h>

#include <vector>

namespace grpc {

class ByteBuffer GRPC_FINAL {
public:
ByteBuffer() : buffer_(nullptr) {}

ByteBuffer(Slice* slices, size_t nslices);

~ByteBuffer() {
if (buffer_) {
grpc_byte_buffer_destroy(buffer_);
}
}

void Dump(std::vector<Slice>* slices);

void Clear();
size_t Length();

private:
friend class CallOpBuffer;

// takes ownership
void set_buffer(grpc_byte_buffer* buf) {
if (buffer_) {
gpr_log(GPR_ERROR, "Overriding existing buffer");
Clear();
}
buffer_ = buf;
}

grpc_byte_buffer* buffer() const {
return buffer_;
}

grpc_byte_buffer* buffer_;
};

} // namespace grpc

#endif // GRPCXX_BYTE_BUFFER_H
62 changes: 62 additions & 0 deletions include/grpc++/generic_stub.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
*
* 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 GRPCXX_GENERIC_STUB_H
#define GRPCXX_GENERIC_STUB_H

#include <grpc++/byte_buffer.h>
#include <grpc++/stream.h>

namespace grpc {

typedef ClientAsyncReaderWriter<ByteBuffer, ByteBuffer>
GenericClientAsyncReaderWriter;

// Generic stubs provide a type-unsafe interface to call gRPC methods
// by name.
class GenericStub GRPC_FINAL {
public:
explicit GenericStub(std::shared_ptr<ChannelInterface> channel)
: channel_(channel) {}

// begin a call to a named method
std::unique_ptr<GenericClientAsyncReaderWriter> Call(
ClientContext* context, const grpc::string& method);

private:
std::shared_ptr<ChannelInterface> channel_;
};

} // namespace grpc

#endif // GRPCXX_GENERIC_STUB_H
11 changes: 8 additions & 3 deletions include/grpc++/impl/call.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
#define GRPCXX_IMPL_CALL_H

#include <grpc/grpc.h>
#include <grpc++/completion_queue.h>
#include <grpc++/config.h>
#include <grpc++/status.h>
#include <grpc++/completion_queue.h>

#include <memory>
#include <map>
Expand All @@ -47,6 +47,7 @@ struct grpc_op;

namespace grpc {

class ByteBuffer;
class Call;

class CallOpBuffer : public CompletionQueueTag {
Expand All @@ -62,7 +63,9 @@ class CallOpBuffer : public CompletionQueueTag {
void AddSendInitialMetadata(ClientContext *ctx);
void AddRecvInitialMetadata(ClientContext *ctx);
void AddSendMessage(const grpc::protobuf::Message &message);
void AddSendMessage(const ByteBuffer& message);
void AddRecvMessage(grpc::protobuf::Message *message);
void AddRecvMessage(ByteBuffer *message);
void AddClientSendClose();
void AddClientRecvStatus(ClientContext *ctx, Status *status);
void AddServerSendStatus(std::multimap<grpc::string, grpc::string> *metadata,
Expand Down Expand Up @@ -90,10 +93,12 @@ class CallOpBuffer : public CompletionQueueTag {
grpc_metadata_array recv_initial_metadata_arr_;
// Send message
const grpc::protobuf::Message *send_message_;
grpc_byte_buffer *send_message_buf_;
const ByteBuffer *send_message_buffer_;
grpc_byte_buffer *send_buf_;
// Recv message
grpc::protobuf::Message *recv_message_;
grpc_byte_buffer *recv_message_buf_;
ByteBuffer *recv_message_buffer_;
grpc_byte_buffer *recv_buf_;
// Client send close
bool client_send_close_;
// Client recv status
Expand Down
8 changes: 8 additions & 0 deletions include/grpc++/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ struct grpc_server;

namespace grpc {
class AsynchronousService;
class GenericServerContext;
class AsyncGenericService;
class RpcService;
class RpcServiceMethod;
class ServerCredentials;
Expand All @@ -69,6 +71,7 @@ class Server GRPC_FINAL : private CallHook,
void Wait();

private:
friend class AsyncGenericService;
friend class ServerBuilder;

class SyncRequest;
Expand All @@ -81,6 +84,7 @@ class Server GRPC_FINAL : private CallHook,
// The service must exist for the lifetime of the Server instance.
bool RegisterService(RpcService* service);
bool RegisterAsyncService(AsynchronousService* service);
void RegisterAsyncGenericService(AsyncGenericService* service);
// Add a listening port. Can be called multiple times.
int AddPort(const grpc::string& addr, ServerCredentials* creds);
// Start the server.
Expand All @@ -98,6 +102,10 @@ class Server GRPC_FINAL : private CallHook,
ServerAsyncStreamingInterface* stream,
CompletionQueue* cq, void* tag) GRPC_OVERRIDE;

void RequestAsyncGenericCall(GenericServerContext* context,
ServerAsyncStreamingInterface* stream,
CompletionQueue* cq, void* tag);

// Completion queue.
CompletionQueue cq_;

Expand Down
5 changes: 5 additions & 0 deletions include/grpc++/server_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

namespace grpc {

class AsyncGenericService;
class AsynchronousService;
class CompletionQueue;
class RpcService;
Expand All @@ -64,6 +65,9 @@ class ServerBuilder {
// instance returned by BuildAndStart().
void RegisterAsyncService(AsynchronousService* service);

// Register a generic service.
void RegisterAsyncGenericService(AsyncGenericService* service);

// Add a listening port. Can be called multiple times.
void AddPort(const grpc::string& addr,
std::shared_ptr<ServerCredentials> creds,
Expand All @@ -87,6 +91,7 @@ class ServerBuilder {
std::vector<AsynchronousService*> async_services_;
std::vector<Port> ports_;
std::shared_ptr<ServerCredentials> creds_;
AsyncGenericService* generic_service_;
ThreadPoolInterface* thread_pool_;
};

Expand Down
2 changes: 1 addition & 1 deletion include/grpc++/server_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CompletionQueue;
class Server;

// Interface of server side rpc context.
class ServerContext GRPC_FINAL {
class ServerContext {
public:
ServerContext(); // for async calls
~ServerContext();
Expand Down
Loading

0 comments on commit d3d55c7

Please sign in to comment.