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/CMakeLists.txt | 5 +- common/clientapi.cpp | 181 --------------------------------------------- common/clientapi.h | 65 +--------------- common/notifier.cpp | 69 +++++++++++++++++ common/notifier.h | 46 ++++++++++++ common/resourcecontrol.cpp | 129 ++++++++++++++++++++++++++++++++ common/resourcecontrol.h | 62 ++++++++++++++++ 7 files changed, 312 insertions(+), 245 deletions(-) delete mode 100644 common/clientapi.cpp create mode 100644 common/notifier.cpp create mode 100644 common/notifier.h create mode 100644 common/resourcecontrol.cpp create mode 100644 common/resourcecontrol.h (limited to 'common') diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 02335ad..0becad6 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -34,11 +34,12 @@ set(storage_LIBS lmdb) set(command_SRCS store.cpp + notifier.cpp + resourcecontrol.cpp modelresult.cpp definitions.cpp log.cpp entitybuffer.cpp - clientapi.cpp facadefactory.cpp commands.cpp facade.cpp @@ -104,6 +105,8 @@ add_clang_static_analysis(${PROJECT_NAME}) install(FILES store.h + notifier.h + resourcecontrol.h clientapi.h domain/applicationdomaintype.h query.h 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 - diff --git a/common/clientapi.h b/common/clientapi.h index a1d2469..8333987 100644 --- a/common/clientapi.h +++ b/common/clientapi.h @@ -20,67 +20,6 @@ #pragma once -#include "sink_export.h" -#include -#include - -#include - #include "store.h" - -#include "query.h" -#include "inspection.h" -#include "applicationdomaintype.h" - -class QAbstractItemModel; - -namespace Sink { -class ResourceAccess; -class Notification; - -class SINK_EXPORT Notifier { -public: - Notifier(const QSharedPointer &resourceAccess); - Notifier(const QByteArray &resourceInstanceIdentifier); - // Notifier(const QByteArrayList &resource); - void registerHandler(std::function); - -private: - class Private; - QSharedPointer d; -}; - - -namespace ResourceControl { - -template -KAsync::Job SINK_EXPORT inspect(const Inspection &inspectionCommand); - -/** - * Shutdown resource. - */ -KAsync::Job SINK_EXPORT shutdown(const QByteArray &resourceIdentifier); - -/** - * Start resource. - * - * The resource is ready for operation once this command completes. - * This command is only necessary if a resource was shutdown previously, - * otherwise the resource process will automatically start as necessary. - */ -KAsync::Job SINK_EXPORT start(const QByteArray &resourceIdentifier); - -/** - * Flushes any pending messages to disk - */ -KAsync::Job SINK_EXPORT flushMessageQueue(const QByteArrayList &resourceIdentifier); - -/** - * Flushes any pending messages that haven't been replayed to the source. - */ -KAsync::Job SINK_EXPORT flushReplayQueue(const QByteArrayList &resourceIdentifier); - -} - -} - +#include "notifier.h" +#include "resourcecontrol.h" diff --git a/common/notifier.cpp b/common/notifier.cpp new file mode 100644 index 0000000..e4248df --- /dev/null +++ b/common/notifier.cpp @@ -0,0 +1,69 @@ +/* + * 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 "notifier.h" + +#include + +#include "resourceaccess.h" +#include "log.h" + +using namespace Sink; + +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; +} diff --git a/common/notifier.h b/common/notifier.h new file mode 100644 index 0000000..d16a311 --- /dev/null +++ b/common/notifier.h @@ -0,0 +1,46 @@ +/* + * 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 . + */ + +#pragma once + +#include "sink_export.h" +#include +#include + +#include + +class QAbstractItemModel; + +namespace Sink { +class ResourceAccess; +class Notification; + +class SINK_EXPORT Notifier { +public: + Notifier(const QSharedPointer &resourceAccess); + Notifier(const QByteArray &resourceInstanceIdentifier); + void registerHandler(std::function); + +private: + class Private; + QSharedPointer d; +}; + +} diff --git a/common/resourcecontrol.cpp b/common/resourcecontrol.cpp new file mode 100644 index 0000000..4a6f9b4 --- /dev/null +++ b/common/resourcecontrol.cpp @@ -0,0 +1,129 @@ +/* + * 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 = 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(); + } + } + }); + }); +} + +#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 + diff --git a/common/resourcecontrol.h b/common/resourcecontrol.h new file mode 100644 index 0000000..5bfa67f --- /dev/null +++ b/common/resourcecontrol.h @@ -0,0 +1,62 @@ +/* + * 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 . + */ + +#pragma once + +#include "sink_export.h" +#include + +#include + +#include "inspection.h" + +namespace Sink { +namespace ResourceControl { + +template +KAsync::Job SINK_EXPORT inspect(const Inspection &inspectionCommand); + +/** + * Shutdown resource. + */ +KAsync::Job SINK_EXPORT shutdown(const QByteArray &resourceIdentifier); + +/** + * Start resource. + * + * The resource is ready for operation once this command completes. + * This command is only necessary if a resource was shutdown previously, + * otherwise the resource process will automatically start as necessary. + */ +KAsync::Job SINK_EXPORT start(const QByteArray &resourceIdentifier); + +/** + * Flushes any pending messages to disk + */ +KAsync::Job SINK_EXPORT flushMessageQueue(const QByteArrayList &resourceIdentifier); + +/** + * Flushes any pending messages that haven't been replayed to the source. + */ +KAsync::Job SINK_EXPORT flushReplayQueue(const QByteArrayList &resourceIdentifier); + + } +} + -- cgit v1.2.3