-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
f35807c
commit dfa81a8
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |