9#include "absl/status/status.h"
10#include "absl/strings/str_cat.h"
11#include "asio/post.hpp"
16 : io_(io), fetch_(std::move(fetch)), timer_(io) {}
25 timer_.expires_after(std::chrono::seconds(1));
26 timer_.async_wait([
this](std::error_code error) {
33void WorldList::Ask() {
39 asio::post(thread_, [
this] {
40 absl::StatusOr<std::vector<ListedWorld>> worlds = fetch_();
41 asio::post(io_, [
this, worlds = std::move(worlds)]()
mutable {
43 Take(std::move(worlds), Clock::now());
49 Clock::time_point at) {
51 std::cerr <<
"nanommo gateway: couldn't list the worlds: "
52 << worlds.status() <<
"\n";
55 worlds_ = *std::move(worlds);
61 Clock::time_point now)
const {
62 if (!fetched_ || now - *fetched_ >
kStale) {
63 return absl::UnavailableError(
"the gateway has no recent list of worlds");
66 if (world.public_host == public_host && world.port == port) {
70 return absl::NotFoundError(
71 absl::StrCat(
"no world at ", public_host,
":", port));
WorldList(asio::io_context &io, Fetch fetch)
absl::StatusOr< std::string > Find(std::string_view public_host, uint16_t port, Clock::time_point now) const
void Take(absl::StatusOr< std::vector< ListedWorld > > worlds, Clock::time_point at)
~WorldList()
Destroy it once the io_context has stopped.
std::function< absl::StatusOr< std::vector< ListedWorld > >()> Fetch
void Start()
Asks for the list now, and every second after.
static constexpr std::chrono::seconds kStale
A list this old says nothing about now, so every world is refused.