2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
11 #include "utils/Job.h"
18 class CCriticalSection
;
19 /// this class provides support for zeroconf
20 /// while the different zeroconf implementations have asynchronous APIs
21 /// this class hides it and provides only few ways to interact
22 /// with the services. If more control is needed, feel
23 /// free to add it. The main purpose currently is to provide an easy
24 /// way to publish services in the different StartXXX/StopXXX methods
26 //! @todo Make me safe for use in static initialization. CritSec is a static member :/
27 //! use e.g. loki's singleton implementation to make do it properly
32 //tries to publish this service via zeroconf
33 //fcr_identifier can be used to stop or reannounce this service later
34 //fcr_type is the zeroconf service type to publish (e.g. _http._tcp for webserver)
35 //fcr_name is the name of the service to publish. The hostname is currently automatically appended
36 // and used for name collisions. e.g. XBMC would get published as fcr_name@Martn or, after collision fcr_name@Martn-2
37 //f_port port of the service to publish
38 // returns false if fcr_identifier was already present
39 bool PublishService(const std::string
& fcr_identifier
,
40 const std::string
& fcr_type
,
41 const std::string
& fcr_name
,
43 std::vector
<std::pair
<std::string
, std::string
> > txt
/*= std::vector<std::pair<std::string, std::string> >()*/);
45 //tries to rebroadcast that service on the network without removing/readding
46 //this can be achieved by changing a fake txt record. Implementations should
47 //implement it by doing so.
49 //fcr_identifier - the identifier of the already published service which should be reannounced
50 // returns true on successful reannonuce - false if this service isn't published yet
51 bool ForceReAnnounceService(const std::string
& fcr_identifier
);
53 ///removes the specified service
54 ///returns false if fcr_identifier does not exist
55 bool RemoveService(const std::string
& fcr_identifier
);
57 ///returns true if fcr_identifier exists
58 bool HasService(const std::string
& fcr_identifier
) const;
61 //services that were added with PublishService(...) while Zeroconf wasn't
62 //started, get published now.
65 // unpublishes all services (but keeps them stored in this class)
66 // a call to Start() will republish them
70 // access to singleton; singleton gets created on call if not existent
71 // if zeroconf is disabled (!HAS_ZEROCONF), this will return a dummy implementation that
72 // just does nothings, otherwise the platform specific one
73 static CZeroconf
* GetInstance();
74 // release the singleton; (save to call multiple times)
75 static void ReleaseInstance();
76 // returns false if ReleaseInstance() was called before
77 static bool IsInstantiated() { return smp_instance
!= 0; }
78 // win32: process results from the bonjour daemon
79 virtual void ProcessResults() {}
80 // returns if the service is started and services are announced
81 bool IsStarted() { return m_started
; }
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;
92 //methods to implement for concrete implementations
94 virtual bool doForceReAnnounceService(const std::string
& fcr_identifier
) = 0;
96 //removes the service if published
97 virtual bool doRemoveService(const std::string
& fcr_ident
) = 0;
99 //removes all services (short hand for "for i in m_service_map doRemoveService(i)")
100 virtual void doStop() = 0;
102 // return true if the zeroconf daemon is running
103 virtual bool IsZCdaemonRunning() { return true; }
106 //singleton: we don't want to get instantiated nor copied or deleted from outside
108 CZeroconf(const CZeroconf
&);
109 virtual ~CZeroconf();
116 std::vector
<std::pair
<std::string
, std::string
> > txt
;
120 CCriticalSection
* mp_crit_sec
;
121 typedef std::map
<std::string
, PublishInfo
> tServiceMap
;
122 tServiceMap m_service_map
;
123 bool m_started
= false;
125 static CZeroconf
* smp_instance
;
127 class CPublish
: public CJob
130 CPublish(const std::string
& fcr_identifier
, const PublishInfo
& pubinfo
);
131 explicit CPublish(const tServiceMap
& servmap
);
133 bool DoWork() override
;
136 tServiceMap m_servmap
;