Future<Unit> wangle fixup
[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/Handler.h>
19 #include <folly/wangle/service/Service.h>
20
21 namespace folly { namespace wangle {
22
23 /**
24  * Dispatch a request, satisfying Promise `p` with the response;
25  * the returned Future is satisfied when the response is received:
26  * only one request is allowed at a time.
27  */
28 template <typename Pipeline, typename Req, typename Resp = Req>
29 class SerialClientDispatcher : public HandlerAdapter<Req, Resp>
30                              , public Service<Req, Resp> {
31  public:
32   typedef typename HandlerAdapter<Req, Resp>::Context Context;
33
34   ~SerialClientDispatcher() {
35     if (pipeline_) {
36       try {
37         pipeline_->remove(this).finalize();
38       } catch (const std::invalid_argument& e) {
39         // not in pipeline; this is fine
40       }
41     }
42   }
43
44   void setPipeline(Pipeline* pipeline) {
45     try {
46       pipeline->template remove<SerialClientDispatcher>();
47     } catch (const std::invalid_argument& e) {
48       // no existing dispatcher; this is fine
49     }
50     pipeline_ = pipeline;
51     pipeline_->addBack(this);
52     pipeline_->finalize();
53   }
54
55   void read(Context* ctx, Req in) override {
56     DCHECK(p_);
57     p_->setValue(std::move(in));
58     p_ = none;
59   }
60
61   virtual Future<Resp> operator()(Req arg) override {
62     CHECK(!p_);
63     DCHECK(pipeline_);
64
65     p_ = Promise<Resp>();
66     auto f = p_->getFuture();
67     pipeline_->write(std::move(arg));
68     return f;
69   }
70
71   virtual Future<Unit> close() override {
72     return HandlerAdapter<Req, Resp>::close(nullptr);
73   }
74
75   virtual Future<Unit> close(Context* ctx) override {
76     return HandlerAdapter<Req, Resp>::close(ctx);
77   }
78
79   void detachPipeline(Context* ctx) override {
80     pipeline_ = nullptr;
81   }
82
83  private:
84   Pipeline* pipeline_{nullptr};
85   folly::Optional<Promise<Resp>> p_;
86 };
87
88 }} // namespace