2016-04-19 20:41:59 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2016 Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include "RpcQueue.h"
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
RpcQueue::RpcQueue(QObject* parent) :
|
|
|
|
QObject(parent),
|
|
|
|
myTolerateErrors(false)
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(&myFutureWatcher, SIGNAL(finished()), SLOT(stepFinished()));
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
RpcResponseFuture RpcQueue::future()
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return myPromise.future();
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void RpcQueue::stepFinished()
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
RpcResponse result;
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (myFutureWatcher.future().isResultReadyAt(0))
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
result = myFutureWatcher.result();
|
|
|
|
RpcResponseFuture future = myFutureWatcher.future();
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// we can't handle network errors, abort queue and pass the error upwards
|
|
|
|
if (result.networkError != QNetworkReply::NoError)
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(!result.success);
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
myPromise.reportFinished(&result);
|
|
|
|
deleteLater();
|
|
|
|
return;
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// call user-handler for ordinary errors
|
|
|
|
if (!result.success && myNextErrorHandler)
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myNextErrorHandler(future);
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// run next request, if we have one to run and there was no error (or if we tolerate errors)
|
|
|
|
if ((result.success || myTolerateErrors) && !myQueue.isEmpty())
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
runNext(future);
|
|
|
|
return;
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(!myNextErrorHandler);
|
|
|
|
assert(myQueue.isEmpty());
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// one way or another, the last step returned nothing.
|
|
|
|
// assume it is OK and ensure that we're not going to give an empty response object to any of the next steps.
|
|
|
|
result.success = true;
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
myPromise.reportFinished(&result);
|
|
|
|
deleteLater();
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void RpcQueue::runNext(RpcResponseFuture const& response)
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(!myQueue.isEmpty());
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-07-15 05:46:31 +00:00
|
|
|
RpcResponseFuture const oldFuture = myFutureWatcher.future();
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
auto next = myQueue.dequeue();
|
|
|
|
myNextErrorHandler = next.second;
|
|
|
|
myFutureWatcher.setFuture((next.first)(response));
|
|
|
|
|
|
|
|
if (oldFuture != myFutureWatcher.future())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (myQueue.isEmpty())
|
|
|
|
{
|
|
|
|
deleteLater();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void RpcQueue::run()
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
runNext(RpcResponseFuture());
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|