Moving to full C++.

This commit is contained in:
Nicolás Ortega Froysa
2018-03-29 15:04:17 +02:00
parent 1c9e1859d2
commit fac59cfd2f
21 changed files with 830 additions and 1629 deletions

View File

@ -1,116 +0,0 @@
/*
* Copyright (C) 2017 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/>.
*/
#include "neocomm.h"
#include "error.hpp"
#include "globals.hpp"
std::map<std::string, struct channel_info> channels;
static std::mt19937_64 rand_dev { dht::crypto::random_device{}() };
static std::uniform_int_distribution<dht::Value::Id> rand_id;
static std::map<time_t, int> sent_messages;
static std::string channel_list;
int NeoComm_join_channel(const char *channel_name) {
if(not node.isRunning())
{
add_error("NeoComm must be initialized.");
return 0;
}
else if(channel_name[0] != '#')
{
add_error("Channel name must start with `#'.");
return 0;
}
const std::string s_chan_name(channel_name);
if(s_chan_name.empty())
{
add_error("Chan name is empty.");
return 0;
}
// if it already exists then just exit
if(channels.find(s_chan_name) not_eq channels.end())
return 1;
dht::InfoHash chan_hash(s_chan_name);
{
static constexpr dht::InfoHash INVALID_ID {};
if(chan_hash == INVALID_ID)
chan_hash = dht::InfoHash::get(s_chan_name);
}
channels[s_chan_name] = {
/*.hash =*/ chan_hash,
/*.token =*/ node.listen<dht::ImMessage>(chan_hash,
[&](dht::ImMessage &&msg) {
channels[s_chan_name].msgs.push(msg);
return true;
}),
/*.msgs =*/ std::queue<dht::ImMessage>()
};
return 1;
}
int NeoComm_leave_channel(const char *channel_name) {
if(not node.isRunning())
{
add_error("NeoComm must be initialized.");
return 0;
}
else if(channel_name[0] != '#')
{
add_error("Channel name must start with `#'.");
return 0;
}
const std::string s_chan_name(channel_name);
if(channels.find(s_chan_name) == channels.end())
{
add_error("Channel does not exist.");
return 0;
}
else if(s_chan_name.empty())
{
add_error("Chan name is empty.");
return 0;
}
struct channel_info *channel = &channels[s_chan_name];
channel->token.wait();
node.cancelListen(channel->hash, channel->token.get());
channels.erase(s_chan_name);
return 1;
}
const char *NeoComm_get_channel_list() {
channel_list.clear();
for(auto &i : channels)
channel_list += i.first + " ";
return channel_list.c_str();
}
unsigned int NeoComm_get_num_channels() {
return channels.size();
}

View File

@ -1,34 +0,0 @@
/*
* Copyright (C) 2017 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/>.
*/
#include "neocomm.h"
#include "error.hpp"
#include <string>
static std::string last_error;
void add_error(const std::string &error) {
last_error = error;
}
const char *NeoComm_get_last_error() {
if(last_error.empty())
return NULL;
return last_error.c_str();
}

View File

