server disconnection bug 1

This commit is contained in:
liranbg 2014-09-21 13:31:26 +03:00
parent ac0973cf11
commit 32e4272f83
4 changed files with 178 additions and 143 deletions

View file

@ -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;

View file

@ -10,7 +10,7 @@
#define milisTimeOut 4000
class jceSSLClient : QSslSocket
class jceSSLClient : public QSslSocket
{
Q_OBJECT
public:

View file

@ -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";
}
}

View file

@ -5,11 +5,19 @@
#include "./src/jceSettings/user.h"
#include "jceLoginHtmlScripts.h"
#include <QObject>
#include <QString>
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();