Skip to content

Commit

Permalink
Add Echo Server/Client Example
Browse files Browse the repository at this point in the history
Summary:
I got a lot of feedback from my blog post saying there wasn't much examples to learn Wangle so I thought I'd write a few examples. I will start off with the most basic example - an Echo Server/Client.
Closes #21

Reviewed By: djwatson

Differential Revision: D2816631

Pulled By: viswanathgs

fb-gh-sync-id: 24027b37bda5bd7962e3ae2e2fcdcbe9a2ff0961
  • Loading branch information
oh-its-jimjam authored and facebook-github-bot-8 committed Jan 8, 2016
1 parent f35807c commit dfa81a8
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
4 changes: 4 additions & 0 deletions wangle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ endif()
option(BUILD_EXAMPLES "BUILD_EXAMPLES" OFF)

if(BUILD_EXAMPLES)
add_executable(EchoClient example/echo/EchoClient.cpp)
target_link_libraries(EchoClient wangle)
add_executable(EchoServer example/echo/EchoServer.cpp)
target_link_libraries(EchoServer wangle)
add_executable(TelnetClient example/telnet/TelnetClient.cpp)
target_link_libraries(TelnetClient wangle)
add_executable(TelnetServer example/telnet/TelnetServer.cpp)
Expand Down
86 changes: 86 additions & 0 deletions wangle/example/echo/EchoClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2016, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include <gflags/gflags.h>
#include <iostream>

#include <wangle/bootstrap/ClientBootstrap.h>
#include <wangle/channel/AsyncSocketHandler.h>
#include <wangle/channel/EventBaseHandler.h>
#include <wangle/codec/LineBasedFrameDecoder.h>
#include <wangle/codec/StringCodec.h>

using namespace folly;
using namespace wangle;

DEFINE_int32(port, 8080, "echo server port");
DEFINE_string(host, "::1", "echo server address");

typedef Pipeline<folly::IOBufQueue&, std::string> EchoPipeline;

// the handler for receiving messages back from the server
class EchoHandler : public HandlerAdapter<std::string> {
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<EchoPipeline> {
public:
EchoPipeline::Ptr newPipeline(std::shared_ptr<AsyncTransportWrapper> 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) {
google::ParseCommandLineFlags(&argc, &argv, true);

ClientBootstrap<EchoPipeline> client;
client.group(std::make_shared<wangle::IOThreadPoolExecutor>(1));
client.pipelineFactory(std::make_shared<EchoPipelineFactory>());
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;
}
57 changes: 57 additions & 0 deletions wangle/example/echo/EchoServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2016, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include <gflags/gflags.h>

#include <wangle/bootstrap/ServerBootstrap.h>
#include <wangle/channel/AsyncSocketHandler.h>
#include <wangle/codec/LineBasedFrameDecoder.h>
#include <wangle/codec/StringCodec.h>

using namespace folly;
using namespace wangle;

DEFINE_int32(port, 8080, "echo server port");

typedef Pipeline<IOBufQueue&, std::string> EchoPipeline;

// the main logic of our echo server; receives a string and writes it straight
// back
class EchoHandler : public HandlerAdapter<std::string> {
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<EchoPipeline> {
public:
EchoPipeline::Ptr newPipeline(std::shared_ptr<AsyncTransportWrapper> 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) {
google::ParseCommandLineFlags(&argc, &argv, true);

ServerBootstrap<EchoPipeline> server;
server.childPipeline(std::make_shared<EchoPipelineFactory>());
server.bind(FLAGS_port);
server.waitForStop();

return 0;
}

0 comments on commit dfa81a8

Please sign in to comment.