##// END OF EJS Templates
Some WIP refactoring, trying to remove TimeController object...
Some WIP refactoring, trying to remove TimeController object SciQLOP core should be usable OOTB without creating controllers objects. Time range should be given on variable creation not taken from a global object. Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>

File last commit:

r1344:213f5ecfb65d
r1345:ce477e992869
Show More
Downloader.cpp
123 lines | 4.0 KiB | text/x-c | CppLexer
Really basic implementation of Downloader which might replace current...
r1342 #include <Network/Downloader.h>
#include <memory>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QAuthenticator>
#include <QVariant>
#include <QHash>
#include <QPair>
#include <QCoreApplication>
Basic Async implementation of downloader...
r1343 #include <QReadWriteLock>
Really basic implementation of Downloader which might replace current...
r1342
class Downloader::p_Downloader
{
using login_pair=QPair<QString,QString>;
QNetworkAccessManager manager;
QHash<QString,login_pair> auth;
Basic Async implementation of downloader...
r1343 QReadWriteLock pending_requests_lock;
QHash<QUuid,QNetworkReply*> pending_requests;
QNetworkRequest buildRequest(const QString& url, const QString &user="", const QString &passwd="")
{
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "SciQLop 1.0");
if(user!="" and passwd!="")
{
//might grow quickly since we can have tons of URLs for the same host
auth[url]=login_pair(user,passwd);
QString login = "Basic "+user+":"+passwd;
request.setRawHeader("Authorization",login.toLocal8Bit());
}
return request;
}
Really basic implementation of Downloader which might replace current...
r1342
public:
explicit p_Downloader()
{
auto login_bambda = [this](QNetworkReply * reply, QAuthenticator * authenticator)
{
if(auth.contains(reply->url().toString()))
{
auto login = auth[reply->url().toString()];
authenticator->setUser(login.first);
authenticator->setPassword(login.second);
}
};
Basic Async implementation of downloader...
r1343
Really basic implementation of Downloader which might replace current...
r1342 QObject::connect(&manager, &QNetworkAccessManager::authenticationRequired, login_bambda);
}
Response get(const QString& url, const QString &user="", const QString &passwd="")
{
Basic Async implementation of downloader...
r1343 QNetworkRequest request = buildRequest(url, user, passwd);
Really basic implementation of Downloader which might replace current...
r1342 QNetworkReply *reply = manager.get(request);
while (!reply->isFinished())
QCoreApplication::processEvents();
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
Basic Async implementation of downloader...
r1343 Response resp = Response(reply->readAll(), status_code.toInt());
delete reply;
Empty passwd storing hash map in downloader once download is complete...
r1344 if(user!="" and passwd!="")
auth.remove(url);
Basic Async implementation of downloader...
r1343 return resp;
}
QUuid getAsync(const QString &url, std::function<void (QUuid ,Response)> callback, const QString &user, const QString &passwd)
{
auto uuid = QUuid::createUuid();
QNetworkRequest request = buildRequest(url, user, passwd);
QNetworkReply *reply = manager.get(request);
Empty passwd storing hash map in downloader once download is complete...
r1344 auto callback_wrapper = [url, uuid, callback, this](){
Basic Async implementation of downloader...
r1343 QNetworkReply* reply;
{
QWriteLocker locker(&pending_requests_lock);
reply = pending_requests.take(uuid);
}
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
Response resp = Response(reply->readAll(), status_code.toInt());
Empty passwd storing hash map in downloader once download is complete...
r1344 auth.remove(url);
Basic Async implementation of downloader...
r1343 delete reply;
callback(uuid, resp);
};
QObject::connect(reply, &QNetworkReply::finished, callback_wrapper);
{
QWriteLocker locker(&pending_requests_lock);
pending_requests[uuid] = reply;
}
return uuid;
}
bool downloadFinished(QUuid uuid)
{
QReadLocker locker(&pending_requests_lock);
if(pending_requests.contains(uuid))
{
auto req = pending_requests[uuid];
return req->isFinished();
}
return true;
Really basic implementation of Downloader which might replace current...
r1342 }
};
Basic Async implementation of downloader...
r1343 Response Downloader::get(const QString &url, const QString &user, const QString &passwd)
Really basic implementation of Downloader which might replace current...
r1342 {
Basic Async implementation of downloader...
r1343 return Downloader::instance().impl->get(url, user, passwd);
Really basic implementation of Downloader which might replace current...
r1342 }
Basic Async implementation of downloader...
r1343 QUuid Downloader::getAsync(const QString &url, std::function<void (QUuid ,Response)> callback, const QString &user, const QString &passwd)
Really basic implementation of Downloader which might replace current...
r1342 {
Basic Async implementation of downloader...
r1343 return Downloader::instance().impl->getAsync(url, callback, user, passwd);
}
bool Downloader::downloadFinished(QUuid uuid)
{
return Downloader::instance().impl->downloadFinished(uuid);
Really basic implementation of Downloader which might replace current...
r1342 }
Downloader::Downloader()
:impl(spimpl::make_unique_impl<p_Downloader>())
{
}