#include <GeoResolver.hpp>
Public Types | |
| using | ResolvedCallback = std::function<void(const std::string& ip, const GeoEntry&)> |
Public Member Functions | |
| GeoResolver (const GeoResolver &)=delete | |
| GeoResolver & | operator= (const GeoResolver &)=delete |
| std::optional< GeoEntry > | resolve (const std::string &ip) |
| std::optional< GeoEntry > | resolve_self () |
| void | on_resolved (ResolvedCallback cb) |
| void | stop () |
Static Public Member Functions | |
| static GeoResolver & | instance () |
Private Member Functions | |
| GeoResolver () | |
| ~GeoResolver () | |
| void | worker_loop () |
| std::optional< GeoEntry > | fetch_from_ipapi (const std::string &ip) |
Static Private Member Functions | |
| static bool | is_private_ip (const std::string &ip) |
Private Attributes | |
| std::mutex | mu_ |
| std::condition_variable | cv_ |
| std::atomic_bool | running_ {true} |
| std::queue< std::string > | queue_ |
| std::unordered_set< std::string > | in_flight_ |
| std::unordered_map< std::string, GeoEntry > | cache_ |
| std::vector< ResolvedCallback > | callbacks_ |
| std::thread | worker_ |
| std::chrono::steady_clock::time_point | last_request_ {} |
Service singleton che risolve IP -> GeoEntry. Ha un worker thread che processa una coda di lookup; la UI si iscrive via on_resolved(callback) per ricevere i risultati mano mano che arrivano.
Definition at line 51 of file GeoResolver.hpp.
| using bsc::ui_util::GeoResolver::ResolvedCallback = std::function<void(const std::string& ip, const GeoEntry&)> |
Definition at line 53 of file GeoResolver.hpp.
|
delete |
References GeoResolver().
Referenced by GeoResolver(), instance(), and operator=().
|
private |
|
private |
|
private |
Definition at line 174 of file GeoResolver.cpp.
References bsc::ui_util::GeoEntry::asn, bsc::ui_util::GeoEntry::city, bsc::ui_util::GeoEntry::country_code, bsc::ui_util::GeoEntry::country_name, bsc::ui_util::GeoEntry::fetched_at, bsc::ui_util::GeoEntry::isp, last_request_, bsc::ui_util::GeoEntry::lat, bsc::ui_util::GeoEntry::lon, and bsc::ui_util::GeoEntry::region.
Referenced by operator=(), and worker_loop().
|
static |
Definition at line 77 of file GeoResolver.cpp.
References GeoResolver().
Referenced by NetworkPage::NetworkPage(), NetworkPage::noteConnection(), and NetworkPage::requestSelfLocation().
|
staticprivate |
Definition at line 95 of file GeoResolver.cpp.
References AF_INET, and AF_INET6.
Referenced by operator=(), and resolve().
| void bsc::ui_util::GeoResolver::on_resolved | ( | ResolvedCallback | cb | ) |
Registra un callback per i risultati asincroni. Thread-safe.
Definition at line 169 of file GeoResolver.cpp.
References callbacks_, and mu_.
Referenced by NetworkPage::NetworkPage(), and operator=().
|
delete |
References fetch_from_ipapi(), GeoResolver(), is_private_ip(), on_resolved(), resolve(), resolve_self(), stop(), and worker_loop().
| std::optional< GeoEntry > bsc::ui_util::GeoResolver::resolve | ( | const std::string & | ip | ) |
Se l'IP e' gia' in cache ritorna subito la GeoEntry. Altrimenti ritorna nullopt e mette l'IP in coda: il callback registrato via on_resolved sara' chiamato quando il lookup completa.
Gli IP privati/loopback/link-local non vengono mai risolti (ritorno un GeoEntry "locale" con country_code = "" e lat/lon = 0).
Definition at line 121 of file GeoResolver.cpp.
References cache_, bsc::ui_util::GeoEntry::country_code, bsc::ui_util::GeoEntry::country_name, cv_, bsc::ui_util::GeoEntry::fetched_at, in_flight_, is_private_ip(), mu_, and queue_.
Referenced by operator=().
| std::optional< GeoEntry > bsc::ui_util::GeoResolver::resolve_self | ( | ) |
Risolve l'IP pubblico del PC locale e la sua geolocalizzazione. Chiamando ip-api.com senza IP, il servizio geolocalizza il chiamante. Il risultato viene cached come qualsiasi altro IP, sotto la chiave speciale "__self__".
Ritorna subito la cache se presente, altrimenti mette in coda e ritorna nullopt. Il callback on_resolved() sara' chiamato con ip == "__self__" quando la risoluzione completa.
Definition at line 146 of file GeoResolver.cpp.
References cache_, cv_, in_flight_, mu_, and queue_.
Referenced by operator=().
| void bsc::ui_util::GeoResolver::stop | ( | ) |
Shutdown ordinato del worker thread.
Definition at line 89 of file GeoResolver.cpp.
References cv_, running_, and worker_.
Referenced by operator=(), and ~GeoResolver().
|
private |
Definition at line 211 of file GeoResolver.cpp.
References cache_, callbacks_, cv_, fetch_from_ipapi(), in_flight_, mu_, queue_, and running_.
Referenced by GeoResolver(), and operator=().
|
private |
Definition at line 101 of file GeoResolver.hpp.
Referenced by resolve(), resolve_self(), and worker_loop().
|
private |
Definition at line 102 of file GeoResolver.hpp.
Referenced by on_resolved(), and worker_loop().
|
private |
Definition at line 97 of file GeoResolver.hpp.
Referenced by resolve(), resolve_self(), stop(), and worker_loop().
|
private |
Definition at line 100 of file GeoResolver.hpp.
Referenced by resolve(), resolve_self(), and worker_loop().
|
private |
Definition at line 106 of file GeoResolver.hpp.
Referenced by fetch_from_ipapi().
|
private |
Definition at line 96 of file GeoResolver.hpp.
Referenced by on_resolved(), resolve(), resolve_self(), and worker_loop().
|
private |
Definition at line 99 of file GeoResolver.hpp.
Referenced by resolve(), resolve_self(), and worker_loop().
|
private |
Definition at line 98 of file GeoResolver.hpp.
Referenced by stop(), and worker_loop().
|
private |
Definition at line 103 of file GeoResolver.hpp.
Referenced by GeoResolver(), and stop().