/* * Copyright (C) 2015 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 "resourcecontrol.h" #include #include #include #include "resourceaccess.h" #include "commands.h" #include "log.h" #include "notifier.h" #undef DEBUG_AREA #define DEBUG_AREA "client.resourcecontrol" 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 = ResourceAccessFactory::instance().getAccess(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 }); } KAsync::Job ResourceControl::start(const QByteArray &identifier) { Trace() << "start " << identifier; auto time = QSharedPointer::create(); time->start(); auto resourceAccess = ResourceAccessFactory::instance().getAccess(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 = ResourceAccessFactory::instance().getAccess(resource); resourceAccess->open(); resourceAccess->synchronizeResource(false, true).then([&future, resourceAccess]() { future.setFinished(); }).exec(); }); } 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 = ResourceAccessFactory::instance().getAccess(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(); } } }); }); } #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