OpenCPN Partial API docs
mdns_cache.cpp
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright (C) 2024 Alec Leamas *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 2 of the License, or *
7  * (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License *
15  * along with this program; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18  **************************************************************************/
19 
22 #include <algorithm>
23 
24 #include <curl/curl.h>
25 
26 #include "model/logger.h"
27 #include "model/mdns_cache.h"
28 
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);
39  curl_easy_cleanup(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));
43  return ok;
44 }
45 
46 MdnsCache& MdnsCache::GetInstance() {
47  static MdnsCache mdns_cache;
48  return mdns_cache;
49 }
50 
51 bool MdnsCache::Add(const Entry& entry) {
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);
59  return true;
60 }
61 
62 bool MdnsCache::Add(const std::string& service, const std::string& host,
63  const std::string& _ip, const std::string& _port) {
64  return Add(Entry(service, host, _ip, _port));
65 }
66 
67 bool MdnsCache::Add(const std::string& _ip, const std::string& _port) {
68  return Add(Entry("opencpn", "unknown", _ip, _port));
69 }
70 
72  std::unique_lock lock(m_mutex);
73  for (auto it = m_cache.begin(); it != m_cache.end();) {
74  if (!Ping(it->ip)) {
75  m_cache.erase(it);
76  } else {
77  it++;
78  }
79  }
80 }
Singleton cache for hosts looked up using mdns.
Definition: mdns_cache.h:37
bool Add(const Entry &entry)
Add new entry to the cache.
Definition: mdns_cache.cpp:51
void Validate()
Check that all entries are accessible, remove stale ones.
Definition: mdns_cache.cpp:71