proxygen
EchoServer.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, 8080, "echo server port");
29 
31 
32 // the main logic of our echo server; receives a string and writes it straight
33 // back
34 class EchoHandler : public HandlerAdapter<std::string> {
35  public:
36  void read(Context* ctx, std::string msg) override {
37  std::cout << "handling " << msg << std::endl;
38  write(ctx, msg + "\r\n");
39  }
40 };
41 
42 // where we define the chain of handlers for each messeage received
43 class EchoPipelineFactory : public PipelineFactory<EchoPipeline> {
44  public:
46  std::shared_ptr<AsyncTransportWrapper> sock) override {
47  auto pipeline = EchoPipeline::create();
48  pipeline->addBack(AsyncSocketHandler(sock));
49  pipeline->addBack(LineBasedFrameDecoder(8192));
50  pipeline->addBack(StringCodec());
51  pipeline->addBack(EchoHandler());
52  pipeline->finalize();
53  return pipeline;
54  }
55 };
56 
57 int main(int argc, char** argv) {
58  folly::Init init(&argc, &argv);
59 
61  server.childPipeline(std::make_shared<EchoPipelineFactory>());
62  server.bind(FLAGS_port);
63  server.waitForStop();
64 
65  return 0;
66 }
Pipeline< IOBufQueue &, std::string > EchoPipeline
Definition: EchoServer.cpp:30
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
void bind(folly::AsyncServerSocket::UniquePtr s)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
DEFINE_int32(http_port, 11000,"Port to listen on with HTTP protocol")
void read(Context *ctx, std::string msg) override
Definition: EchoServer.cpp:36
ServerBootstrap * childPipeline(std::shared_ptr< PipelineFactory< Pipeline >> factory)
EchoPipeline::Ptr newPipeline(std::shared_ptr< AsyncTransportWrapper > sock) override
Definition: EchoServer.cpp:45
void init(int *argc, char ***argv, bool removeFlags)
Definition: Init.cpp:34
char ** argv
const char * string
Definition: Conv.cpp:212
static Ptr create()
Definition: Pipeline.h:174
std::shared_ptr< Pipeline > Ptr
Definition: Pipeline.h:172
int main(int argc, char *argv[])
Definition: EchoServer.cpp:54