proxygen
EchoClient.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, 8080, "echo server port");
32 DEFINE_string(host, "::1", "echo server address");
33 
35 
36 // the handler for receiving messages back from the server
37 class EchoHandler : public HandlerAdapter<std::string> {
38  public:
39  void read(Context*, std::string msg) override {
40  std::cout << "received back: " << msg;
41  }
42  void readException(Context* ctx, exception_wrapper e) override {
43  std::cout << exceptionStr(e) << std::endl;
44  close(ctx);
45  }
46  void readEOF(Context* ctx) override {
47  std::cout << "EOF received :(" << std::endl;
48  close(ctx);
49  }
50 };
51 
52 // chains the handlers together to define the response pipeline
53 class EchoPipelineFactory : public PipelineFactory<EchoPipeline> {
54  public:
56  std::shared_ptr<AsyncTransportWrapper> sock) override {
57  auto pipeline = EchoPipeline::create();
58  pipeline->addBack(AsyncSocketHandler(sock));
59  pipeline->addBack(
60  EventBaseHandler()); // ensure we can write from any thread
61  pipeline->addBack(LineBasedFrameDecoder(8192, false));
62  pipeline->addBack(StringCodec());
63  pipeline->addBack(EchoHandler());
64  pipeline->finalize();
65  return pipeline;
66  }
67 };
68 
69 int main(int argc, char** argv) {
70  folly::Init init(&argc, &argv);
71 
73  client.group(std::make_shared<folly::IOThreadPoolExecutor>(1));
74  client.pipelineFactory(std::make_shared<EchoPipelineFactory>());
75  auto pipeline = client.connect(SocketAddress(FLAGS_host, FLAGS_port)).get();
76 
77  try {
78  while (true) {
79  std::string line;
80  std::getline(std::cin, line);
81  if (line == "") {
82  break;
83  }
84 
85  pipeline->write(line + "\r\n").get();
86  if (line == "bye") {
87  pipeline->close();
88  break;
89  }
90  }
91  } catch (const std::exception& e) {
92  std::cout << exceptionStr(e) << std::endl;
93  }
94 
95  return 0;
96 }
void read(Context *, std::string msg) override
Definition: EchoClient.cpp:39
DEFINE_int32(port, 8080,"echo server port")
fbstring exceptionStr(const std::exception &e)
void readEOF(Context *ctx) override
Definition: EchoClient.cpp:46
void BENCHFUN() getline(size_t iters, size_t arg)
folly::Future< Pipeline * > connect(const folly::SocketAddress &address, std::chrono::milliseconds timeout=std::chrono::milliseconds(0)) override
void readException(Context *ctx, exception_wrapper e) override
Definition: EchoClient.cpp:42
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
EchoPipeline::Ptr newPipeline(std::shared_ptr< AsyncTransportWrapper > sock) override
Definition: EchoClient.cpp:55
void init(int *argc, char ***argv, bool removeFlags)
Definition: Init.cpp:34
char ** argv
int main(int argc, char **argv)
Definition: EchoClient.cpp:69
BaseClientBootstrap< Pipeline > * pipelineFactory(std::shared_ptr< PipelineFactory< Pipeline >> factory) noexcept
Pipeline< folly::IOBufQueue &, std::string > EchoPipeline
Definition: EchoClient.cpp:34
const char * string
Definition: Conv.cpp:212
static Ptr create()
Definition: Pipeline.h:174
DEFINE_string(host,"::1","echo server address")
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