Copyright 2014->2015
[folly.git] / folly / wangle / bootstrap / ClientBootstrap.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/ChannelPipeline.h>
19
20 namespace folly {
21
22 /*
23  * A thin wrapper around ChannelPipeline and AsyncSocket to match
24  * ServerBootstrap.  On connect() a new pipeline is created.
25  */
26 template <typename Pipeline>
27 class ClientBootstrap {
28  public:
29   ClientBootstrap() {
30   }
31   ClientBootstrap* bind(int port) {
32     port_ = port;
33     return this;
34   }
35   ClientBootstrap* connect(SocketAddress address) {
36     DCHECK(pipelineFactory_);
37     pipeline_.reset(
38       pipelineFactory_->newPipeline(
39         AsyncSocket::newSocket(EventBaseManager::get()->getEventBase(), address)
40       ));
41     return this;
42   }
43
44   ClientBootstrap* pipelineFactory(
45       std::shared_ptr<PipelineFactory<Pipeline>> factory) {
46     pipelineFactory_ = factory;
47     return this;
48   }
49
50   Pipeline* getPipeline() {
51     return pipeline_.get();
52   }
53
54   virtual ~ClientBootstrap() {}
55
56  protected:
57   std::unique_ptr<Pipeline,
58                   folly::DelayedDestruction::Destructor> pipeline_;
59
60   int port_;
61
62   std::shared_ptr<PipelineFactory<Pipeline>> pipelineFactory_;
63 };
64
65 } // namespace