gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
rpc_service_method.h
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPCXX_IMPL_RPC_SERVICE_METHOD_H
35 #define GRPCXX_IMPL_RPC_SERVICE_METHOD_H
36 
37 #include <functional>
38 #include <map>
39 #include <memory>
40 #include <vector>
41 
42 #include <grpc++/config.h>
43 #include <grpc++/impl/rpc_method.h>
44 #include <grpc++/status.h>
45 #include <grpc++/stream.h>
46 
47 namespace grpc {
48 class ServerContext;
49 class StreamContextInterface;
50 
51 // TODO(rocking): we might need to split this file into multiple ones.
52 
53 // Base class for running an RPC handler.
55  public:
56  virtual ~MethodHandler() {}
58  HandlerParameter(Call* c, ServerContext* context,
59  const grpc::protobuf::Message* req,
60  grpc::protobuf::Message* resp)
61  : call(c), server_context(context), request(req), response(resp) {}
62  Call* call;
63  ServerContext* server_context;
64  const grpc::protobuf::Message* request;
65  grpc::protobuf::Message* response;
66  };
67  virtual Status RunHandler(const HandlerParameter& param) = 0;
68 };
69 
70 // A wrapper class of an application provided rpc method handler.
71 template <class ServiceType, class RequestType, class ResponseType>
73  public:
75  std::function<Status(ServiceType*, ServerContext*, const RequestType*,
76  ResponseType*)> func,
77  ServiceType* service)
78  : func_(func), service_(service) {}
79 
80  Status RunHandler(const HandlerParameter& param) GRPC_FINAL {
81  // Invoke application function, cast proto messages to their actual types.
82  return func_(service_, param.server_context,
83  dynamic_cast<const RequestType*>(param.request),
84  dynamic_cast<ResponseType*>(param.response));
85  }
86 
87  private:
88  // Application provided rpc handler function.
89  std::function<Status(ServiceType*, ServerContext*, const RequestType*,
90  ResponseType*)> func_;
91  // The class the above handler function lives in.
92  ServiceType* service_;
93 };
94 
95 // A wrapper class of an application provided client streaming handler.
96 template <class ServiceType, class RequestType, class ResponseType>
98  public:
100  std::function<Status(ServiceType*, ServerContext*,
101  ServerReader<RequestType>*, ResponseType*)> func,
102  ServiceType* service)
103  : func_(func), service_(service) {}
104 
105  Status RunHandler(const HandlerParameter& param) GRPC_FINAL {
106  ServerReader<RequestType> reader(param.call, param.server_context);
107  return func_(service_, param.server_context, &reader,
108  dynamic_cast<ResponseType*>(param.response));
109  }
110 
111  private:
112  std::function<Status(ServiceType*, ServerContext*, ServerReader<RequestType>*,
113  ResponseType*)> func_;
114  ServiceType* service_;
115 };
116 
117 // A wrapper class of an application provided server streaming handler.
118 template <class ServiceType, class RequestType, class ResponseType>
120  public:
122  std::function<Status(ServiceType*, ServerContext*, const RequestType*,
124  ServiceType* service)
125  : func_(func), service_(service) {}
126 
127  Status RunHandler(const HandlerParameter& param) GRPC_FINAL {
128  ServerWriter<ResponseType> writer(param.call, param.server_context);
129  return func_(service_, param.server_context,
130  dynamic_cast<const RequestType*>(param.request), &writer);
131  }
132 
133  private:
134  std::function<Status(ServiceType*, ServerContext*, const RequestType*,
136  ServiceType* service_;
137 };
138 
139 // A wrapper class of an application provided bidi-streaming handler.
140 template <class ServiceType, class RequestType, class ResponseType>
142  public:
144  std::function<Status(ServiceType*, ServerContext*,
146  func,
147  ServiceType* service)
148  : func_(func), service_(service) {}
149 
150  Status RunHandler(const HandlerParameter& param) GRPC_FINAL {
152  param.server_context);
153  return func_(service_, param.server_context, &stream);
154  }
155 
156  private:
157  std::function<Status(ServiceType*, ServerContext*,
159  ServiceType* service_;
160 };
161 
162 // Server side rpc method class
163 class RpcServiceMethod : public RpcMethod {
164  public:
165  // Takes ownership of the handler and two prototype objects.
166  RpcServiceMethod(const char* name, RpcMethod::RpcType type,
167  MethodHandler* handler,
168  grpc::protobuf::Message* request_prototype,
169  grpc::protobuf::Message* response_prototype)
170  : RpcMethod(name, type, nullptr),
171  handler_(handler),
172  request_prototype_(request_prototype),
173  response_prototype_(response_prototype) {}
174 
175  MethodHandler* handler() { return handler_.get(); }
176 
177  grpc::protobuf::Message* AllocateRequestProto() {
178  return request_prototype_->New();
179  }
180  grpc::protobuf::Message* AllocateResponseProto() {
181  return response_prototype_->New();
182  }
183 
184  private:
185  std::unique_ptr<MethodHandler> handler_;
186  std::unique_ptr<grpc::protobuf::Message> request_prototype_;
187  std::unique_ptr<grpc::protobuf::Message> response_prototype_;
188 };
189 
190 // This class contains all the method information for an rpc service. It is
191 // used for registering a service on a grpc server.
192 class RpcService {
193  public:
194  // Takes ownership.
195  void AddMethod(RpcServiceMethod* method) { methods_.emplace_back(method); }
196 
197  RpcServiceMethod* GetMethod(int i) { return methods_[i].get(); }
198  int GetMethodCount() const { return methods_.size(); }
199 
200  private:
201  std::vector<std::unique_ptr<RpcServiceMethod>> methods_;
202 };
203 
204 } // namespace grpc
205 
206 #endif // GRPCXX_IMPL_RPC_SERVICE_METHOD_H
Definition: rpc_service_method.h:57
Definition: rpc_service_method.h:119
Definition: completion_queue.h:53
Definition: rpc_service_method.h:141
Definition: status.h:42
Definition: rpc_service_method.h:72
Definition: rpc_service_method.h:163
Definition: channel.h:53
Definition: rpc_service_method.h:97
Definition: completion_queue.h:55
Definition: chttp2_transport.c:307
Definition: completion_queue.h:57
Definition: _call.h:44
Definition: rpc_service_method.h:192
Definition: rpc_service_method.h:54
Definition: rpc_method.h:39
Definition: server_context.h:70
Definition: channel_create.c:62