MPD  0.20.18
Discovery.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef _UPNPPDISC_H_X_INCLUDED_
21 #define _UPNPPDISC_H_X_INCLUDED_
22 
23 #include "Compat.hxx"
24 #include "Callback.hxx"
25 #include "Device.hxx"
26 #include "WorkQueue.hxx"
27 #include "thread/Mutex.hxx"
28 #include "Compiler.h"
29 
30 #include <upnp.h>
31 
32 #include <list>
33 #include <vector>
34 #include <string>
35 #include <memory>
36 #include <chrono>
37 
38 #if UPNP_VERSION < 10800
39 #define UpnpDiscovery Upnp_Discovery
40 #endif
41 
43 
45 public:
46  virtual void FoundUPnP(const ContentDirectoryService &service) = 0;
47  virtual void LostUPnP(const ContentDirectoryService &service) = 0;
48 };
49 
62  struct DiscoveredTask {
63  std::string url;
64  std::string device_id;
65  std::chrono::steady_clock::duration expires;
66 
67  DiscoveredTask(const UpnpDiscovery *disco)
69  device_id(UpnpDiscovery_get_DeviceID_cstr(disco)),
70  expires(std::chrono::seconds(UpnpDiscovery_get_Expires(disco))) {}
71  };
72 
77  class ContentDirectoryDescriptor {
78  public:
79  std::string id;
80 
81  UPnPDevice device;
82 
86  std::chrono::steady_clock::time_point expires;
87 
88  ContentDirectoryDescriptor() = default;
89 
90  ContentDirectoryDescriptor(std::string &&_id,
91  std::chrono::steady_clock::time_point last,
92  std::chrono::steady_clock::duration exp)
93  :id(std::move(_id)),
94  expires(last + exp + std::chrono::seconds(20)) {}
95 
96  void Parse(const std::string &url, const char *description) {
97  device.Parse(url, description);
98  }
99  };
100 
101  const UpnpClient_Handle handle;
102  UPnPDiscoveryListener *const listener;
103 
104  Mutex mutex;
105  std::list<ContentDirectoryDescriptor> directories;
107 
113  int search_timeout = 2;
114 
118  std::chrono::steady_clock::time_point last_search = std::chrono::steady_clock::time_point();
119 
120 public:
121  UPnPDeviceDirectory(UpnpClient_Handle _handle,
122  UPnPDiscoveryListener *_listener=nullptr);
124 
125  UPnPDeviceDirectory(const UPnPDeviceDirectory &) = delete;
127 
128  void Start();
129 
131  std::vector<ContentDirectoryService> GetDirectories();
132 
136  ContentDirectoryService GetServer(const char *friendly_name);
137 
138 private:
139  void Search();
140 
148  void ExpireDevices();
149 
150  void LockAdd(ContentDirectoryDescriptor &&d);
151  void LockRemove(const std::string &id);
152 
158  static void *Explore(void *);
159  void Explore();
160 
161  int OnAlive(const UpnpDiscovery *disco);
162  int OnByeBye(const UpnpDiscovery *disco);
163 
164  /* virtual methods from class UpnpCallback */
165  virtual int Invoke(Upnp_EventType et, const void *evp) override;
166 };
167 
168 
169 #endif /* _UPNPPDISC_H_X_INCLUDED_ */
virtual void LostUPnP(const ContentDirectoryService &service)=0
UPnPDeviceDirectory & operator=(const UPnPDeviceDirectory &)=delete
A WorkQueue manages the synchronisation around a queue of work items, where a number of client thread...
Definition: WorkQueue.hxx:49
A class that is supposed to be used for libupnp asynchronous callbacks.
Definition: Callback.hxx:29
ContentDirectoryService GetServer(const char *friendly_name)
Get server by friendly name.
void Parse(const std::string &url, const char *description)
Build device from xml description downloaded from discovery.
Definition: Mutex.hxx:43
Data holder for a UPnP device, parsed from the XML description obtained during discovery.
Definition: Device.hxx:53
Manage UPnP discovery and maintain a directory of active devices.
Definition: Discovery.hxx:56
Upnp_Discovery UpnpDiscovery
Definition: Compat.hxx:28
std::vector< ContentDirectoryService > GetDirectories()
Retrieve the directory services currently seen on the network.
virtual void FoundUPnP(const ContentDirectoryService &service)=0
Content Directory Service class.
static gcc_pure int UpnpDiscovery_get_Expires(const UpnpDiscovery *disco) noexcept
Definition: Compat.hxx:37
UPnPDeviceDirectory(UpnpClient_Handle _handle, UPnPDiscoveryListener *_listener=nullptr)
static gcc_pure const char * UpnpDiscovery_get_DeviceID_cstr(const UpnpDiscovery *disco) noexcept
Definition: Compat.hxx:44
static gcc_pure const char * UpnpDiscovery_get_Location_cstr(const UpnpDiscovery *disco) noexcept
Definition: Compat.hxx:65