Had to rewrite this entire bitch.

This commit is contained in:
Nicolás Ortega Froysa 2018-09-01 19:40:04 +02:00
parent b53556599f
commit 45c1879880
No known key found for this signature in database
GPG Key ID: FEC70E3BAE2E69BF
6 changed files with 55 additions and 233 deletions

View File

@ -40,12 +40,11 @@ include_directories(
# Define files # Define files
set(SRCS set(SRCS
src/node.cpp) "src/node.cpp")
set(HDRS set(HDRS
include/neocomm/channel.hpp "include/neocomm/channel.hpp"
include/neocomm/message.hpp "include/neocomm/node.hpp")
include/neocomm/node.hpp)
# Define C++ compiler flags # Define C++ compiler flags
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Wextra -Wpedantic -Werror -Wfatal-errors -pedantic-errors -fno-elide-constructors") set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Wextra -Wpedantic -Werror -Wfatal-errors -pedantic-errors -fno-elide-constructors")
@ -78,7 +77,7 @@ else()
add_library(${TARGET_NAME} STATIC ${SRCS}) add_library(${TARGET_NAME} STATIC ${SRCS})
endif() endif()
set_target_properties(${TARGET_NAME} set_target_properties(${TARGET_NAME}
PROPERTIES PUBLIC_HEADER ${HDRS}) PROPERTIES PUBLIC_HEADER "${HDRS}")
install(TARGETS ${TARGET_NAME} install(TARGETS ${TARGET_NAME}
ARCHIVE DESTINATION lib/ ARCHIVE DESTINATION lib/

View File

@ -19,28 +19,15 @@
#pragma once #pragma once
#include <string> #include <string>
#include <list>
#include "identity.hpp" #include <opendht.h>
namespace neocomm { namespace neocomm {
/** struct channel {
* @brief Container class of all data and basic functions pertaining to dht::InfoHash key;
* channel functionality. std::future<size_t> token;
*/ std::list<std::string> messages;
class channel {
public:
/**
* @brief Initialize the channel class and start receiving messages
* for the given ID.
*
* @param chan_id The ID of the channel.
*/
channel(const std::string &chan_id); // TODO
~channel(); // TODO
private:
const std::string chan_id;
std::vector<message> msgs; // messages to and from the channel
}; };
} }

View File

@ -1,38 +0,0 @@
/*
* 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 <ctime>
#include "identity.hpp"
namespace neocomm {
/**
* @brief Container for data of a user message.
*/
class message {
private:
const std::string text;
const struct tm time_stamp;
const identity from;
};
}

View File

@ -18,17 +18,4 @@
#pragma once #pragma once
namespace neocomm { #include "node.hpp"
/**
* @brief Container class for data pertaining to a user.
*/
class identity {
public:
private:
const std::string id;
std::string nick;
std::string client_info;
};
}

View File

@ -18,89 +18,39 @@
#pragma once #pragma once
#include "identity.hpp" #include "neocomm/channel.hpp"
#include "channel.hpp"
#include <vector>
#include <string>
#include <opendht.h> #include <opendht.h>
#include <map>
namespace neocomm { namespace neocomm {
/**
* @brief NeoComm node object.
*
* @details Contains the necessary interfaces to create a
* NeoComm node and connect to a NeoComm network.
*/
class node { class node {
public: public:
/** node(const unsigned short port = 31133);
* @brief Initialize a NeoComm node.
*
* @param port Listen on a given port (8085 by default).
*/
node(unsigned short port = 8085);
/**
* @brief Initialize a NeoComm node inputting a list of
* known nodes from a file.
*
* @param node_file Path to file containing node list.
* @param port Listen on a given port (8085 by default).
*/
node(const std::string &node_file,
unsigned short port = 8085);
~node(); ~node();
/** inline void connect(const std::string &address,
* @brief Connect the node to a given address. const std::string &port) {
* network.bootstrap(address, port);
* @param address The IP or domain name of the other node. }
* @param port The port of the other node. void import_nodes(const std::string &file_path); // TODO
*/ void export_nodes(const std::string &file_path); // TODO
void connect(const std::string &address,
unsigned short port);
/**
* @brief Import and connect to a list of nodes from a
* previous session.
*
* @param node_file File containing the node list.
*/
void import_nodes(const std::string &node_file);
/**
* @brief Export currently connected nodes to a file.
*
* @param node_file File to export nodes to.
*/
void export_nodes(const std::string &node_file);
/**
* @brief Join and receive messages from a channel.
*
* @param chan_id The ID of the channel.
*/
void join_channel(const std::string &chan_id); // TODO
/**
* @brief Retrieve the channel object.
*
* @param chan_id The ID of the channel.
*
* @return The channel object.
*/
channel* get_channel(const std::string &chan_id); // TODO
/**
* @brief Leave a channel.
*
* @param chan_id The ID of the channel.
*/
void leave_channel(const std::string &chan_id); // TODO
void join_channel(const std::string &name);
void leave_channel(const std::string &name);
private: private:
unsigned int pm_timeout; // timeout for private message reception confirmation inline struct channel *get_channel(
unsigned int chan_timeout; // timeout for channel presence message const std::string &name) {
std::vector<channel> channels; // channels connected to via the node for(auto &i : channels)
std::vector<identity> identities; // known users on the network {
dht::DhtRunner dht_node; // the actual OpenDHT node if(i.first == name)
return &i.second;
}
return nullptr;
}
dht::DhtRunner network;
std::map<std::string, struct channel> channels;
}; };
} }

