faster and better!

This commit is contained in:
Liran BN 2014-06-26 18:15:04 +03:00
parent b7faa20288
commit cc96141657
9 changed files with 198 additions and 146 deletions

View file

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

View file

@ -107,6 +107,7 @@ QString loginHandler::getCurrentPageContect()
void loginHandler::makeDisconnectionRequest()
{
jceLog->closeAll();
this->logggedInFlag = false;
}
int loginHandler::makeGradeRequest()

View file

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

View file

@ -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)
{
}

View file

@ -0,0 +1,43 @@
#ifndef JCESSLCLIENT_H
#define JCESSLCLIENT_H
#include <QObject>
#include <QSslSocket>
#include <QThread>
#include <QEventLoop>
#include <iostream>
#include <string>
#define milisTimeOut 3500
#include <QPlainTextEdit>
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

View file

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

View file

@ -1,41 +0,0 @@
#ifndef QTSSLSOCKET_H
#define QTSSLSOCKET_H
#include <QObject>
#include <QSslSocket>
#include <QtNetwork/QSslCipher>
#include <QString>
#include <iostream>
#include <string>
#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

View file

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

View file

@ -4,11 +4,10 @@
#include <string>
#include <fstream>
#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;
};