diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-02-10 11:02:39 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-02-10 11:02:39 +0100 |
commit | 668cead7d8f98615d90dbc933f194105b3cf0bc3 (patch) | |
tree | 596ef6d2cf0f90ccbeb399f60d7904e8821a7e9a /common/clientapi.cpp | |
parent | 0991561c9630fcb89b9666b8d57c2b7ea592fec6 (diff) | |
download | sink-668cead7d8f98615d90dbc933f194105b3cf0bc3.tar.gz sink-668cead7d8f98615d90dbc933f194105b3cf0bc3.zip |
Moved Notifier and ResourceAccess to separate files.
Diffstat (limited to 'common/clientapi.cpp')
-rw-r--r-- | common/clientapi.cpp | 181 |
1 files changed, 0 insertions, 181 deletions
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 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 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 "clientapi.h" | ||
22 | |||
23 | #include <QtConcurrent/QtConcurrentRun> | ||
24 | #include <QTimer> | ||
25 | #include <QTime> | ||
26 | #include <QEventLoop> | ||
27 | #include <QAbstractItemModel> | ||
28 | #include <QDir> | ||
29 | #include <QUuid> | ||
30 | #include <functional> | ||
31 | #include <memory> | ||
32 | |||
33 | #include "resourceaccess.h" | ||
34 | #include "commands.h" | ||
35 | #include "resourcefacade.h" | ||
36 | #include "definitions.h" | ||
37 | #include "resourceconfig.h" | ||
38 | #include "facadefactory.h" | ||
39 | #include "modelresult.h" | ||
40 | #include "storage.h" | ||
41 | #include "log.h" | ||
42 | |||
43 | #undef DEBUG_AREA | ||
44 | #define DEBUG_AREA "client.clientapi" | ||
45 | |||
46 | namespace Sink | ||
47 | { | ||
48 | |||
49 | KAsync::Job<void> ResourceControl::shutdown(const QByteArray &identifier) | ||
50 | { | ||
51 | Trace() << "shutdown " << identifier; | ||
52 | auto time = QSharedPointer<QTime>::create(); | ||
53 | time->start(); | ||
54 | return ResourceAccess::connectToServer(identifier).then<void, QSharedPointer<QLocalSocket>>([identifier, time](QSharedPointer<QLocalSocket> socket, KAsync::Future<void> &future) { | ||
55 | //We can't currently reuse the socket | ||
56 | socket->close(); | ||
57 | auto resourceAccess = QSharedPointer<Sink::ResourceAccess>::create(identifier); | ||
58 | resourceAccess->open(); | ||
59 | resourceAccess->sendCommand(Sink::Commands::ShutdownCommand).then<void>([&future, resourceAccess, time]() { | ||
60 | Trace() << "Shutdown complete." << Log::TraceTime(time->elapsed()); | ||
61 | future.setFinished(); | ||
62 | }).exec(); | ||
63 | }, | ||
64 | [](int, const QString &) { | ||
65 | Trace() << "Resource is already closed."; | ||
66 | //Resource isn't started, nothing to shutdown | ||
67 | }) | ||
68 | //FIXME JOBAPI this is only required because we don't care about the return value of connectToServer | ||
69 | .template then<void>([](){}); | ||
70 | } | ||
71 | |||
72 | KAsync::Job<void> ResourceControl::start(const QByteArray &identifier) | ||
73 | { | ||
74 | Trace() << "start " << identifier; | ||
75 | auto time = QSharedPointer<QTime>::create(); | ||
76 | time->start(); | ||
77 | auto resourceAccess = QSharedPointer<Sink::ResourceAccess>::create(identifier); | ||
78 | resourceAccess->open(); | ||
79 | return resourceAccess->sendCommand(Sink::Commands::PingCommand).then<void>([resourceAccess, time]() { | ||
80 | Trace() << "Start complete." << Log::TraceTime(time->elapsed()); | ||
81 | }); | ||
82 | } | ||
83 | |||
84 | KAsync::Job<void> ResourceControl::flushMessageQueue(const QByteArrayList &resourceIdentifier) | ||
85 | { | ||
86 | Trace() << "flushMessageQueue" << resourceIdentifier; | ||
87 | return KAsync::iterate(resourceIdentifier) | ||
88 | .template each<void, QByteArray>([](const QByteArray &resource, KAsync::Future<void> &future) { | ||
89 | Trace() << "Flushing message queue " << resource; | ||
90 | auto resourceAccess = QSharedPointer<Sink::ResourceAccess>::create(resource); | ||
91 | resourceAccess->open(); | ||
92 | resourceAccess->synchronizeResource(false, true).then<void>([&future, resourceAccess]() { | ||
93 | future.setFinished(); | ||
94 | }).exec(); | ||
95 | }) | ||
96 | //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) | ||
97 | .template then<void>([](){}); | ||
98 | } | ||
99 | |||
100 | KAsync::Job<void> ResourceControl::flushReplayQueue(const QByteArrayList &resourceIdentifier) | ||
101 | { | ||
102 | return flushMessageQueue(resourceIdentifier); | ||
103 | } | ||
104 | |||
105 | template <class DomainType> | ||
106 | KAsync::Job<void> ResourceControl::inspect(const Inspection &inspectionCommand) | ||
107 | { | ||
108 | auto resource = inspectionCommand.resourceIdentifier; | ||
109 | |||
110 | auto time = QSharedPointer<QTime>::create(); | ||
111 | time->start(); | ||
112 | Trace() << "Sending inspection " << resource; | ||
113 | auto resourceAccess = QSharedPointer<Sink::ResourceAccess>::create(resource); | ||
114 | resourceAccess->open(); | ||
115 | auto notifier = QSharedPointer<Sink::Notifier>::create(resourceAccess); | ||
116 | auto id = QUuid::createUuid().toByteArray(); | ||
117 | return resourceAccess->sendInspectionCommand(id, ApplicationDomain::getTypeName<DomainType>(), inspectionCommand.entityIdentifier, inspectionCommand.property, inspectionCommand.expectedValue) | ||
118 | .template then<void>([resourceAccess, notifier, id, time](KAsync::Future<void> &future) { | ||
119 | notifier->registerHandler([&future, id, time](const Notification ¬ification) { | ||
120 | if (notification.id == id) { | ||
121 | Trace() << "Inspection complete." << Log::TraceTime(time->elapsed()); | ||
122 | if (notification.code) { | ||
123 | future.setError(-1, "Inspection returned an error: " + notification.message); | ||
124 | } else { | ||
125 | future.setFinished(); | ||
126 | } | ||
127 | } | ||
128 | }); | ||
129 | }); | ||
130 | } | ||
131 | |||
132 | class Sink::Notifier::Private { | ||
133 | public: | ||
134 | Private() | ||
135 | : context(new QObject) | ||
136 | { | ||
137 | |||
138 | } | ||
139 | QList<QSharedPointer<ResourceAccess> > resourceAccess; | ||
140 | QList<std::function<void(const Notification &)> > handler; | ||
141 | QSharedPointer<QObject> context; | ||
142 | }; | ||
143 | |||
144 | Notifier::Notifier(const QSharedPointer<ResourceAccess> &resourceAccess) | ||
145 | : d(new Sink::Notifier::Private) | ||
146 | { | ||
147 | QObject::connect(resourceAccess.data(), &ResourceAccess::notification, d->context.data(), [this](const Notification ¬ification) { | ||
148 | for (const auto &handler : d->handler) { | ||
149 | handler(notification); | ||
150 | } | ||
151 | }); | ||
152 | d->resourceAccess << resourceAccess; | ||
153 | } | ||
154 | |||
155 | Notifier::Notifier(const QByteArray &instanceIdentifier) | ||
156 | : d(new Sink::Notifier::Private) | ||
157 | { | ||
158 | auto resourceAccess = Sink::ResourceAccess::Ptr::create(instanceIdentifier); | ||
159 | resourceAccess->open(); | ||
160 | QObject::connect(resourceAccess.data(), &ResourceAccess::notification, d->context.data(), [this](const Notification ¬ification) { | ||
161 | for (const auto &handler : d->handler) { | ||
162 | handler(notification); | ||
163 | } | ||
164 | }); | ||
165 | d->resourceAccess << resourceAccess; | ||
166 | } | ||
167 | |||
168 | void Notifier::registerHandler(std::function<void(const Notification &)> handler) | ||
169 | { | ||
170 | d->handler << handler; | ||
171 | } | ||
172 | |||
173 | #define REGISTER_TYPE(T) template KAsync::Job<void> ResourceControl::inspect<T>(const Inspection &); \ | ||
174 | |||
175 | REGISTER_TYPE(ApplicationDomain::Event); | ||
176 | REGISTER_TYPE(ApplicationDomain::Mail); | ||
177 | REGISTER_TYPE(ApplicationDomain::Folder); | ||
178 | REGISTER_TYPE(ApplicationDomain::SinkResource); | ||
179 | |||
180 | } // namespace Sink | ||
181 | |||