From e029f9c61536151448dbe2c55b4d8ad6107ecce8 Mon Sep 17 00:00:00 2001 From: liranbg Date: Thu, 18 Sep 2014 03:34:49 +0300 Subject: [PATCH] add network checking. now we can handle network link errors --- main/mainscreen.cpp | 26 ++++----- src/jceConnection/jcesslclient.cpp | 91 +++++++++++++++++------------- src/jceConnection/jcesslclient.h | 11 ++-- src/jceSettings/jcelogin.cpp | 1 - 4 files changed, 72 insertions(+), 57 deletions(-) diff --git a/main/mainscreen.cpp b/main/mainscreen.cpp index cd0eb14..89c73bc 100644 --- a/main/mainscreen.cpp +++ b/main/mainscreen.cpp @@ -185,7 +185,9 @@ void MainScreen::on_ratesButton_clicked() int status = 0; if (loginHandel->isLoggedInFlag()) { - if ((status = loginHandel->makeGradeRequest(ui->spinBoxCoursesFromYear->value(),ui->spinBoxCoursesToYear->value(),ui->spinBoxCoursesFromSemester->value(),ui->spinBoxCoursesToSemester->value())) == jceLogin::JCE_GRADE_PAGE_PASSED) + if ((status = loginHandel->makeGradeRequest(ui->spinBoxCoursesFromYear->value(), + ui->spinBoxCoursesToYear->value(),ui->spinBoxCoursesFromSemester->value(), + ui->spinBoxCoursesToSemester->value())) == jceLogin::JCE_GRADE_PAGE_PASSED) { pageString = loginHandel->getCurrentPageContect(); courseTableMgr->setCoursesList(pageString); @@ -283,13 +285,13 @@ void MainScreen::on_actionCredits_triggered() QMessageBox::about(this, "About", tr("CREDITS-ROOL-UP1") + " v1.0

" + tr("CREDITS-ROOL-UP2")+"
GNU LESSER GENERAL PUBLIC LICENSE V2
" + tr("CREDITS-ROOL-UP3")+"
" - "jceAverageCalculator Repository" - "

"+tr("CREDITS-ROOL-UP4")+" Jce Connection

" + "jceAverageCalculator Repository" + "

"+tr("CREDITS-ROOL-UP4")+" Jce Connection

" +tr("DevBy")+":" - ""); + ""); } void MainScreen::on_actionExit_triggered() { @@ -301,13 +303,11 @@ void MainScreen::on_actionHow_To_triggered() "How To.." ""); - } diff --git a/src/jceConnection/jcesslclient.cpp b/src/jceConnection/jcesslclient.cpp index bc28116..5e54774 100644 --- a/src/jceConnection/jcesslclient.cpp +++ b/src/jceConnection/jcesslclient.cpp @@ -1,18 +1,21 @@ #include "jcesslclient.h" + /** * @brief jceSSLClient::jceSSLClient Constructer, setting the signals */ -jceSSLClient::jceSSLClient() : flag(false), packet("") +jceSSLClient::jceSSLClient() : flag(false), packet(""), networkConf() { //setting signals connect(this,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(checkErrors(QAbstractSocket::SocketError))); connect(this,SIGNAL(connected()),this,SLOT(setConnected())); connect(this,SIGNAL(encrypted()),this,SLOT(setEncrypted())); connect(this,SIGNAL(disconnected()),this,SLOT(setDisconnected())); + connect(&networkConf,SIGNAL(onlineStateChanged(bool)),this,SLOT(setOnlineState(bool))); //loop event will connect the server, and when it is connected, it will quit - but connection will be open - connect(this, SIGNAL(encrypted()), &loop, SLOT(quit())); - connect(this,SIGNAL(error(QAbstractSocket::SocketError)),&loop,SLOT(quit())); + connect(this, SIGNAL(encrypted()), &loop, SLOT(quit())); + connect(this,SIGNAL(error(QAbstractSocket::SocketError)),&loop,SLOT(quit())); + } @@ -51,37 +54,16 @@ bool jceSSLClient::makeConnect(QString server, int port) */ bool jceSSLClient::makeDiconnect() { - qDebug() << "jceSSLClient::makeDiconnect;"; + if (loop.isRunning()) - { - qWarning() << "jceSSLClient::makeDiconnect; Killing connection thread"; + { + qWarning() << Q_FUNC_INFO << "Killing connection thread"; loop.exit(); - } - if (state() == QAbstractSocket::SocketState::UnconnectedState) - { - qDebug() << "jceSSLClient::makeDiconnect; Disconnected with [UnconnectedState]"; - flag = false; } - else if (state() == QAbstractSocket::SocketState::ConnectedState) - { - qWarning() << "jceSSLClient::makeDiconnect; Disconnecting with [ConnectedState] "; - abort(); - if (state() != QAbstractSocket::SocketState::UnconnectedState) - { - qWarning() << "jceSSLClient::makeDiconnect; still open! recursion call to disconnect"; - abort();//still connected? ensure the disconnection - flag = makeDiconnect(); //recursion call! - } - flag = false; - } - else - { - qWarning() << "jceSSLClient::makeDiconnect; Disconnecting [else] "; - abort(); //ensure the disconnection - flag = false; - } - qDebug() << "jceSSLClient::makeDiconnect; disconnect return with " << flag; - return flag; + qDebug() << Q_FUNC_INFO << "emitting disconnected()"; + emit disconnected(); + return isConnected(); + } /** @@ -101,8 +83,17 @@ bool jceSSLClient::isConnected() } else if (state() == QAbstractSocket::SocketState::ConnectedState) { - flag = true; + if (this->networkConf.isOnline()) + flag = true; + else + { + this->setSocketState(QAbstractSocket::SocketState::UnconnectedState); + flag = false; + } + } + if (!this->networkConf.isOnline()) //no link, ethernet\wifi + flag = false; return flag; } /** @@ -177,6 +168,21 @@ void jceSSLClient::readIt() packet.append(p); }while (p.size() > 0); +} + +void jceSSLClient::setOnlineState(bool isOnline) +{ + qWarning() << Q_FUNC_INFO << "isOnline status change: " << isOnline; + if (isOnline) + { + this->makeConnect(); + + } + else + { + this->makeDiconnect(); + } + } /** * @brief jceSSLClient::setConnected called when signaled with connected, calling the encryption function @@ -186,10 +192,13 @@ void jceSSLClient::setConnected() waitForEncrypted(); } /** - * @brief jceSSLClient::setDisconnected called when signaled with disconnected, setting flag to false + * @brief jceSSLClient::setDisconnected closing socket, updating state and setting flag to false */ void jceSSLClient::setDisconnected() { + qDebug() << Q_FUNC_INFO << "DISCONNECTED"; + this->setSocketState(QAbstractSocket::SocketState::UnconnectedState); + abort(); flag = false; } /** @@ -199,8 +208,7 @@ void jceSSLClient::setEncrypted() { setReadBufferSize(10000); setSocketOption(QAbstractSocket::KeepAliveOption,1); - if (state() == QAbstractSocket::SocketState::ConnectedState) - flag = true; + if (isConnected()); else { qWarning() << "jceSSLClient::setEncrypted(); Connection status didnt change!"; @@ -226,6 +234,7 @@ void jceSSLClient::showIfErrorMsg() break; case QAbstractSocket::SocketError::RemoteHostClosedError: errorString = QObject::tr("RemoteHostClosedError"); + relevantError = true; break; case QAbstractSocket::SocketError::HostNotFoundError: @@ -240,8 +249,12 @@ void jceSSLClient::showIfErrorMsg() break; case QAbstractSocket::SocketError::SocketTimeoutError: errorString = QObject::tr("SocketTimeoutError"); - if (!isConnected()) + + if (state() == QAbstractSocket::SocketState::UnconnectedState) + { + relevantError = true; + } break; case QAbstractSocket::SocketError::DatagramTooLargeError: errorString = QObject::tr("DatagramTooLargeError"); @@ -315,8 +328,10 @@ void jceSSLClient::showIfErrorMsg() */ void jceSSLClient::checkErrors(QAbstractSocket::SocketError a) { - qWarning() << "jceSSLClient::checkErrors; Var Error: " << a; - qWarning() << "jceSSLClient::checkErrors; Error: " << this->errorString(); + qWarning() << Q_FUNC_INFO << "isOnline?: " << this->networkConf.isOnline(); + qWarning() << Q_FUNC_INFO << "state is: " << state(); + qWarning() << Q_FUNC_INFO << "Var Error: " << a; + qWarning() << Q_FUNC_INFO << "Error: " << errorString(); showIfErrorMsg(); } diff --git a/src/jceConnection/jcesslclient.h b/src/jceConnection/jcesslclient.h index bb8913d..3d61644 100644 --- a/src/jceConnection/jcesslclient.h +++ b/src/jceConnection/jcesslclient.h @@ -6,10 +6,9 @@ #include #include #include +#include -#include -#include -#define milisTimeOut 3500 +#define milisTimeOut 4000 class jceSSLClient : QSslSocket { @@ -17,11 +16,11 @@ class jceSSLClient : QSslSocket public: jceSSLClient(); - bool makeConnect(QString server, int port); + bool makeConnect(QString server = "yedion.jce.ac.il", int port = 443); + bool makeDiconnect(); bool isConnected(); bool sendData(QString str); bool recieveData(QString &str, bool fast); - bool makeDiconnect(); void showIfErrorMsg(); private slots: @@ -30,12 +29,14 @@ private slots: void setEncrypted(); void setDisconnected(); void readIt(); + void setOnlineState(bool isOnline); private: bool flag; QString packet; QEventLoop loop; //handle the connection as thread + QNetworkConfigurationManager networkConf; //checking online }; diff --git a/src/jceSettings/jcelogin.cpp b/src/jceSettings/jcelogin.cpp index 7e5ccf0..76c9cf8 100644 --- a/src/jceSettings/jcelogin.cpp +++ b/src/jceSettings/jcelogin.cpp @@ -202,7 +202,6 @@ int jceLogin::getCalendar(int year, int semester) */ int jceLogin::getGrades(int fromYear, int toYear, int fromSemester, int toSemester) { - std::cout << fromYear << " " << toYear << " " << fromSemester << " " << toSemester << std::endl; if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getGradesPath(*jceA,fromYear, toYear, fromSemester, toSemester))))) { if (!(JceConnector->recieveData(*recieverPage,false)))