From 7eee79c14570cea5ae2a890bb443cbbf6c05d657 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 4 Jun 2018 21:48:39 +0200 Subject: [PATCH] neighbor/udisks: move code to class UDisks2::ParseObjects(ODBus::Message) --- src/lib/dbus/UDisks2.cxx | 16 +++++++++++++ src/lib/dbus/UDisks2.hxx | 15 +++++++++++- src/neighbor/plugins/UdisksNeighborPlugin.cxx | 24 +++++-------------- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/lib/dbus/UDisks2.cxx b/src/lib/dbus/UDisks2.cxx index 3c3f0c5c4..18610ecb7 100644 --- a/src/lib/dbus/UDisks2.cxx +++ b/src/lib/dbus/UDisks2.cxx @@ -18,6 +18,7 @@ */ #include "UDisks2.hxx" +#include "Message.hxx" #include "ReadIter.hxx" #include "ObjectManager.hxx" #include "util/StringAPI.hxx" @@ -113,4 +114,19 @@ ParseObjects(ODBus::ReadMessageIter &&i, }); } +void +ParseObjects(ODBus::Message &reply, + std::function callback) +{ + using namespace ODBus; + + reply.CheckThrowError(); + + ReadMessageIter i(*reply.Get()); + if (i.GetArgType() != DBUS_TYPE_ARRAY) + throw std::runtime_error("Malformed response"); + + ParseObjects(i.Recurse(), std::move(callback)); +} + } // namespace UDisks2 diff --git a/src/lib/dbus/UDisks2.hxx b/src/lib/dbus/UDisks2.hxx index b0a068fd1..a3675ad62 100644 --- a/src/lib/dbus/UDisks2.hxx +++ b/src/lib/dbus/UDisks2.hxx @@ -26,7 +26,10 @@ #define UDISKS2_PATH "/org/freedesktop/UDisks2" #define UDISKS2_INTERFACE "org.freedesktop.UDisks2" -namespace ODBus { class ReadMessageIter; } +namespace ODBus { +class Message; +class ReadMessageIter; +} namespace UDisks2 { @@ -66,6 +69,16 @@ void ParseObjects(ODBus::ReadMessageIter &&i, std::function callback); +/** + * Parse objects from a GetManagedObjects reply and invoke the + * callback for each. + * + * Throws on error. + */ +void +ParseObjects(ODBus::Message &reply, + std::function callback); + } // namespace UDisks2 #endif diff --git a/src/neighbor/plugins/UdisksNeighborPlugin.cxx b/src/neighbor/plugins/UdisksNeighborPlugin.cxx index dc809062d..a161346ec 100644 --- a/src/neighbor/plugins/UdisksNeighborPlugin.cxx +++ b/src/neighbor/plugins/UdisksNeighborPlugin.cxx @@ -34,7 +34,6 @@ #include "event/Call.hxx" #include "thread/Mutex.hxx" #include "thread/SafeSingleton.hxx" -#include "util/Domain.hxx" #include "util/Manual.hxx" #include "Log.hxx" @@ -43,8 +42,6 @@ #include #include -static constexpr Domain udisks_domain("udisks"); - static NeighborInfo ToNeighborInfo(const UDisks2::Object &o) noexcept { @@ -216,24 +213,15 @@ UdisksNeighborExplorer::Remove(const std::string &path) noexcept inline void UdisksNeighborExplorer::OnListNotify(ODBus::Message reply) noexcept { - using namespace ODBus; - - try { - reply.CheckThrowError(); + try{ + ParseObjects(reply, + std::bind(&UdisksNeighborExplorer::Insert, + this, std::placeholders::_1)); } catch (...) { - LogError(std::current_exception()); + LogError(std::current_exception(), + "Failed to parse GetManagedObjects reply"); return; } - - ReadMessageIter i(*reply.Get()); - if (i.GetArgType() != DBUS_TYPE_ARRAY) { - LogError(udisks_domain, "Malformed response"); - return; - } - - ParseObjects(i.Recurse(), - std::bind(&UdisksNeighborExplorer::Insert, - this, std::placeholders::_1)); } inline DBusHandlerResult