Commit 293288a5 authored by Hauke Goldhammer's avatar Hauke Goldhammer

Update qt example to new remote group api

parent d3480579
...@@ -27,15 +27,17 @@ ChatWidget::~ChatWidget() { ...@@ -27,15 +27,17 @@ ChatWidget::~ChatWidget() {
void ChatWidget::init(actor_system& system) { void ChatWidget::init(actor_system& system) {
super::init(system); super::init(system);
set_message_handler ([=](actor_companion* self) -> message_handler { set_message_handler([=](actor_companion* self) -> message_handler {
return { return {
[=](join_atom, const group& what) { [=](join_atom, const group& what) {
if (chatroom_) { auto groups = self->joined_groups();
self->send(chatroom_, name_ + " has left the chatroom"); for (auto&& grp : groups) {
self->leave(chatroom_); print(("*** leave " + to_string(grp)).c_str());
self->send(grp, name_ + " has left the chatroom");
self->leave(grp);
} }
print(("*** join " + to_string(what)).c_str());
self->join(what); self->join(what);
print(("*** joined " + to_string(what)).c_str());
chatroom_ = what; chatroom_ = what;
self->send(what, name_ + " has entered the chatroom"); self->send(what, name_ + " has entered the chatroom");
}, },
...@@ -44,9 +46,7 @@ void ChatWidget::init(actor_system& system) { ...@@ -44,9 +46,7 @@ void ChatWidget::init(actor_system& system) {
name_ = std::move(name); name_ = std::move(name);
print("*** changed name to " + QString::fromUtf8(name_.c_str())); print("*** changed name to " + QString::fromUtf8(name_.c_str()));
}, },
[=](quit_atom) { [=](quit_atom) { quit_and_close(); },
quit_and_close();
},
[=](const string& txt) { [=](const string& txt) {
// don't print own messages // don't print own messages
if (self != self->current_sender()) if (self != self->current_sender())
...@@ -55,7 +55,15 @@ void ChatWidget::init(actor_system& system) { ...@@ -55,7 +55,15 @@ void ChatWidget::init(actor_system& system) {
[=](const group_down_msg& gdm) { [=](const group_down_msg& gdm) {
print("*** chatroom offline: " print("*** chatroom offline: "
+ QString::fromUtf8(to_string(gdm.source).c_str())); + QString::fromUtf8(to_string(gdm.source).c_str()));
} },
[=](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);
}
},
}; };
}); });
} }
...@@ -68,7 +76,7 @@ void ChatWidget::sendChatMessage() { ...@@ -68,7 +76,7 @@ void ChatWidget::sendChatMessage() {
if (line.startsWith('/')) { if (line.startsWith('/')) {
vector<string> words; vector<string> words;
split(words, line.midRef(1).toUtf8().constData(), is_any_of(" ")); split(words, line.midRef(1).toUtf8().constData(), is_any_of(" "));
if (words.size() > 1) if (words.size() > 1) {
if (words.front() == "join" && words.size() == 3) { if (words.front() == "join" && words.size() == 3) {
auto x = system().groups().get(words[1], words[2]); auto x = system().groups().get(words[1], words[2]);
if (!x) if (!x)
...@@ -78,12 +86,13 @@ void ChatWidget::sendChatMessage() { ...@@ -78,12 +86,13 @@ void ChatWidget::sendChatMessage() {
self()->send(self(), join_atom_v, std::move(*x)); self()->send(self(), join_atom_v, std::move(*x));
} else if (words.front() == "setName" && words.size() == 2) } else if (words.front() == "setName" && words.size() == 2)
send_as(as_actor(), as_actor(), set_name_atom_v, std::move(words[1])); send_as(as_actor(), as_actor(), set_name_atom_v, std::move(words[1]));
} else { } else {
print("*** list of commands:\n" print("*** list of commands:\n"
"/join <module> <group id>\n" "/join <module> <group id>\n"
"/setName <new name>\n"); "/setName <new name>\n");
return; return;
} }
}
if (name_.empty()) { if (name_.empty()) {
print("*** please set a name before sending messages"); print("*** please set a name before sending messages");
return; return;
......
...@@ -33,13 +33,10 @@ using namespace caf; ...@@ -33,13 +33,10 @@ using namespace caf;
class config : public actor_system_config { class config : public actor_system_config {
public: public:
std::string name;
std::string group_id;
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_id, "group,g", "join group (format: <module>:<id>)"); .add<std::string>("group,g", "join group");
} }
}; };
...@@ -52,25 +49,26 @@ int main(int argc, char** argv) { ...@@ -52,25 +49,26 @@ int main(int argc, char** argv) {
if (cfg.cli_helptext_printed) if (cfg.cli_helptext_printed)
return 0; return 0;
cfg.load<io::middleman>(); cfg.load<io::middleman>();
actor_system system{cfg}; actor_system sys{cfg};
auto name = cfg.name; std::string name;
if (auto config_name = get_if<std::string>(&cfg, "name"))
name = *config_name;
while (name.empty()) {
std::cout << "please enter your name: " << std::flush;
if (!std::getline(std::cin, name)) {
std::cerr << "*** no name given... terminating" << std::endl;
return EXIT_FAILURE;
}
}
group grp; group grp;
// evaluate group parameters // evaluate group parameters
if (! cfg.group_id.empty()) { if (auto locator = get_if<std::string>(&cfg, "group")) {
auto p = cfg.group_id.find(':'); if (auto grp_p = sys.groups().get(*locator)) {
if (p == std::string::npos) { grp = std::move(*grp_p);
cerr << "*** error parsing argument " << cfg.group_id
<< ", expected format: <module_name>:<group_id>";
} else { } else {
auto module = cfg.group_id.substr(0, p); std::cerr << R"(*** failed to parse ")" << *locator
auto group_uri = cfg.group_id.substr(p + 1); << R"(" as group locator: )" << to_string(grp_p.error())
auto g = system.groups().get(module, group_uri); << std::endl;
if (! g) {
cerr << "*** unable to get group " << group_uri << " from module "
<< module << ": " << to_string(g.error()) << endl;
return -1;
}
grp = std::move(*g);
} }
} }
QApplication app{argc, argv}; QApplication app{argc, argv};
...@@ -78,7 +76,7 @@ int main(int argc, char** argv) { ...@@ -78,7 +76,7 @@ int main(int argc, char** argv) {
QMainWindow mw; QMainWindow mw;
Ui::ChatWindow helper; Ui::ChatWindow helper;
helper.setupUi(&mw); helper.setupUi(&mw);
helper.chatwidget->init(system); helper.chatwidget->init(sys);
auto client = helper.chatwidget->as_actor(); auto client = helper.chatwidget->as_actor();
if (! name.empty()) if (! name.empty())
send_as(client, client, set_name_atom_v, move(name)); send_as(client, client, set_name_atom_v, move(name));
......
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