diff --git a/include/neocomm/channel.hpp b/include/neocomm/channel.hpp index 5193098..1d97269 100644 --- a/include/neocomm/channel.hpp +++ b/include/neocomm/channel.hpp @@ -26,12 +26,26 @@ namespace neocomm { -/** - * @brief Object to contain channel information. - */ -struct channel { - dht::InfoHash key; //!< The sha1 hash of the channel name. - std::future token; //!< Token used to leave channel. - std::list messages; //!< Messages received by channel. +class channel { +public: + channel(const std::string &name, dht::DhtRunner *network); + ~channel(); + + inline std::string get_name() const { + return name; + } + //inline dht::InfoHash get_key() const { + //return key; + //} + //inline size_t get_token() { + //return token.get(); + //} + +private: + const std::string name; + const dht::InfoHash key; + dht::DhtRunner *network; + std::future token; + std::list messages; }; } diff --git a/include/neocomm/node.hpp b/include/neocomm/node.hpp index 348fc22..fc422dd 100644 --- a/include/neocomm/node.hpp +++ b/include/neocomm/node.hpp @@ -21,7 +21,7 @@ #include "neocomm/channel.hpp" #include -#include +#include namespace neocomm { @@ -64,18 +64,12 @@ public: */ void export_nodes(const std::string &file_path); // TODO - /** - * @brief Join a channel. - * - * @param name The name of the channel to join. - */ - void join_channel(const std::string &name); - /** - * @brief Leave an already joined channel. - * - * @param name Name of the channel. - */ + channel *join_channel(const std::string &name); + channel *get_channel(const std::string &name); void leave_channel(const std::string &name); + inline void leave_channel(channel *chan) { + leave_channel(chan->get_name()); + } /** * @brief Get the current alias for the user. @@ -95,18 +89,9 @@ public: } private: - inline struct channel *get_channel( - const std::string &name) { - for(auto &i : channels) - { - if(i.first == name) - return &i.second; - } - return nullptr; - } std::string alias; dht::DhtRunner network; - std::map channels; + std::vector channels; }; } diff --git a/src/channel.cpp b/src/channel.cpp new file mode 100644 index 0000000..3d6bf70 --- /dev/null +++ b/src/channel.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2018 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "neocomm/channel.hpp" + +using namespace neocomm; + +channel::channel(const std::string &name, + dht::DhtRunner *network) : + name(name), key(dht::InfoHash::get(name)), + network(network) { + token = network->listen(key, + [&](struct message &&msg) { + messages.push_back(msg); + return true; + }); +} + +channel::~channel() { + network->cancelListen(key, token.get()); +} diff --git a/src/node.cpp b/src/node.cpp index 60e2b25..7c2e060 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -31,22 +31,33 @@ node::~node() { network.join(); } -void node::join_channel(const std::string &name) { +channel *node::join_channel(const std::string &name) { + // don't do anything if already joined if(get_channel(name)) - return; - channels[name].key = dht::InfoHash::get(name); - channels[name].token = network.listen( - channels[name].key, - [&](struct message &&msg) { - channels[name].messages.push_back(msg); - return true; - }); + return nullptr; + + channels.push_back(new channel(name, &network)); + return channels.back(); +} + +channel *node::get_channel(const std::string &name) { + for(auto i : channels) + { + if(i->get_name() == name) + return i; + } + return nullptr; } void node::leave_channel(const std::string &name) { - if(not get_channel(name)) - return; - network.cancelListen(channels[name].key, - channels[name].token.get()); - channels.erase(name); + for(auto i = channels.begin(); + i not_eq channels.end(); ++i) + { + if((*i)->get_name() == name) + { + delete *i; + channels.erase(i); + return; + } + } }