24 #include <curl/curl.h>
26 #include "model/logger.h"
27 #include "model/mdns_cache.h"
33 static bool Ping(
const std::string& url,
long port = 8443L) {
34 CURL* c = curl_easy_init();
35 curl_easy_setopt(c, CURLOPT_URL, url.c_str());
36 curl_easy_setopt(c, CURLOPT_PORT, port);
37 curl_easy_setopt(c, CURLOPT_TIMEOUT_MS, 2000L);
38 CURLcode result = curl_easy_perform(c);
40 bool ok = result == CURLE_RECV_ERROR || result == CURLE_OK;
41 DEBUG_LOG <<
"Checked mdns host: " << url <<
": "
42 << (ok ?
"ok" : curl_easy_strerror(result));
52 std::unique_lock lock(m_mutex);
53 auto found = std::find_if(m_cache.begin(), m_cache.end(),
54 [entry](
Entry& e) { return e.ip == entry.ip; });
55 DEBUG_LOG <<
"Added mdns cache entry, ip: " << entry.ip
56 <<
", status: " << (found == m_cache.end() ?
"true" :
"false");
57 if (found != m_cache.end())
return false;
58 m_cache.push_back(entry);
63 const std::string& _ip,
const std::string& _port) {
64 return Add(
Entry(service, host, _ip, _port));
68 return Add(
Entry(
"opencpn",
"unknown", _ip, _port));
72 std::unique_lock lock(m_mutex);
73 for (
auto it = m_cache.begin(); it != m_cache.end();) {
Singleton cache for hosts looked up using mdns.
bool Add(const Entry &entry)
Add new entry to the cache.
void Validate()
Check that all entries are accessible, remove stale ones.