[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / network / Zeroconf.h
1 #pragma once
2
3 /*
4  *      Copyright (C) 2005-2013 Team XBMC
5  *      http://www.xbmc.org
6  *
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)
10  *  any later version.
11  *
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.
16  *
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/>.
20  *
21  */
22
23 #include <string>
24 #include <map>
25 #include <vector>
26 #include "utils/Job.h"
27
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
35 /// in CApplication
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
38 class CZeroconf
39 {
40 public:
41
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,
52                       unsigned int f_port,
53                       const std::vector<std::pair<std::string, std::string> >& txt);
54
55   ///removes the specified service
56   ///returns false if fcr_identifier does not exist
57   bool RemoveService(const std::string& fcr_identifier);
58
59   ///returns true if fcr_identifier exists
60   bool HasService(const std::string& fcr_identifier) const;
61
62   //starts publishing
63   //services that were added with PublishService(...) while Zeroconf wasn't
64   //started, get published now.
65   void Start();
66
67   // unpublishs all services (but keeps them stored in this class)
68   // a call to Start() will republish them
69   void Stop();
70
71   // class methods
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() {}
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   //removes the service if published
92   virtual bool doRemoveService(const std::string& fcr_ident) = 0;
93
94   //removes all services (short hand for "for i in m_service_map doRemoveService(i)")
95   virtual void doStop() = 0;
96
97   // return true if the zeroconf daemon is running
98   virtual bool IsZCdaemonRunning() { return  true; }
99
100 protected:
101   //singleton: we don't want to get instantiated nor copied or deleted from outside
102   CZeroconf();
103   CZeroconf(const CZeroconf&);
104   virtual ~CZeroconf();
105
106 private:
107   struct PublishInfo{
108     std::string type;
109     std::string name;
110     unsigned int port;
111     std::vector<std::pair<std::string, std::string> > txt;
112   };
113
114   //protects data
115   CCriticalSection* mp_crit_sec;
116   typedef std::map<std::string, PublishInfo> tServiceMap;
117   tServiceMap m_service_map;
118   bool m_started;
119
120   //protects singleton creation/destruction
121   static long sm_singleton_guard;
122   static CZeroconf* smp_instance;
123
124   class CPublish : public CJob
125   {
126   public:
127     CPublish(const std::string& fcr_identifier, const PublishInfo& pubinfo);
128     CPublish(const tServiceMap& servmap);
129
130     bool DoWork();
131
132   private:
133     tServiceMap m_servmap;
134   };
135 };