From 9f3ba737d608b58eb4ebdff37a753cd3de735290 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 26 Jan 2014 13:14:15 +0100 Subject: upnp/Discovery: add listener interface --- src/lib/upnp/Discovery.cxx | 28 +++++++++++++++++++++++++++- src/lib/upnp/Discovery.hxx | 10 +++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) (limited to 'src/lib') diff --git a/src/lib/upnp/Discovery.cxx b/src/lib/upnp/Discovery.cxx index 5203dba83..da32372ba 100644 --- a/src/lib/upnp/Discovery.cxx +++ b/src/lib/upnp/Discovery.cxx @@ -53,6 +53,24 @@ isMSDevice(const char *st) return memcmp(MediaServerDType, st, sz) == 0; } +static void +AnnounceFoundUPnP(UPnPDiscoveryListener &listener, const UPnPDevice &device) +{ + for (const auto &service : device.services) + if (isCDService(service.serviceType.c_str())) + listener.FoundUPnP(ContentDirectoryService(device, + service)); +} + +static void +AnnounceLostUPnP(UPnPDiscoveryListener &listener, const UPnPDevice &device) +{ + for (const auto &service : device.services) + if (isCDService(service.serviceType.c_str())) + listener.LostUPnP(ContentDirectoryService(device, + service)); +} + inline void UPnPDeviceDirectory::LockAdd(ContentDirectoryDescriptor &&d) { @@ -66,6 +84,9 @@ UPnPDeviceDirectory::LockAdd(ContentDirectoryDescriptor &&d) } directories.emplace_back(std::move(d)); + + if (listener != nullptr) + AnnounceFoundUPnP(*listener, directories.back().device); } inline void @@ -76,6 +97,9 @@ UPnPDeviceDirectory::LockRemove(const std::string &id) for (auto i = directories.begin(), end = directories.end(); i != end; ++i) { if (i->id == id) { + if (listener != nullptr) + AnnounceLostUPnP(*listener, i->device); + directories.erase(i); break; } @@ -208,8 +232,10 @@ UPnPDeviceDirectory::expireDevices(Error &error) return true; } -UPnPDeviceDirectory::UPnPDeviceDirectory(LibUPnP *_lib) +UPnPDeviceDirectory::UPnPDeviceDirectory(LibUPnP *_lib, + UPnPDiscoveryListener *_listener) :lib(_lib), + listener(_listener), discoveredQueue("DiscoveredQueue"), m_searchTimeout(2), m_lastSearch(0) { diff --git a/src/lib/upnp/Discovery.hxx b/src/lib/upnp/Discovery.hxx index 4c64fe420..3208f8727 100644 --- a/src/lib/upnp/Discovery.hxx +++ b/src/lib/upnp/Discovery.hxx @@ -34,6 +34,12 @@ class LibUPnP; class ContentDirectoryService; +class UPnPDiscoveryListener { +public: + virtual void FoundUPnP(const ContentDirectoryService &service) = 0; + virtual void LostUPnP(const ContentDirectoryService &service) = 0; +}; + /** * Manage UPnP discovery and maintain a directory of active devices. Singleton. * @@ -86,6 +92,7 @@ class UPnPDeviceDirectory { }; LibUPnP *const lib; + UPnPDiscoveryListener *const listener; Mutex mutex; std::list directories; @@ -104,7 +111,8 @@ class UPnPDeviceDirectory { unsigned m_lastSearch; public: - UPnPDeviceDirectory(LibUPnP *_lib); + UPnPDeviceDirectory(LibUPnP *_lib, + UPnPDiscoveryListener *_listener=nullptr); ~UPnPDeviceDirectory(); UPnPDeviceDirectory(const UPnPDeviceDirectory &) = delete; -- cgit v1.2.3