From b1aa31403b2c7a48d5a11116ecb8210b58534bd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Tue, 21 Nov 2017 16:34:50 +0100 Subject: [PATCH] Message system now uses queues instead of vectors. --- include/neocomm.h | 10 ++++------ src/channel.cpp | 4 ++-- src/globals.hpp | 4 ++-- src/message.cpp | 21 +++++++++++++-------- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/include/neocomm.h b/include/neocomm.h index 15e42ca..b1dfa8f 100644 --- a/include/neocomm.h +++ b/include/neocomm.h @@ -36,15 +36,15 @@ extern "C" { * @brief A structure with necessary variables for other users. */ struct NeoComm_user { - const char *nick; ///< The alias used by the user. - const char *hash; ///< The unique hash of the user. + char *nick; ///< The alias used by the user. + char *hash; ///< The unique hash of the user. }; /** * @brief Structure with relevant message information. */ struct NeoComm_message { - const char *msg; ///< The text message. + char *msg; ///< The text message. time_t sent; ///< The time it was sent. struct NeoComm_user from; ///< Info on the peer who sent the message. }; @@ -176,9 +176,7 @@ struct NeoComm_message *NeoComm_get_next_message(const char *channel_name); * * @param msg The message to free from memory. */ -static inline void NeoComm_free_message(struct NeoComm_message *msg) { - free(msg); -} +void NeoComm_free_message(struct NeoComm_message *msg); /** * @brief Send a message to a channel. diff --git a/src/channel.cpp b/src/channel.cpp index f627b62..e465a93 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -64,10 +64,10 @@ int NeoComm_join_channel(const char *channel_name) { /*.hash =*/ chan_hash, /*.token =*/ node.listen(chan_hash, [&](dht::ImMessage &&msg) { - channels[s_chan_name].msgs.push_back(msg); + channels[s_chan_name].msgs.push(msg); return true; }), - /*.msgs = */ std::vector() + /*.msgs =*/ std::queue() }; return 1; diff --git a/src/globals.hpp b/src/globals.hpp index 8f57996..34a97ae 100644 --- a/src/globals.hpp +++ b/src/globals.hpp @@ -20,7 +20,7 @@ #include #include -#include +#include #include #include #include @@ -28,7 +28,7 @@ struct channel_info { dht::InfoHash hash; std::future token; - std::vector msgs; + std::queue msgs; }; extern dht::DhtRunner node; diff --git a/src/message.cpp b/src/message.cpp index 5d626b5..cdf79ba 100644 --- a/src/message.cpp +++ b/src/message.cpp @@ -36,18 +36,23 @@ struct NeoComm_message *NeoComm_get_next_message(const char *channel_name) { struct NeoComm_message *msg = static_cast(malloc(sizeof(struct NeoComm_message))); - *msg = { - /*.msg =*/ new_msg->msg.c_str(), - /*.sent =*/ new_msg->date, - /*.from =*/ { - /*.nick =*/ "DEFAULT", // TODO: Implement this!!! - /*.hash =*/ new_msg->from.to_c_str() - } - }; + msg->msg = static_cast(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(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()) {