Should now be able to join channels.
This commit is contained in:
parent
7d6cda8d1b
commit
0df937d3fd
@ -21,10 +21,7 @@
|
|||||||
#include "error.hpp"
|
#include "error.hpp"
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
|
|
||||||
#include <string>
|
std::unordered_map<std::string, struct channel_info> channels;
|
||||||
|
|
||||||
dht::DhtRunner node;
|
|
||||||
std::unordered_map<std::string, dht::InfoHash> chans;
|
|
||||||
|
|
||||||
int NeoComm_join_channel(const char *channel_name) {
|
int NeoComm_join_channel(const char *channel_name) {
|
||||||
if(not node.isRunning())
|
if(not node.isRunning())
|
||||||
@ -40,21 +37,38 @@ int NeoComm_join_channel(const char *channel_name) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it already exists then just exit
|
// if it already exists then just exit
|
||||||
if(chans.find(s_chan_name) == chans.end())
|
if(channels.find(s_chan_name) == channels.end())
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
dht::InfoHash chan(s_chan_name);
|
dht::InfoHash chan_hash(s_chan_name);
|
||||||
{
|
{
|
||||||
static constexpr dht::InfoHash INVALID_ID {};
|
static constexpr dht::InfoHash INVALID_ID {};
|
||||||
if(chan == INVALID_ID)
|
if(chan_hash == INVALID_ID)
|
||||||
chan = dht::InfoHash::get(s_chan_name);
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NeoComm_leave_channel(const char *channel_name) {
|
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);
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,18 @@
|
|||||||
|
|
||||||
#include <opendht.h>
|
#include <opendht.h>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
#include <string>
|
#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 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;
|
||||||
|
Loading…
Reference in New Issue
Block a user