Commit ff082a26 authored by Dominik Charousset's avatar Dominik Charousset

Fix UB and branch for the '/quit' command

parent e4ff83c9
...@@ -45,10 +45,11 @@ behavior client(event_based_actor* self, const std::string& name) { ...@@ -45,10 +45,11 @@ behavior client(event_based_actor* self, const std::string& name) {
} }
}, },
[=](join_atom, const group& what) { [=](join_atom, const group& what) {
for (const auto& g : self->joined_groups()) { auto groups = self->joined_groups();
std::cout << "*** leave " << to_string(g) << std::endl; for (auto&& grp : groups) {
self->send(g, name + " has left the chatroom"); std::cout << "*** leave " << to_string(grp) << std::endl;
self->leave(g); self->send(grp, name + " has left the chatroom");
self->leave(grp);
} }
std::cout << "*** join " << to_string(what) << std::endl; std::cout << "*** join " << to_string(what) << std::endl;
self->join(what); self->join(what);
...@@ -62,20 +63,28 @@ behavior client(event_based_actor* self, const std::string& name) { ...@@ -62,20 +63,28 @@ behavior client(event_based_actor* self, const std::string& name) {
[=](const group_down_msg& g) { [=](const group_down_msg& g) {
std::cout << "*** chatroom offline: " << to_string(g.source) << std::endl; std::cout << "*** chatroom offline: " << to_string(g.source) << std::endl;
}, },
[=](leave_atom) {
auto groups = self->joined_groups();
for (auto&& grp : groups) {
std::cout << "*** leave " << to_string(grp) << std::endl;
self->send(grp, name + " has left the chatroom");
self->leave(grp);
}
},
}; };
} }
class config : public actor_system_config { class config : public actor_system_config {
public: public:
std::string name; std::string name;
std::vector<std::string> group_uris; std::vector<std::string> group_locators;
uint16_t port = 0; uint16_t port = 0;
bool server_mode = false; bool server_mode = false;
config() { config() {
opt_group{custom_options_, "global"} opt_group{custom_options_, "global"}
.add(name, "name,n", "set name") .add(name, "name,n", "set name")
.add(group_uris, "group,g", "join group") .add(group_locators, "group,g", "join group")
.add(server_mode, "server,s", "run in server mode") .add(server_mode, "server,s", "run in server mode")
.add(port, "port,p", "set port (ignored in client mode)"); .add(port, "port,p", "set port (ignored in client mode)");
} }
...@@ -105,13 +114,13 @@ void run_client(actor_system& system, const config& cfg) { ...@@ -105,13 +114,13 @@ void run_client(actor_system& system, const config& cfg) {
} }
} }
auto client_actor = system.spawn(client, name); auto client_actor = system.spawn(client, name);
for (auto& uri : cfg.group_uris) { for (auto& locator : cfg.group_locators) {
if (auto grp = system.groups().get(uri)) { if (auto grp = system.groups().get(locator)) {
std::cout << "*** join " << to_string(grp) << '\n';
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 ")" << uri << R"(" as group URI: )" std::cerr << R"(*** failed to parse ")" << locator
<< to_string(grp.error()) << std::endl; << R"(" as group locator: )" << to_string(grp.error())
<< std::endl;
} }
} }
std::cout << "*** starting client, type '/help' for a list of commands\n"; std::cout << "*** starting client, type '/help' for a list of commands\n";
...@@ -129,7 +138,7 @@ void run_client(actor_system& system, const config& cfg) { ...@@ -129,7 +138,7 @@ void run_client(actor_system& system, const config& cfg) {
else else
std::cerr << "*** failed to join group: " << to_string(grp.error()) std::cerr << "*** failed to join group: " << to_string(grp.error())
<< std::endl; << std::endl;
} else if (words.size() == 1 && words[0] == "quit") { } else if (words.size() == 1 && words[0] == "/quit") {
std::cin.setstate(std::ios_base::eofbit); std::cin.setstate(std::ios_base::eofbit);
} else { } else {
std::cout << "*** available commands:\n" std::cout << "*** available commands:\n"
...@@ -141,7 +150,7 @@ void run_client(actor_system& system, const config& cfg) { ...@@ -141,7 +150,7 @@ void run_client(actor_system& system, const config& cfg) {
anon_send(client_actor, broadcast_atom_v, i->str); anon_send(client_actor, broadcast_atom_v, i->str);
} }
} }
// force actor to quit anon_send(client_actor, leave_atom_v);
anon_send_exit(client_actor, exit_reason::user_shutdown); anon_send_exit(client_actor, exit_reason::user_shutdown);
} }
......
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