Check status of sent messages.

This commit is contained in:
Nicolás Ortega Froysa
2017-09-25 17:19:16 +02:00
parent 763277dd9b
commit 59d59549e1
2 changed files with 50 additions and 10 deletions

View File

@ -23,9 +23,11 @@
std::map<std::string, struct channel_info> 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<dht::Value::Id> rand_id;
static std::map<time_t, int> 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<dht::ImMessage>(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;
}