diff options
Diffstat (limited to 'common/resourcecontrol.cpp')
-rw-r--r-- | common/resourcecontrol.cpp | 49 |
1 files changed, 19 insertions, 30 deletions
diff --git a/common/resourcecontrol.cpp b/common/resourcecontrol.cpp index 5e8e09a..ea660c5 100644 --- a/common/resourcecontrol.cpp +++ b/common/resourcecontrol.cpp | |||
@@ -37,38 +37,27 @@ KAsync::Job<void> ResourceControl::shutdown(const QByteArray &identifier) | |||
37 | SinkTrace() << "shutdown " << identifier; | 37 | SinkTrace() << "shutdown " << identifier; |
38 | auto time = QSharedPointer<QTime>::create(); | 38 | auto time = QSharedPointer<QTime>::create(); |
39 | time->start(); | 39 | time->start(); |
40 | return ResourceAccess::connectToServer(identifier) | 40 | |
41 | .then<void, QSharedPointer<QLocalSocket>>( | 41 | auto resourceAccess = ResourceAccessFactory::instance().getAccess(identifier, ResourceConfig::getResourceType(identifier)); |
42 | [identifier, time](const KAsync::Error &error, QSharedPointer<QLocalSocket> socket) { | 42 | return resourceAccess->shutdown() |
43 | if (error) { | 43 | .addToContext(resourceAccess) |
44 | SinkTrace() << "Resource is already closed."; | 44 | .then<void>([resourceAccess, time](KAsync::Future<void> &future) { |
45 | // Resource isn't started, nothing to shutdown | 45 | SinkTrace() << "Shutdown command complete, waiting for shutdown." << Log::TraceTime(time->elapsed()); |
46 | return KAsync::null(); | 46 | if (!resourceAccess->isReady()) { |
47 | future.setFinished(); | ||
48 | return; | ||
49 | } | ||
50 | auto guard = new QObject; | ||
51 | QObject::connect(resourceAccess.data(), &ResourceAccess::ready, guard, [&future, guard](bool ready) { | ||
52 | if (!ready) { | ||
53 | //Protect against callback getting called twice. | ||
54 | delete guard; | ||
55 | future.setFinished(); | ||
47 | } | 56 | } |
48 | // We can't currently reuse the socket | ||
49 | socket->close(); | ||
50 | auto resourceAccess = ResourceAccessFactory::instance().getAccess(identifier, ResourceConfig::getResourceType(identifier)); | ||
51 | resourceAccess->open(); | ||
52 | return resourceAccess->sendCommand(Sink::Commands::ShutdownCommand) | ||
53 | .addToContext(resourceAccess) | ||
54 | .then<void>([resourceAccess, time](KAsync::Future<void> &future) { | ||
55 | SinkTrace() << "Shutdown command complete, waiting for shutdown." << Log::TraceTime(time->elapsed()); | ||
56 | if (!resourceAccess->isReady()) { | ||
57 | future.setFinished(); | ||
58 | return; | ||
59 | } | ||
60 | auto guard = new QObject; | ||
61 | QObject::connect(resourceAccess.data(), &ResourceAccess::ready, guard, [&future, guard](bool ready) { | ||
62 | if (!ready) { | ||
63 | //Protect against callback getting called twice. | ||
64 | delete guard; | ||
65 | future.setFinished(); | ||
66 | } | ||
67 | }); | ||
68 | }).then([time] { | ||
69 | SinkTrace() << "Shutdown complete." << Log::TraceTime(time->elapsed()); | ||
70 | }); | ||
71 | }); | 57 | }); |
58 | }).then([time] { | ||
59 | SinkTrace() << "Shutdown complete." << Log::TraceTime(time->elapsed()); | ||
60 | }); | ||
72 | } | 61 | } |
73 | 62 | ||
74 | KAsync::Job<void> ResourceControl::start(const QByteArray &identifier) | 63 | KAsync::Job<void> ResourceControl::start(const QByteArray &identifier) |