proxygen
RpcServer.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>
20 #include <wangle/service/Service.h>
29 
31 
32 using namespace folly;
33 using namespace wangle;
34 
35 using thrift::test::Bonk;
36 using thrift::test::Xtruct;
37 
39 
40 DEFINE_int32(port, 8080, "test server port");
41 
42 class RpcService : public Service<Bonk, Xtruct> {
43  public:
44  Future<Xtruct> operator()(Bonk request) override {
45  // Oh no, we got Bonked! Quick, Bonk back
46  printf("Bonk: %s, %i\n", request.message.c_str(), request.type);
47 
48  /* sleep override: ignore lint
49  * useful for testing dispatcher behavior by hand
50  */
51  // Wait for a bit
52  return futures::sleep(std::chrono::seconds(request.type))
53  .thenValue([request](auto&&) {
54  Xtruct response;
55  response.string_thing = "Stop saying " + request.message + "!";
56  response.i32_thing = request.type;
57  return response;
58  });
59  }
60 };
61 
62 class RpcPipelineFactory : public PipelineFactory<SerializePipeline> {
63  public:
65  std::shared_ptr<AsyncTransportWrapper> sock) override {
66  auto pipeline = SerializePipeline::create();
67  pipeline->addBack(AsyncSocketHandler(sock));
68  // ensure we can write from any thread
69  pipeline->addBack(EventBaseHandler());
70  pipeline->addBack(LengthFieldBasedFrameDecoder());
71  pipeline->addBack(LengthFieldPrepender());
72  pipeline->addBack(ServerSerializeHandler());
73  // We could use a serial dispatcher instead easily
74  // pipeline->addBack(SerialServerDispatcher<Bonk>(&service_));
75  // Or a Pipelined Dispatcher
76  // pipeline->addBack(PipelinedServerDispatcher<Bonk>(&service_));
77  pipeline->addBack(MultiplexServerDispatcher<Bonk, Xtruct>(&service_));
78  pipeline->finalize();
79 
80  return pipeline;
81  }
82 
83  private:
85  std::make_shared<CPUThreadPoolExecutor>(10),
86  std::make_shared<RpcService>()};
87 };
88 
89 int main(int argc, char** argv) {
90  folly::Init init(&argc, &argv);
91 
93  server.childPipeline(std::make_shared<RpcPipelineFactory>());
94  server.bind(FLAGS_port);
95  server.waitForStop();
96 
97  return 0;
98 }
void bind(folly::AsyncServerSocket::UniquePtr s)
Future< Unit > sleep(Duration dur, Timekeeper *tk)
Definition: Future.cpp:42
Future< Xtruct > operator()(Bonk request) override
Definition: RpcServer.cpp:44
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
ServerBootstrap * childPipeline(std::shared_ptr< PipelineFactory< Pipeline >> factory)
void init(int *argc, char ***argv, bool removeFlags)
Definition: Init.cpp:34
char ** argv
int main(int argc, char **argv)
Definition: RpcServer.cpp:89
static Ptr create()
Definition: Pipeline.h:174
DEFINE_int32(port, 8080,"test server port")
std::shared_ptr< Pipeline > Ptr
Definition: Pipeline.h:172
SerializePipeline::Ptr newPipeline(std::shared_ptr< AsyncTransportWrapper > sock) override
Definition: RpcServer.cpp:64