diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-07-27 02:26:47 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-09-15 16:14:19 +0200 |
commit | 26816c21f60450e461a5b6ef4ef740f6070ce278 (patch) | |
tree | 55e8aee03e094abf702438e6cd26233047345e70 /common/resourceaccess.cpp | |
parent | 9a9bb39f7641a818434cafa0dae0c8aa47124c0b (diff) | |
download | sink-26816c21f60450e461a5b6ef4ef740f6070ce278.tar.gz sink-26816c21f60450e461a5b6ef4ef740f6070ce278.zip |
Ported to the kasync revamp
Diffstat (limited to 'common/resourceaccess.cpp')
-rw-r--r-- | common/resourceaccess.cpp | 117 |
1 files changed, 58 insertions, 59 deletions
diff --git a/common/resourceaccess.cpp b/common/resourceaccess.cpp index 7b4d839..364616c 100644 --- a/common/resourceaccess.cpp +++ b/common/resourceaccess.cpp | |||
@@ -159,67 +159,65 @@ KAsync::Job<void> ResourceAccess::Private::tryToConnect() | |||
159 | // We may have a socket from the last connection leftover | 159 | // We may have a socket from the last connection leftover |
160 | socket.reset(); | 160 | socket.reset(); |
161 | auto counter = QSharedPointer<int>::create(0); | 161 | auto counter = QSharedPointer<int>::create(0); |
162 | return KAsync::dowhile([this]() -> bool { return !socket; }, | 162 | return KAsync::dowhile( |
163 | [this, counter](KAsync::Future<void> &future) { | 163 | [this, counter]() { |
164 | SinkTrace() << "Loop"; | 164 | SinkTrace() << "Loop"; |
165 | connectToServer(resourceInstanceIdentifier) | 165 | return connectToServer(resourceInstanceIdentifier) |
166 | .then<void, QSharedPointer<QLocalSocket>>( | 166 | .then<KAsync::ControlFlowFlag, QSharedPointer<QLocalSocket>>( |
167 | [this, &future](const QSharedPointer<QLocalSocket> &s) { | 167 | [this, counter](const KAsync::Error &error, const QSharedPointer<QLocalSocket> &s) { |
168 | Q_ASSERT(s); | 168 | if (error) { |
169 | socket = s; | 169 | static int waitTime = 10; |
170 | future.setFinished(); | 170 | static int timeout = 500; |
171 | }, | 171 | static int maxRetries = timeout / waitTime; |
172 | [&future, counter, this](int errorCode, const QString &errorString) { | 172 | if (*counter > maxRetries) { |
173 | static int waitTime = 10; | 173 | SinkTrace() << "Giving up"; |
174 | static int timeout = 500; | 174 | return KAsync::error<KAsync::ControlFlowFlag>("Failed to connect to socket"); |
175 | static int maxRetries = timeout / waitTime; | 175 | } else { |
176 | if (*counter > maxRetries) { | 176 | *counter = *counter + 1; |
177 | SinkTrace() << "Giving up"; | 177 | return KAsync::wait(waitTime).then(KAsync::value(KAsync::Continue)); |
178 | future.setError(-1, "Failed to connect to socket"); | 178 | } |
179 | } else { | 179 | } else { |
180 | KAsync::wait(waitTime).then<void>([&future]() { future.setFinished(); }).exec(); | 180 | Q_ASSERT(s); |
181 | socket = s; | ||
182 | return KAsync::value(KAsync::Break); | ||
181 | } | 183 | } |
182 | *counter = *counter + 1; | 184 | }); |
183 | }) | ||
184 | .exec(); | ||
185 | }); | 185 | }); |
186 | } | 186 | } |
187 | 187 | ||
188 | KAsync::Job<void> ResourceAccess::Private::initializeSocket() | 188 | KAsync::Job<void> ResourceAccess::Private::initializeSocket() |
189 | { | 189 | { |
190 | return KAsync::start<void>([this](KAsync::Future<void> &future) { | 190 | return KAsync::start<void>([this] { |
191 | SinkTrace() << "Trying to connect"; | 191 | SinkTrace() << "Trying to connect"; |
192 | connectToServer(resourceInstanceIdentifier) | 192 | return connectToServer(resourceInstanceIdentifier) |
193 | .then<void, QSharedPointer<QLocalSocket>>( | 193 | .then<void, QSharedPointer<QLocalSocket>>( |
194 | [this, &future](const QSharedPointer<QLocalSocket> &s) { | 194 | [this](const KAsync::Error &error, const QSharedPointer<QLocalSocket> &s) { |
195 | SinkTrace() << "Connected to resource, without having to start it."; | 195 | if (error) { |
196 | Q_ASSERT(s); | 196 | SinkTrace() << "Failed to connect, starting resource"; |
197 | socket = s; | 197 | // We failed to connect, so let's start the resource |
198 | future.setFinished(); | 198 | QStringList args; |
199 | }, | 199 | if (Sink::Test::testModeEnabled()) { |
200 | [this, &future](int errorCode, const QString &errorString) { | 200 | args << "--test"; |
201 | SinkTrace() << "Failed to connect, starting resource"; | 201 | } |
202 | // We failed to connect, so let's start the resource | 202 | args << resourceInstanceIdentifier << resourceName; |
203 | QStringList args; | 203 | qint64 pid = 0; |
204 | if (Sink::Test::testModeEnabled()) { | 204 | if (QProcess::startDetached("sink_synchronizer", args, QDir::homePath(), &pid)) { |
205 | args << "--test"; | 205 | SinkTrace() << "Started resource " << pid; |
206 | } | 206 | return tryToConnect() |
207 | args << resourceInstanceIdentifier << resourceName; | 207 | .onError([this](const KAsync::Error &error) { |
208 | qint64 pid = 0; | 208 | SinkWarning() << "Failed to connect to started resource"; |
209 | if (QProcess::startDetached("sink_synchronizer", args, QDir::homePath(), &pid)) { | 209 | }); |
210 | SinkTrace() << "Started resource " << pid; | 210 | } else { |
211 | tryToConnect() | 211 | SinkWarning() << "Failed to start resource"; |
212 | .then<void>([&future]() { future.setFinished(); }, | 212 | } |
213 | [this, &future](int errorCode, const QString &errorString) { | 213 | return KAsync::null(); |
214 | SinkWarning() << "Failed to connect to started resource"; | ||
215 | future.setError(errorCode, errorString); | ||
216 | }) | ||
217 | .exec(); | ||
218 | } else { | 214 | } else { |
219 | SinkWarning() << "Failed to start resource"; | 215 | SinkTrace() << "Connected to resource, without having to start it."; |
216 | Q_ASSERT(s); | ||
217 | socket = s; | ||
218 | return KAsync::null(); | ||
220 | } | 219 | } |
221 | }) | 220 | }); |
222 | .exec(); | ||
223 | }); | 221 | }); |
224 | } | 222 | } |
225 | 223 | ||
@@ -383,17 +381,18 @@ void ResourceAccess::open() | |||
383 | d->openingSocket = true; | 381 | d->openingSocket = true; |
384 | d->initializeSocket() | 382 | d->initializeSocket() |
385 | .then<void>( | 383 | .then<void>( |
386 | [this, time]() { | 384 | [this, time](const KAsync::Error &error) { |
387 | SinkTrace() << "Socket is initialized." << Log::TraceTime(time->elapsed()); | ||
388 | d->openingSocket = false; | 385 | d->openingSocket = false; |
389 | QObject::connect(d->socket.data(), &QLocalSocket::disconnected, this, &ResourceAccess::disconnected); | 386 | if (error) { |
390 | QObject::connect(d->socket.data(), SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(connectionError(QLocalSocket::LocalSocketError))); | 387 | SinkWarning() << "Failed to initialize socket " << error.errorMessage; |
391 | QObject::connect(d->socket.data(), &QIODevice::readyRead, this, &ResourceAccess::readResourceMessage); | 388 | } else { |
392 | connected(); | 389 | SinkTrace() << "Socket is initialized." << Log::TraceTime(time->elapsed()); |
393 | }, | 390 | QObject::connect(d->socket.data(), &QLocalSocket::disconnected, this, &ResourceAccess::disconnected); |
394 | [this](int error, const QString &errorString) { | 391 | QObject::connect(d->socket.data(), SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(connectionError(QLocalSocket::LocalSocketError))); |
395 | d->openingSocket = false; | 392 | QObject::connect(d->socket.data(), &QIODevice::readyRead, this, &ResourceAccess::readResourceMessage); |
396 | SinkWarning() << "Failed to initialize socket " << errorString; | 393 | connected(); |
394 | } | ||
395 | return KAsync::null(); | ||
397 | }) | 396 | }) |
398 | .exec(); | 397 | .exec(); |
399 | } | 398 | } |