OpenCPN Partial API docs
mdns_cache.h
1 
2 /***************************************************************************
3  * Copyright (C) 2024 Alec Leamas *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  **************************************************************************/
20 
30 #ifndef MDNS_CACHE_H
31 #define MDNS_CACHE_H
32 
33 #include <mutex>
34 #include <string>
35 #include <vector>
36 
37 class MdnsCache {
38 public:
39  struct Entry {
40  std::string service_instance;
41  std::string hostname;
42  std::string ip;
43  std::string port;
44  Entry(const std::string& service, const std::string host,
45  const std::string& _ip, const std::string _port)
46  : service_instance(service), hostname(host), ip(_ip), port(_port) {}
47  };
48 
49  static MdnsCache& GetInstance();
50 
51  MdnsCache& operator=(MdnsCache&) = delete;
52  MdnsCache(const MdnsCache&) = delete;
53 
55  void Validate();
56 
62  bool Add(const Entry& entry);
63 
69  bool Add(const std::string& service, const std::string& host,
70  const std::string& _ip, const std::string& _port);
71 
77  bool Add(const std::string& _ip, const std::string& _port);
78 
80  const std::vector<Entry>& GetCache() const { return m_cache; }
81 
82 private:
83  mutable std::mutex m_mutex;
84  std::vector<Entry> m_cache;
85 
86  MdnsCache() = default;
87 };
88 
89 #endif // MDNS_CACHE_H
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
const std::vector< Entry > & GetCache() const
Return read-only cached entries reference.
Definition: mdns_cache.h:80