From 32e4272f83137e8cad185923fc69f058b897cfad Mon Sep 17 00:00:00 2001 From: liranbg Date: Sun, 21 Sep 2014 13:31:26 +0300 Subject: [PATCH] server disconnection bug 1 --- src/jceConnection/jcesslclient.cpp | 17 +- src/jceConnection/jcesslclient.h | 2 +- src/jceSettings/jcelogin.cpp | 288 +++++++++++++++-------------- src/jceSettings/jcelogin.h | 14 +- 4 files changed, 178 insertions(+), 143 deletions(-) diff --git a/src/jceConnection/jcesslclient.cpp b/src/jceConnection/jcesslclient.cpp index 81d756f..a4e2ceb 100644 --- a/src/jceConnection/jcesslclient.cpp +++ b/src/jceConnection/jcesslclient.cpp @@ -26,18 +26,31 @@ jceSSLClient::jceSSLClient() : flag(false), packet(""), networkConf(), reConnect */ bool jceSSLClient::makeConnect(QString server, int port) { - qDebug() << Q_FUNC_INFO << "Making connection"; + if (reConnection) //reset reconnectiong flag + { + qDebug() << Q_FUNC_INFO << "Making Reconnection"; + } + else + qDebug() << Q_FUNC_INFO << "Making Connection"; + if (isConnected()) { qDebug() << Q_FUNC_INFO << "flag=true, calling makeDisconnect()"; makeDiconnect(); } + + qDebug() << Q_FUNC_INFO << "Connection to: " << server << "On Port: " << port; connectToHostEncrypted(server.toStdString().c_str(), port); loop.exec(); //starting connection, waiting to encryption and then it ends qDebug() << Q_FUNC_INFO << "returning the connection status: " << isConnected(); + if (reConnection) + { + reConnection = false; + emit serverDisconnectedbyRemote(); + } return isConnected(); } @@ -205,7 +218,7 @@ void jceSSLClient::setDisconnected() */ void jceSSLClient::setEncrypted() { - qDebug() << Q_FUNC_INFO << "ENCRYPTED"; + qDebug() << Q_FUNC_INFO << "ENCRYPTED"; setReadBufferSize(10000); setSocketOption(QAbstractSocket::KeepAliveOption,true); flag = true; diff --git a/src/jceConnection/jcesslclient.h b/src/jceConnection/jcesslclient.h index 8b8d0ef..f67b2ac 100644 --- a/src/jceConnection/jcesslclient.h +++ b/src/jceConnection/jcesslclient.h @@ -10,7 +10,7 @@ #define milisTimeOut 4000 -class jceSSLClient : QSslSocket +class jceSSLClient : public QSslSocket { Q_OBJECT public: diff --git a/src/jceSettings/jcelogin.cpp b/src/jceSettings/jcelogin.cpp index 76c9cf8..255c4a1 100644 --- a/src/jceSettings/jcelogin.cpp +++ b/src/jceSettings/jcelogin.cpp @@ -4,20 +4,21 @@ * @brief jceLogin::jceLogin * @param username pointer to allocated user settings */ -jceLogin::jceLogin(user * username) +jceLogin::jceLogin(user* username) { - this->recieverPage = new QString(); - this->jceA = username; - this->JceConnector = new jceSSLClient(); + this->recieverPage = new QString(); + this->jceA = username; + this->JceConnector = new jceSSLClient(); + JceConnector->connect(JceConnector,SIGNAL(serverDisconnectedbyRemote()),this,SLOT(reValidation())); } jceLogin::~jceLogin() { - this->jceA = NULL; - delete recieverPage; - delete JceConnector; - JceConnector = NULL; - recieverPage = NULL; + this->jceA = NULL; + delete recieverPage; + delete JceConnector; + JceConnector = NULL; + recieverPage = NULL; } /** * @brief jceLogin::makeConnection Connecting to JCE student web site with JceA (username object) and validate it. @@ -25,69 +26,69 @@ jceLogin::~jceLogin() */ void jceLogin::makeConnection() throw (jceStatus) { - qDebug() << "jceLogin::makeConnection(); connection to be make"; + qDebug() << "jceLogin::makeConnection(); connection to be make"; - if (this->recieverPage == NULL) - this->recieverPage = new QString(); + if (this->recieverPage == NULL) + this->recieverPage = new QString(); - if (JceConnector->makeConnect(dst_host,dst_port) == false) //couldnt make a connection - throw jceStatus::ERROR_ON_OPEN_SOCKET; + if (JceConnector->makeConnect(dst_host,dst_port) == false) //couldnt make a connection + throw jceStatus::ERROR_ON_OPEN_SOCKET; - int returnMode; //gets status according to called function of validation step - jceStatus status = jceStatus::JCE_NOT_CONNECTED; + int returnMode; //gets status according to called function of validation step + jceStatus status = jceStatus::JCE_NOT_CONNECTED; - returnMode = checkConnection(); //checking socket status. is connected? + returnMode = checkConnection(); //checking socket status. is connected? - if (returnMode == true) //connected to host + if (returnMode == true) //connected to host { - returnMode = makeFirstVisit(); - if (returnMode == true) //requst and send first validation + returnMode = makeFirstVisit(); + if (returnMode == true) //requst and send first validation { - status = jceStatus::JCE_START_VALIDATING_PROGRESS; - returnMode = checkValidation(); - if (returnMode == true) //check if username and password are matching + status = jceStatus::JCE_START_VALIDATING_PROGRESS; + returnMode = checkValidation(); + if (returnMode == true) //check if username and password are matching { - status = jceStatus::JCE_VALIDATION_PASSED; - returnMode = makeSecondVisit(); - if (returnMode == true) //siging in the website + status = jceStatus::JCE_VALIDATION_PASSED; + returnMode = makeSecondVisit(); + if (returnMode == true) //siging in the website { - qDebug() << "jceLogin::makeConnection(); Signed in succeesfully"; - status = jceStatus::JCE_YOU_ARE_IN; - setLoginFlag(true); + qDebug() << "jceLogin::makeConnection(); Signed in succeesfully"; + status = jceStatus::JCE_YOU_ARE_IN; + setLoginFlag(true); } - else if (returnMode == jceLogin::ERROR_ON_GETTING_INFO) + else if (returnMode == jceLogin::ERROR_ON_GETTING_INFO) { - status = jceLogin::ERROR_ON_GETTING_INFO; + status = jceLogin::ERROR_ON_GETTING_INFO; } - else if (returnMode == jceLogin::ERROR_ON_SEND_REQUEST) + else if (returnMode == jceLogin::ERROR_ON_SEND_REQUEST) { - status = jceLogin::ERROR_ON_SEND_REQUEST; + status = jceLogin::ERROR_ON_SEND_REQUEST; } - else - status = jceStatus::ERROR_ON_VALIDATION; + else + status = jceStatus::ERROR_ON_VALIDATION; } - else - status = jceStatus::ERROR_ON_VALIDATION; + else + status = jceStatus::ERROR_ON_VALIDATION; } - else if (returnMode == jceLogin::ERROR_ON_GETTING_INFO) + else if (returnMode == jceLogin::ERROR_ON_GETTING_INFO) { - status = jceLogin::ERROR_ON_GETTING_INFO; + status = jceLogin::ERROR_ON_GETTING_INFO; } - else if (returnMode == jceLogin::ERROR_ON_SEND_REQUEST) + else if (returnMode == jceLogin::ERROR_ON_SEND_REQUEST) { - status = jceLogin::ERROR_ON_SEND_REQUEST; + status = jceLogin::ERROR_ON_SEND_REQUEST; } - else - status = jceStatus::ERROR_ON_VALIDATION_USER_BLOCKED; + else + status = jceStatus::ERROR_ON_VALIDATION_USER_BLOCKED; } - else - status = jceStatus::JCE_NOT_CONNECTED; + else + status = jceStatus::JCE_NOT_CONNECTED; - //we throw status even if we are IN! - qDebug() << "jceLogin::makeConnection(); throw status: " << status; - throw status; + //we throw status even if we are IN! + qDebug() << "jceLogin::makeConnection(); throw status: " << status; + throw status; } /** @@ -96,10 +97,10 @@ void jceLogin::makeConnection() throw (jceStatus) */ bool jceLogin::checkConnection() const { - if (JceConnector->isConnected()) - return true; + if (JceConnector->isConnected()) + return true; - return false; + return false; } /** * @brief jceLogin::reConnect @@ -108,29 +109,29 @@ bool jceLogin::checkConnection() const */ void jceLogin::reConnect() throw (jceStatus) { - closeAll(); - if (this->JceConnector != NULL) - delete JceConnector; - this->recieverPage = new QString(); - this->JceConnector = new jceSSLClient(); + closeAll(); + if (this->JceConnector != NULL) + delete JceConnector; + this->recieverPage = new QString(); + this->JceConnector = new jceSSLClient(); - try - { - makeConnection(); - } - catch (jceLogin::jceStatus &a) - { - throw a; - } + try + { + makeConnection(); + } + catch (jceLogin::jceStatus &a) + { + throw a; + } } /** * @brief jceLogin::closeAll */ void jceLogin::closeAll() { - JceConnector->makeDiconnect(); - delete recieverPage; - recieverPage = NULL; + JceConnector->makeDiconnect(); + delete recieverPage; + recieverPage = NULL; } /** @@ -139,17 +140,17 @@ void jceLogin::closeAll() */ int jceLogin::makeFirstVisit() { - QString usr = jceA->getUsername(); - QString psw = jceA->getPassword(); - if (JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getFirstValidationStep(*jceA)))) + QString usr = jceA->getUsername(); + QString psw = jceA->getPassword(); + if (JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getFirstValidationStep(*jceA)))) { - if (!JceConnector->recieveData(*recieverPage,true)) - return jceLogin::ERROR_ON_GETTING_INFO; + if (!JceConnector->recieveData(*recieverPage,true)) + return jceLogin::ERROR_ON_GETTING_INFO; } - else - return jceLogin::ERROR_ON_SEND_REQUEST; + else + return jceLogin::ERROR_ON_SEND_REQUEST; - return true; + return true; } /** * @brief jceLogin::makeSecondVisit making the second validation step of jce student portal login @@ -157,19 +158,19 @@ int jceLogin::makeFirstVisit() */ int jceLogin::makeSecondVisit() { - QString usrid=jceA->getUserID(); - QString pswid=jceA->getHashedPassword(); - if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getSecondValidationStep(*jceA))))) + QString usrid=jceA->getUserID(); + QString pswid=jceA->getHashedPassword(); + if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getSecondValidationStep(*jceA))))) { - if (!(JceConnector->recieveData(*recieverPage,true))) - return jceLogin::ERROR_ON_GETTING_INFO; + if (!(JceConnector->recieveData(*recieverPage,true))) + return jceLogin::ERROR_ON_GETTING_INFO; - return true; + return true; } - else - return jceLogin::ERROR_ON_SEND_REQUEST; + else + return jceLogin::ERROR_ON_SEND_REQUEST; - return true; + return true; } /** * @brief jceLogin::getCalendar according to parameters, we make an HTML request and send it over socket to server @@ -179,17 +180,17 @@ int jceLogin::makeSecondVisit() */ int jceLogin::getCalendar(int year, int semester) { - if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getCalendar(*jceA,year,semester))))) + if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getCalendar(*jceA,year,semester))))) { - if (!(JceConnector->recieveData(*recieverPage,false))) - return jceLogin::ERROR_ON_GETTING_GRADES; - else - return jceLogin::JCE_GRADE_PAGE_PASSED; + if (!(JceConnector->recieveData(*recieverPage,false))) + return jceLogin::ERROR_ON_GETTING_GRADES; + else + return jceLogin::JCE_GRADE_PAGE_PASSED; } - else - return jceLogin::ERROR_ON_SEND_REQUEST; + else + return jceLogin::ERROR_ON_SEND_REQUEST; - return true; + return true; } /** @@ -202,17 +203,17 @@ int jceLogin::getCalendar(int year, int semester) */ int jceLogin::getGrades(int fromYear, int toYear, int fromSemester, int toSemester) { - if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getGradesPath(*jceA,fromYear, toYear, fromSemester, toSemester))))) + if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getGradesPath(*jceA,fromYear, toYear, fromSemester, toSemester))))) { - if (!(JceConnector->recieveData(*recieverPage,false))) - return jceLogin::ERROR_ON_GETTING_GRADES; - else - return jceLogin::JCE_GRADE_PAGE_PASSED; + if (!(JceConnector->recieveData(*recieverPage,false))) + return jceLogin::ERROR_ON_GETTING_GRADES; + else + return jceLogin::JCE_GRADE_PAGE_PASSED; } - else - return jceLogin::ERROR_ON_SEND_REQUEST; + else + return jceLogin::ERROR_ON_SEND_REQUEST; - return true; + return true; } /** @@ -222,43 +223,43 @@ int jceLogin::getGrades(int fromYear, int toYear, int fromSemester, int toSemest bool jceLogin::checkValidation() { - //finds the hashed password - QString constUserID_TAG = "value=\"-N"; - QString constHassID_TAG = "-A,-N"; - QString hasspass,hassid; - std::size_t hasspass_position1,hasspass_position2; - std::size_t id_position1,id_position2; + //finds the hashed password + QString constUserID_TAG = "value=\"-N"; + QString constHassID_TAG = "-A,-N"; + QString hasspass,hassid; + std::size_t hasspass_position1,hasspass_position2; + std::size_t id_position1,id_position2; - hasspass_position1 = this->recieverPage->toStdString().find(constHassID_TAG.toStdString()); //looking for hasspass index - if (hasspass_position1 == std::string::npos) //didnt find the tag - return false; - else - hasspass_position1 += constHassID_TAG.length(); //skip the index of tag - hasspass_position2 = this->recieverPage->toStdString().find(",-A,-A", hasspass_position1); - //finds the hass pass - if (hasspass_position2 != std::string::npos) //found the hasspass! storing it - hasspass = recieverPage->mid(hasspass_position1,hasspass_position2-hasspass_position1); - else - return false; - //finds the user id - id_position1 = this->recieverPage->toStdString().find(constUserID_TAG.toStdString(), 0); //looking for hassid index - if (id_position1 == std::string::npos) //didnt find the tag - return false; - else - id_position1 += constUserID_TAG.length(); //skip the index of tag - id_position2 = this->recieverPage->toStdString().find(",-A", id_position1); - if (id_position2 != std::string::npos) //found the hassid! storing it - hassid = recieverPage->mid(id_position1,id_position2-id_position1); - else - return false; + hasspass_position1 = this->recieverPage->toStdString().find(constHassID_TAG.toStdString()); //looking for hasspass index + if (hasspass_position1 == std::string::npos) //didnt find the tag + return false; + else + hasspass_position1 += constHassID_TAG.length(); //skip the index of tag + hasspass_position2 = this->recieverPage->toStdString().find(",-A,-A", hasspass_position1); + //finds the hass pass + if (hasspass_position2 != std::string::npos) //found the hasspass! storing it + hasspass = recieverPage->mid(hasspass_position1,hasspass_position2-hasspass_position1); + else + return false; + //finds the user id + id_position1 = this->recieverPage->toStdString().find(constUserID_TAG.toStdString(), 0); //looking for hassid index + if (id_position1 == std::string::npos) //didnt find the tag + return false; + else + id_position1 += constUserID_TAG.length(); //skip the index of tag + id_position2 = this->recieverPage->toStdString().find(",-A", id_position1); + if (id_position2 != std::string::npos) //found the hassid! storing it + hassid = recieverPage->mid(id_position1,id_position2-id_position1); + else + return false; - //setting user information with given data hassid and hasspass - jceA->setHashedPassword(hasspass); - jceA->setUserID(hassid); + //setting user information with given data hassid and hasspass + jceA->setHashedPassword(hasspass); + jceA->setUserID(hassid); - qDebug() << "jceLogin::checkValidation(); Found Hashed: " << hasspass << "And ID: " << hassid; + qDebug() << "jceLogin::checkValidation(); Found Hashed: " << hasspass << "And ID: " << hassid; - return true; + return true; } /** * @brief jceLogin::setLoginFlag @@ -266,7 +267,7 @@ bool jceLogin::checkValidation() */ void jceLogin::setLoginFlag(bool x) { - this->loginFlag = x; + this->loginFlag = x; } /** * @brief jceLogin::isLoginFlag checking if there is a connection, if true - > return if we signed in. otherwise, return not (not connected dough) @@ -274,9 +275,9 @@ void jceLogin::setLoginFlag(bool x) */ bool jceLogin::isLoginFlag() const { - if (checkConnection()) - return this->loginFlag; - return false; + if (checkConnection()) + return this->loginFlag; + return false; } /** @@ -285,5 +286,16 @@ bool jceLogin::isLoginFlag() const */ QString jceLogin::getPage() { - return *recieverPage; + return *recieverPage; +} + +void jceLogin::reValidation() +{ + qDebug() << Q_FUNC_INFO << "Revalidating user"; + if (makeFirstVisit()) + { + if (makeSecondVisit()) + qDebug() << Q_FUNC_INFO << "Validated"; + } + } diff --git a/src/jceSettings/jcelogin.h b/src/jceSettings/jcelogin.h index dee3572..f55d50e 100644 --- a/src/jceSettings/jcelogin.h +++ b/src/jceSettings/jcelogin.h @@ -5,11 +5,19 @@ #include "./src/jceSettings/user.h" #include "jceLoginHtmlScripts.h" +#include #include + class jceLogin { + + Q_OBJECT + public: + jceLogin() {} + jceLogin(user* username); + ~jceLogin(); enum jceStatus { JCE_NOT_CONNECTED, @@ -26,8 +34,7 @@ public: JCE_GRADE_PAGE_PASSED }; - jceLogin(user* username); - ~jceLogin(); + void makeConnection() throw (jceStatus); void reConnect() throw (jceStatus); @@ -41,6 +48,9 @@ public: QString getPage(); +private slots: + void reValidation(); + private: int makeFirstVisit();