From 5b8ea480581549a5c3a9f45cd773a50b0326c473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Sun, 2 Sep 2018 12:33:23 +0200 Subject: [PATCH] Adding a message object. --- include/neocomm/channel.hpp | 5 +++-- include/neocomm/message.hpp | 31 +++++++++++++++++++++++++++++++ include/neocomm/node.hpp | 23 ++++++++++++++++++++++- src/node.cpp | 8 +++++--- 4 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 include/neocomm/message.hpp diff --git a/include/neocomm/channel.hpp b/include/neocomm/channel.hpp index f34f8ad..5193098 100644 --- a/include/neocomm/channel.hpp +++ b/include/neocomm/channel.hpp @@ -18,6 +18,8 @@ #pragma once +#include "neocomm/message.hpp" + #include #include #include @@ -30,7 +32,6 @@ namespace neocomm { 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. + std::list messages; //!< Messages received by channel. }; - } diff --git a/include/neocomm/message.hpp b/include/neocomm/message.hpp new file mode 100644 index 0000000..3ac2cd6 --- /dev/null +++ b/include/neocomm/message.hpp @@ -0,0 +1,31 @@ +/* + * 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 . + */ + +#pragma once + +#include +#include + +namespace neocomm { + +struct message { + std::string alias; + std::string msg; + MSGPACK_DEFINE(alias, msg) +}; +} diff --git a/include/neocomm/node.hpp b/include/neocomm/node.hpp index 7fd3916..348fc22 100644 --- a/include/neocomm/node.hpp +++ b/include/neocomm/node.hpp @@ -34,8 +34,10 @@ public: * @brief Initalize the node. * * @param port Local port to bind to. + * @param alias Set the alias of the user. */ - node(const unsigned short port = 31133); + node(const unsigned short port = 31133, + const std::string &alias = "unknown"); ~node(); /** @@ -74,6 +76,24 @@ public: * @param name Name of the channel. */ void leave_channel(const std::string &name); + + /** + * @brief Get the current alias for the user. + * + * @return The current alias of the user. + */ + inline std::string get_alias() const { + return alias; + } + /** + * @brief Set the alias of the user. + * + * @param alias The new alias of the user. + */ + inline void set_alias(const std::string &alias) { + this->alias = alias; + } + private: inline struct channel *get_channel( const std::string &name) { @@ -84,6 +104,7 @@ private: } return nullptr; } + std::string alias; dht::DhtRunner network; std::map channels; }; diff --git a/src/node.cpp b/src/node.cpp index a39d2a0..60e2b25 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -17,10 +17,12 @@ */ #include "neocomm/node.hpp" +#include "neocomm/message.hpp" using namespace neocomm; -node::node(unsigned short port) { +node::node(unsigned short port, const std::string &alias) : + alias(alias) { // TODO: see about preserving an identity network.run(port, dht::crypto::generateIdentity(), true); } @@ -33,9 +35,9 @@ void node::join_channel(const std::string &name) { if(get_channel(name)) return; channels[name].key = dht::InfoHash::get(name); - channels[name].token = network.listen( + channels[name].token = network.listen( channels[name].key, - [&](std::string &&msg) { + [&](struct message &&msg) { channels[name].messages.push_back(msg); return true; });