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

Integrate review feedback

parent c2cf530b
// 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 <iostream>
#include <random>
#include <string>
using std::cout;
using std::endl;
......@@ -12,32 +17,31 @@ using std::endl;
using namespace caf;
// 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::minstd_rand gen{rd()};
const auto count = std::uniform_int_distribution<>{20, 100}(gen);
std::uniform_int_distribution<> dis{33, 126};
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
// is received, or no new characters arrive for 100ms
behavior collector(stateful_actor<std::string>* self, size_t awaited_size) {
// Collects the incoming characters until no new characters arrive for 500ms.
// Prints every 60 characters.
behavior collector(stateful_actor<std::string>* self) {
using namespace std::chrono_literals;
self->state.reserve(awaited_size);
return {
[=](char c) {
[self](char 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
<< "Message content: " << self->state << endl;
self->quit();
self->state.clear();
}
},
// trigger if we dont receive a message for 100ms
caf::after(100ms) >>
caf::after(500ms) >>
[self]() {
cout << "Timeout reached!" << endl;
if (!self->state.empty()) {
......@@ -50,7 +54,7 @@ behavior collector(stateful_actor<std::string>* self, size_t awaited_size) {
}
void caf_main(actor_system& system) {
auto col = system.spawn(collector, 60);
auto col = system.spawn(collector);
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