Commit 52110a9b authored by Hauke Goldhammer's avatar Hauke Goldhammer

Port protobuf example to CAF 0.18 message types

parent 2e9ba05d
...@@ -17,15 +17,18 @@ CAF_PUSH_WARNINGS ...@@ -17,15 +17,18 @@ CAF_PUSH_WARNINGS
#include "pingpong.pb.h" #include "pingpong.pb.h"
CAF_POP_WARNINGS CAF_POP_WARNINGS
CAF_BEGIN_TYPE_ID_BLOCK(protobuf_example, first_custom_type_id)
CAF_ADD_ATOM(protobuf_example, kickoff_atom)
CAF_END_TYPE_ID_BLOCK(protobuf_example)
namespace { namespace {
using namespace std; using namespace std;
using namespace caf; using namespace caf;
using namespace caf::io; using namespace caf::io;
using ping_atom = atom_constant<atom("ping")>;
using pong_atom = atom_constant<atom("pong")>;
using kickoff_atom = atom_constant<atom("kickoff")>;
// utility function to print an exit message with custom name // utility function to print an exit message with custom name
void print_on_exit(scheduled_actor* self, const std::string& name) { void print_on_exit(scheduled_actor* self, const std::string& name) {
...@@ -40,26 +43,20 @@ struct ping_state { ...@@ -40,26 +43,20 @@ struct ping_state {
behavior ping(stateful_actor<ping_state>* self, size_t num_pings) { behavior ping(stateful_actor<ping_state>* self, size_t num_pings) {
print_on_exit(self, "ping"); print_on_exit(self, "ping");
return { return {[=](kickoff_atom, const actor& pong) {
[=](kickoff_atom, const actor& pong) { self->send(pong, ping_atom_v, 1);
self->send(pong, ping_atom::value, 1); self->become([=](pong_atom, int value) -> message {
self->become (
[=](pong_atom, int value) -> message {
if (++(self->state.count) >= num_pings) if (++(self->state.count) >= num_pings)
self->quit(); self->quit();
return make_message(ping_atom::value, value + 1); return make_message(ping_atom_v, value + 1);
} });
); }};
}
};
} }
behavior pong(event_based_actor* self) { behavior pong(event_based_actor* self) {
print_on_exit(self, "pong"); print_on_exit(self, "pong");
return { return {
[=](ping_atom, int value) { [=](ping_atom, int value) { return make_message(pong_atom_v, value); },
return make_message(pong_atom::value, value);
}
}; };
} }
...@@ -105,10 +102,10 @@ void protobuf_io(broker* self, connection_handle hdl, const actor& buddy) { ...@@ -105,10 +102,10 @@ void protobuf_io(broker* self, connection_handle hdl, const actor& buddy) {
org::libcppa::PingOrPong p; org::libcppa::PingOrPong p;
p.ParseFromArray(msg.buf.data(), static_cast<int>(msg.buf.size())); p.ParseFromArray(msg.buf.data(), static_cast<int>(msg.buf.size()));
if (p.has_ping()) { if (p.has_ping()) {
self->send(buddy, ping_atom::value, p.ping().id()); self->send(buddy, ping_atom_v, p.ping().id());
} }
else if (p.has_pong()) { else if (p.has_pong()) {
self->send(buddy, pong_atom::value, p.pong().id()); self->send(buddy, pong_atom_v, p.pong().id());
} }
else { else {
self->quit(exit_reason::user_shutdown); self->quit(exit_reason::user_shutdown);
...@@ -187,7 +184,7 @@ void run_client(actor_system& system, const config& cfg) { ...@@ -187,7 +184,7 @@ void run_client(actor_system& system, const config& cfg) {
<< to_string(io_actor.error()) << endl; << to_string(io_actor.error()) << endl;
return; return;
} }
send_as(*io_actor, ping_actor, kickoff_atom::value, *io_actor); send_as(*io_actor, ping_actor, kickoff_atom_v, *io_actor);
} }
void caf_main(actor_system& system, const config& cfg) { void caf_main(actor_system& system, const config& cfg) {
...@@ -197,4 +194,4 @@ void caf_main(actor_system& system, const config& cfg) { ...@@ -197,4 +194,4 @@ void caf_main(actor_system& system, const config& cfg) {
} // namespace } // namespace
CAF_MAIN(io::middleman) CAF_MAIN(id_block::protobuf_example, io::middleman)
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