db/upnp/Discovery: use std::list instead of std::map

There will only be very few items, and that does not justify the bloat
of std::map.
This commit is contained in:
Max Kellermann 2014-01-23 22:18:40 +01:00
parent c9af358e4b
commit b8e382b011
2 changed files with 33 additions and 16 deletions

View File

@ -54,19 +54,32 @@ isMSDevice(const char *st)
} }
inline void inline void
UPnPDeviceDirectory::LockAdd(std::string &&id, ContentDirectoryDescriptor &&d) UPnPDeviceDirectory::LockAdd(ContentDirectoryDescriptor &&d)
{ {
const ScopeLock protect(mutex); const ScopeLock protect(mutex);
directories[std::move(id)] = std::move(d);
for (auto &i : directories) {
if (i.id == d.id) {
i = std::move(d);
return;
}
}
directories.emplace_back(std::move(d));
} }
inline void inline void
UPnPDeviceDirectory::LockRemove(const std::string &id) UPnPDeviceDirectory::LockRemove(const std::string &id)
{ {
const ScopeLock protect(mutex); const ScopeLock protect(mutex);
auto i = directories.find(id);
if (i != directories.end()) for (auto i = directories.begin(), end = directories.end();
directories.erase(i); i != end; ++i) {
if (i->id == id) {
directories.erase(i);
break;
}
}
} }
inline void inline void
@ -91,7 +104,8 @@ UPnPDeviceDirectory::discoExplorer()
} }
// Update or insert the device // Update or insert the device
ContentDirectoryDescriptor d(MonotonicClockS(), tsk->expires); ContentDirectoryDescriptor d(std::move(tsk->deviceId),
MonotonicClockS(), tsk->expires);
{ {
Error error2; Error error2;
@ -104,7 +118,7 @@ UPnPDeviceDirectory::discoExplorer()
} }
} }
LockAdd(std::move(tsk->deviceId), std::move(d)); LockAdd(std::move(d));
delete tsk; delete tsk;
} }
} }
@ -180,7 +194,7 @@ UPnPDeviceDirectory::expireDevices(Error &error)
for (auto it = directories.begin(); for (auto it = directories.begin();
it != directories.end();) { it != directories.end();) {
if (now > it->second.expires) { if (now > it->expires) {
it = directories.erase(it); it = directories.erase(it);
didsomething = true; didsomething = true;
} else { } else {
@ -263,9 +277,9 @@ UPnPDeviceDirectory::getDirServices(std::vector<ContentDirectoryService> &out,
for (auto dit = directories.begin(); for (auto dit = directories.begin();
dit != directories.end(); dit++) { dit != directories.end(); dit++) {
for (const auto &service : dit->second.device.services) { for (const auto &service : dit->device.services) {
if (isCDService(service.serviceType.c_str())) { if (isCDService(service.serviceType.c_str())) {
out.emplace_back(dit->second.device, service); out.emplace_back(dit->device, service);
} }
} }
} }
@ -285,7 +299,7 @@ UPnPDeviceDirectory::getServer(const char *friendlyName,
const ScopeLock protect(mutex); const ScopeLock protect(mutex);
for (const auto &i : directories) { for (const auto &i : directories) {
const auto &device = i.second.device; const auto &device = i.device;
if (device.friendlyName != friendlyName) if (device.friendlyName != friendlyName)
continue; continue;

View File

@ -27,7 +27,7 @@
#include <upnp/upnp.h> #include <upnp/upnp.h>
#include <map> #include <list>
#include <vector> #include <vector>
#include <string> #include <string>
@ -63,6 +63,8 @@ class UPnPDeviceDirectory {
*/ */
class ContentDirectoryDescriptor { class ContentDirectoryDescriptor {
public: public:
std::string id;
UPnPDevice device; UPnPDevice device;
/** /**
@ -73,8 +75,9 @@ class UPnPDeviceDirectory {
ContentDirectoryDescriptor() = default; ContentDirectoryDescriptor() = default;
ContentDirectoryDescriptor(unsigned last, int exp) ContentDirectoryDescriptor(std::string &&_id,
:expires(last + exp + 20) {} unsigned last, int exp)
:id(std::move(_id)), expires(last + exp + 20) {}
bool Parse(const std::string &url, const char *description, bool Parse(const std::string &url, const char *description,
Error &_error) { Error &_error) {
@ -85,7 +88,7 @@ class UPnPDeviceDirectory {
LibUPnP *const lib; LibUPnP *const lib;
Mutex mutex; Mutex mutex;
std::map<std::string, ContentDirectoryDescriptor> directories; std::list<ContentDirectoryDescriptor> directories;
WorkQueue<DiscoveredTask *> discoveredQueue; WorkQueue<DiscoveredTask *> discoveredQueue;
/** /**
@ -129,7 +132,7 @@ private:
*/ */
bool expireDevices(Error &error); bool expireDevices(Error &error);
void LockAdd(std::string &&id, ContentDirectoryDescriptor &&d); void LockAdd(ContentDirectoryDescriptor &&d);
void LockRemove(const std::string &id); void LockRemove(const std::string &id);
/** /**