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/>.
26 #include "utils/Job.h"
28 class CCriticalSection;
29 /// this class provides support for zeroconf
30 /// while the different zeroconf implementations have asynchronous APIs
31 /// this class hides it and provides only few ways to interact
32 /// with the services. If more control is needed, feel
33 /// free to add it. The main purpose currently is to provide an easy
34 /// way to publish services in the different StartXXX/StopXXX methods
36 /// TODO: Make me safe for use in static initialization. CritSec is a static member :/
37 /// use e.g. loki's singleton implementation to make do it properly
42 //tries to publish this service via zeroconf
43 //fcr_identifier can be used to stop this service later
44 //fcr_type is the zeroconf service type to publish (e.g. _http._tcp for webserver)
45 //fcr_name is the name of the service to publish. The hostname is currently automatically appended
46 // and used for name collisions. e.g. XBMC would get published as fcr_name@Martn or, after collision fcr_name@Martn-2
47 //f_port port of the service to publish
48 // returns false if fcr_identifier was already present
49 bool PublishService(const std::string& fcr_identifier,
50 const std::string& fcr_type,
51 const std::string& fcr_name,
53 const std::vector<std::pair<std::string, std::string> >& txt);
55 ///removes the specified service
56 ///returns false if fcr_identifier does not exist
57 bool RemoveService(const std::string& fcr_identifier);
59 ///returns true if fcr_identifier exists
60 bool HasService(const std::string& fcr_identifier) const;
63 //services that were added with PublishService(...) while Zeroconf wasn't
64 //started, get published now.
67 // unpublishs all services (but keeps them stored in this class)
68 // a call to Start() will republish them
72 // access to singleton; singleton gets created on call if not existent
73 // if zeroconf is disabled (!HAS_ZEROCONF), this will return a dummy implementation that
74 // just does nothings, otherwise the platform specific one
75 static CZeroconf* GetInstance();
76 // release the singleton; (save to call multiple times)
77 static void ReleaseInstance();
78 // returns false if ReleaseInstance() was called befores
79 static bool IsInstantiated() { return smp_instance != 0; }
80 // win32: process results from the bonjour daemon
81 virtual void ProcessResults() {}
84 //methods to implement for concrete implementations
85 //publishs this service
86 virtual bool doPublishService(const std::string& fcr_identifier,
87 const std::string& fcr_type,
88 const std::string& fcr_name,
90 const std::vector<std::pair<std::string, std::string> >& txt) = 0;
91 //removes the service if published
92 virtual bool doRemoveService(const std::string& fcr_ident) = 0;
94 //removes all services (short hand for "for i in m_service_map doRemoveService(i)")
95 virtual void doStop() = 0;
97 // return true if the zeroconf daemon is running
98 virtual bool IsZCdaemonRunning() { return true; }
101 //singleton: we don't want to get instantiated nor copied or deleted from outside
103 CZeroconf(const CZeroconf&);
104 virtual ~CZeroconf();
111 std::vector<std::pair<std::string, std::string> > txt;
115 CCriticalSection* mp_crit_sec;
116 typedef std::map<std::string, PublishInfo> tServiceMap;
117 tServiceMap m_service_map;
120 //protects singleton creation/destruction
121 static long sm_singleton_guard;
122 static CZeroconf* smp_instance;
124 class CPublish : public CJob
127 CPublish(const std::string& fcr_identifier, const PublishInfo& pubinfo);
128 CPublish(const tServiceMap& servmap);
133 tServiceMap m_servmap;