8ee8fad9a4f0b8f43532df8a217730c90fc71aa9
[folly.git] / folly / wangle / bootstrap / ClientBootstrap.h
1 /*
2  * Copyright 2014 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     pipeline_.reset(
37       newPipeline(
38         AsyncSocket::newSocket(EventBaseManager::get()->getEventBase(), address)
39       ));
40     return this;
41   }
42
43   virtual ~ClientBootstrap() {}
44
45  protected:
46   std::unique_ptr<Pipeline,
47                   folly::DelayedDestruction::Destructor> pipeline_;
48
49   int port_;
50
51   virtual Pipeline* newPipeline(std::shared_ptr<AsyncSocket> socket) = 0;
52 };
53
54 } // namespace