db/upnp/Device: move code to method Parse()

Forward the Error to the caller.
This commit is contained in:
Max Kellermann 2014-01-18 13:47:12 +01:00
parent 04b7648e00
commit 22dd3c8048
4 changed files with 39 additions and 31 deletions

View File

@ -21,7 +21,6 @@
#include "Device.hxx"
#include "Util.hxx"
#include "Expat.hxx"
#include "Log.hxx"
#include "util/Error.hxx"
#include <stdlib.h>
@ -99,16 +98,15 @@ protected:
}
};
UPnPDevice::UPnPDevice(const std::string &url, const char *description)
:ok(false)
bool
UPnPDevice::Parse(const std::string &url, const char *description,
Error &error)
{
{
UPnPDeviceParser mparser(*this);
Error error;
if (!mparser.Parse(description, strlen(description), true,
error)) {
// TODO: pass Error to caller
LogError(error);
return;
if (!mparser.Parse(description, strlen(description),
true, error))
return false;
}
if (URLBase.empty()) {
@ -129,5 +127,6 @@ UPnPDevice::UPnPDevice(const std::string &url, const char *description)
}
}
}
ok = true;
return true;
}

View File

@ -61,7 +61,6 @@ struct UPnPService {
*/
class UPnPDevice {
public:
bool ok;
// e.g. urn:schemas-upnp-org:device:MediaServer:1
std::string deviceType;
// e.g. MediaTomb
@ -78,17 +77,17 @@ public:
// Services provided by this device.
std::vector<UPnPService> services;
UPnPDevice() = default;
UPnPDevice(const UPnPDevice &) = delete;
UPnPDevice(UPnPDevice &&) = default;
UPnPDevice &operator=(UPnPDevice &&) = default;
/** Build device from xml description downloaded from discovery
* @param url where the description came from
* @param description the xml device description
*/
UPnPDevice(const std::string &url, const char *description);
UPnPDevice() : ok(false) {}
UPnPDevice(const UPnPDevice &) = delete;
UPnPDevice(UPnPDevice &&) = default;
UPnPDevice &operator=(UPnPDevice &&) = default;
bool Parse(const std::string &url, const char *description,
Error &error);
};
#endif /* _UPNPDEV_HXX_INCLUDED_ */

View File

@ -22,6 +22,7 @@
#include "Domain.hxx"
#include "ContentDirectoryService.hxx"
#include "upnpplib.hxx"
#include "Log.hxx"
#include <upnp/upnptools.h>
@ -73,13 +74,18 @@ UPnPDeviceDirectory::discoExplorer()
}
// Update or insert the device
ContentDirectoryDescriptor d(tsk->url, buf,
time(0), tsk->expires);
ContentDirectoryDescriptor d(time(0), tsk->expires);
{
Error error2;
bool success = d.Parse(tsk->url, buf, error2);
free(buf);
if (!d.device.ok) {
if (!success) {
delete tsk;
LogError(error2);
continue;
}
}
const ScopeLock protect(mutex);
directories[std::move(tsk->deviceId)] = std::move(d);

View File

@ -65,15 +65,19 @@ class UPnPDeviceDirectory {
*/
class ContentDirectoryDescriptor {
public:
ContentDirectoryDescriptor() = default;
ContentDirectoryDescriptor(const std::string &url,
const char *description,
time_t last, int exp)
:device(url, description), last_seen(last), expires(exp+20) {}
UPnPDevice device;
time_t last_seen;
int expires; // seconds valid
ContentDirectoryDescriptor() = default;
ContentDirectoryDescriptor(time_t last, int exp)
:last_seen(last), expires(exp+20) {}
bool Parse(const std::string &url, const char *description,
Error &_error) {
return device.Parse(url, description, _error);
}
};
LibUPnP *const lib;