Skip to content

Instantly share code, notes, and snippets.

@sourcedelica
Created October 31, 2016 22:42
Show Gist options
  • Save sourcedelica/c419b4608a68453b1bfb4d8002ad712c to your computer and use it in GitHub Desktop.
Save sourcedelica/c419b4608a68453b1bfb4d8002ad712c to your computer and use it in GitHub Desktop.
#include "caf/all.hpp"
#include "ActorSystem.h"
using namespace caf;
using work_atom = atom_constant<atom("work")>;
using work_ok_atom = atom_constant<atom("work_ok")>;
caf::behavior serverBehavior(event_based_actor *self) {
return {
[=](work_atom, const std::string &work) {
aout(self) << "Work: " << work << std::endl;
}
};
}
caf::behavior clientBehavior(event_based_actor *self, actor server, actor serverProxy) {
self->monitor(serverProxy);
self->send(server, work_atom::value, "to server");
self->send(serverProxy, work_atom::value, "to server proxy");
self->set_down_handler([](down_msg &) {
std::cout << "Server is down" << std::endl;
});
return {
[](work_ok_atom) {}
};
}
void waitForDown(const std::string &desc) {
std::cout << desc << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
}
int main() {
uint16_t ServerPort = 60000;
actor_system_config config, clientConfig;
config.load<io::middleman>();
clientConfig.load<io::middleman>();
actor_system system{config}, clientSystem{clientConfig};
auto server = system.spawn(serverBehavior);
auto serverExp = system.middleman().publish(server, ServerPort, nullptr, true);
if (!serverExp) {
std::cout << "Error publishing server" << std::endl;
}
auto serverProxyExp = clientSystem.middleman().remote_actor("localhost", ServerPort);
if (!serverProxyExp) {
std::cout << "No server" << std::endl;
exit(1);
}
auto serverProxy = serverProxyExp.value();
auto client = system.spawn(clientBehavior, server, serverProxy);
system.middleman().unpublish(server);
waitForDown("after unpublish");
anon_send(actor_cast<actor>(system.middleman().actor_handle()),
down_msg{serverProxy.address(), exit_reason::user_shutdown});
waitForDown("after sending down to middleman actor");
anon_send_exit(serverProxy, exit_reason::user_shutdown);
waitForDown("after send_exit");
std::cout << "Shutting down" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment