Unverified Commit 0b7ecad7 authored by Dominik Charousset's avatar Dominik Charousset Committed by GitHub

Merge pull request #1517

Add example for caf::after
parents 35d6cdce b39e7039
......@@ -24,6 +24,7 @@ add_core_example(. hello_world)
add_core_example(config read-json)
# basic message passing primitives
add_core_example(message_passing after)
add_core_example(message_passing calculator)
add_core_example(message_passing cell)
add_core_example(message_passing dancing_kirby)
......
// This example shows how to use caf::after.
#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;
using namespace caf;
// Sends a random number of printable characters to `sink` and then quits.
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(sink, static_cast<char>(dis(gen)));
}
}
// 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;
return {
[self](char c) {
self->state.push_back(c);
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->state.clear();
}
},
caf::after(500ms) >>
[self]() {
cout << "Timeout reached!" << endl;
if (!self->state.empty()) {
cout << "Received message length: " << self->state.size() << endl
<< "Message content: " << self->state << endl;
}
self->quit();
},
};
}
void caf_main(actor_system& system) {
auto col = system.spawn(collector);
system.spawn(generator, col);
}
CAF_MAIN()
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