upnp/Discovery: add listener interface

This commit is contained in:
Max Kellermann 2014-01-26 13:14:15 +01:00
parent f86f5ad224
commit 9f3ba737d6
2 changed files with 36 additions and 2 deletions

View File

@ -53,6 +53,24 @@ isMSDevice(const char *st)
return memcmp(MediaServerDType, st, sz) == 0; 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 inline void
UPnPDeviceDirectory::LockAdd(ContentDirectoryDescriptor &&d) UPnPDeviceDirectory::LockAdd(ContentDirectoryDescriptor &&d)
{ {
@ -66,6 +84,9 @@ UPnPDeviceDirectory::LockAdd(ContentDirectoryDescriptor &&d)
} }
directories.emplace_back(std::move(d)); directories.emplace_back(std::move(d));
if (listener != nullptr)
AnnounceFoundUPnP(*listener, directories.back().device);
} }
inline void inline void
@ -76,6 +97,9 @@ UPnPDeviceDirectory::LockRemove(const std::string &id)
for (auto i = directories.begin(), end = directories.end(); for (auto i = directories.begin(), end = directories.end();
i != end; ++i) { i != end; ++i) {
if (i->id == id) { if (i->id == id) {
if (listener != nullptr)
AnnounceLostUPnP(*listener, i->device);
directories.erase(i); directories.erase(i);
break; break;
} }
@ -208,8 +232,10 @@ UPnPDeviceDirectory::expireDevices(Error &error)
return true; return true;
} }
UPnPDeviceDirectory::UPnPDeviceDirectory(LibUPnP *_lib) UPnPDeviceDirectory::UPnPDeviceDirectory(LibUPnP *_lib,
UPnPDiscoveryListener *_listener)
:lib(_lib), :lib(_lib),
listener(_listener),
discoveredQueue("DiscoveredQueue"), discoveredQueue("DiscoveredQueue"),
m_searchTimeout(2), m_lastSearch(0) m_searchTimeout(2), m_lastSearch(0)
{ {

View File

@ -34,6 +34,12 @@
class LibUPnP; class LibUPnP;
class ContentDirectoryService; 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. * Manage UPnP discovery and maintain a directory of active devices. Singleton.
* *
@ -86,6 +92,7 @@ class UPnPDeviceDirectory {
}; };
LibUPnP *const lib; LibUPnP *const lib;
UPnPDiscoveryListener *const listener;
Mutex mutex; Mutex mutex;
std::list<ContentDirectoryDescriptor> directories; std::list<ContentDirectoryDescriptor> directories;
@ -104,7 +111,8 @@ class UPnPDeviceDirectory {
unsigned m_lastSearch; unsigned m_lastSearch;
public: public:
UPnPDeviceDirectory(LibUPnP *_lib); UPnPDeviceDirectory(LibUPnP *_lib,
UPnPDiscoveryListener *_listener=nullptr);
~UPnPDeviceDirectory(); ~UPnPDeviceDirectory();
UPnPDeviceDirectory(const UPnPDeviceDirectory &) = delete; UPnPDeviceDirectory(const UPnPDeviceDirectory &) = delete;