navid/veins / b8f7327bf194b843bd36e1a9bd5d6d3d5d123eea3f36bf4e780cc97678358890 / xveins/src/promisor.cpp
promisor.cpp
← xveins/src · 1540 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