diff --git a/examples/wangle/echo/BUILD b/examples/wangle/echo/BUILD new file mode 100644 index 000000000..9557acad1 --- /dev/null +++ b/examples/wangle/echo/BUILD @@ -0,0 +1,25 @@ +licenses(["notice"]) + +cc_binary( + name = "echo_server", + visibility = ["//visibility:public"], + srcs = ["EchoServer.cpp"], + deps = [ + "//third_party/wangle:wangle", + "//third_party/folly:folly", + "//third_party/glog:glog" + ] +) + +cc_binary( + name = "echo_client", + visibility = ["//visibility:public"], + srcs = ["EchoClient.cpp"], + deps = [ + "//third_party/wangle:wangle", + "//third_party/folly:folly", + "//third_party/glog:glog" + ] +) + + diff --git a/examples/wangle/echo/EchoClient.cpp b/examples/wangle/echo/EchoClient.cpp new file mode 100644 index 000000000..e9cafa2a4 --- /dev/null +++ b/examples/wangle/echo/EchoClient.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace folly; +using namespace wangle; + +DEFINE_int32(port, 8080, "echo server port"); +DEFINE_string(host, "::1", "echo server address"); + +typedef Pipeline EchoPipeline; + +// the handler for receiving messages back from the server +class EchoHandler : public HandlerAdapter { + public: + virtual void read(Context* ctx, std::string msg) override { + std::cout << "received back: " << msg; + } + virtual void readException(Context* ctx, exception_wrapper e) override { + std::cout << exceptionStr(e) << std::endl; + close(ctx); + } + virtual void readEOF(Context* ctx) override { + std::cout << "EOF received :(" << std::endl; + close(ctx); + } +}; + +// chains the handlers together to define the response pipeline +class EchoPipelineFactory : public PipelineFactory { + public: + EchoPipeline::Ptr newPipeline(std::shared_ptr sock) { + auto pipeline = EchoPipeline::create(); + pipeline->addBack(AsyncSocketHandler(sock)); + pipeline->addBack( + EventBaseHandler()); // ensure we can write from any thread + pipeline->addBack(LineBasedFrameDecoder(8192, false)); + pipeline->addBack(StringCodec()); + pipeline->addBack(EchoHandler()); + pipeline->finalize(); + return pipeline; + } +}; + +int main(int argc, char** argv) { + gflags::ParseCommandLineFlags(&argc, &argv, true); + + ClientBootstrap client; + client.group(std::make_shared(1)); + client.pipelineFactory(std::make_shared()); + auto pipeline = client.connect(SocketAddress(FLAGS_host, FLAGS_port)).get(); + + try { + while (true) { + std::string line; + std::getline(std::cin, line); + if (line == "") { + break; + } + + pipeline->write(line + "\r\n").get(); + if (line == "bye") { + pipeline->close(); + break; + } + } + } catch (const std::exception& e) { + std::cout << exceptionStr(e) << std::endl; + } + + return 0; +} + diff --git a/examples/wangle/echo/EchoServer.cpp b/examples/wangle/echo/EchoServer.cpp new file mode 100644 index 000000000..4d6157c29 --- /dev/null +++ b/examples/wangle/echo/EchoServer.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include + +using namespace folly; +using namespace wangle; + +DEFINE_int32(port, 8080, "echo server port"); + +typedef Pipeline EchoPipeline; + +// the main logic of our echo server; receives a string and writes it straight +// back +class EchoHandler : public HandlerAdapter { + public: + virtual void read(Context* ctx, std::string msg) override { + std::cout << "handling " << msg << std::endl; + write(ctx, msg + "\r\n"); + } +}; + +// where we define the chain of handlers for each messeage received +class EchoPipelineFactory : public PipelineFactory { + public: + EchoPipeline::Ptr newPipeline(std::shared_ptr sock) { + auto pipeline = EchoPipeline::create(); + pipeline->addBack(AsyncSocketHandler(sock)); + pipeline->addBack(LineBasedFrameDecoder(8192)); + pipeline->addBack(StringCodec()); + pipeline->addBack(EchoHandler()); + pipeline->finalize(); + return pipeline; + } +}; + +int main(int argc, char** argv) { + gflags::ParseCommandLineFlags(&argc, &argv, true); + + ServerBootstrap server; + server.childPipeline(std::make_shared()); + server.bind(FLAGS_port); + server.waitForStop(); + + return 0; +} + diff --git a/examples/wangle/echo/README.md b/examples/wangle/echo/README.md new file mode 100644 index 000000000..f6c2f07b7 --- /dev/null +++ b/examples/wangle/echo/README.md @@ -0,0 +1,13 @@ +# Wangle Examples + +This directory provides examples of using the Wangle Echo Server. + +## Compiling + + Echo Server: + + `bazel build //examples/wangle/echo:echo_server` + + Echo Client: + + `bazel build //examples/wangle/echo:echo_client` diff --git a/third_party/wangle/BUILD b/third_party/wangle/BUILD index ee5046d5a..b0b2be19f 100644 --- a/third_party/wangle/BUILD +++ b/third_party/wangle/BUILD @@ -84,6 +84,23 @@ cc_library( ] ) +cc_library( + name = "codec", + visibility = ["//visibility:public"], + includes = [ + "upstream", + ], + hdrs = glob(["upstream/wangle/codec/*.h"]), + srcs = [ + "upstream/wangle/codec/LineBasedFrameDecoder.cpp", + "upstream/wangle/codec/LengthFieldPrepender.cpp", + "upstream/wangle/codec/LengthFieldBasedFrameDecoder.cpp" + ], + deps = [ + ":channel" + ] +) + # Disabled because dependings on boost/thread # # cc_test( @@ -119,5 +136,6 @@ cc_library( ":channel", ":concurrent", ":ssl", + ":codec" ] )