1 #ifndef PAGESPEED_CONTROLLER_RPC_HANDLER_H_
2 #define PAGESPEED_CONTROLLER_RPC_HANDLER_H_
20 #include "base/logging.h"
21 #include "base/macros.h"
26 namespace net_instaweb {
54 template <
typename AsyncService,
typename RequestT,
typename ResponseT>
56 :
public RefCounted<RpcHandler<AsyncService, RequestT, ResponseT>> {
61 typedef ::grpc::ServerAsyncReaderWriter<ResponseT, RequestT> ReaderWriterT;
63 RpcHandler(AsyncService* service, ::grpc::ServerCompletionQueue* cq);
73 bool Write(
const ResponseT& resp);
77 bool Finish(const ::grpc::Status& status);
88 WAITING_FOR_FIRST_READ,
97 virtual void HandleRequest(
const RequestT& req) = 0;
104 virtual void HandleError() = 0;
107 virtual void HandleWriteDone() { }
114 virtual void InitResponder(AsyncService* service, ::grpc::ServerContext* ctx,
115 ReaderWriterT* responder,
116 ::grpc::ServerCompletionQueue* cq,
122 virtual RpcHandler* CreateHandler(AsyncService* service,
123 ::grpc::ServerCompletionQueue* cq) = 0;
133 void CallHandleError(
RefPtrT r);
137 bool IsClientWriteable()
const;
139 AsyncService* service_;
140 ::grpc::ServerCompletionQueue* cq_;
141 ::grpc::ServerContext ctx_;
142 ReaderWriterT responder_;
149 bool write_outstanding_;
154 template <
typename AsyncService,
typename RequestT,
typename ResponseT>
156 AsyncService* service, ::grpc::ServerCompletionQueue* cq)
161 write_outstanding_(false) {
167 template <
typename AsyncService,
typename RequestT,
typename ResponseT>
172 InitResponder(service_, &ctx_, &responder_, cq_,
174 &RpcHandler::CallHandleError,
RefPtrT(
this)));
177 template <
typename AsyncService,
typename RequestT,
typename ResponseT>
179 CreateHandler(service_, cq_)->
Start();
181 if (state_ != FINISHED) {
182 state_ = WAITING_FOR_FIRST_READ;
199 template <
typename AsyncService,
typename RequestT,
typename ResponseT>
200 void RpcHandler<AsyncService, RequestT, ResponseT>::AttemptRead(RefPtrT ref) {
201 if (state_ != WAITING_FOR_FIRST_READ) {
202 DCHECK_EQ(state_, RUNNING);
205 if (state_ != FINISHED) {
206 responder_.Read(&request_,
MakeFunction(
this, &RpcHandler::ReadDone,
207 &RpcHandler::CallHandleError, ref));
211 template <
typename AsyncService,
typename RequestT,
typename ResponseT>
212 void RpcHandler<AsyncService, RequestT, ResponseT>::ReadDone(RefPtrT ref) {
213 if (state_ == WAITING_FOR_FIRST_READ) {
217 HandleRequest(request_);
220 if (state_ != FINISHED) {
225 template <
typename AsyncService,
typename RequestT,
typename ResponseT>
227 const ::grpc::Status& status) {
228 if (state_ == FINISHED) {
231 if (IsClientWriteable()) {
234 &RpcHandler::CallHandleError,
RefPtrT(
this)));
240 template <
typename AsyncService,
typename RequestT,
typename ResponseT>
242 const ResponseT& response) {
243 if (IsClientWriteable() && !write_outstanding_) {
244 write_outstanding_ =
true;
245 responder_.Write(response,
247 &RpcHandler::CallHandleError,
RefPtrT(
this)));
254 template <
typename AsyncService,
typename RequestT,
typename ResponseT>
256 write_outstanding_ =
false;
260 template <
typename AsyncService,
typename RequestT,
typename ResponseT>
261 void RpcHandler<AsyncService, RequestT, ResponseT>::FinishDone(RefPtrT ref) {
262 DCHECK_EQ(state_, FINISHED);
266 template <
typename AsyncService,
typename RequestT,
typename ResponseT>
267 void RpcHandler<AsyncService, RequestT, ResponseT>::CallHandleError(
272 if (state_ == RUNNING) {
279 template <
typename AsyncService,
typename RequestT,
typename ResponseT>
280 bool RpcHandler<AsyncService, RequestT, ResponseT>::IsClientWriteable()
const {
281 return state_ == WAITING_FOR_FIRST_READ || state_ == RUNNING;
Function * MakeFunction(C *object, void(C::*run)())
Makes a Function* that calls a 0-arg class method.
Definition: function.h:291
RpcHandler(AsyncService *service,::grpc::ServerCompletionQueue *cq)
Definition: rpc_handler.h:155
void Start()
Definition: rpc_handler.h:168
Definition: ref_counted_ptr.h:41
Definition: rpc_handler.h:55
bool Finish(const ::grpc::Status &status)
Definition: rpc_handler.h:226
Definition: ref_counted_ptr.h:69
bool Write(const ResponseT &resp)
Definition: rpc_handler.h:241