@ -1,23 +0,0 @@
/*
* Copyright (C) 2017 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>
void add_error(const std::string &error);

View File

@ -1,37 +0,0 @@
/*
* Copyright (C) 2017 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 <opendht.h>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <future>
struct channel_info {
dht::InfoHash hash;
std::future<size_t> token;
std::queue<dht::ImMessage> msgs;
};
extern dht::DhtRunner node;
// the key element is the name of the channel
extern std::map<std::string, struct channel_info> channels;

View File

@ -1,91 +0,0 @@
/*
* Copyright (C) 2017 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/>.
*/
#include "neocomm.h"
#include "globals.hpp"
#include "error.hpp"
std::map<std::string, struct channel_info> channels;
static std::mt19937_64 rand_dev { dht::crypto::random_device{}() };
static std::uniform_int_distribution<dht::Value::Id> rand_id;
static std::map<time_t, int> sent_messages;
struct NeoComm_message *NeoComm_get_next_message(const char *channel_name) {
const std::string s_chan_name(channel_name);
if(channels[s_chan_name].msgs.empty())
return NULL;
const dht::ImMessage *new_msg = &channels[s_chan_name].msgs.front();
struct NeoComm_message *msg =
static_cast<struct NeoComm_message*>(
malloc(sizeof(struct NeoComm_message)));
msg->msg = static_cast<char*>(malloc(new_msg->msg.size()));
strcpy(msg->msg, new_msg->msg.c_str());
msg->sent = new_msg->date;
msg->from.nick = NULL; // TODO: Implement this!
msg->from.hash = static_cast<char*>(
malloc(strlen(new_msg->from.to_c_str())));
strcpy(msg->from.hash, new_msg->from.to_c_str());
channels[s_chan_name].msgs.pop();
return msg;
}
void NeoComm_free_message(struct NeoComm_message *msg) {
free(msg->msg);
free(msg->from.hash);
free(msg);
}
time_t NeoComm_send_message(const char *channel_name, const char *message) {
if(not node.isRunning())
{
add_error("NeoComm must be initialized.");
return 0;
}
const dht::InfoHash chan_hash = channels[channel_name].hash;
const time_t now =
std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
if(channels.find(channel_name) == channels.end())
{
add_error("Not connected to channel");
return 0;
}
sent_messages[now] = NC_PENDING;
node.putSigned(chan_hash,
dht::ImMessage(rand_id(rand_dev), message, now), [&](bool sent) {
sent_messages[now] = sent ? NC_GOOD : NC_BAD;
});
return now;
}
// TODO: Devise a way so that the message can be resent.
int NeoComm_check_message(const time_t token) {
if(sent_messages.find(token) == sent_messages.end())
{
add_error("No message found with that token.");
return 0;
}
const int status = sent_messages[token];
sent_messages.erase(token);
return status;
}

View File

@ -1,126 +0,0 @@
/*
* Copyright (C) 2017 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/>.
*/
#include "neocomm.h"
#include "globals.hpp"
#include "error.hpp"
#include <opendht.h>
#include <string>
#include <fstream>
#include <stdexcept>
extern "C" {
#include <gnutls/gnutls.h>
}
#define DEFAULT_PORT 1335
dht::DhtRunner node;
int NeoComm_init(const unsigned short port) {
#ifdef WOE32
gnutls_global_init();
#endif //WOE32
try {
node.run((port == 0 ? DEFAULT_PORT : port),
dht::crypto::generateIdentity(), true);
} catch(const std::exception &e) {
add_error(e.what());
return 0;
}
return 1;
}
void NeoComm_deinit() {
node.join();
#ifdef WOE32
gnutls_global_deinit();
#endif //WOE32
}
int NeoComm_connect(const char *address, const unsigned short port) {
if(not node.isRunning())
{
add_error("NeoComm must be initialized.");
return 0;
}
node.bootstrap(address, std::to_string(port));
return 1;
}
int NeoComm_import_nodes(const char *node_file) {
if(not node.isRunning())
{
add_error("NeoComm must be initialized.");
return 0;
}
msgpack::unpacker upak;
{
std::ifstream import_file(node_file, std::ios::binary bitor
std::ios::ate);
if(not import_file.is_open())
{
add_error(std::string("Failed to open file ") + node_file);
return 0;
}
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);
upak.buffer_consumed(file_size);
}
msgpack::object_handle obj_handler;
while(upak.next(obj_handler))
{
auto imported_nodes =
obj_handler.get().as<std::vector<dht::NodeExport>>();
node.bootstrap(imported_nodes);
}
return 1;
}
int NeoComm_export_nodes(const char *node_file) {
if(not node.isRunning())
{
add_error("NeoComm must be initialized.");
return 0;
}
std::ofstream export_file(node_file, std::ios::binary);
if(not export_file.is_open())
{
add_error(std::string("Failed to open file ") + node_file);
return 0;
}
msgpack::pack(export_file, node.exportNodes());
return 1;
}
struct NeoComm_stats NeoComm_get_node_stats() {
struct NeoComm_stats statistics;
statistics.total = node.getNodesStats(
AF_UNSPEC,
&statistics.good,
&statistics.dubious,
&statistics.cached,
&statistics.incoming);
return statistics;
}