From e3dc8586977fe97b528d52a857aa71d7db90b430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Mon, 25 Sep 2017 16:21:16 +0200 Subject: [PATCH 01/15] These functions are not yet implemented. --- include/neocomm.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/neocomm.h b/include/neocomm.h index 1eee86a..52be390 100644 --- a/include/neocomm.h +++ b/include/neocomm.h @@ -82,7 +82,7 @@ int NeoComm_connect(const char *address, const unsigned short port); * @return 1 upon success, 0 upon failure. Use NeoComm_get_last_error for a * text description of the error. */ -int NeoComm_import_nodes(const char *node_file); +//int NeoComm_import_nodes(const char *node_file); /** * @brief Export current list of nodes into a file. @@ -92,7 +92,7 @@ int NeoComm_import_nodes(const char *node_file); * @return 1 upon success, 0 upon failure. Use NeoComm_get_last_error for a * text description of the error. */ -int NeoComm_export_nodes(const char *node_file); +//int NeoComm_export_nodes(const char *node_file); /** * @brief Join a channel. From e4e59b56a2d168bf4788efb0168ec5daf6a2bcda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Mon, 25 Sep 2017 16:34:45 +0200 Subject: [PATCH 02/15] Fixed the warning tag --- include/neocomm.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/neocomm.h b/include/neocomm.h index 52be390..7f7b9b6 100644 --- a/include/neocomm.h +++ b/include/neocomm.h @@ -119,8 +119,8 @@ void NeoComm_leave_channel(const char *channel_name); * * @return A structure of the message. If NULL then there are no new messages. * - * @notice This command will remove the message from the internal list. - * @notice The message must be freed from memory using NeoComm_free_message. + * @warning This command will remove the message from the internal list. + * @warning The message must be freed from memory using NeoComm_free_message. */ struct message *NeoComm_get_next_message(const char *channel_name); From 80269a53e49c266dda1d41c6e7ff456a3131479f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Mon, 25 Sep 2017 16:37:09 +0200 Subject: [PATCH 03/15] So Doxygen won't fuck up the docs. --- include/neocomm.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/neocomm.h b/include/neocomm.h index 7f7b9b6..f47135d 100644 --- a/include/neocomm.h +++ b/include/neocomm.h @@ -74,7 +74,7 @@ void NeoComm_deinit(); */ int NeoComm_connect(const char *address, const unsigned short port); -/** +/* * @brief Import a list of nodes from a node file and connect to them. * * @param node_file Path to the node list file. @@ -84,7 +84,7 @@ int NeoComm_connect(const char *address, const unsigned short port); */ //int NeoComm_import_nodes(const char *node_file); -/** +/* * @brief Export current list of nodes into a file. * * @param node_file path to node list file. From 763277dd9b28ef4a48c096b214038ab067ce6391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Mon, 25 Sep 2017 16:53:41 +0200 Subject: [PATCH 04/15] Fixed spacing. --- src/channel.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/channel.cpp b/src/channel.cpp index da7bfea..55c0310 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -100,16 +100,18 @@ void NeoComm_free_message(struct message *msg) { int NeoComm_send_message(const char *channel_name, const char *message) { 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()); + 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; } - node.putSigned(chan_hash, dht::ImMessage(rand_id(rand_dev), message, now), [](bool sent) { - // TODO: Figure out what to do here for failed messages. - }); + node.putSigned(chan_hash, + dht::ImMessage(rand_id(rand_dev), message, now), [](bool sent) { + // TODO: Figure out what to do here for failed messages. + }); return 1; } From 59d59549e1b0a53d4daf0258d936fcf6f9c9da3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Mon, 25 Sep 2017 17:19:16 +0200 Subject: [PATCH 05/15] Check status of sent messages. --- include/neocomm.h | 28 +++++++++++++++++++++++++--- src/channel.cpp | 32 +++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/include/neocomm.h b/include/neocomm.h index f47135d..f2f19fd 100644 --- a/include/neocomm.h +++ b/include/neocomm.h @@ -47,6 +47,15 @@ struct message { struct user from; ///< Info on the peer who sent the message. }; +/** + * @brief Status of a message. + */ +enum { + NC_PENDING = 1, ///< Status of message is pending. + NC_GOOD = 2, ///< Message was sent properly. + NC_BAD = 3, ///< Message failed to send. +}; + /** * @brief Initialize a local DHT node. If port is 0 then the default port * number will be used. @@ -137,10 +146,23 @@ void NeoComm_free_message(struct message *msg); * @param channel_name Name of the channel to send the message to. * @param message A message to send to the channel. * - * @return 1 upon success, 0 upon failure. Use NeoComm_get_last_error for a - * text description of the error. + * @return Upon success it returns the time that the message was sent which + * can be used as a token to see if the message was sent properly using + * NeoComm_check_message, if the function failed then 0 will be returned. */ -int NeoComm_send_message(const char *channel_name, const char *message); +time_t NeoComm_send_message(const char *channel_name, const char *message); + +/** + * @brief Check the status of a sent message. + * + * @param token The token of a message to check its status. + * + * @return The status of the message (NC_GOOD, NC_BAD, or NC_PENDING). If the + * token does not exist then 0 is returned. + * + * @warning Once checked the token becomes invalid. + */ +int NeoComm_check_message(const time_t token); /** * @brief Get the last error that occurred in text. diff --git a/src/channel.cpp b/src/channel.cpp index 55c0310..fa46d07 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -23,9 +23,11 @@ std::map channels; -static std::mt19937_64 rand_dev {dht::crypto::random_device{}()}; +static std::mt19937_64 rand_dev { dht::crypto::random_device{}() }; static std::uniform_int_distribution rand_id; +static std::map sent_messages; + int NeoComm_join_channel(const char *channel_name) { if(not node.isRunning()) { @@ -54,7 +56,7 @@ int NeoComm_join_channel(const char *channel_name) { channels[s_chan_name] = { /*.hash =*/ chan_hash, /*.token =*/ node.listen(chan_hash, - [=](dht::ImMessage &&msg) { + [&](dht::ImMessage &&msg) { channels[s_chan_name].msgs.push_back(msg); return true; }), @@ -98,7 +100,12 @@ void NeoComm_free_message(struct message *msg) { free(msg); } -int NeoComm_send_message(const char *channel_name, const char *message) { +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()); @@ -108,10 +115,21 @@ int NeoComm_send_message(const char *channel_name, const char *message) { 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) { - // TODO: Figure out what to do here for failed messages. + dht::ImMessage(rand_id(rand_dev), message, now), [&](bool sent) { + sent_messages[now] = sent ? NC_GOOD : NC_BAD; }); - - return 1; + return now; +} + +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; } From 0278d786a9da0fff6abe885a6eba150d8743cd90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Thu, 28 Sep 2017 18:07:26 +0200 Subject: [PATCH 06/15] Gets the hash of the sender. --- src/channel.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/channel.cpp b/src/channel.cpp index fa46d07..0f6ae75 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -90,7 +90,10 @@ struct message *NeoComm_get_next_message(const char *channel_name) { *msg = { /*.msg =*/ new_msg->msg.c_str(), /*.sent =*/ new_msg->date, - // TODO: add the rest of these + /*.from =*/ { + /*.nick =*/ "DEFAULT", // TODO: Implement this!!! + /*.hash =*/ new_msg->from.to_c_str() + } }; return msg; From ca0a69102038b7d78556b13c65a07e8f0e2331b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Fri, 29 Sep 2017 10:33:39 +0200 Subject: [PATCH 07/15] Inline the free function. --- include/neocomm.h | 6 +++++- src/channel.cpp | 4 ---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/neocomm.h b/include/neocomm.h index f2f19fd..8de742e 100644 --- a/include/neocomm.h +++ b/include/neocomm.h @@ -26,8 +26,10 @@ #ifdef __cplusplus extern "C" { #include +#include #else #include +#include #endif /** @@ -138,7 +140,9 @@ struct message *NeoComm_get_next_message(const char *channel_name); * * @param msg The message to free from memory. */ -void NeoComm_free_message(struct message *msg); +static inline void NeoComm_free_message(struct message *msg) { + free(msg); +} /** * @brief Send a message to a channel. diff --git a/src/channel.cpp b/src/channel.cpp index 0f6ae75..df8a576 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -99,10 +99,6 @@ struct message *NeoComm_get_next_message(const char *channel_name) { return msg; } -void NeoComm_free_message(struct message *msg) { - free(msg); -} - time_t NeoComm_send_message(const char *channel_name, const char *message) { if(not node.isRunning()) { From 5c3e6889de9826b71cbdbc6895f045da01056d34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Fri, 29 Sep 2017 10:38:30 +0200 Subject: [PATCH 08/15] Added more return statements to commented out code. --- src/node.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/node.cpp b/src/node.cpp index 9149cf5..a760561 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -103,6 +103,7 @@ int NeoComm_connect(const char *address, const unsigned short port) { 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()) @@ -111,4 +112,5 @@ int NeoComm_connect(const char *address, const unsigned short port) { return 0; } msgpack::pack(export_file, node.exportNodes()); + return 1; }*/ From 0224cd157f403a2d481095d3e0a0b1a03f117164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Fri, 29 Sep 2017 10:40:36 +0200 Subject: [PATCH 09/15] Clarification in documentation. --- include/neocomm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/neocomm.h b/include/neocomm.h index 8de742e..e7f5b98 100644 --- a/include/neocomm.h +++ b/include/neocomm.h @@ -50,7 +50,7 @@ struct message { }; /** - * @brief Status of a message. + * @brief Status of a sent message. */ enum { NC_PENDING = 1, ///< Status of message is pending. From 71edd76b6007aae37eefe4f9e24556214672d0f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Fri, 29 Sep 2017 10:43:41 +0200 Subject: [PATCH 10/15] Added to TODO --- src/channel.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/channel.cpp b/src/channel.cpp index df8a576..bd67dad 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -122,6 +122,7 @@ time_t NeoComm_send_message(const char *channel_name, const char *message) { 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()) { From 36444aeb668e086c8faf1d214949c8203d83f525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Tue, 3 Oct 2017 16:57:02 +0200 Subject: [PATCH 11/15] Change version. --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 899a84f..d347e9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,8 +18,8 @@ cmake_minimum_required(VERSION 3.1) project(NeoComm) set(TARGET_NAME "neocomm") -set(TARGET_VERSION_MAJOR 0) -set(TARGET_VERSION_MINOR 1) +set(TARGET_VERSION_MAJOR 1) +set(TARGET_VERSION_MINOR 0) if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "release") From e40d5e8329a46d2de9232e3f12be6eb0013a21dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Mon, 23 Oct 2017 13:28:39 +0200 Subject: [PATCH 12/15] Require version 1.4.0 or later. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d347e9c..7c64e8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,7 @@ option(BUILD_SHARED_LIB find_package(GnuTLS REQUIRED) find_package(PkgConfig REQUIRED) -pkg_search_module(OPENDHT REQUIRED opendht) +pkg_search_module(OPENDHT REQUIRED opendht>=1.4.0) include_directories( "include/" From 433860b3cd82351c857e3d49a4258edad6a92a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Mon, 30 Oct 2017 16:45:06 +0100 Subject: [PATCH 13/15] Set minimum OpenDHT version. --- CMakeLists.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c64e8f..c0ae266 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,7 @@ option(BUILD_SHARED_LIB find_package(GnuTLS REQUIRED) find_package(PkgConfig REQUIRED) -pkg_search_module(OPENDHT REQUIRED opendht>=1.4.0) +pkg_check_modules(OPENDHT REQUIRED opendht>=1.4.0) include_directories( "include/" @@ -43,7 +43,8 @@ set(SRCS src/error.cpp src/node.cpp) -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") set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") set(CMAKE_CXX_FLAGS_RELEASE "-O3") set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g -O3") From 78a7daf984a48591b5ae6f5b08645a7d5ec63c93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Mon, 30 Oct 2017 16:45:22 +0100 Subject: [PATCH 14/15] Uncomment import/export functions. --- include/neocomm.h | 8 ++++---- src/node.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/neocomm.h b/include/neocomm.h index e7f5b98..3564cce 100644 --- a/include/neocomm.h +++ b/include/neocomm.h @@ -85,7 +85,7 @@ void NeoComm_deinit(); */ int NeoComm_connect(const char *address, const unsigned short port); -/* +/** * @brief Import a list of nodes from a node file and connect to them. * * @param node_file Path to the node list file. @@ -93,9 +93,9 @@ int NeoComm_connect(const char *address, const unsigned short port); * @return 1 upon success, 0 upon failure. Use NeoComm_get_last_error for a * text description of the error. */ -//int NeoComm_import_nodes(const char *node_file); +int NeoComm_import_nodes(const char *node_file); -/* +/** * @brief Export current list of nodes into a file. * * @param node_file path to node list file. @@ -103,7 +103,7 @@ int NeoComm_connect(const char *address, const unsigned short port); * @return 1 upon success, 0 upon failure. Use NeoComm_get_last_error for a * text description of the error. */ -//int NeoComm_export_nodes(const char *node_file); +int NeoComm_export_nodes(const char *node_file); /** * @brief Join a channel. diff --git a/src/node.cpp b/src/node.cpp index a760561..7d7e9a7 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -67,7 +67,7 @@ int NeoComm_connect(const char *address, const unsigned short port) { // NOTICE: When the new OpenDHT release comes out this code should work. // Meanwhile just keep it commented out. -/*int NeoComm_import_nodes(const char *node_file) { +int NeoComm_import_nodes(const char *node_file) { if(not node.isRunning()) { add_error("NeoComm must be initialized."); @@ -97,9 +97,9 @@ int NeoComm_connect(const char *address, const unsigned short port) { node.bootstrap(imported_nodes); } return 1; -}*/ +} -/*int NeoComm_export_nodes(const char *node_file) { +int NeoComm_export_nodes(const char *node_file) { if(not node.isRunning()) { add_error("NeoComm must be initialized."); @@ -113,4 +113,4 @@ int NeoComm_connect(const char *address, const unsigned short port) { } msgpack::pack(export_file, node.exportNodes()); return 1; -}*/ +} From 0c0ce8def2b731a3ef48f85ca804b9a92eaa9ec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Tue, 31 Oct 2017 16:54:51 +0100 Subject: [PATCH 15/15] No need for those comments anymore. --- src/node.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/node.cpp b/src/node.cpp index 7d7e9a7..7ae15e6 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -65,8 +65,6 @@ int NeoComm_connect(const char *address, const unsigned short port) { return 1; } -// NOTICE: When the new OpenDHT release comes out this code should work. -// Meanwhile just keep it commented out. int NeoComm_import_nodes(const char *node_file) { if(not node.isRunning()) {