proxygen
TelnetClient.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 <iostream>
18 
19 #include <gflags/gflags.h>
20 
21 #include <folly/init/Init.h>
27 
28 using namespace folly;
29 using namespace wangle;
30 
31 DEFINE_int32(port, 23, "test telnet server port");
32 DEFINE_string(host, "::1", "test telnet server address");
33 
35 
36 class TelnetHandler : public HandlerAdapter<std::string> {
37  public:
38  void read(Context*, std::string msg) override {
39  std::cout << msg;
40  }
41  void readException(Context* ctx, exception_wrapper e) override {
42  std::cout << exceptionStr(e) << std::endl;
43  close(ctx);
44  }
45  void readEOF(Context* ctx) override {
46  std::cout << "EOF received :(" << std::endl;
47  close(ctx);
48  }
49 };
50 
51 class TelnetPipelineFactory : public PipelineFactory<TelnetPipeline> {
52  public:
54  std::shared_ptr<AsyncTransportWrapper> sock) override {
55  auto pipeline = TelnetPipeline::create();
56  pipeline->addBack(AsyncSocketHandler(sock));
57  pipeline->addBack(EventBaseHandler()); // ensure we can write from any thread
58  pipeline->addBack(LineBasedFrameDecoder(8192, false));
59  pipeline->addBack(StringCodec());
60  pipeline->addBack(TelnetHandler());
61  pipeline->finalize();
62 
63  return pipeline;
64  }
65 };
66 
67 int main(int argc, char** argv) {
68  folly::Init init(&argc, &argv);
69 
71  client.group(std::make_shared<folly::IOThreadPoolExecutor>(1));
72  client.pipelineFactory(std::make_shared<TelnetPipelineFactory>());
73  auto pipeline = client.connect(SocketAddress(FLAGS_host,FLAGS_port)).get();
74 
75  try {
76  while (true) {
77  std::string line;
78  std::getline(std::cin, line);
79  if (line == "") {
80  break;
81  }
82 
83  // Sync write will throw exception if server goes away
84  pipeline->write(line + "\r\n").get();
85  if (line == "bye") {
86  pipeline->close();
87  break;
88  }
89  }
90  } catch(const std::exception& e) {
91  std::cout << exceptionStr(e) << std::endl;
92  }
93 
94  return 0;
95 }
void readEOF(Context *ctx) override
Pipeline< folly::IOBufQueue &, std::string > TelnetPipeline
TelnetPipeline::Ptr newPipeline(std::shared_ptr< AsyncTransportWrapper > sock) override
fbstring exceptionStr(const std::exception &e)
void BENCHFUN() getline(size_t iters, size_t arg)
DEFINE_int32(port, 23,"test telnet server port")
folly::Future< Pipeline * > connect(const folly::SocketAddress &address, std::chrono::milliseconds timeout=std::chrono::milliseconds(0)) override
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void init(int *argc, char ***argv, bool removeFlags)
Definition: Init.cpp:34
char ** argv
BaseClientBootstrap< Pipeline > * pipelineFactory(std::shared_ptr< PipelineFactory< Pipeline >> factory) noexcept
void read(Context *, std::string msg) override
void readException(Context *ctx, exception_wrapper e) override
int main(int argc, char **argv)
DEFINE_string(host,"::1","test telnet server address")
const char * string
Definition: Conv.cpp:212
static Ptr create()
Definition: Pipeline.h:174
std::shared_ptr< Pipeline > Ptr
Definition: Pipeline.h:172
ClientBootstrap * group(std::shared_ptr< folly::IOThreadPoolExecutor > group)
int close(NetworkSocket s)
Definition: NetOps.cpp:90