4 * Copyright (C) 2005-2013 Team XBMC
7 * This Program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This Program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with XBMC; see the file COPYING. If not, see
19 * <http://www.gnu.org/licenses/>.
28 #include "utils/StdString.h"
31 #undef SetPort // WIN32INCLUDES this is defined as SetPortA in WinSpool.h which is being included _somewhere_
35 class CCriticalSection;
37 /// this class provides support for zeroconf browsing
38 class CZeroconfBrowser
44 typedef std::map<std::string, std::string> tTxtRecordMap;
47 ZeroconfService(const CStdString& fcr_name, const CStdString& fcr_type, const CStdString& fcr_domain);
49 /// easy conversion to string and back (used in czeronfdiretory to store this service)
51 static CStdString toPath(const ZeroconfService& fcr_service);
52 static ZeroconfService fromPath(const CStdString& fcr_path); //throws std::runtime_error on failure
55 /// general access methods
57 void SetName(const CStdString& fcr_name);
58 const CStdString& GetName() const {return m_name;}
60 void SetType(const CStdString& fcr_type);
61 const CStdString& GetType() const {return m_type;}
63 void SetDomain(const CStdString& fcr_domain);
64 const CStdString& GetDomain() const {return m_domain;}
67 /// access methods needed during resolve
69 void SetIP(const CStdString& fcr_ip);
70 const CStdString& GetIP() const {return m_ip;}
72 void SetPort(int f_port);
73 int GetPort() const {return m_port;}
75 void SetTxtRecords(const tTxtRecordMap& txt_records);
76 const tTxtRecordMap& GetTxtRecords() const { return m_txtrecords_map;}
79 //3 entries below identify a service
84 //2 entries below store 1 ip:port pair for this service
88 //1 entry below stores the txt-record as a key value map for this service
89 tTxtRecordMap m_txtrecords_map;
98 ///returns the list of found services
99 /// if this is updated, the following message with "zeroconf://" as path is sent:
100 /// CGUIMessage message(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE_PATH);
101 std::vector<ZeroconfService> GetFoundServices();
104 // resolves a ZeroconfService to ip + port
105 // @param fcr_service the service to resolve
106 // @param f_timeout timeout in seconds for resolving
107 // the protocol part of CURL is the raw zeroconf service type
108 // added with AddServiceType (== needs further processing! e.g. _smb._tcp -> smb)
109 // @return true if it was successfully resolved (or scheduled), false if resolve
110 // failed (async or not)
111 bool ResolveService(ZeroconfService& fr_service, double f_timeout = 1.0);
114 // access to singleton; singleton gets created on call if not existent
115 // if zeroconf is disabled (!HAS_ZEROCONF), this will return a dummy implementation that
116 // just does nothings, otherwise the platform specific one
117 static CZeroconfBrowser* GetInstance();
118 // release the singleton; (save to call multiple times)
119 static void ReleaseInstance();
120 // returns false if ReleaseInstance() was called befores
121 static bool IsInstantiated() { return smp_instance != 0; }
123 virtual void ProcessResults() {}
126 // pure virtual methods to implement for OS specific implementations
127 virtual bool doAddServiceType(const CStdString& fcr_service_type) = 0;
128 virtual bool doRemoveServiceType(const CStdString& fcr_service_type) = 0;
129 virtual std::vector<ZeroconfService> doGetFoundServices() = 0;
130 virtual bool doResolveService(ZeroconfService& fr_service, double f_timeout) = 0;
133 //singleton: we don't want to get instantiated nor copied or deleted from outside
135 CZeroconfBrowser(const CZeroconfBrowser&);
136 virtual ~CZeroconfBrowser();
139 /// methods for browsing and getting results of it
141 /// adds a service type for browsing
142 /// @param fcr_service_type the service type as string, e.g. _smb._tcp.
143 /// @return false if it was already there
144 bool AddServiceType(const CStdString& fcr_service_type);
146 /// remove the specified service from discovery
147 /// @param fcr_service_type the service type as string, e.g. _smb._tcp.
148 /// @return if it was not found
149 bool RemoveServiceType(const CStdString& fcr_service_type);
157 CCriticalSection* mp_crit_sec;
158 typedef std::set<CStdString> tServices;
159 tServices m_services;
162 //protects singleton creation/destruction
163 static long sm_singleton_guard;
164 static CZeroconfBrowser* smp_instance;
169 inline std::ostream& operator<<(std::ostream& o, const CZeroconfBrowser::ZeroconfService& service){
170 o << "(" << service.GetName() << "|" << service.GetType() << "|" << service.GetDomain() << ")";
175 inline bool operator<(CZeroconfBrowser::ZeroconfService const& fcr_lhs, CZeroconfBrowser::ZeroconfService const& fcr_rhs)
177 return (fcr_lhs.GetName() + fcr_lhs.GetType() + fcr_lhs.GetDomain() < fcr_rhs.GetName() + fcr_rhs.GetType() + fcr_rhs.GetDomain());
180 inline bool operator==(CZeroconfBrowser::ZeroconfService const& fcr_lhs, CZeroconfBrowser::ZeroconfService const& fcr_rhs)
182 return (fcr_lhs.GetName() == fcr_rhs.GetName() && fcr_lhs.GetType() == fcr_rhs.GetType() && fcr_lhs.GetDomain() == fcr_rhs.GetDomain() );