Commit 44c32c26 authored by Samir Halilcevic's avatar Samir Halilcevic

Integrate review feedback

parent c2cf530b
// This example shows how to use caf::after // This example shows how to use caf::after
#include "caf/all.hpp" #include "caf/after.hpp"
#include "caf/caf_main.hpp"
#include "caf/event_based_actor.hpp"
#include "caf/stateful_actor.hpp"
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
#include <random> #include <random>
#include <string>
using std::cout; using std::cout;
using std::endl; using std::endl;
...@@ -12,32 +17,31 @@ using std::endl; ...@@ -12,32 +17,31 @@ using std::endl;
using namespace caf; using namespace caf;
// Sends a random number of printable characters to buddy and exits // Sends a random number of printable characters to buddy and exits
void generator(event_based_actor* self, actor buddy) { void generator(event_based_actor* self, actor sink) {
std::random_device rd; std::random_device rd;
std::minstd_rand gen{rd()}; std::minstd_rand gen{rd()};
const auto count = std::uniform_int_distribution<>{20, 100}(gen); const auto count = std::uniform_int_distribution<>{20, 100}(gen);
std::uniform_int_distribution<> dis{33, 126}; std::uniform_int_distribution<> dis{33, 126};
for (auto i = 0; i < count; i++) { for (auto i = 0; i < count; i++) {
self->send(buddy, static_cast<char>(dis(gen))); self->send(sink, static_cast<char>(dis(gen)));
} }
} }
// Collects the incoming characters until either the awaited_size of characters // Collects the incoming characters until no new characters arrive for 500ms.
// is received, or no new characters arrive for 100ms // Prints every 60 characters.
behavior collector(stateful_actor<std::string>* self, size_t awaited_size) { behavior collector(stateful_actor<std::string>* self) {
using namespace std::chrono_literals; using namespace std::chrono_literals;
self->state.reserve(awaited_size);
return { return {
[=](char c) { [self](char c) {
self->state.push_back(c); self->state.push_back(c);
if (self->state.size() == awaited_size) { constexpr auto flush_threshold = 60;
if (self->state.size() == flush_threshold) {
cout << "Received message length: " << self->state.size() << endl cout << "Received message length: " << self->state.size() << endl
<< "Message content: " << self->state << endl; << "Message content: " << self->state << endl;
self->quit(); self->state.clear();
} }
}, },
// trigger if we dont receive a message for 100ms caf::after(500ms) >>
caf::after(100ms) >>
[self]() { [self]() {
cout << "Timeout reached!" << endl; cout << "Timeout reached!" << endl;
if (!self->state.empty()) { if (!self->state.empty()) {
...@@ -50,7 +54,7 @@ behavior collector(stateful_actor<std::string>* self, size_t awaited_size) { ...@@ -50,7 +54,7 @@ behavior collector(stateful_actor<std::string>* self, size_t awaited_size) {
} }
void caf_main(actor_system& system) { void caf_main(actor_system& system) {
auto col = system.spawn(collector, 60); auto col = system.spawn(collector);
system.spawn(generator, col); system.spawn(generator, col);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment