Finagle interfaces
[folly.git] / folly / wangle / service / ClientDispatcher.h
1 /*
2  * Copyright 2015 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17
18 #include <folly/wangle/channel/ChannelHandler.h>
19
20 namespace folly { namespace wangle {
21
22 /**
23  * Dispatch a request, satisfying Promise `p` with the response;
24  * the returned Future is satisfied when the response is received:
25  * only one request is allowed at a time.
26  */
27 template <typename Pipeline, typename Req, typename Resp = Req>
28 class SerialClientDispatcher : public ChannelHandlerAdapter<Req, Resp>
29                              , public Service<Req, Resp> {
30  public:
31
32   typedef typename ChannelHandlerAdapter<Req, Resp>::Context Context;
33
34   void setPipeline(Pipeline* pipeline) {
35     pipeline_ = pipeline;
36     pipeline->addBack(
37       ChannelHandlerPtr<SerialClientDispatcher<Pipeline, Req, Resp>, false>(
38         this));
39     pipeline->finalize();
40   }
41
42   void read(Context* ctx, Req in) override {
43     DCHECK(p_);
44     p_->setValue(std::move(in));
45     p_ = none;
46   }
47
48   virtual Future<Resp> operator()(Req arg) override {
49     CHECK(!p_);
50     DCHECK(pipeline_);
51
52     p_ = Promise<Resp>();
53     auto f = p_->getFuture();
54     pipeline_->write(std::move(arg));
55     return f;
56   }
57
58  private:
59   Pipeline* pipeline_{nullptr};
60   folly::Optional<Promise<Resp>> p_;
61 };
62
63 }} // namespace