proxygen
TelnetServer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present 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 
17 #include <gflags/gflags.h>
18 
19 #include <folly/init/Init.h>
24 
25 using namespace folly;
26 using namespace wangle;
27 
28 DEFINE_int32(port, 23, "test telnet server port");
29 
31 
32 class TelnetHandler : public HandlerAdapter<std::string> {
33  public:
34  void read(Context* ctx, std::string msg) override {
35  if (msg.empty()) {
36  write(ctx, "Please type something.\r\n");
37  } else if (msg == "bye") {
38  write(ctx, "Have a fabulous day!\r\n").thenValue([ctx, this](auto&&) {
39  close(ctx);
40  });
41  } else {
42  write(ctx, "Did you say '" + msg + "'?\r\n");
43  }
44  }
45 
46  void transportActive(Context* ctx) override {
47  auto sock = ctx->getTransport();
48  SocketAddress localAddress;
49  sock->getLocalAddress(&localAddress);
50  write(ctx, "Welcome to " + localAddress.describe() + "!\r\n");
51  write(ctx, "Type 'bye' to disconnect.\r\n");
52  }
53 };
54 
55 class TelnetPipelineFactory : public PipelineFactory<TelnetPipeline> {
56  public:
58  std::shared_ptr<AsyncTransportWrapper> sock) override {
59  auto pipeline = TelnetPipeline::create();
60  pipeline->addBack(AsyncSocketHandler(sock));
61  pipeline->addBack(LineBasedFrameDecoder(8192));
62  pipeline->addBack(StringCodec());
63  pipeline->addBack(TelnetHandler());
64  pipeline->finalize();
65 
66  return pipeline;
67  }
68 };
69 
70 int main(int argc, char** argv) {
71  folly::Init init(&argc, &argv);
72 
74  server.childPipeline(std::make_shared<TelnetPipelineFactory>());
75  server.bind(FLAGS_port);
76  server.waitForStop();
77 
78  return 0;
79 }
int main(int argc, char **argv)
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
Pipeline< IOBufQueue &, std::string > TelnetPipeline
std::shared_ptr< folly::AsyncTransport > getTransport()
void bind(folly::AsyncServerSocket::UniquePtr s)
TelnetPipeline::Ptr newPipeline(std::shared_ptr< AsyncTransportWrapper > sock) override
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::string describe() const
ServerBootstrap * childPipeline(std::shared_ptr< PipelineFactory< Pipeline >> factory)
void init(int *argc, char ***argv, bool removeFlags)
Definition: Init.cpp:34
char ** argv
void transportActive(Context *ctx) override
const char * string
Definition: Conv.cpp:212
static Ptr create()
Definition: Pipeline.h:174
std::shared_ptr< Pipeline > Ptr
Definition: Pipeline.h:172
DEFINE_int32(port, 23,"test telnet server port")
void read(Context *ctx, std::string msg) override
int close(NetworkSocket s)
Definition: NetOps.cpp:90