2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <folly/wangle/channel/Handler.h>
19 #include <folly/wangle/service/Service.h>
21 namespace folly { namespace wangle {
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.
28 template <typename Pipeline, typename Req, typename Resp = Req>
29 class SerialClientDispatcher : public HandlerAdapter<Req, Resp>
30 , public Service<Req, Resp> {
33 typedef typename HandlerAdapter<Req, Resp>::Context Context;
35 void setPipeline(Pipeline* pipeline) {
37 pipeline->addBack(this);
41 void read(Context* ctx, Req in) override {
43 p_->setValue(std::move(in));
47 virtual Future<Resp> operator()(Req arg) override {
52 auto f = p_->getFuture();
53 pipeline_->write(std::move(arg));
57 virtual Future<void> close() override {
58 return HandlerAdapter<Req, Resp>::close(nullptr);
61 virtual Future<void> close(Context* ctx) override {
62 return HandlerAdapter<Req, Resp>::close(ctx);
65 Pipeline* pipeline_{nullptr};
66 folly::Optional<Promise<Resp>> p_;