diff options
author | Max Kellermann <max@duempel.org> | 2014-01-26 13:14:15 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2014-01-26 13:41:40 +0100 |
commit | 9f3ba737d608b58eb4ebdff37a753cd3de735290 (patch) | |
tree | ab5cc29b145aee628a9dea749e6075cf96a5470c | |
parent | f86f5ad2240c49f35c4948bed09027d9f5530d0b (diff) | |
download | mpd-9f3ba737d608b58eb4ebdff37a753cd3de735290.tar.gz mpd-9f3ba737d608b58eb4ebdff37a753cd3de735290.tar.xz mpd-9f3ba737d608b58eb4ebdff37a753cd3de735290.zip |
upnp/Discovery: add listener interface
-rw-r--r-- | src/lib/upnp/Discovery.cxx | 28 | ||||
-rw-r--r-- | src/lib/upnp/Discovery.hxx | 10 |
2 files changed, 36 insertions, 2 deletions
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<ContentDirectoryDescriptor> 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; |