Discovery.hxx 4.61 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef _UPNPPDISC_H_X_INCLUDED_
#define _UPNPPDISC_H_X_INCLUDED_

23
#include "Compat.hxx"
24
#include "Callback.hxx"
25 26 27
#include "Device.hxx"
#include "WorkQueue.hxx"
#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 39 40 41
#if UPNP_VERSION < 10800
#define UpnpDiscovery Upnp_Discovery
#endif

42 43
class ContentDirectoryService;

44 45 46 47 48 49
class UPnPDiscoveryListener {
public:
	virtual void FoundUPnP(const ContentDirectoryService &service) = 0;
	virtual void LostUPnP(const ContentDirectoryService &service) = 0;
};

50 51 52 53 54 55
/**
 * Manage UPnP discovery and maintain a directory of active devices. Singleton.
 *
 * We are only interested in MediaServers with a ContentDirectory service
 * for now, but this could be made more general, by removing the filtering.
 */
56
class UPnPDeviceDirectory final : UpnpCallback {
57 58 59 60 61 62 63
	/**
	 * Each appropriate discovery event (executing in a libupnp thread
	 * context) queues the following task object for processing by the
	 * discovery thread.
	 */
	struct DiscoveredTask {
		std::string url;
64
		std::string device_id;
65
		std::chrono::steady_clock::duration expires;
66

67 68 69 70
		DiscoveredTask(const UpnpDiscovery *disco)
			:url(UpnpDiscovery_get_Location_cstr(disco)),
			 device_id(UpnpDiscovery_get_DeviceID_cstr(disco)),
			 expires(std::chrono::seconds(UpnpDiscovery_get_Expires(disco))) {}
71 72 73 74 75 76 77 78
	};

	/**
	 * Descriptor for one device having a Content Directory
	 * service found on the network.
	 */
	class ContentDirectoryDescriptor {
	public:
79 80
		std::string id;

81
		UPnPDevice device;
82 83

		/**
84
		 * The time stamp when this device expires.
85
		 */
86
		std::chrono::steady_clock::time_point expires;
87 88 89

		ContentDirectoryDescriptor() = default;

90
		ContentDirectoryDescriptor(std::string &&_id,
91 92 93 94
					   std::chrono::steady_clock::time_point last,
					   std::chrono::steady_clock::duration exp)
			:id(std::move(_id)),
			 expires(last + exp + std::chrono::seconds(20)) {}
95

96 97
		void Parse(const std::string &url, const char *description) {
			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;
106
	WorkQueue<std::unique_ptr<DiscoveredTask>> queue;
107

108 109 110 111 112
	/**
	 * The UPnP device search timeout, which should actually be
	 * called delay because it's the base of a random delay that
	 * the devices apply to avoid responding all at the same time.
	 */
113
	int search_timeout = 2;
114

115
	/**
116
	 * The time stamp of the last search.
117
	 */
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);
123
	~UPnPDeviceDirectory();
124

125 126 127
	UPnPDeviceDirectory(const UPnPDeviceDirectory &) = delete;
	UPnPDeviceDirectory& operator=(const UPnPDeviceDirectory &) = delete;

128
	void Start();
129

130
	/** Retrieve the directory services currently seen on the network */
131
	std::vector<ContentDirectoryService> GetDirectories();
132 133

	/**
134
	 * Get server by friendly name.
135
	 */
136
	ContentDirectoryService GetServer(const char *friendly_name);
137 138

private:
139
	void Search();
140 141 142 143 144

	/**
	 * Look at the devices and get rid of those which have not
	 * been seen for too long. We do this when listing the top
	 * directory.
145 146
	 *
	 * Caller must lock #mutex.
147
	 */
148
	void ExpireDevices();
149

150
	void LockAdd(ContentDirectoryDescriptor &&d);
151 152
	void LockRemove(const std::string &id);

153 154 155 156 157
	/**
	 * Worker routine for the discovery queue. Get messages about
	 * devices appearing and disappearing, and update the
	 * directory pool accordingly.
	 */
158 159
	static void *Explore(void *);
	void Explore();
160

161 162
	int OnAlive(const UpnpDiscovery *disco);
	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_ */