summaryrefslogtreecommitdiffstats
path: root/common/resourcecontrol.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-02-10 11:02:39 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-02-10 11:02:39 +0100
commit668cead7d8f98615d90dbc933f194105b3cf0bc3 (patch)
tree596ef6d2cf0f90ccbeb399f60d7904e8821a7e9a /common/resourcecontrol.cpp
parent0991561c9630fcb89b9666b8d57c2b7ea592fec6 (diff)
downloadsink-668cead7d8f98615d90dbc933f194105b3cf0bc3.tar.gz
sink-668cead7d8f98615d90dbc933f194105b3cf0bc3.zip
Moved Notifier and ResourceAccess to separate files.
Diffstat (limited to 'common/resourcecontrol.cpp')
-rw-r--r--common/resourcecontrol.cpp129
1 files changed, 129 insertions, 0 deletions
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 @@
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 //FIXME JOBAPI this is only required because we don't care about the return value of connectToServer
58 .template then<void>([](){});
59}
60
61KAsync::Job<void> ResourceControl::start(const QByteArray &identifier)
62{
63 Trace() << "start " << identifier;
64 auto time = QSharedPointer<QTime>::create();
65 time->start();
66 auto resourceAccess = QSharedPointer<Sink::ResourceAccess>::create(identifier);
67 resourceAccess->open();
68 return resourceAccess->sendCommand(Sink::Commands::PingCommand).then<void>([resourceAccess, time]() {
69 Trace() << "Start complete." << Log::TraceTime(time->elapsed());
70 });
71}
72
73KAsync::Job<void> ResourceControl::flushMessageQueue(const QByteArrayList &resourceIdentifier)
74{
75 Trace() << "flushMessageQueue" << resourceIdentifier;
76 return KAsync::iterate(resourceIdentifier)
77 .template each<void, QByteArray>([](const QByteArray &resource, KAsync::Future<void> &future) {
78 Trace() << "Flushing message queue " << resource;
79 auto resourceAccess = QSharedPointer<Sink::ResourceAccess>::create(resource);
80 resourceAccess->open();
81 resourceAccess->synchronizeResource(false, true).then<void>([&future, resourceAccess]() {
82 future.setFinished();
83 }).exec();
84 })
85 //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)
86 .template then<void>([](){});
87}
88
89KAsync::Job<void> ResourceControl::flushReplayQueue(const QByteArrayList &resourceIdentifier)
90{
91 return flushMessageQueue(resourceIdentifier);
92}
93
94template <class DomainType>
95KAsync::Job<void> ResourceControl::inspect(const Inspection &inspectionCommand)
96{
97 auto resource = inspectionCommand.resourceIdentifier;
98
99 auto time = QSharedPointer<QTime>::create();
100 time->start();
101 Trace() << "Sending inspection " << resource;
102 auto resourceAccess = QSharedPointer<Sink::ResourceAccess>::create(resource);
103 resourceAccess->open();
104 auto notifier = QSharedPointer<Sink::Notifier>::create(resourceAccess);
105 auto id = QUuid::createUuid().toByteArray();
106 return resourceAccess->sendInspectionCommand(id, ApplicationDomain::getTypeName<DomainType>(), inspectionCommand.entityIdentifier, inspectionCommand.property, inspectionCommand.expectedValue)
107 .template then<void>([resourceAccess, notifier, id, time](KAsync::Future<void> &future) {
108 notifier->registerHandler([&future, id, time](const Notification &notification) {
109 if (notification.id == id) {
110 Trace() << "Inspection complete." << Log::TraceTime(time->elapsed());
111 if (notification.code) {
112 future.setError(-1, "Inspection returned an error: " + notification.message);
113 } else {
114 future.setFinished();
115 }
116 }
117 });
118 });
119}
120
121#define REGISTER_TYPE(T) template KAsync::Job<void> ResourceControl::inspect<T>(const Inspection &); \
122
123REGISTER_TYPE(ApplicationDomain::Event);
124REGISTER_TYPE(ApplicationDomain::Mail);
125REGISTER_TYPE(ApplicationDomain::Folder);
126REGISTER_TYPE(ApplicationDomain::SinkResource);
127
128} // namespace Sink
129