xbmc
Zeroconf.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 "utils/Job.h"
12 
13 #include <map>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
18 class CCriticalSection;
28 class CZeroconf
29 {
30 public:
31 
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,
42  unsigned int f_port,
43  std::vector<std::pair<std::string, std::string> > txt /*= std::vector<std::pair<std::string, std::string> >()*/);
44 
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.
48  //
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);
52 
55  bool RemoveService(const std::string& fcr_identifier);
56 
58  bool HasService(const std::string& fcr_identifier) const;
59 
60  //starts publishing
61  //services that were added with PublishService(...) while Zeroconf wasn't
62  //started, get published now.
63  bool Start();
64 
65  // unpublishes all services (but keeps them stored in this class)
66  // a call to Start() will republish them
67  void Stop();
68 
69  // class methods
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; }
82 
83 protected:
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,
89  unsigned int f_port,
90  const std::vector<std::pair<std::string, std::string> >& txt) = 0;
91 
92  //methods to implement for concrete implementations
93  //update this service
94  virtual bool doForceReAnnounceService(const std::string& fcr_identifier) = 0;
95 
96  //removes the service if published
97  virtual bool doRemoveService(const std::string& fcr_ident) = 0;
98 
99  //removes all services (short hand for "for i in m_service_map doRemoveService(i)")
100  virtual void doStop() = 0;
101 
102  // return true if the zeroconf daemon is running
103  virtual bool IsZCdaemonRunning() { return true; }
104 
105 protected:
106  //singleton: we don't want to get instantiated nor copied or deleted from outside
107  CZeroconf();
108  CZeroconf(const CZeroconf&);
109  virtual ~CZeroconf();
110 
111 private:
112  struct PublishInfo{
113  std::string type;
114  std::string name;
115  unsigned int port;
116  std::vector<std::pair<std::string, std::string> > txt;
117  };
118 
119  //protects data
120  CCriticalSection* mp_crit_sec;
121  typedef std::map<std::string, PublishInfo> tServiceMap;
122  tServiceMap m_service_map;
123  bool m_started = false;
124 
125  static CZeroconf* smp_instance;
126 
127  class CPublish : public CJob
128  {
129  public:
130  CPublish(const std::string& fcr_identifier, const PublishInfo& pubinfo);
131  explicit CPublish(const tServiceMap& servmap);
132 
133  bool DoWork() override;
134 
135  private:
136  tServiceMap m_servmap;
137  };
138 };
bool HasService(const std::string &fcr_identifier) const
returns true if fcr_identifier exists
Definition: Zeroconf.cpp:107
Base class for jobs that are executed asynchronously.
Definition: Job.h:109
bool RemoveService(const std::string &fcr_identifier)
removes the specified service returns false if fcr_identifier does not exist
Definition: Zeroconf.cpp:85
this class provides support for zeroconf while the different zeroconf implementations have asynchrono...
Definition: Zeroconf.h:28