#include "network.h"
#include <QDebug>
#define NETWORK_TIMEOUT_MS 15000
#define NETWORK_GET_INTERVAL 2000
network
::network(QString server,
/* QString username,QString password,*/QObject *parent
) :{
_host = server;
// _username = username;
// _password = password;
nam = new QNetworkAccessManager(this);
connect(nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(namfinished(QNetworkReply*)));
connect(nam,SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),this,SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
connect(nam,SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),this,SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
connect(aborter,SIGNAL(timeout()),this,SLOT(timeout()));
}
void network::startTesting()
{
qDebug
() <<
QDateTime::currentDateTime().
toString("hh:mm:ss:zzz") <<
"Test is running....";
QTimer::singleShot(NETWORK_GET_INTERVAL,
this,
SLOT(doCheck
()));
}
QNetworkRequest network::_initReq()
{
down = 0;
QNetworkRequest req(_host);
// QString uagent = "User-Agent";
// QString uvalue = "timeOutTestSampleApp";
// req.setRawHeader(uagent.toAscii(), uvalue.toAscii());
// QString concatenated = _username + ":" + _password;
// QByteArray data = concatenated.toLocal8Bit().toBase64();
// QString headerData = "Basic " + data;
// req.setRawHeader("Authorization", headerData.toLocal8Bit());
return req;
}
void network::downloadProgress(qint64 received, qint64 total)
{
down = received;
if(aborter->isActive())
aborter->stop();
aborter->start(NETWORK_TIMEOUT_MS);
}
void network::doCheck()
{
QNetworkRequest req = _initReq();
currentReply = nam->get(req);
connect(currentReply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(reply_error(QNetworkReply::NetworkError)));
connect(currentReply,SIGNAL(finished()),this,SLOT(replyfinished()));
connect(currentReply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadProgress(qint64,qint64)));
aborter->start(NETWORK_TIMEOUT_MS);
qDebug
() <<
QDateTime::currentDateTime().
toString("hh:mm:ss:zzz") << currentReply <<
" Sending GET request ";
if(!currentReply->isRunning())
qDebug
() <<
QDateTime::currentDateTime().
toString("hh:mm:ss:zzz") << currentReply <<
" Is NOT running!!";
}
void network::replyfinished()
{
qDebug
() <<
QDateTime::currentDateTime().
toString("hh:mm:ss:zzz") << this
->sender
() <<
" Finished called ";
}
void network::timeout()
{
if(currentReply)
{
qDebug
() <<
QDateTime::currentDateTime().
toString("hh:mm:ss:zzz") << currentReply <<
" Aborting GET request";
currentReply->abort();
}
else
qDebug
() <<
QDateTime::currentDateTime().
toString("hh:mm:ss:zzz") <<
" Fatal error... timeout with invalid qnetworkreply object :S";
}
void network::reply_error(QNetworkReply::NetworkError err)
{
qDebug
() <<
QDateTime::currentDateTime().
toString("hh:mm:ss:zzz") << this
->sender
() <<
" Single error " << err;
}
void network::authenticationRequired(QNetworkReply *rep, QAuthenticator *auth)
{
qDebug
() <<
QDateTime::currentDateTime().
toString("hh:mm:ss:zzz") << rep <<
" AuthenticationRequired called " << auth;
}
void network::sslErrors(QNetworkReply *rep, const QList<QSslError> &errors)
{
qDebug
() <<
QDateTime::currentDateTime().
toString("hh:mm:ss:zzz") <<
"SslErrors called";
rep->ignoreSslErrors();
}
void network::namfinished(QNetworkReply *rep)
{
if(rep->error() == QNetworkReply::NoError)
qDebug
() <<
QDateTime::currentDateTime().
toString("hh:mm:ss:zzz") << rep <<
" Finished GET request without error bytesReceived == " << down;
else
qDebug
() <<
QDateTime::currentDateTime().
toString("hh:mm:ss:zzz") << rep <<
" Finished GET request with error " <<
" ERR( " << rep
->errorString
() <<
" ) bytesReceived == ";
QTimer::singleShot(NETWORK_GET_INTERVAL,
this,
SLOT(doCheck
()));
rep->deleteLater();
}