Commit 40cae3db authored by Adrien Béraud's avatar Adrien Béraud
Browse files

namedirectory: use dht executor

Change-Id: I232a001d3e62b7470e38adc1aa74eabadcc8b210
parent 827c546d
......@@ -73,7 +73,8 @@ NameDirectory::lookupUri(const std::string& uri, const std::string& default_serv
NameDirectory::NameDirectory(const std::string& s)
: serverHost_(s),
cachePath_(fileutils::get_cache_dir()+DIR_SEPARATOR_STR+CACHE_DIRECTORY+DIR_SEPARATOR_STR+serverHost_)
cachePath_(fileutils::get_cache_dir()+DIR_SEPARATOR_STR+CACHE_DIRECTORY+DIR_SEPARATOR_STR+serverHost_),
executor_(std::make_shared<dht::Executor>(dht::ThreadPool::io(), 8))
{}
void
......@@ -110,7 +111,6 @@ size_t getContentLength(restbed::Response& reply)
void NameDirectory::lookupAddress(const std::string& addr, LookupCallback cb)
{
try {
std::string cacheResult = nameCache(addr);
if (not cacheResult.empty()) {
cb(cacheResult, Response::found);
......@@ -124,8 +124,12 @@ void NameDirectory::lookupAddress(const std::string& addr, LookupCallback cb)
JAMI_DBG("Address lookup for %s: %s", addr.c_str(), uri.to_string().c_str());
auto ret = restbed::Http::async(req, [this,cb,addr](const std::shared_ptr<restbed::Request>&,
const std::shared_ptr<restbed::Response>& reply) {
executor_->run([this, req, cb=std::move(cb), addr] {
try {
restbed::Http::async(req, [this, cb=std::move(cb), addr=std::move(addr)]
(const std::shared_ptr<restbed::Request>&,
const std::shared_ptr<restbed::Response>& reply)
{
auto code = reply->get_status_code();
if (code == 200) {
size_t length = getContentLength(*reply);
......@@ -163,14 +167,12 @@ void NameDirectory::lookupAddress(const std::string& addr, LookupCallback cb)
} else {
cb("", Response::error);
}
}).share();
// avoid blocking on future destruction
dht::ThreadPool::io().run([ret](){ ret.get(); });
});
} catch (const std::exception& e) {
JAMI_ERR("Error when performing address lookup: %s", e.what());
cb("", Response::error);
}
});
}
bool
......@@ -181,7 +183,6 @@ NameDirectory::verify(const std::string& name, const dht::crypto::PublicKey& pk,
void NameDirectory::lookupName(const std::string& n, LookupCallback cb)
{
try {
std::string name {n};
if (not validateName(name)) {
cb(name, Response::invalidResponse);
......@@ -202,8 +203,12 @@ void NameDirectory::lookupName(const std::string& n, LookupCallback cb)
request->set_header("Accept", "*/*");
request->set_header("Host", serverHost_);
auto ret = restbed::Http::async(request, [this,cb,name](const std::shared_ptr<restbed::Request>&,
const std::shared_ptr<restbed::Response>& reply) {
executor_->run([this, request, cb=std::move(cb), name]{
try {
restbed::Http::async(request, [this, cb=std::move(cb), name=std::move(name)]
(const std::shared_ptr<restbed::Request>&,
const std::shared_ptr<restbed::Response>& reply)
{
auto code = reply->get_status_code();
if (code != 200)
JAMI_DBG("Name lookup for %s: got reply code %d", name.c_str(), code);
......@@ -262,14 +267,12 @@ void NameDirectory::lookupName(const std::string& n, LookupCallback cb)
} else {
cb("", Response::error);
}
}).share();
// avoid blocking on future destruction
dht::ThreadPool::io().run([ret](){ ret.get(); });
});
} catch (const std::exception& e) {
JAMI_ERR("Error when performing name lookup: %s", e.what());
cb("", Response::error);
}
});
}
bool NameDirectory::validateName(const std::string& name) const
......@@ -279,7 +282,6 @@ bool NameDirectory::validateName(const std::string& name) const
using Blob = std::vector<uint8_t>;
void NameDirectory::registerName(const std::string& addr, const std::string& n, const std::string& owner, RegistrationCallback cb, const std::string& signedname, const std::string& publickey)
{
try {
std::string name {n};
if (not validateName(name)) {
cb(RegistrationResponse::invalidName);
......@@ -315,8 +317,11 @@ void NameDirectory::registerName(const std::string& addr, const std::string& n,
params->set_connection_timeout(std::chrono::seconds(120));
JAMI_WARN("registerName: sending request %s %s", addr.c_str(), name.c_str());
auto ret = restbed::Http::async(request,
[this,cb,addr,name](const std::shared_ptr<restbed::Request>&,
executor_->run([this, request, params, cb=std::move(cb), addr, name]{
try {
restbed::Http::async(request, [this, cb=std::move(cb), name=std::move(name), addr=std::move(addr)]
(const std::shared_ptr<restbed::Request>&,
const std::shared_ptr<restbed::Response>& reply)
{
auto code = reply->get_status_code();
......@@ -365,14 +370,12 @@ void NameDirectory::registerName(const std::string& addr, const std::string& n,
cb(RegistrationResponse::error);
JAMI_ERR("RegistrationResponse::error");
}
}, params).share();
// avoid blocking on future destruction
dht::ThreadPool::io().run([ret](){ ret.get(); });
}, params);
} catch (const std::exception& e) {
JAMI_ERR("Error when performing name registration: %s", e.what());
cb(RegistrationResponse::error);
}
});
}
void
......
......@@ -23,8 +23,10 @@
#include <map>
#include <string>
#include <mutex>
#include <memory>
namespace dht {
class Executor;
namespace crypto {
struct PublicKey;
}
......@@ -78,6 +80,8 @@ private:
std::map<std::string, std::string> nameCache_ {};
std::map<std::string, std::string> addrCache_ {};
std::shared_ptr<dht::Executor> executor_;
std::string nameCache(const std::string& addr) {
std::lock_guard<std::mutex> l(lock_);
auto cacheRes = nameCache_.find(addr);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment