Message system now uses queues instead of vectors.

This commit is contained in:
Nicolás Ortega Froysa 2017-11-21 16:34:50 +01:00
parent 7f7f9ae047
commit b1aa31403b
No known key found for this signature in database
GPG Key ID: FEC70E3BAE2E69BF
4 changed files with 21 additions and 18 deletions

View File

@ -36,15 +36,15 @@ extern "C" {
* @brief A structure with necessary variables for other users. * @brief A structure with necessary variables for other users.
*/ */
struct NeoComm_user { struct NeoComm_user {
const char *nick; ///< The alias used by the user. char *nick; ///< The alias used by the user.
const char *hash; ///< The unique hash of the user. char *hash; ///< The unique hash of the user.
}; };
/** /**
* @brief Structure with relevant message information. * @brief Structure with relevant message information.
*/ */
struct NeoComm_message { struct NeoComm_message {
const char *msg; ///< The text message. char *msg; ///< The text message.
time_t sent; ///< The time it was sent. time_t sent; ///< The time it was sent.
struct NeoComm_user from; ///< Info on the peer who sent the message. 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. * @param msg The message to free from memory.
*/ */
static inline void NeoComm_free_message(struct NeoComm_message *msg) { void NeoComm_free_message(struct NeoComm_message *msg);
free(msg);
}
/** /**
* @brief Send a message to a channel. * @brief Send a message to a channel.

View File

@ -64,10 +64,10 @@ int NeoComm_join_channel(const char *channel_name) {
/*.hash =*/ chan_hash, /*.hash =*/ chan_hash,
/*.token =*/ node.listen<dht::ImMessage>(chan_hash, /*.token =*/ node.listen<dht::ImMessage>(chan_hash,
[&](dht::ImMessage &&msg) { [&](dht::ImMessage &&msg) {
channels[s_chan_name].msgs.push_back(msg); channels[s_chan_name].msgs.push(msg);
return true; return true;
}), }),
/*.msgs = */ std::vector<dht::ImMessage>() /*.msgs =*/ std::queue<dht::ImMessage>()
}; };
return 1; return 1;

View File

@ -20,7 +20,7 @@
#include <opendht.h> #include <opendht.h>
#include <map> #include <map>
#include <vector> #include <queue>
#include <string> #include <string>
#include <utility> #include <utility>
#include <future> #include <future>
@ -28,7 +28,7 @@
struct channel_info { struct channel_info {
dht::InfoHash hash; dht::InfoHash hash;
std::future<size_t> token; std::future<size_t> token;
std::vector<dht::ImMessage> msgs; std::queue<dht::ImMessage> msgs;
}; };
extern dht::DhtRunner node; extern dht::DhtRunner node;

View File

@ -36,18 +36,23 @@ struct NeoComm_message *NeoComm_get_next_message(const char *channel_name) {
struct NeoComm_message *msg = struct NeoComm_message *msg =
static_cast<struct NeoComm_message*>(malloc(sizeof(struct NeoComm_message))); static_cast<struct NeoComm_message*>(malloc(sizeof(struct NeoComm_message)));
*msg = { msg->msg = static_cast<char*>(malloc(new_msg->msg.size()));
/*.msg =*/ new_msg->msg.c_str(), strcpy(msg->msg, new_msg->msg.c_str());
/*.sent =*/ new_msg->date, msg->sent = new_msg->date;
/*.from =*/ { msg->from.nick = NULL; // TODO: Implement this!
/*.nick =*/ "DEFAULT", // TODO: Implement this!!! msg->from.hash = static_cast<char*>(malloc(strlen(new_msg->from.to_c_str())));
/*.hash =*/ new_msg->from.to_c_str() strcpy(msg->from.hash, new_msg->from.to_c_str());
} channels[s_chan_name].msgs.pop();
};
return msg; 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) { time_t NeoComm_send_message(const char *channel_name, const char *message) {
if(not node.isRunning()) if(not node.isRunning())
{ {