Commit 9f3ba737 authored by Max Kellermann's avatar Max Kellermann

upnp/Discovery: add listener interface

parent f86f5ad2
......@@ -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)
{
......
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment