proxygen
FileServer.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>
25 
26 using namespace folly;
27 using namespace wangle;
28 
29 DEFINE_int32(port, 11219, "test file server port");
30 
32 
33 class FileServerHandler : public HandlerAdapter<std::string> {
34  public:
35  void read(Context* ctx, std::string filename) override {
36  if (filename == "bye") {
37  close(ctx);
38  }
39 
40  int fd = open(filename.c_str(), O_RDONLY);
41  if (fd == -1) {
42  write(ctx, sformat("Error opening {}: {}\r\n",
43  filename,
44  strerror(errno)));
45  return;
46  }
47 
48  struct stat buf;
49  if (fstat(fd, &buf) == -1) {
50  write(ctx, sformat("Could not stat file {}: {}\r\n",
51  filename,
52  strerror(errno)));
53  return;
54  }
55 
56  FileRegion fileRegion(fd, 0, buf.st_size);
57  auto guard = ctx->getPipelineShared();
58  fileRegion.transferTo(ctx->getTransport())
59  .onError([this, guard, ctx, filename](const std::exception& e){
60  write(ctx, sformat("Error sending file {}: {}\r\n",
61  filename,
62  exceptionStr(e)));
63  });
64  }
65 
66  void readException(Context* ctx, exception_wrapper ew) override {
67  write(ctx, sformat("Error: {}\r\n", exceptionStr(ew)))
68  .thenValue([this, ctx](auto&&) { close(ctx); });
69  }
70 
71  void transportActive(Context* ctx) override {
72  SocketAddress localAddress;
73  ctx->getTransport()->getLocalAddress(&localAddress);
74  write(ctx, "Welcome to " + localAddress.describe() + "!\r\n");
75  write(ctx, "Type the name of a file and it will be streamed to you!\r\n");
76  write(ctx, "Type 'bye' to exit.\r\n");
77  }
78 };
79 
80 class FileServerPipelineFactory : public PipelineFactory<FileServerPipeline> {
81  public:
83  std::shared_ptr<AsyncTransportWrapper> sock) override {
84  auto pipeline = FileServerPipeline::create();
85  pipeline->addBack(AsyncSocketHandler(sock));
86  pipeline->addBack(LineBasedFrameDecoder());
87  pipeline->addBack(StringCodec());
88  pipeline->addBack(FileServerHandler());
89  pipeline->finalize();
90 
91  return pipeline;
92  }
93 };
94 
95 int main(int argc, char** argv) {
96  folly::Init init(&argc, &argv);
97 
99  server.childPipeline(std::make_shared<FileServerPipelineFactory>());
100  server.bind(FLAGS_port);
101  server.waitForStop();
102 
103  return 0;
104 }
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
void transportActive(Context *ctx) override
Definition: FileServer.cpp:71
std::shared_ptr< folly::AsyncTransport > getTransport()
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
void bind(folly::AsyncServerSocket::UniquePtr s)
int main(int argc, char **argv)
Definition: FileServer.cpp:95
fbstring exceptionStr(const std::exception &e)
—— 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
DEFINE_int32(port, 11219,"test file server port")
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
const char * string
Definition: Conv.cpp:212
static Ptr create()
Definition: Pipeline.h:174
std::shared_ptr< Pipeline > Ptr
Definition: Pipeline.h:172
Pipeline< IOBufQueue &, std::string > FileServerPipeline
Definition: FileServer.cpp:31
FileServerPipeline::Ptr newPipeline(std::shared_ptr< AsyncTransportWrapper > sock) override
Definition: FileServer.cpp:82
virtual std::shared_ptr< PipelineBase > getPipelineShared()=0
int close(NetworkSocket s)
Definition: NetOps.cpp:90
void readException(Context *ctx, exception_wrapper ew) override
Definition: FileServer.cpp:66
void read(Context *ctx, std::string filename) override
Definition: FileServer.cpp:35