telnet server
[folly.git] / folly / wangle / bootstrap / ServerBootstrap-inl.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/acceptor/Acceptor.h>
19 #include <folly/wangle/bootstrap/ServerSocketFactory.h>
20 #include <folly/io/async/EventBaseManager.h>
21 #include <folly/wangle/concurrent/IOThreadPoolExecutor.h>
22 #include <folly/wangle/acceptor/ManagedConnection.h>
23 #include <folly/wangle/channel/Pipeline.h>
24 #include <folly/wangle/channel/Handler.h>
25
26 namespace folly {
27
28 template <typename Pipeline>
29 class ServerAcceptor
30     : public Acceptor
31     , public folly::wangle::InboundHandler<void*> {
32   typedef std::unique_ptr<Pipeline,
33                           folly::DelayedDestruction::Destructor> PipelinePtr;
34
35   class ServerConnection : public wangle::ManagedConnection,
36                            public wangle::PipelineManager {
37    public:
38     explicit ServerConnection(PipelinePtr pipeline)
39         : pipeline_(std::move(pipeline)) {
40       pipeline_->setPipelineManager(this);
41     }
42
43     ~ServerConnection() {}
44
45     void timeoutExpired() noexcept override {
46     }
47
48     void describe(std::ostream& os) const override {}
49     bool isBusy() const override {
50       return false;
51     }
52     void notifyPendingShutdown() override {}
53     void closeWhenIdle() override {}
54     void dropConnection() override {
55       delete this;
56     }
57     void dumpConnectionState(uint8_t loglevel) override {}
58
59     void deletePipeline(wangle::PipelineBase* p) override {
60       CHECK(p == pipeline_.get());
61       delete this;
62     }
63
64    private:
65     PipelinePtr pipeline_;
66   };
67
68  public:
69   explicit ServerAcceptor(
70         std::shared_ptr<PipelineFactory<Pipeline>> pipelineFactory,
71         std::shared_ptr<folly::wangle::Pipeline<void*>> acceptorPipeline,
72         EventBase* base)
73       : Acceptor(ServerSocketConfig())
74       , base_(base)
75       , childPipelineFactory_(pipelineFactory)
76       , acceptorPipeline_(acceptorPipeline) {
77     Acceptor::init(nullptr, base_);
78     CHECK(acceptorPipeline_);
79
80     acceptorPipeline_->addBack(this);
81     acceptorPipeline_->finalize();
82   }
83
84   void read(Context* ctx, void* conn) {
85     AsyncSocket::UniquePtr transport((AsyncSocket*)conn);
86       std::unique_ptr<Pipeline,
87                        folly::DelayedDestruction::Destructor>
88       pipeline(childPipelineFactory_->newPipeline(
89         std::shared_ptr<AsyncSocket>(
90           transport.release(),
91           folly::DelayedDestruction::Destructor())));
92     auto connection = new ServerConnection(std::move(pipeline));
93     Acceptor::addConnection(connection);
94   }
95
96   /* See Acceptor::onNewConnection for details */
97   void onNewConnection(
98     AsyncSocket::UniquePtr transport, const SocketAddress* address,
99     const std::string& nextProtocolName, const TransportInfo& tinfo) {
100     acceptorPipeline_->read(transport.release());
101   }
102
103   // UDP thunk
104   void onDataAvailable(std::shared_ptr<AsyncUDPSocket> socket,
105                        const folly::SocketAddress& addr,
106                        std::unique_ptr<folly::IOBuf> buf,
107                        bool truncated) noexcept {
108     acceptorPipeline_->read(buf.release());
109   }
110
111  private:
112   EventBase* base_;
113
114   std::shared_ptr<PipelineFactory<Pipeline>> childPipelineFactory_;
115   std::shared_ptr<folly::wangle::Pipeline<void*>> acceptorPipeline_;
116 };
117
118 template <typename Pipeline>
119 class ServerAcceptorFactory : public AcceptorFactory {
120  public:
121   explicit ServerAcceptorFactory(
122     std::shared_ptr<PipelineFactory<Pipeline>> factory,
123     std::shared_ptr<PipelineFactory<folly::wangle::Pipeline<void*>>> pipeline)
124     : factory_(factory)
125     , pipeline_(pipeline) {}
126
127   std::shared_ptr<Acceptor> newAcceptor(EventBase* base) {
128     std::shared_ptr<folly::wangle::Pipeline<void*>> pipeline(
129         pipeline_->newPipeline(nullptr));
130     return std::make_shared<ServerAcceptor<Pipeline>>(factory_, pipeline, base);
131   }
132  private:
133   std::shared_ptr<PipelineFactory<Pipeline>> factory_;
134   std::shared_ptr<PipelineFactory<
135     folly::wangle::Pipeline<void*>>> pipeline_;
136 };
137
138 class ServerWorkerPool : public folly::wangle::ThreadPoolExecutor::Observer {
139  public:
140   explicit ServerWorkerPool(
141     std::shared_ptr<AcceptorFactory> acceptorFactory,
142     folly::wangle::IOThreadPoolExecutor* exec,
143     std::shared_ptr<std::vector<std::shared_ptr<folly::AsyncSocketBase>>> sockets,
144     std::shared_ptr<ServerSocketFactory> socketFactory)
145       : acceptorFactory_(acceptorFactory)
146       , exec_(exec)
147       , sockets_(sockets)
148       , socketFactory_(socketFactory) {
149     CHECK(exec);
150   }
151
152   template <typename F>
153   void forEachWorker(F&& f) const;
154
155   void threadStarted(
156     folly::wangle::ThreadPoolExecutor::ThreadHandle*);
157   void threadStopped(
158     folly::wangle::ThreadPoolExecutor::ThreadHandle*);
159   void threadPreviouslyStarted(
160       folly::wangle::ThreadPoolExecutor::ThreadHandle* thread) {
161     threadStarted(thread);
162   }
163   void threadNotYetStopped(
164       folly::wangle::ThreadPoolExecutor::ThreadHandle* thread) {
165     threadStopped(thread);
166   }
167
168  private:
169   std::map<folly::wangle::ThreadPoolExecutor::ThreadHandle*,
170            std::shared_ptr<Acceptor>> workers_;
171   std::shared_ptr<AcceptorFactory> acceptorFactory_;
172   folly::wangle::IOThreadPoolExecutor* exec_{nullptr};
173   std::shared_ptr<std::vector<std::shared_ptr<folly::AsyncSocketBase>>> sockets_;
174   std::shared_ptr<ServerSocketFactory> socketFactory_;
175 };
176
177 template <typename F>
178 void ServerWorkerPool::forEachWorker(F&& f) const {
179   for (const auto& kv : workers_) {
180     f(kv.second.get());
181   }
182 }
183
184 class DefaultAcceptPipelineFactory
185     : public PipelineFactory<wangle::Pipeline<void*>> {
186   typedef wangle::Pipeline<void*> AcceptPipeline;
187
188  public:
189   std::unique_ptr<AcceptPipeline, folly::DelayedDestruction::Destructor>
190     newPipeline(std::shared_ptr<AsyncSocket>) {
191
192     return std::unique_ptr<AcceptPipeline, folly::DelayedDestruction::Destructor>
193       (new AcceptPipeline);
194   }
195 };
196
197 } // namespace