navid/veins / 89aa02ed6b6660b6cf09e1a7700cf3b020a1b1e2bd33fd14dace8eb1a92db071 / xveins/src/promisor.cpp
promisor.cpp
← xveins/src · 1447 bytes · raw · download
#include "promisor.h"
#include "protocol.h"
#include "store.h"
#include <optional>
#include <string>
#include <vector>
namespace vn {
namespace {
std::string remote_field(std::string_view remote, std::string_view field) {
std::string out = "remote.";
out += remote;
out.push_back('.');
out += field;
return out;
}
bool fetch_missing_chunk(const fs::path &root, const Id &id) {
static thread_local bool active = false;
if (active)
return false;
active = true;
struct Guard {
bool &flag;
~Guard() { flag = false; }
} guard{active};
auto promisor = repo_config_value(root, "promisor");
if (!promisor || promisor->empty())
return false;
auto url = repo_config_value(root, remote_field(*promisor, "url"));
if (!url || url->empty())
return false;
auto key = repo_config_value(root, remote_field(*promisor, "key"));
auto pin = repo_config_value(root, remote_field(*promisor, "tls-pin"));
const std::string repo_name = proto::url_repo_name(*url);
auto client = proto::Client::connect(*url, key ? *key : std::string(),
repo_name, pin ? *pin : std::string());
if (!client)
return false;
const std::vector<Id> ids = {id};
return client->get_chunks(root, ids);
}
} // namespace
void install_promisor_fetcher() {
set_promisor_fetcher([](const fs::path &root, const Id &id) {
return fetch_missing_chunk(root, id);
});
}
} // namespace vn