Commit 0af8c8cc authored by Dominik Charousset's avatar Dominik Charousset

Integrate review feedback

parent 83788cc4
...@@ -76,22 +76,18 @@ behavior client(event_based_actor* self, const std::string& name) { ...@@ -76,22 +76,18 @@ behavior client(event_based_actor* self, const std::string& name) {
class config : public actor_system_config { class config : public actor_system_config {
public: public:
std::string name;
std::vector<std::string> group_locators;
uint16_t port = 0;
bool server_mode = false;
config() { config() {
opt_group{custom_options_, "global"} opt_group{custom_options_, "global"}
.add(name, "name,n", "set name") .add<std::string>("name,n", "set name")
.add(group_locators, "group,g", "join group") .add<std::string>("group,g", "join group")
.add(server_mode, "server,s", "run in server mode") .add<bool>("server,s", "run in server mode")
.add(port, "port,p", "set port (ignored in client mode)"); .add<uint16_t>("port,p", "set port (ignored in client mode)");
} }
}; };
void run_server(actor_system& system, const config& cfg) { void run_server(actor_system& sys) {
auto res = system.middleman().publish_local_groups(cfg.port); auto port = get_or(sys.config(), "port", uint16_t{0});
auto res = sys.middleman().publish_local_groups(port);
if (!res) { if (!res) {
std::cerr << "*** publishing local groups failed: " std::cerr << "*** publishing local groups failed: "
<< to_string(res.error()) << std::endl; << to_string(res.error()) << std::endl;
...@@ -104,8 +100,10 @@ void run_server(actor_system& system, const config& cfg) { ...@@ -104,8 +100,10 @@ void run_server(actor_system& system, const config& cfg) {
std::cout << "... cya" << std::endl; std::cout << "... cya" << std::endl;
} }
void run_client(actor_system& system, const config& cfg) { void run_client(actor_system& sys) {
auto name = cfg.name; std::string name;
if (auto config_name = get_if<std::string>(&sys.config(), "name"))
name = *config_name;
while (name.empty()) { while (name.empty()) {
std::cout << "please enter your name: " << std::flush; std::cout << "please enter your name: " << std::flush;
if (!std::getline(std::cin, name)) { if (!std::getline(std::cin, name)) {
...@@ -113,12 +111,12 @@ void run_client(actor_system& system, const config& cfg) { ...@@ -113,12 +111,12 @@ void run_client(actor_system& system, const config& cfg) {
return; return;
} }
} }
auto client_actor = system.spawn(client, name); auto client_actor = sys.spawn(client, name);
for (auto& locator : cfg.group_locators) { if (auto locator = get_if<std::string>(&sys.config(), "group")) {
if (auto grp = system.groups().get(locator)) { if (auto grp = sys.groups().get(*locator)) {
anon_send(client_actor, join_atom_v, std::move(*grp)); anon_send(client_actor, join_atom_v, std::move(*grp));
} else { } else {
std::cerr << R"(*** failed to parse ")" << locator std::cerr << R"(*** failed to parse ")" << *locator
<< R"(" as group locator: )" << to_string(grp.error()) << R"(" as group locator: )" << to_string(grp.error())
<< std::endl; << std::endl;
} }
...@@ -133,7 +131,7 @@ void run_client(actor_system& system, const config& cfg) { ...@@ -133,7 +131,7 @@ void run_client(actor_system& system, const config& cfg) {
words.clear(); words.clear();
split(words, i->str, is_any_of(" ")); split(words, i->str, is_any_of(" "));
if (words.size() == 3 && words[0] == "/join") { if (words.size() == 3 && words[0] == "/join") {
if (auto grp = system.groups().get(words[1], words[2])) if (auto grp = sys.groups().get(words[1], words[2]))
anon_send(client_actor, join_atom_v, *grp); anon_send(client_actor, join_atom_v, *grp);
else else
std::cerr << "*** failed to join group: " << to_string(grp.error()) std::cerr << "*** failed to join group: " << to_string(grp.error())
...@@ -154,9 +152,9 @@ void run_client(actor_system& system, const config& cfg) { ...@@ -154,9 +152,9 @@ void run_client(actor_system& system, const config& cfg) {
anon_send_exit(client_actor, exit_reason::user_shutdown); anon_send_exit(client_actor, exit_reason::user_shutdown);
} }
void caf_main(actor_system& system, const config& cfg) { void caf_main(actor_system& sys, const config& cfg) {
auto f = cfg.server_mode ? run_server : run_client; auto f = get_or(cfg, "server", false) ? run_server : run_client;
f(system, cfg); f(sys);
} }
CAF_MAIN(id_block::group_chat, io::middleman) CAF_MAIN(id_block::group_chat, io::middleman)
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
namespace caf::detail { namespace caf::detail {
/// Represents a group that runs on a different CAF node. /// Represents a group that runs on a remote CAF node.
class CAF_CORE_EXPORT group_tunnel : public local_group_module::impl { class CAF_CORE_EXPORT group_tunnel : public local_group_module::impl {
public: public:
using super = local_group_module::impl; using super = local_group_module::impl;
......
...@@ -63,7 +63,7 @@ private: ...@@ -63,7 +63,7 @@ private:
return fun(); return fun();
} }
// Removes an instance when connecting it failed. // Stops an instance and removes it from this module.
void drop(const group_tunnel_ptr& instance); void drop(const group_tunnel_ptr& instance);
// Connects an instance when it is still associated to this module. // Connects an instance when it is still associated to this module.
......
...@@ -85,14 +85,14 @@ group_tunnel_ptr remote_group_module::get_impl(actor intermediary, ...@@ -85,14 +85,14 @@ group_tunnel_ptr remote_group_module::get_impl(actor intermediary,
} else { } else {
auto& instances = nodes_[intermediary.node()]; auto& instances = nodes_[intermediary.node()];
if (auto i = instances.find(group_name); i != instances.end()) { if (auto i = instances.find(group_name); i != instances.end()) {
auto result = i->second; auto instance = i->second;
result->connect(std::move(intermediary)); instance->connect(std::move(intermediary));
return result; return instance;
} else { } else {
auto result = make_counted<detail::group_tunnel>( auto instance = make_counted<detail::group_tunnel>(
this, group_name, std::move(intermediary)); this, group_name, std::move(intermediary));
instances.emplace(group_name, result); instances.emplace(group_name, instance);
return result; return instance;
} }
} }
}); });
......
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