Should now be able to join channels.

This commit is contained in:
Nicolás Ortega Froysa 2017-09-21 09:14:31 +02:00
parent 7d6cda8d1b
commit 0df937d3fd
No known key found for this signature in database
GPG Key ID: FEC70E3BAE2E69BF
2 changed files with 37 additions and 12 deletions

View File

@ -21,10 +21,7 @@
#include "error.hpp"
#include "globals.hpp"
#include <string>
dht::DhtRunner node;
std::unordered_map<std::string, dht::InfoHash> chans;
std::unordered_map<std::string, struct channel_info> channels;
int NeoComm_join_channel(const char *channel_name) {
if(not node.isRunning())
@ -40,21 +37,38 @@ int NeoComm_join_channel(const char *channel_name) {
return 0;
}
// If it already exists then just exit
if(chans.find(s_chan_name) == chans.end())
// if it already exists then just exit
if(channels.find(s_chan_name) == channels.end())
return 1;
dht::InfoHash chan(s_chan_name);
dht::InfoHash chan_hash(s_chan_name);
{
static constexpr dht::InfoHash INVALID_ID {};
if(chan == INVALID_ID)
chan = dht::InfoHash::get(s_chan_name);
if(chan_hash == INVALID_ID)
chan_hash = dht::InfoHash::get(s_chan_name);
}
chans[s_chan_name] = chan;
channels[s_chan_name] = {
/*.hash =*/ chan_hash,
/*.token =*/ node.listen<dht::ImMessage>(chan_hash,
[=](dht::ImMessage &&msg) {
if(msg.from not_eq node.getId())
channels[s_chan_name].msgs.push_back(msg);
return true;
}),
/*.msgs = */ std::vector<dht::ImMessage>()
};
return 1;
}
void NeoComm_leave_channel(const char *channel_name) {
chans.erase(std::string(channel_name));
const std::string s_chan_name(channel_name);
if(channels.find(s_chan_name) == channels.end())
return;
struct channel_info *channel = &channels[s_chan_name];
channel->token.wait();
node.cancelListen(channel->hash, channel->token.get());
channels.erase(s_chan_name);
}

View File

@ -20,7 +20,18 @@
#include <opendht.h>
#include <unordered_map>
#include <vector>
#include <string>
#include <utility>
#include <future>
struct channel_info {
dht::InfoHash hash;
std::future<size_t> token;
std::vector<dht::ImMessage> msgs;
};
extern dht::DhtRunner node;
extern std::unordered_map<std::string, dht::InfoHash> chans;
// the key element is the name of the channel
extern std::unordered_map<std::string, struct channel_info> channels;