GRPC C++  0.13.1-pre1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
server_interface.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015-2016, 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_CODEGEN_SERVER_INTERFACE_H
35 #define GRPCXX_IMPL_CODEGEN_SERVER_INTERFACE_H
36 
37 #include <grpc/impl/codegen/grpc_types.h>
41 
42 namespace grpc {
43 
44 class AsyncGenericService;
45 class GenericServerContext;
46 class RpcService;
47 class ServerAsyncStreamingInterface;
48 class ServerCompletionQueue;
49 class ServerContext;
50 class ServerCredentials;
51 class Service;
52 class ThreadPoolInterface;
53 
57 class ServerInterface : public CallHook {
58  public:
59  virtual ~ServerInterface() {}
60 
66  template <class T>
67  void Shutdown(const T& deadline) {
68  ShutdownInternal(TimePoint<T>(deadline).raw_time());
69  }
70 
72  void Shutdown() { ShutdownInternal(gpr_inf_future(GPR_CLOCK_MONOTONIC)); }
73 
78  virtual void Wait() = 0;
79 
80  protected:
81  friend class Service;
82 
85  virtual bool RegisterService(const grpc::string* host, Service* service) = 0;
86 
89  virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
90 
102  virtual int AddListeningPort(const grpc::string& addr,
103  ServerCredentials* creds) = 0;
104 
113  virtual bool Start(ServerCompletionQueue** cqs, size_t num_cqs) = 0;
114 
116  virtual void RunRpc() = 0;
117 
119  virtual void ScheduleCallback() = 0;
120 
121  virtual void ShutdownInternal(gpr_timespec deadline) = 0;
122 
123  virtual int max_message_size() const = 0;
124 
125  virtual grpc_server* server() = 0;
126 
127  virtual void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) = 0;
128 
130  public:
133  CompletionQueue* call_cq, void* tag,
134  bool delete_on_finalize);
135  virtual ~BaseAsyncRequest() {}
136 
137  bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE;
138 
139  protected:
144  void* const tag_;
146  grpc_call* call_;
147  grpc_metadata_array initial_metadata_array_;
148  };
149 
151  public:
154  CompletionQueue* call_cq, void* tag);
155 
156  // uses BaseAsyncRequest::FinalizeResult
157 
158  protected:
159  void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
160  ServerCompletionQueue* notification_cq);
161  };
162 
164  public:
165  NoPayloadAsyncRequest(void* registered_method, ServerInterface* server,
166  ServerContext* context,
168  CompletionQueue* call_cq,
169  ServerCompletionQueue* notification_cq, void* tag)
170  : RegisteredAsyncRequest(server, context, stream, call_cq, tag) {
171  IssueRequest(registered_method, nullptr, notification_cq);
172  }
173 
174  // uses RegisteredAsyncRequest::FinalizeResult
175  };
176 
177  template <class Message>
179  public:
180  PayloadAsyncRequest(void* registered_method, ServerInterface* server,
181  ServerContext* context,
183  CompletionQueue* call_cq,
184  ServerCompletionQueue* notification_cq, void* tag,
185  Message* request)
186  : RegisteredAsyncRequest(server, context, stream, call_cq, tag),
187  request_(request) {
188  IssueRequest(registered_method, &payload_, notification_cq);
189  }
190 
191  bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE {
192  bool serialization_status =
193  *status && payload_ &&
195  payload_, request_, server_->max_message_size()).ok();
196  bool ret = RegisteredAsyncRequest::FinalizeResult(tag, status);
197  *status = serialization_status&&* status;
198  return ret;
199  }
200 
201  private:
202  grpc_byte_buffer* payload_;
203  Message* const request_;
204  };
205 
207  public:
210  CompletionQueue* call_cq,
211  ServerCompletionQueue* notification_cq, void* tag,
212  bool delete_on_finalize);
213 
214  bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE;
215 
216  private:
217  grpc_call_details call_details_;
218  };
219 
220  template <class Message>
223  CompletionQueue* call_cq,
224  ServerCompletionQueue* notification_cq, void* tag,
225  Message* message) {
226  GPR_ASSERT(method);
227  new PayloadAsyncRequest<Message>(method->server_tag(), this, context,
228  stream, call_cq, notification_cq, tag,
229  message);
230  }
231 
234  CompletionQueue* call_cq,
235  ServerCompletionQueue* notification_cq, void* tag) {
236  GPR_ASSERT(method);
237  new NoPayloadAsyncRequest(method->server_tag(), this, context, stream,
238  call_cq, notification_cq, tag);
239  }
240 
243  CompletionQueue* call_cq,
244  ServerCompletionQueue* notification_cq,
245  void* tag) {
246  new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
247  tag, true);
248  }
249 };
250 
251 } // namespace grpc
252 
253 #endif // GRPCXX_IMPL_CODEGEN_SERVER_INTERFACE_H
PayloadAsyncRequest(void *registered_method, ServerInterface *server, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *request)
Definition: server_interface.h:180
virtual ~ServerInterface()
Definition: server_interface.h:59
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:40
Definition: server_interface.h:163
void Shutdown()
Shutdown the server, waiting for all rpc processing to finish.
Definition: server_interface.h:72
bool FinalizeResult(void **tag, bool *status) GRPC_OVERRIDE
std::string string
Definition: config.h:112
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call.h:494
bool FinalizeResult(void **tag, bool *status) GRPC_OVERRIDE
Definition: server_interface.h:178
ServerInterface *const server_
Definition: server_interface.h:140
Definition: service_type.h:63
virtual void ShutdownInternal(gpr_timespec deadline)=0
Definition: server_interface.h:150
Definition: service_type.h:52
RegisteredAsyncRequest(ServerInterface *server, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, void *tag)
Definition: async_generic_service.h:60
Definition: time.h:54
virtual void RunRpc()=0
Process one or more incoming calls.
virtual ~BaseAsyncRequest()
Definition: server_interface.h:135
Definition: rpc_service_method.h:73
void RequestAsyncCall(RpcServiceMethod *method, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:232
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:64
virtual grpc_server * server()=0
Definition: server_credentials.h:49
virtual void PerformOpsOnCall(CallOpSetInterface *ops, Call *call)=0
Definition: call.h:576
const bool delete_on_finalize_
Definition: server_interface.h:145
grpc_metadata_array initial_metadata_array_
Definition: server_interface.h:147
bool FinalizeResult(void **tag, bool *status) GRPC_OVERRIDE
Definition: server_interface.h:191
Definition: server_context.h:90
Models a gRPC server.
Definition: server_interface.h:57
grpc_call * call_
Definition: server_interface.h:146
CompletionQueue *const call_cq_
Definition: server_interface.h:143
virtual bool RegisterService(const grpc::string *host, Service *service)=0
Register a service.
ServerAsyncStreamingInterface *const stream_
Definition: server_interface.h:142
Definition: async_generic_service.h:47
A thin wrapper around grpc_completion_queue (see / src/core/surface/completion_queue.h).
Definition: completion_queue.h:81
#define GRPC_FINAL
Definition: config.h:71
virtual int max_message_size() const =0
void Shutdown(const T &deadline)
Shutdown the server, blocking until all rpc processing finishes.
Definition: server_interface.h:67
BaseAsyncRequest(ServerInterface *server, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, void *tag, bool delete_on_finalize)
virtual void ScheduleCallback()=0
Schedule RunRpc to run in the threadpool.
ServerContext *const context_
Definition: server_interface.h:141
virtual void RegisterAsyncGenericService(AsyncGenericService *service)=0
Register a generic service.
virtual int AddListeningPort(const grpc::string &addr, ServerCredentials *creds)=0
Tries to bind server to the given addr.
void * server_tag() const
Definition: rpc_service_method.h:81
Definition: server_interface.h:206
void RequestAsyncGenericCall(GenericServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:241
void *const tag_
Definition: server_interface.h:144
void IssueRequest(void *registered_method, grpc_byte_buffer **payload, ServerCompletionQueue *notification_cq)
virtual void Wait()=0
Block waiting for all work to complete.
A specific type of completion queue used by the processing of notifications by servers.
Definition: completion_queue.h:194
GenericAsyncRequest(ServerInterface *server, GenericServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, bool delete_on_finalize)
NoPayloadAsyncRequest(void *registered_method, ServerInterface *server, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag)
Definition: server_interface.h:165
Channel and Server implement this to allow them to hook performing ops.
Definition: call_hook.h:43
#define GRPC_OVERRIDE
Definition: config.h:77
Definition: server_interface.h:129
void RequestAsyncCall(RpcServiceMethod *method, ServerContext *context, ServerAsyncStreamingInterface *stream, CompletionQueue *call_cq, ServerCompletionQueue *notification_cq, void *tag, Message *message)
Definition: server_interface.h:221
virtual bool Start(ServerCompletionQueue **cqs, size_t num_cqs)=0
Start the server.
::google::protobuf::Message Message
Definition: config_protobuf.h:60