From 668cead7d8f98615d90dbc933f194105b3cf0bc3 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 10 Feb 2016 11:02:39 +0100 Subject: Moved Notifier and ResourceAccess to separate files. --- common/clientapi.cpp | 181 --------------------------------------------------- 1 file changed, 181 deletions(-) delete mode 100644 common/clientapi.cpp (limited to 'common/clientapi.cpp') diff --git a/common/clientapi.cpp b/common/clientapi.cpp deleted file mode 100644 index 01411c2..0000000 --- a/common/clientapi.cpp +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Copyright (C) 2014 Christian Mollekopf - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) version 3, or any - * later version accepted by the membership of KDE e.V. (or its - * successor approved by the membership of KDE e.V.), which shall - * act as a proxy defined in Section 6 of version 3 of the license. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - */ - -#include "clientapi.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "resourceaccess.h" -#include "commands.h" -#include "resourcefacade.h" -#include "definitions.h" -#include "resourceconfig.h" -#include "facadefactory.h" -#include "modelresult.h" -#include "storage.h" -#include "log.h" - -#undef DEBUG_AREA -#define DEBUG_AREA "client.clientapi" - -namespace Sink -{ - -KAsync::Job ResourceControl::shutdown(const QByteArray &identifier) -{ - Trace() << "shutdown " << identifier; - auto time = QSharedPointer::create(); - time->start(); - return ResourceAccess::connectToServer(identifier).then>([identifier, time](QSharedPointer socket, KAsync::Future &future) { - //We can't currently reuse the socket - socket->close(); - auto resourceAccess = QSharedPointer::create(identifier); - resourceAccess->open(); - resourceAccess->sendCommand(Sink::Commands::ShutdownCommand).then([&future, resourceAccess, time]() { - Trace() << "Shutdown complete." << Log::TraceTime(time->elapsed()); - future.setFinished(); - }).exec(); - }, - [](int, const QString &) { - Trace() << "Resource is already closed."; - //Resource isn't started, nothing to shutdown - }) - //FIXME JOBAPI this is only required because we don't care about the return value of connectToServer - .template then([](){}); -} - -KAsync::Job ResourceControl::start(const QByteArray &identifier) -{ - Trace() << "start " << identifier; - auto time = QSharedPointer::create(); - time->start(); - auto resourceAccess = QSharedPointer::create(identifier); - resourceAccess->open(); - return resourceAccess->sendCommand(Sink::Commands::PingCommand).then([resourceAccess, time]() { - Trace() << "Start complete." << Log::TraceTime(time->elapsed()); - }); -} - -KAsync::Job ResourceControl::flushMessageQueue(const QByteArrayList &resourceIdentifier) -{ - Trace() << "flushMessageQueue" << resourceIdentifier; - return KAsync::iterate(resourceIdentifier) - .template each([](const QByteArray &resource, KAsync::Future &future) { - Trace() << "Flushing message queue " << resource; - auto resourceAccess = QSharedPointer::create(resource); - resourceAccess->open(); - resourceAccess->synchronizeResource(false, true).then([&future, resourceAccess]() { - future.setFinished(); - }).exec(); - }) - //FIXME JOBAPI this is only required because we don't care about the return value of each (and each shouldn't even have a return value) - .template then([](){}); -} - -KAsync::Job ResourceControl::flushReplayQueue(const QByteArrayList &resourceIdentifier) -{ - return flushMessageQueue(resourceIdentifier); -} - -template -KAsync::Job ResourceControl::inspect(const Inspection &inspectionCommand) -{ - auto resource = inspectionCommand.resourceIdentifier; - - auto time = QSharedPointer::create(); - time->start(); - Trace() << "Sending inspection " << resource; - auto resourceAccess = QSharedPointer::create(resource); - resourceAccess->open(); - auto notifier = QSharedPointer::create(resourceAccess); - auto id = QUuid::createUuid().toByteArray(); - return resourceAccess->sendInspectionCommand(id, ApplicationDomain::getTypeName(), inspectionCommand.entityIdentifier, inspectionCommand.property, inspectionCommand.expectedValue) - .template then([resourceAccess, notifier, id, time](KAsync::Future &future) { - notifier->registerHandler([&future, id, time](const Notification ¬ification) { - if (notification.id == id) { - Trace() << "Inspection complete." << Log::TraceTime(time->elapsed()); - if (notification.code) { - future.setError(-1, "Inspection returned an error: " + notification.message); - } else { - future.setFinished(); - } - } - }); - }); -} - -class Sink::Notifier::Private { -public: - Private() - : context(new QObject) - { - - } - QList > resourceAccess; - QList > handler; - QSharedPointer context; -}; - -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; -} - -Notifier::Notifier(const QByteArray &instanceIdentifier) - : d(new Sink::Notifier::Private) -{ - auto resourceAccess = Sink::ResourceAccess::Ptr::create(instanceIdentifier); - 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; -} - -void Notifier::registerHandler(std::function handler) -{ - d->handler << handler; -} - -#define REGISTER_TYPE(T) template KAsync::Job ResourceControl::inspect(const Inspection &); \ - -REGISTER_TYPE(ApplicationDomain::Event); -REGISTER_TYPE(ApplicationDomain::Mail); -REGISTER_TYPE(ApplicationDomain::Folder); -REGISTER_TYPE(ApplicationDomain::SinkResource); - -} // namespace Sink - -- cgit v1.2.3