MPD  0.20.6
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 "Callback.hxx"
24 #include "Device.hxx"
25 #include "WorkQueue.hxx"
26 #include "thread/Mutex.hxx"
27 #include "Compiler.h"
28 
29 #include <upnp/upnp.h>
30 
31 #include <list>
32 #include <vector>
33 #include <string>
34 #include <memory>
35 #include <chrono>
36 
38 
40 public:
41  virtual void FoundUPnP(const ContentDirectoryService &service) = 0;
42  virtual void LostUPnP(const ContentDirectoryService &service) = 0;
43 };
44 
57  struct DiscoveredTask {
58  std::string url;
59  std::string device_id;
60  std::chrono::steady_clock::duration expires;
61 
62  DiscoveredTask(const Upnp_Discovery *disco)
63  :url(disco->Location),
64  device_id(disco->DeviceId),
65  expires(std::chrono::seconds(disco->Expires)) {}
66  };
67 
72  class ContentDirectoryDescriptor {
73  public:
74  std::string id;
75 
76  UPnPDevice device;
77 
81  std::chrono::steady_clock::time_point expires;
82 
83  ContentDirectoryDescriptor() = default;
84 
85  ContentDirectoryDescriptor(std::string &&_id,
86  std::chrono::steady_clock::time_point last,
87  std::chrono::steady_clock::duration exp)
88  :id(std::move(_id)),
89  expires(last + exp + std::chrono::seconds(20)) {}
90 
91  void Parse(const std::string &url, const char *description) {
92  device.Parse(url, description);
93  }
94  };
95 
96  const UpnpClient_Handle handle;
97  UPnPDiscoveryListener *const listener;
98 
99  Mutex mutex;
100  std::list<ContentDirectoryDescriptor> directories;
102 
108  int search_timeout = 2;
109 
113  std::chrono::steady_clock::time_point last_search = std::chrono::steady_clock::time_point();
114 
115 public:
116  UPnPDeviceDirectory(UpnpClient_Handle _handle,
117  UPnPDiscoveryListener *_listener=nullptr);
119 
120  UPnPDeviceDirectory(const UPnPDeviceDirectory &) = delete;
122 
123  void Start();
124 
126  std::vector<ContentDirectoryService> GetDirectories();
127 
131  ContentDirectoryService GetServer(const char *friendly_name);
132 
133 private:
134  void Search();
135 
143  void ExpireDevices();
144 
145  void LockAdd(ContentDirectoryDescriptor &&d);
146  void LockRemove(const std::string &id);
147 
153  static void *Explore(void *);
154  void Explore();
155 
156  int OnAlive(Upnp_Discovery *disco);
157  int OnByeBye(Upnp_Discovery *disco);
158 
159  /* virtual methods from class UpnpCallback */
160  virtual int Invoke(Upnp_EventType et, void *evp) override;
161 };
162 
163 
164 #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:51
std::vector< ContentDirectoryService > GetDirectories()
Retrieve the directory services currently seen on the network.
virtual void FoundUPnP(const ContentDirectoryService &service)=0
Content Directory Service class.
UPnPDeviceDirectory(UpnpClient_Handle _handle, UPnPDiscoveryListener *_listener=nullptr)