Adding a message object.

This commit is contained in:
Nicolás Ortega Froysa 2018-09-02 12:33:23 +02:00
parent 414375d0e6
commit 5b8ea48058
No known key found for this signature in database
GPG Key ID: FEC70E3BAE2E69BF
4 changed files with 61 additions and 6 deletions

View File

@ -18,6 +18,8 @@
#pragma once
#include "neocomm/message.hpp"
#include <string>
#include <list>
#include <opendht.h>
@ -30,7 +32,6 @@ namespace neocomm {
struct channel {
dht::InfoHash key; //!< The sha1 hash of the channel name.
std::future<size_t> token; //!< Token used to leave channel.
std::list<std::string> messages; //!< Messages received by channel.
std::list<struct message> messages; //!< Messages received by channel.
};
}

View File

@ -0,0 +1,31 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <opendht.h>
namespace neocomm {
struct message {
std::string alias;
std::string msg;
MSGPACK_DEFINE(alias, msg)
};
}

View File

@ -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<std::string, struct channel> channels;
};

View File

@ -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<std::string>(
channels[name].token = network.listen<struct message>(
channels[name].key,
[&](std::string &&msg) {
[&](struct message &&msg) {
channels[name].messages.push_back(msg);
return true;
});