Skip to content
Snippets Groups Projects
Commit 40cae3db authored by Adrien Béraud's avatar Adrien Béraud
Browse files

namedirectory: use dht executor

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