Commit ab34ff1b authored by Dominik Charousset's avatar Dominik Charousset

moved behavior definition of ping & pong to factory functions

parent 5e602a8b
...@@ -6,104 +6,76 @@ ...@@ -6,104 +6,76 @@
#include "cppa/sb_actor.hpp" #include "cppa/sb_actor.hpp"
#include "cppa/to_string.hpp" #include "cppa/to_string.hpp"
namespace { size_t s_pongs = 0; }
using std::cout; using std::cout;
using std::endl; using std::endl;
using namespace cppa; using namespace cppa;
size_t pongs() { namespace {
return s_pongs;
}
void ping(size_t num_pings) { size_t s_pongs = 0;
s_pongs = 0;
do_receive ( behavior ping_behavior(size_t num_pings) {
on<atom("pong"), int>() >> [&](int value) { return on(atom("pong"), arg_match) >> [num_pings](int value) {
//cout << to_string(self->last_dequeued()) << endl; //cout << to_string(self->last_dequeued()) << endl;
if (++s_pongs == num_pings) { if (++s_pongs >= num_pings) {
reply(atom("EXIT"), exit_reason::user_defined); reply(atom("EXIT"), exit_reason::user_defined);
self->quit();
} }
else { else {
reply(atom("ping"), value); reply(atom("ping"), value);
} }
}, },
others() >> []() { others() >> [] {
cout << "unexpected message; " cout << "unexpected message; "
<< __FILE__ << " line " << __LINE__ << ": " << __FILE__ << " line " << __LINE__ << ": "
<< to_string(self->last_dequeued()) << endl; << to_string(self->last_dequeued()) << endl;
self->quit(exit_reason::user_defined); self->quit(exit_reason::user_defined);
} };
)
.until(gref(s_pongs) == num_pings);
} }
actor_ptr spawn_event_based_ping(size_t num_pings) { behavior pong_behavior() {
s_pongs = 0; return on<atom("ping"), int>() >> [](int value) {
struct impl : public sb_actor<impl> {
behavior init_state;
impl(size_t num_pings) {
init_state = (
on<atom("pong"), int>() >> [num_pings, this](int value) {
//cout << to_string(self->last_dequeued()) << endl; //cout << to_string(self->last_dequeued()) << endl;
if (++s_pongs >= num_pings) { reply(atom("pong"), value + 1);
reply(atom("EXIT"), exit_reason::user_defined);
quit();
}
else {
reply(atom("ping"), value);
}
}, },
others() >> []() { others() >> []() {
cout << "unexpected message; " cout << "unexpected message; "
<< __FILE__ << " line " << __LINE__ << ": " << __FILE__ << " line " << __LINE__ << ": "
<< to_string(self->last_dequeued()) << endl; << to_string(self->last_dequeued()) << endl;
self->quit(exit_reason::user_defined); self->quit(exit_reason::user_defined);
}
);
}
}; };
return spawn<impl>(num_pings); }
} // namespace <anonymous>
size_t pongs() {
return s_pongs;
}
void ping(size_t num_pings) {
s_pongs = 0;
receive_loop(ping_behavior(num_pings));
}
actor_ptr spawn_event_based_ping(size_t num_pings) {
s_pongs = 0;
return factory::event_based(
[num_pings] {
self->become(ping_behavior(num_pings));
}
).spawn();
} }
void pong(actor_ptr ping_actor) { void pong(actor_ptr ping_actor) {
// kickoff // kickoff
send(ping_actor, atom("pong"), 0); send(ping_actor, atom("pong"), 0);
receive_loop ( receive_loop (pong_behavior());
on<atom("ping"), int>() >> [](int value) {
//cout << to_string(self->last_dequeued()) << endl;
reply(atom("pong"), value + 1);
},
others() >> []() {
cout << "unexpected message; "
<< __FILE__ << " line " << __LINE__ << ": "
<< to_string(self->last_dequeued()) << endl;
self->quit(exit_reason::user_defined);
}
);
} }
actor_ptr spawn_event_based_pong(actor_ptr ping_actor) { actor_ptr spawn_event_based_pong(actor_ptr ping_actor) {
CPPA_REQUIRE(ping_actor.get() != nullptr); CPPA_REQUIRE(ping_actor.get() != nullptr);
struct impl : public sb_actor<impl> { return factory::event_based([=] {
behavior init_state; self->become(pong_behavior());
impl() { send(ping_actor, atom("pong"), 0);
init_state = ( }).spawn();
on<atom("ping"), int>() >> [](int value) {
//cout << to_string(self->last_dequeued()) << endl;
reply(atom("pong"), value + 1);
},
others() >> []() {
cout << "unexpected message; "
<< __FILE__ << " line " << __LINE__ << ": "
<< to_string(self->last_dequeued()) << endl;
self->quit(exit_reason::user_defined);
}
);
}
};
auto pptr = spawn<impl>();
// kickoff
ping_actor->enqueue(pptr.get(), make_any_tuple(atom("pong"), 0));
return pptr;
} }
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