##// END OF EJS Templates
Added TimeSeries library...
Added TimeSeries library Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>

File last commit:

r34:8f50e908dd31
r57:0093c733b640
Show More
Downloader.cpp
152 lines | 5.1 KiB | text/x-c | CppLexer
First init from SciQLop Core module...
r0 #include <Network/Downloader.h>
#include <memory>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QAuthenticator>
#include <QVariant>
#include <QHash>
#include <QCoreApplication>
#include <QReadWriteLock>
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 #include <QMutex>
Removed old IDataProvider interface, added small tweak in downloader...
r28 #include <QThread>
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 #include <QObject>
#include <QtConcurrent/QtConcurrent>
First init from SciQLop Core module...
r0
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 class Downloader::p_Downloader : public QObject
First init from SciQLop Core module...
r0 {
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 Q_OBJECT
First init from SciQLop Core module...
r0 using login_pair=QPair<QString,QString>;
QNetworkAccessManager manager;
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 QMutex manager_lock;
QReadWriteLock auth_lock;
First init from SciQLop Core module...
r0 QHash<QString,login_pair> auth;
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;
}
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 signals:
void _get_sig(const QNetworkRequest& request,std::function<void (QNetworkReply* reply)> callback);
private slots:
void _get(const QNetworkRequest& request,std::function<void (QNetworkReply* reply)> callback)
{
QNetworkReply *reply = manager.get(request);
QObject::connect(reply, &QNetworkReply::finished, [reply=reply,callback=callback](){callback(reply);});
}
First init from SciQLop Core module...
r0
public:
explicit p_Downloader()
{
auto login_bambda = [this](QNetworkReply * reply, QAuthenticator * authenticator)
{
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 QWriteLocker loc{&auth_lock};
First init from SciQLop Core module...
r0 if(auth.contains(reply->url().toString()))
{
auto login = auth[reply->url().toString()];
authenticator->setUser(login.first);
authenticator->setPassword(login.second);
}
};
QObject::connect(&manager, &QNetworkAccessManager::authenticationRequired, login_bambda);
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 QObject::connect(this, &p_Downloader::_get_sig, this, &p_Downloader::_get, Qt::QueuedConnection);
First init from SciQLop Core module...
r0 }
Response get(const QString& url, const QString &user="", const QString &passwd="")
{
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 QNetworkAccessManager manager;
auto login_bambda = [user=user,passwd=passwd](QNetworkReply * reply, QAuthenticator * authenticator)
{
authenticator->setUser(user);
authenticator->setPassword(passwd);
};
QObject::connect(&manager, &QNetworkAccessManager::authenticationRequired, login_bambda);
Response resp;
First init from SciQLop Core module...
r0 QNetworkRequest request = buildRequest(url, user, passwd);
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 auto reply = manager.get(request);
while (reply->isRunning())
Removed old IDataProvider interface, added small tweak in downloader...
r28 {
First init from SciQLop Core module...
r0 QCoreApplication::processEvents();
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 QThread::usleep(1000);
Removed old IDataProvider interface, added small tweak in downloader...
r28 }
First init from SciQLop Core module...
r0 QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 resp = Response(reply->readAll(), status_code.toInt());
First init from SciQLop Core module...
r0 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);
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 manager_lock.lock();
First init from SciQLop Core module...
r0 QNetworkReply *reply = manager.get(request);
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 manager_lock.unlock();
First init from SciQLop Core module...
r0 auto callback_wrapper = [url, uuid, callback, this](){
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());
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 {
QWriteLocker loc{&auth_lock};
auth.remove(url);
}
First init from SciQLop Core module...
r0 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;
}
};
Response Downloader::get(const QString &url, const QString &user, const QString &passwd)
{
return Downloader::instance().impl->get(url, user, passwd);
}
QUuid Downloader::getAsync(const QString &url, std::function<void (QUuid ,Response)> callback, const QString &user, const QString &passwd)
{
return Downloader::instance().impl->getAsync(url, callback, user, passwd);
}
bool Downloader::downloadFinished(QUuid uuid)
{
return Downloader::instance().impl->downloadFinished(uuid);
}
Downloader::Downloader()
:impl(spimpl::make_unique_impl<p_Downloader>())
{
}
Made downloader thread safe, added debugging helper for DateTimeRange...
r34 #include "Downloader.moc"