From d40710eb005cde8c6962d6f5689bc63a4745b1c1 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 15 Mar 2017 18:41:42 +0100 Subject: Preparation to get useful notifications to Kube --- common/notification.h | 1 + common/notifier.cpp | 54 +++++++++++++++++++++++++++++++++++++++------------ common/notifier.h | 5 +++-- 3 files changed, 46 insertions(+), 14 deletions(-) (limited to 'common') diff --git a/common/notification.h b/common/notification.h index 8224f2a..b67a84a 100644 --- a/common/notification.h +++ b/common/notification.h @@ -34,6 +34,7 @@ public: enum NoticationType { Shutdown, Status, + Info, Warning, Progress, Inspection, diff --git a/common/notifier.cpp b/common/notifier.cpp index 53db5be..28d56bf 100644 --- a/common/notifier.cpp +++ b/common/notifier.cpp @@ -24,6 +24,8 @@ #include "resourceaccess.h" #include "resourceconfig.h" +#include "query.h" +#include "facadefactory.h" #include "log.h" using namespace Sink; @@ -34,6 +36,18 @@ public: Private() : context(new QObject) { } + + void listenForNotifications(const QSharedPointer &access) + { + SinkWarningCtx(Sink::Log::Context{"foobar"}) << "Listening for notifications"; + QObject::connect(access.data(), &ResourceAccess::notification, context.data(), [this](const Notification ¬ification) { + for (const auto &handler : handler) { + handler(notification); + } + }); + resourceAccess << access; + } + QList> resourceAccess; QList> handler; QSharedPointer context; @@ -41,30 +55,46 @@ public: Notifier::Notifier(const QSharedPointer &resourceAccess) : d(new Sink::Notifier::Private) { - QObject::connect(resourceAccess.data(), &ResourceAccess::notification, d->context.data(), [this](const Notification ¬ification) { - for (const auto &handler : d->handler) { - handler(notification); - } - }); - d->resourceAccess << resourceAccess; + d->listenForNotifications(resourceAccess); } Notifier::Notifier(const QByteArray &instanceIdentifier, const QByteArray &resourceType) : d(new Sink::Notifier::Private) { auto resourceAccess = Sink::ResourceAccess::Ptr::create(instanceIdentifier, resourceType); resourceAccess->open(); - QObject::connect(resourceAccess.data(), &ResourceAccess::notification, d->context.data(), [this](const Notification ¬ification) { - for (const auto &handler : d->handler) { - handler(notification); - } - }); - d->resourceAccess << resourceAccess; + d->listenForNotifications(resourceAccess); } Notifier::Notifier(const QByteArray &instanceIdentifier) : Notifier(instanceIdentifier, ResourceConfig::getResourceType(instanceIdentifier)) { } +Notifier::Notifier(const Sink::Query &resourceQuery) : d(new Sink::Notifier::Private) +{ + Sink::Log::Context resourceCtx{"notifier"}; + auto facade = FacadeFactory::instance().getFacade(); + Q_ASSERT(facade); + + auto result = facade->load(resourceQuery, resourceCtx); + auto emitter = result.second; + emitter->onAdded([=](const ApplicationDomain::SinkResource::Ptr &resource) { + auto resourceAccess = Sink::ResourceAccess::Ptr::create(resource->identifier(), ResourceConfig::getResourceType(resource->identifier())); + resourceAccess->open(); + d->listenForNotifications(resourceAccess); + }); + emitter->onModified([](const ApplicationDomain::SinkResource::Ptr &) { + }); + emitter->onRemoved([](const ApplicationDomain::SinkResource::Ptr &) { + }); + emitter->onInitialResultSetComplete([](const ApplicationDomain::SinkResource::Ptr &, bool) { + }); + emitter->onComplete([resourceCtx]() { + SinkTraceCtx(resourceCtx) << "Resource query complete"; + }); + emitter->fetch({}); + result.first.exec(); +} + void Notifier::registerHandler(std::function handler) { d->handler << handler; diff --git a/common/notifier.h b/common/notifier.h index 290458a..b5d3dfa 100644 --- a/common/notifier.h +++ b/common/notifier.h @@ -23,14 +23,14 @@ #include "sink_export.h" #include #include - -#include +#include class QAbstractItemModel; namespace Sink { class ResourceAccess; class Notification; +class Query; class SINK_EXPORT Notifier { @@ -38,6 +38,7 @@ public: Notifier(const QSharedPointer &resourceAccess); Notifier(const QByteArray &resourceInstanceIdentifier); Notifier(const QByteArray &resourceInstanceIdentifier, const QByteArray &resourceType); + Notifier(const Sink::Query &resourceQuery); void registerHandler(std::function); private: -- cgit v1.2.3