summaryrefslogtreecommitdiffstats
path: root/common/resourcecontrol.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/resourcecontrol.cpp')
-rw-r--r--common/resourcecontrol.cpp125
1 files changed, 125 insertions, 0 deletions
diff --git a/common/resourcecontrol.cpp b/common/resourcecontrol.cpp
new file mode 100644
index 0000000..83558bd
--- /dev/null
+++ b/common/resourcecontrol.cpp
@@ -0,0 +1,125 @@
1/*
2 * Copyright (C) 2015 Christian Mollekopf <chrigi_1@fastmail.fm>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) version 3, or any
8 * later version accepted by the membership of KDE e.V. (or its
9 * successor approved by the membership of KDE e.V.), which shall
10 * act as a proxy defined in Section 6 of version 3 of the license.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "resourcecontrol.h"
22
23#include <QTime>
24#include <QUuid>
25#include <functional>
26
27#include "resourceaccess.h"
28#include "commands.h"
29#include "log.h"
30#include "notifier.h"
31
32#undef DEBUG_AREA
33#define DEBUG_AREA "client.resourcecontrol"
34
35namespace Sink
36{
37
38KAsync::Job<void> ResourceControl::shutdown(const QByteArray &identifier)
39{
40 Trace() << "shutdown " << identifier;
41 auto time = QSharedPointer<QTime>::create();
42 time->start();
43 return ResourceAccess::connectToServer(identifier).then<void, QSharedPointer<QLocalSocket>>([identifier, time](QSharedPointer<QLocalSocket> socket, KAsync::Future<void> &future) {
44 //We can't currently reuse the socket
45 socket->close();
46 auto resourceAccess = QSharedPointer<Sink::ResourceAccess>::create(identifier);
47 resourceAccess->open();
48 resourceAccess->sendCommand(Sink::Commands::ShutdownCommand).then<void>([&future, resourceAccess, time]() {
49 Trace() << "Shutdown complete." << Log::TraceTime(time->elapsed());
50 future.setFinished();
51 }).exec();
52 },
53 [](int, const QString &) {
54 Trace() << "Resource is already closed.";
55 //Resource isn't started, nothing to shutdown
56 });
57}
58
59KAsync::Job<void> ResourceControl::start(const QByteArray &identifier)
60{
61 Trace() << "start " << identifier;
62 auto time = QSharedPointer<QTime>::create();
63 time->start();
64 auto resourceAccess = QSharedPointer<Sink::ResourceAccess>::create(identifier);
65 resourceAccess->open();
66 return resourceAccess->sendCommand(Sink::Commands::PingCommand).then<void>([resourceAccess, time]() {
67 Trace() << "Start complete." << Log::TraceTime(time->elapsed());
68 });
69}
70
71KAsync::Job<void> ResourceControl::flushMessageQueue(const QByteArrayList &resourceIdentifier)
72{
73 Trace() << "flushMessageQueue" << resourceIdentifier;
74 return KAsync::iterate(resourceIdentifier)
75 .template each<void, QByteArray>([](const QByteArray &resource, KAsync::Future<void> &future) {
76 Trace() << "Flushing message queue " << resource;
77 auto resourceAccess = QSharedPointer<Sink::ResourceAccess>::create(resource);
78 resourceAccess->open();
79 resourceAccess->synchronizeResource(false, true).then<void>([&future, resourceAccess]() {
80 future.setFinished();
81 }).exec();
82 });
83}
84
85KAsync::Job<void> ResourceControl::flushReplayQueue(const QByteArrayList &resourceIdentifier)
86{
87 return flushMessageQueue(resourceIdentifier);
88}
89
90template <class DomainType>
91KAsync::Job<void> ResourceControl::inspect(const Inspection &inspectionCommand)
92{
93 auto resource = inspectionCommand.resourceIdentifier;
94
95 auto time = QSharedPointer<QTime>::create();
96 time->start();
97 Trace() << "Sending inspection " << resource;
98 auto resourceAccess = QSharedPointer<Sink::ResourceAccess>::create(resource);
99 resourceAccess->open();
100 auto notifier = QSharedPointer<Sink::Notifier>::create(resourceAccess);
101 auto id = QUuid::createUuid().toByteArray();
102 return resourceAccess->sendInspectionCommand(id, ApplicationDomain::getTypeName<DomainType>(), inspectionCommand.entityIdentifier, inspectionCommand.property, inspectionCommand.expectedValue)
103 .template then<void>([resourceAccess, notifier, id, time](KAsync::Future<void> &future) {
104 notifier->registerHandler([&future, id, time](const Notification &notification) {
105 if (notification.id == id) {
106 Trace() << "Inspection complete." << Log::TraceTime(time->elapsed());
107 if (notification.code) {
108 future.setError(-1, "Inspection returned an error: " + notification.message);
109 } else {
110 future.setFinished();
111 }
112 }
113 });
114 });
115}
116
117#define REGISTER_TYPE(T) template KAsync::Job<void> ResourceControl::inspect<T>(const Inspection &); \
118
119REGISTER_TYPE(ApplicationDomain::Event);
120REGISTER_TYPE(ApplicationDomain::Mail);
121REGISTER_TYPE(ApplicationDomain::Folder);
122REGISTER_TYPE(ApplicationDomain::SinkResource);
123
124} // namespace Sink
125