kodi
ZeroconfBrowser.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include <string>
12 #include <set>
13 #include <vector>
14 #include <map>
15 
16 #ifdef TARGET_WINDOWS
17 #undef SetPort // WIN32INCLUDES this is defined as SetPortA in WinSpool.h which is being included _somewhere_
18 #endif
19 
20 //forwards
21 class CCriticalSection;
22 
25 {
26 public:
28  {
29  public:
30  typedef std::map<std::string, std::string> tTxtRecordMap;
31 
32  ZeroconfService() = default;
33  ZeroconfService(const std::string& fcr_name, const std::string& fcr_type, const std::string& fcr_domain);
34 
37  static std::string toPath(const ZeroconfService& fcr_service);
38  static ZeroconfService fromPath(const std::string& fcr_path); //throws std::runtime_error on failure
40 
43  void SetName(const std::string& fcr_name);
44  const std::string& GetName() const {return m_name;}
45 
46  void SetType(const std::string& fcr_type);
47  const std::string& GetType() const {return m_type;}
48 
49  void SetDomain(const std::string& fcr_domain);
50  const std::string& GetDomain() const {return m_domain;}
52 
55  void SetIP(const std::string& fcr_ip);
56  const std::string& GetIP() const {return m_ip;}
57 
58  void SetHostname(const std::string& fcr_hostname);
59  const std::string& GetHostname() const {return m_hostname;}
60 
61  void SetPort(int f_port);
62  int GetPort() const {return m_port;}
63 
64  void SetTxtRecords(const tTxtRecordMap& txt_records);
65  const tTxtRecordMap& GetTxtRecords() const { return m_txtrecords_map;}
67  private:
68  //3 entries below identify a service
69  std::string m_name;
70  std::string m_type;
71  std::string m_domain;
72 
73  //2 entries below store 1 ip:port pair for this service
74  std::string m_ip;
75  int m_port = 0;
76 
77  //used for mdns in case dns resolution fails
78  //we store the hostname and resolve with mdns functions again
79  std::string m_hostname;
80 
81  //1 entry below stores the txt-record as a key value map for this service
82  tTxtRecordMap m_txtrecords_map;
83  };
84 
85  // starts browsing
86  void Start();
87 
88  // stops browsing
89  void Stop();
90 
94  std::vector<ZeroconfService> GetFoundServices();
96 
97  // resolves a ZeroconfService to ip + port
98  // @param fcr_service the service to resolve
99  // @param f_timeout timeout in seconds for resolving
100  // the protocol part of CURL is the raw zeroconf service type
101  // added with AddServiceType (== needs further processing! e.g. _smb._tcp -> smb)
102  // @return true if it was successfully resolved (or scheduled), false if resolve
103  // failed (async or not)
104  bool ResolveService(ZeroconfService& fr_service, double f_timeout = 1.0);
105 
106  // class methods
107  // access to singleton; singleton gets created on call if not existent
108  // if zeroconf is disabled (!HAS_ZEROCONF), this will return a dummy implementation that
109  // just does nothings, otherwise the platform specific one
110  static CZeroconfBrowser* GetInstance();
111  // release the singleton; (save to call multiple times)
112  static void ReleaseInstance();
113  // returns false if ReleaseInstance() was called before
114  static bool IsInstantiated() { return smp_instance != 0; }
115 
116  virtual void ProcessResults() {}
117 
123  bool AddServiceType(const std::string& fcr_service_type);
124 
128  bool RemoveServiceType(const std::string& fcr_service_type);
129 
130 protected:
131  //singleton: we don't want to get instantiated nor copied or deleted from outside
133  CZeroconfBrowser(const CZeroconfBrowser&) = delete;
134  CZeroconfBrowser& operator=(const CZeroconfBrowser&) = delete;
135  virtual ~CZeroconfBrowser();
136 
137  // pure virtual methods to implement for OS specific implementations
138  virtual bool doAddServiceType(const std::string& fcr_service_type) = 0;
139  virtual bool doRemoveServiceType(const std::string& fcr_service_type) = 0;
140  virtual std::vector<ZeroconfService> doGetFoundServices() = 0;
141  virtual bool doResolveService(ZeroconfService& fr_service, double f_timeout) = 0;
142 
143 private:
144  struct ServiceInfo
145  {
146  std::string type;
147  };
148 
149  //protects data
150  CCriticalSection* mp_crit_sec;
151  typedef std::set<std::string> tServices;
152  tServices m_services;
153  bool m_started = false;
154 
155  static CZeroconfBrowser* smp_instance;
156 };
157 #include <iostream>
158 
159 //debugging helper
160 inline std::ostream& operator<<(std::ostream& o, const CZeroconfBrowser::ZeroconfService& service){
161  o << "(" << service.GetName() << "|" << service.GetType() << "|" << service.GetDomain() << ")";
162  return o;
163 }
164 
165 //inline methods
166 inline bool operator<(CZeroconfBrowser::ZeroconfService const& fcr_lhs, CZeroconfBrowser::ZeroconfService const& fcr_rhs)
167 {
168  return (fcr_lhs.GetName() + fcr_lhs.GetType() + fcr_lhs.GetDomain() < fcr_rhs.GetName() + fcr_rhs.GetType() + fcr_rhs.GetDomain());
169 }
170 
171 inline bool operator==(CZeroconfBrowser::ZeroconfService const& fcr_lhs, CZeroconfBrowser::ZeroconfService const& fcr_rhs)
172 {
173  return (fcr_lhs.GetName() == fcr_rhs.GetName() && fcr_lhs.GetType() == fcr_rhs.GetType() && fcr_lhs.GetDomain() == fcr_rhs.GetDomain() );
174 }
bool RemoveServiceType(const std::string &fcr_service_type)
remove the specified service from discovery
Definition: ZeroconfBrowser.cpp:102
bool AddServiceType(const std::string &fcr_service_type)
methods for browsing and getting results of itadds a service type for browsing
Definition: ZeroconfBrowser.cpp:87
std::vector< ZeroconfService > GetFoundServices()
returns the list of found services if this is updated, the following message with "zeroconf://" as pa...
Definition: ZeroconfBrowser.cpp:114
static std::string toPath(const ZeroconfService &fcr_service)
easy conversion to string and back (used in czeronfdiretory to store this service) ...
Definition: ZeroconfBrowser.cpp:224
void SetIP(const std::string &fcr_ip)
access methods needed during resolve
Definition: ZeroconfBrowser.cpp:203
Definition: ZeroconfBrowser.h:27
void SetName(const std::string &fcr_name)
general access methods
Definition: ZeroconfBrowser.cpp:177
this class provides support for zeroconf browsing
Definition: ZeroconfBrowser.h:24