View File

@ -18,96 +18,33 @@
#include "neocomm/node.hpp" #include "neocomm/node.hpp"
#include <iostream> using namespace neocomm;
#include <fstream>
#include <stdexcept>
neocomm::node::node(unsigned short port) { node::node(unsigned short port) {
#ifdef WOE32 // TODO: see about preserving an identity
gnutls_global_init(); network.run(port, dht::crypto::generateIdentity(), true);
#endif
try {
dht_node.run(port,
dht::crypto::generateIdentity(), true);
}
catch(const std::exception &e)
{
// rethrow exception
throw e;
}
} }
neocomm::node::node(const std::string &node_file, node::~node() {
unsigned short port) { network.join();
#ifdef WOE32
gnutls_global_init();
#endif
try {
dht_node.run(port,
dht::crypto::generateIdentity(), true);
}
catch(const std::exception &e)
{
// rethrow exception
throw e;
}
import_nodes(node_file);
} }
neocomm::node::~node() { void node::join_channel(const std::string &name) {
dht_node.join(); if(get_channel(name))
#ifdef WOE32 return;
gnutls_global_deinit(); channels[name].key = dht::InfoHash::get(name);
#endif channels[name].token = network.listen<std::string>(
channels[name].key,
[&](std::string &&msg) {
channels[name].messages.push_back(msg);
return true;
});
} }
void neocomm::node::connect(const std::string &address, void node::leave_channel(const std::string &name) {
unsigned short port) { if(!get_channel(name))
if(not dht_node.isRunning()) return;
throw std::runtime_error("Node is not yet running."); network.cancelListen(channels[name].key,
dht_node.bootstrap(address, std::to_string(port)); channels[name].token.get());
} channels.erase(name);
void neocomm::node::import_nodes(
const std::string &node_file) {
if(not dht_node.isRunning())
throw std::runtime_error("Node is not yet running.");
msgpack::unpacker upak;
{
std::ifstream import_file(node_file, std::ios::binary
bitor std::ios::ate);
if(not import_file.is_open())
{
throw std::runtime_error("Failed to open file " +
node_file);
}
auto file_size = import_file.tellg();
import_file.seekg(0, std::ios::beg);
upak.reserve_buffer(file_size);
import_file.read(upak.buffer(), file_size);
}
msgpack::object_handle obj_handler;
while(upak.next(obj_handler))
{
auto imported_nodes =
obj_handler.get().as<
std::vector<dht::NodeExport>>();
dht_node.bootstrap(imported_nodes);
}
}
void neocomm::node::export_nodes(
const std::string &node_file) {
if(not dht_node.isRunning())
throw std::runtime_error("Node is not yet running.");
std::ofstream export_file(node_file, std::ios::binary);
if(not export_file.is_open())
{
throw std::runtime_error("Failed to open file " +
node_file);
}
msgpack::pack(export_file, dht_node.exportNodes());
} }