From cc961416579112783a0de8197ea6f7ed5065f882 Mon Sep 17 00:00:00 2001 From: Liran BN Date: Thu, 26 Jun 2014 18:15:04 +0300 Subject: [PATCH] faster and better! --- jceGrade.pro | 10 ++- main/loginhandler.cpp | 1 + main/loginhandler.h | 2 +- src/connection/jcesslclient.cpp | 135 ++++++++++++++++++++++++++++++++ src/connection/jcesslclient.h | 43 ++++++++++ src/connection/qtsslsocket.cpp | 87 -------------------- src/connection/qtsslsocket.h | 41 ---------- src/jce/jcelogin.cpp | 19 +++-- src/jce/jcelogin.h | 6 +- 9 files changed, 198 insertions(+), 146 deletions(-) create mode 100644 src/connection/jcesslclient.cpp create mode 100644 src/connection/jcesslclient.h delete mode 100644 src/connection/qtsslsocket.cpp delete mode 100644 src/connection/qtsslsocket.h diff --git a/jceGrade.pro b/jceGrade.pro index a7183ec..02fd473 100644 --- a/jceGrade.pro +++ b/jceGrade.pro @@ -16,21 +16,22 @@ SOURCES += main/coursestablemanager.cpp \ main/loginhandler.cpp \ main/main.cpp \ main/mainscreen.cpp \ - src/connection/qtsslsocket.cpp \ + src/connection/jcesslclient.cpp \ src/user.cpp \ src/grades/Course.cpp \ src/grades/GradePage.cpp \ src/grades/Page.cpp \ src/jce/jcedate.cpp \ src/jce/jcelogin.cpp \ - src/data/savedata.cpp + src/data/savedata.cpp \ + HEADERS += main/coursestablemanager.h \ main/loginhandler.h \ main/mainscreen.h \ - src/connection/qtsslsocket.h \ + src/connection/jcesslclient.h \ src/user.h \ src/grades/Course.h \ src/grades/GradePage.h \ @@ -38,7 +39,8 @@ HEADERS += main/coursestablemanager.h \ src/jce/jcedate.h \ src/jce/jcelogin.h \ src/jce/jceLoginHtmlScripts.h \ - src/data/savedata.h + src/data/savedata.h \ + diff --git a/main/loginhandler.cpp b/main/loginhandler.cpp index 14e1b73..9d83708 100644 --- a/main/loginhandler.cpp +++ b/main/loginhandler.cpp @@ -107,6 +107,7 @@ QString loginHandler::getCurrentPageContect() void loginHandler::makeDisconnectionRequest() { jceLog->closeAll(); + this->logggedInFlag = false; } int loginHandler::makeGradeRequest() diff --git a/main/loginhandler.h b/main/loginhandler.h index ed89a91..55e665a 100644 --- a/main/loginhandler.h +++ b/main/loginhandler.h @@ -10,6 +10,7 @@ #include "src/jce/jcelogin.h" #include "./src/data/savedata.h" + class loginHandler { public: @@ -36,7 +37,6 @@ private: QLineEdit *usrnmEditPtr; - }; #endif // LOGINHANDLER_H diff --git a/src/connection/jcesslclient.cpp b/src/connection/jcesslclient.cpp new file mode 100644 index 0000000..7812d8a --- /dev/null +++ b/src/connection/jcesslclient.cpp @@ -0,0 +1,135 @@ +#include "jcesslclient.h" + +jceSSLClient::jceSSLClient(QObject *parent) : flag(false), packet("") +{ + 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())); + +} + +bool jceSSLClient::makeConnect(std::string server, int port) +{ + QEventLoop loop; + QObject::connect(this, SIGNAL(encrypted()), &loop, SLOT(quit())); + if (isConnected()) + abort(); + if (isOpen()) + abort(); + + connectToHostEncrypted(server.c_str(), port); + loop.exec(); + return isConnected(); + +} +bool jceSSLClient::makeDiconnect() +{ + + abort(); + if (state() == QAbstractSocket::SocketState::ConnectedState) + { + flag = false; + } + else + flag = true; + + return flag; +} + + +bool jceSSLClient::isConnected() +{ + + return flag; +} + +bool jceSSLClient::sendData(std::string str) +{ + if (isConnected()) //if connected + { + write(str.c_str(),str.length()); + while (waitForBytesWritten()); + return true; + } + return false; +} +/** + * @brief jceSSLClient::recieveData + * @param str this variable will store the recieved data + * @param fast true for LOGIN ONLY, false to retrieve all data + * @return true if recieved data bigger than zero + */ +bool jceSSLClient::recieveData(std::string &str, bool fast) +{ + packet = ""; + bool sflag = false; + + if (fast) //fast connection, good for login only!! + { + QEventLoop loop; + connect(this, SIGNAL(readyRead()), &loop, SLOT(quit())); + connect(this, SIGNAL(readyRead()), this, SLOT(readIt())); + loop.exec(); + disconnect(this, SIGNAL(readyRead()), &loop, SLOT(quit())); + disconnect(this, SIGNAL(readyRead()), this, SLOT(readIt())); + } + else + { + QString p; + while (waitForReadyRead(milisTimeOut)) + { + do + { + p = readAll(); + packet.append(p); + debugEdit->setPlainText(debugEdit->toPlainText() + "\n Size" + QString::number(p.size())); + }while (p.size() > 0); + } + } + + str = packet.toStdString(); + debugEdit->setPlainText(debugEdit->toPlainText() + "\n" + QString::fromStdString(str)); + if (str.size() > 0) + sflag = true; + + return sflag; + +} +void jceSSLClient::readIt() +{ + QString p; + do + { + p = readAll(); + packet.append(p); + debugEdit->setPlainText(debugEdit->toPlainText() + "\n Size" + QString::number(p.size())); + }while (p.size() > 0); + + + +} +void jceSSLClient::setConnected() +{ + waitForEncrypted(); +} + +void jceSSLClient::setEncrypted() +{ + setReadBufferSize(10000); + setSocketOption(QAbstractSocket::KeepAliveOption,1); + flag = true; +} + +void jceSSLClient::setDisconnected() +{ + abort(); + flag = false; +} + + +void jceSSLClient::checkErrors(QAbstractSocket::SocketError) +{ + +} + diff --git a/src/connection/jcesslclient.h b/src/connection/jcesslclient.h new file mode 100644 index 0000000..4c45aa6 --- /dev/null +++ b/src/connection/jcesslclient.h @@ -0,0 +1,43 @@ +#ifndef JCESSLCLIENT_H +#define JCESSLCLIENT_H + +#include +#include +#include +#include + +#include +#include +#define milisTimeOut 3500 + +#include + +class jceSSLClient : QSslSocket +{ + Q_OBJECT +public: + jceSSLClient(QObject *parent = 0); + + bool makeConnect(std::string server,int port); + bool isConnected(); + bool sendData(std::string str); + bool recieveData(std::string &str, bool fast); + bool makeDiconnect(); + +private slots: + void checkErrors(QAbstractSocket::SocketError); + void setConnected(); + void setEncrypted(); + void setDisconnected(); + void readIt(); + +private: + + bool flag; + QString packet; + + QPlainTextEdit* debugEdit; + +}; + +#endif // JCESSLCLIENT_H diff --git a/src/connection/qtsslsocket.cpp b/src/connection/qtsslsocket.cpp deleted file mode 100644 index d5e5823..0000000 --- a/src/connection/qtsslsocket.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include "qtsslsocket.h" - - -bool qtsslsocket::makeConnect(std::string server,int port) -{ - if (isCon()) - socket->abort(); - - - socket->connectToHostEncrypted(server.c_str(), port); - socket->waitForEncrypted(); - - return true; //return true/false upon isCon function -} - -qtsslsocket::qtsslsocket() : flag(false) -{ - socket = new QSslSocket(); - connect(socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(checkErrors(QAbstractSocket::SocketError))); - connect(socket,SIGNAL(connected()),this,SLOT(setConnected())); - connect(socket,SIGNAL(encrypted()),this,SLOT(setEncrypted())); - this->flag = true; - -} - - -qtsslsocket::~qtsslsocket() { - socket->close(); - socket = NULL; -} - -bool qtsslsocket::isCon() -{ - return ((flag) && (this->socket != NULL)); - -} -//need to fix the method -bool qtsslsocket::send(std::string str) -{ - // int status; - bool flag = isCon(); - if (flag) //if connected - { - socket->write(str.c_str(),str.length()); - while (socket->waitForBytesWritten()); - } - return flag; -} -bool qtsslsocket::recieve(std::string &str) -{ - bool flag = false; - QString s = ""; - while (socket->waitForReadyRead(milisTimeOut)) - s.append(socket->readAll()); - - str = s.toStdString(); - if (s.size() > 0) - flag = true; - - return flag; -} - -bool qtsslsocket::makeDiconnect() -{ - this->socket->abort(); - if (socket->isOpen()) - return false; - return true; -} - -void qtsslsocket::checkErrors(QAbstractSocket::SocketError info) -{ -} - -void qtsslsocket::setConnected() -{ -// socket->waitForEncrypted(); - -} - -void qtsslsocket::setEncrypted() -{ - -// this->flag = true; -} - - diff --git a/src/connection/qtsslsocket.h b/src/connection/qtsslsocket.h deleted file mode 100644 index fb60910..0000000 --- a/src/connection/qtsslsocket.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef QTSSLSOCKET_H -#define QTSSLSOCKET_H -#include - -#include -#include -#include -#include -#include -#define milisTimeOut 3500 - -class qtsslsocket : public QObject -{ - Q_OBJECT - -public: - qtsslsocket(); - ~qtsslsocket(); - - bool makeConnect(std::string server,int port); - bool isCon(); - bool send(std::string str); - bool recieve(std::string &str); - bool makeDiconnect(); - - -private slots: - void checkErrors(QAbstractSocket::SocketError); - void setConnected(); - void setEncrypted(); - - -private: - - QSslSocket *socket; - bool flag; - -}; - - -#endif // QTSSLSOCKET_H diff --git a/src/jce/jcelogin.cpp b/src/jce/jcelogin.cpp index d2b0fe5..5828553 100644 --- a/src/jce/jcelogin.cpp +++ b/src/jce/jcelogin.cpp @@ -4,8 +4,7 @@ jceLogin::jceLogin(user * username) { this->recieverPage = new std::string(); this->jceA = username; - this->JceConnector = new qtsslsocket(); - + this->JceConnector = new jceSSLClient(); } jceLogin::~jceLogin() @@ -85,7 +84,7 @@ void jceLogin::makeConnection() throw (jceStatus) bool jceLogin::checkConnection() { - if (JceConnector->isCon()) + if (JceConnector->isConnected()) return true; return false; @@ -97,7 +96,7 @@ void jceLogin::reConnect() throw (jceStatus) if (this->JceConnector != NULL) delete JceConnector; this->recieverPage = new std::string(); - this->JceConnector = new qtsslsocket(); + this->JceConnector = new jceSSLClient(); try { @@ -122,9 +121,9 @@ int jceLogin::makeFirstVisit() { std::string usr = jceA->getUsername(); std::string psw = jceA->getPassword(); - if (JceConnector->send(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getFirstValidationStep(*jceA)))) + if (JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getFirstValidationStep(*jceA)))) { - if (!JceConnector->recieve(*recieverPage)) + if (!JceConnector->recieveData(*recieverPage,true)) return jceLogin::ERROR_ON_GETTING_INFO; } else @@ -137,9 +136,9 @@ int jceLogin::makeSecondVisit() { std::string usrid=jceA->getUserID(); std::string pswid=jceA->getHashedPassword(); - if ((JceConnector->send(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getSecondValidationStep(*jceA))))) + if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getSecondValidationStep(*jceA))))) { - if (!(JceConnector->recieve(*recieverPage))) + if (!(JceConnector->recieveData(*recieverPage,true))) return jceLogin::ERROR_ON_GETTING_INFO; return true; @@ -152,9 +151,9 @@ int jceLogin::makeSecondVisit() int jceLogin::getGrades() { - if ((JceConnector->send(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getGradesPath(*jceA))))) + if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getGradesPath(*jceA))))) { - if (!(JceConnector->recieve(*recieverPage))) + if (!(JceConnector->recieveData(*recieverPage,false))) return jceLogin::ERROR_ON_GETTING_GRADES; else return jceLogin::JCE_GRADE_PAGE_PASSED; diff --git a/src/jce/jcelogin.h b/src/jce/jcelogin.h index e3a1a4c..cd805ad 100644 --- a/src/jce/jcelogin.h +++ b/src/jce/jcelogin.h @@ -4,11 +4,10 @@ #include #include -#include "../connection/qtsslsocket.h" +#include "../connection/jcesslclient.h" #include "../user.h" #include "jceLoginHtmlScripts.h" - class jceLogin { public: @@ -52,7 +51,8 @@ private: bool loginFlag; std::string * recieverPage; user * jceA; - qtsslsocket * JceConnector; + jceSSLClient * JceConnector; + };