-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvs21.cpp
60 lines (52 loc) · 1.59 KB
/
vs21.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <stdexcept>
#include <string>
#include "caf/all.hpp"
#include "caf/io/all.hpp"
using namespace caf;
behavior unreliable_worker(event_based_actor* self) {
return {
[=](add_atom, int x, int y) -> result<int> {
if (x % 2 == 1 || y % 2 == 1) {
self->quit(sec::invalid_argument);
return make_error(sec::invalid_argument, "I don't do odd numbers!");
}
return x + y;
},
};
}
struct state {
actor worker;
};
behavior adder(stateful_actor<state>* self) {
self->state.worker = self->spawn(unreliable_worker);
self->link_to(self->state.worker);
self->set_exit_handler([=](const exit_msg& dm) {
if (dm.source == self->state.worker) {
std::cout << "<<<restart failed worker>>>\n";
// <linked> is a shortcut for calling link_to() afterwards
self->state.worker = self->spawn<linked>(unreliable_worker);
}
});
return {
[=](add_atom a, int x, int y) {
return self->delegate(self->state.worker, a, x, y);
},
};
}
std::ostream& operator<<(std::ostream& out, const expected<message>& x) {
return out << to_string(x);
}
void caf_main(actor_system& sys) {
auto worker = sys.spawn(adder);
scoped_actor self{sys};
for (auto msg :
{make_message(add_atom_v, 10, 20), make_message(add_atom_v, 11, 20),
make_message(add_atom_v, 2, 4)}) {
std::cout << to_string(msg) << " = ";
self->request(worker, infinite, msg)
.receive([&](int result) { std::cout << result << '\n'; },
[&](const error& err) { std::cout << to_string(err) << '\n'; });
}
}
CAF_MAIN(io::middleman)