diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp index d4b48b81e0d0500c9ad08c4855dfa35c88ca899e..ae0555a073e27cef106a87c355930981733bab0e 100644 --- a/src/jamidht/jamiaccount.cpp +++ b/src/jamidht/jamiaccount.cpp @@ -2127,37 +2127,10 @@ JamiAccount::doRegister_() }); #endif - auto currentDhtStatus = std::make_shared<dht::NodeStatus>(dht::NodeStatus::Disconnected); - dht_.setOnStatusChanged([this, currentDhtStatus](dht::NodeStatus s4, dht::NodeStatus s6) { - JAMI_DBG("[Account %s] Dht status : IPv4 %s; IPv6 %s", getAccountID().c_str(), dhtStatusStr(s4), dhtStatusStr(s6)); - RegistrationState state; - auto newStatus = std::max(s4, s6); - if (newStatus == *currentDhtStatus) - return; - switch (newStatus) { - case dht::NodeStatus::Connecting: - JAMI_WARN("[Account %s] connecting to the DHT network...", getAccountID().c_str()); - state = RegistrationState::TRYING; - break; - case dht::NodeStatus::Connected: - JAMI_WARN("[Account %s] connected to the DHT network", getAccountID().c_str()); - state = RegistrationState::REGISTERED; - break; - case dht::NodeStatus::Disconnected: - JAMI_WARN("[Account %s] disconnected from the DHT network", getAccountID().c_str()); - state = RegistrationState::UNREGISTERED; - break; - default: - state = RegistrationState::ERROR_GENERIC; - break; - } - *currentDhtStatus = newStatus; - setRegistrationState(state); - }); - dht::DhtRunner::Config config {}; config.dht_config.node_config.network = 0; config.dht_config.node_config.maintain_storage = false; + config.dht_config.node_config.persist_path = cachePath_+DIR_SEPARATOR_STR "dhtstate"; config.dht_config.id = identity_; config.proxy_server = getDhtProxyServer(); config.push_node_id = getAccountID(); @@ -2172,16 +2145,7 @@ JamiAccount::doRegister_() dht_.setPushNotificationToken(deviceKey_); } - dht_.run((in_port_t)dhtPortUsed_, config); - - dht_.setLocalCertificateStore([](const dht::InfoHash& pk_id) { - std::vector<std::shared_ptr<dht::crypto::Certificate>> ret; - if (auto cert = tls::CertificateStore::instance().getCertificate(pk_id.toString())) - ret.emplace_back(std::move(cert)); - JAMI_DBG("Query for local certificate store: %s: %zu found.", pk_id.toString().c_str(), ret.size()); - return ret; - }); - + dht::DhtRunner::Context context {}; auto dht_log_level = Manager::instance().dhtLogLevel.load(); if (dht_log_level > 0) { static auto silent = [](char const* /*m*/, va_list /*args*/) {}; @@ -2189,7 +2153,7 @@ JamiAccount::doRegister_() static auto log_warn = [](char const* m, va_list args) { Logger::vlog(LOG_WARNING, nullptr, 0, true, m, args); }; static auto log_debug = [](char const* m, va_list args) { Logger::vlog(LOG_DEBUG, nullptr, 0, true, m, args); }; #ifndef _MSC_VER - dht_.setLoggers( + context.logger = std::make_unique<dht::Logger>( log_error, (dht_log_level > 1) ? log_warn : silent, (dht_log_level > 2) ? log_debug : silent); @@ -2200,23 +2164,56 @@ JamiAccount::doRegister_() auto now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); jami::emitSignal<DRing::DebugSignal::MessageSend>(std::to_string(now) + " " + std::string(tmp)); }; - dht_.setLoggers(log_all, log_all, silent); + context.logger = std::make_unique<dht::Logger>(log_all, log_all, silent); #else if (dht_log_level > 2) { - dht_.setLoggers(log_error, log_warn, log_debug); + context.logger = std::make_unique<dht::Logger>(log_error, log_warn, log_debug); } else if (dht_log_level > 1) { - dht_.setLoggers(log_error, log_warn, silent); + context.logger = std::make_unique<dht::Logger>(log_error, log_warn, silent); } else { - dht_.setLoggers(log_error, silent, silent); + context.logger = std::make_unique<dht::Logger>(log_error, silent, silent); } #endif } + context.certificateStore = [](const dht::InfoHash& pk_id) { + std::vector<std::shared_ptr<dht::crypto::Certificate>> ret; + if (auto cert = tls::CertificateStore::instance().getCertificate(pk_id.toString())) + ret.emplace_back(std::move(cert)); + JAMI_DBG("Query for local certificate store: %s: %zu found.", pk_id.toString().c_str(), ret.size()); + return ret; + }; - dht_.importValues(loadValues()); + auto currentDhtStatus = std::make_shared<dht::NodeStatus>(dht::NodeStatus::Disconnected); + context.statusChangedCallback = [this, currentDhtStatus](dht::NodeStatus s4, dht::NodeStatus s6) { + JAMI_DBG("[Account %s] Dht status : IPv4 %s; IPv6 %s", getAccountID().c_str(), dhtStatusStr(s4), dhtStatusStr(s6)); + RegistrationState state; + auto newStatus = std::max(s4, s6); + if (newStatus == *currentDhtStatus) + return; + switch (newStatus) { + case dht::NodeStatus::Connecting: + JAMI_WARN("[Account %s] connecting to the DHT network...", getAccountID().c_str()); + state = RegistrationState::TRYING; + break; + case dht::NodeStatus::Connected: + JAMI_WARN("[Account %s] connected to the DHT network", getAccountID().c_str()); + state = RegistrationState::REGISTERED; + break; + case dht::NodeStatus::Disconnected: + JAMI_WARN("[Account %s] disconnected from the DHT network", getAccountID().c_str()); + state = RegistrationState::UNREGISTERED; + break; + default: + state = RegistrationState::ERROR_GENERIC; + break; + } + *currentDhtStatus = newStatus; + setRegistrationState(state); + }; setRegistrationState(RegistrationState::TRYING); + dht_.run((in_port_t)dhtPortUsed_, config, std::move(context)); - dht_.bootstrap(loadNodes()); auto bootstrap = loadBootstrap(); if (not bootstrap.empty()) dht_.bootstrap(bootstrap); @@ -2606,6 +2603,10 @@ JamiAccount::doUnregister(std::function<void(bool)> released_cb) } JAMI_WARN("[Account %s] unregistering account %p", getAccountID().c_str(), this); + dht_.shutdown([this](){ + JAMI_WARN("[Account %s] dht shutdown complete", getAccountID().c_str()); + }); + { std::lock_guard<std::mutex> lock(callsMutex_); pendingCalls_.clear(); @@ -2618,8 +2619,6 @@ JamiAccount::doUnregister(std::function<void(bool)> released_cb) upnp_->removeMappings(); } - saveNodes(dht_.exportNodes()); - saveValues(dht_.exportValues()); dht_.join(); lock.unlock();