Skip to content

Commit

Permalink
add ablity to list all users in room
Browse files Browse the repository at this point in the history
  • Loading branch information
marty1885 committed Jul 23, 2023
1 parent 80843c0 commit e1aa368
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
15 changes: 14 additions & 1 deletion examples/messenger/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,21 @@ Task<> service(const GNUNET_CONFIGURATION_Handle* cfg)
shutdown();
co_return;
}
else if(line == "/list") {
std::cout << "Members of room " << room_str << ":" << std::endl;
auto members = room->members();
for(auto& contact : members) {
std::string disp_name = contact.name();
std::string key = "<unknown>";
if(contact.key())
key = to_string(*contact.key());
if(disp_name.empty())
disp_name = "<unknown>";
std::cout << "* " << disp_name << " (" << key << ")" << std::endl;
}
}
// don't send empty messages
if(!line.empty())
else if(!line.empty())
room->sendMessage(line);
}
}
Expand Down
24 changes: 24 additions & 0 deletions gnunetpp/gnunetpp-messenger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,30 @@ void Room::sendPrivateMessage(const UserSendibleMessage &value, Contact contact)
GNUNET_MESSENGER_send_message(room, &msg, contact.contact);
}

int Room::forEachMember(std::function<void(const Contact &)> cb)
{
auto ptr = new std::function<void(const Contact &)>(cb);
// IMPORTANT: this method is synchronous, so we can safely delete the pointer after the call
int num_members = GNUNET_MESSENGER_iterate_members(room,
[] (void* cls, GNUNET_MESSENGER_Room* room, const GNUNET_MESSENGER_Contact* contact) -> int {
auto cb = (std::function<void(const Contact &)>*)cls;
(*cb)(Contact{contact});
return GNUNET_OK;
}, ptr);
delete ptr;
return num_members;
}

std::vector<Contact> Room::members()
{
std::vector<Contact> contacts;
int n = forEachMember([&contacts] (const Contact& contact) {
contacts.push_back(contact);
});
GNUNET_assert(n == contacts.size());
return contacts;
}

std::string Contact::name() const
{
auto name = GNUNET_MESSENGER_contact_get_name(contact);
Expand Down
9 changes: 9 additions & 0 deletions gnunetpp/gnunetpp-messenger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ struct Room : public NonCopyable
*/
void setReadMessageCallback(std::function<void(const Message&)> cb) { recv_cb = std::move(cb); }

/**
* @brief iterate over all members of this room
*
* @return the number of members
*/
int forEachMember(std::function<void(const Contact&)> cb);

std::vector<Contact> members();

GNUNET_MESSENGER_Room* room = nullptr;
std::function<void(const Message&)> recv_cb;
};
Expand Down

0 comments on commit e1aa368

Please sign in to comment.