changing socket engine

This commit is contained in:
liranbg 2014-10-10 18:20:33 +03:00
parent 0246b8bcef
commit 01f49ddfc7
5 changed files with 173 additions and 152 deletions

View file

@ -64,7 +64,7 @@ background: qlineargradient(spread:pad, x1:0.496, y1:0, x2:0.508, y2:1, stop:0 r
<enum>QTabWidget::Rounded</enum>
</property>
<property name="currentIndex">
<number>2</number>
<number>0</number>
</property>
<property name="documentMode">
<bool>false</bool>

View file

@ -3,7 +3,8 @@
/**
* @brief jceSSLClient::jceSSLClient Constructer, setting the signals
*/
jceSSLClient::jceSSLClient(QProgressBar *progressbarPtr) : flag(false), packet(""),networkConf(), reConnection(false)
jceSSLClient::jceSSLClient(QProgressBar *progressbarPtr) : loggedIAndConnectedFlag(false), readingFlag(false),
reConnectionFlag(false), networkConf(), packet(""), recieveLastPacket(false), packetSizeRecieved(0)
{
this->progressBar = progressbarPtr;
//setting signals
@ -14,8 +15,8 @@ jceSSLClient::jceSSLClient(QProgressBar *progressbarPtr) : flag(false), packet("
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()), &loginThreadLoop, SLOT(quit()));
connect(this, SIGNAL(error(QAbstractSocket::SocketError)),&loginThreadLoop,SLOT(quit()));
}
/**
@ -43,7 +44,7 @@ bool jceSSLClient::makeConnect(QString server, int port)
qDebug() << Q_FUNC_INFO << "we're online";
if (reConnection) //reset reconnectiong flag
if (reConnectionFlag) //reset reconnectiong flag
{
qDebug() << Q_FUNC_INFO << "Making Reconnection";
}
@ -59,12 +60,12 @@ bool jceSSLClient::makeConnect(QString server, int port)
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
loginThreadLoop.exec(); //starting connection, waiting to encryption and then it ends
qDebug() << Q_FUNC_INFO << "returning the connection status: " << isConnected();
if (reConnection)
if (reConnectionFlag)
{
reConnection = false;
reConnectionFlag = false;
emit serverDisconnectedbyRemote();
}
return isConnected();
@ -76,10 +77,10 @@ bool jceSSLClient::makeConnect(QString server, int port)
*/
bool jceSSLClient::makeDiconnect()
{
if (loop.isRunning())
if (loginThreadLoop.isRunning())
{
qWarning() << Q_FUNC_INFO << "Killing connection thread";
loop.exit();
loginThreadLoop.exit();
}
qDebug() << Q_FUNC_INFO << "disconnecting from host and emitting disconnected()";
this->disconnectFromHost(); //emits disconnected > setDisconnected
@ -117,7 +118,7 @@ bool jceSSLClient::isConnected()
}
if (!isConnectedToNetwork()) //no link, ethernet\wifi
tempFlag = false;
return ((flag) && (tempFlag));
return ((loggedIAndConnectedFlag) && (tempFlag));
}
/**
* @brief jceSSLClient::sendData - given string, send it to server
@ -127,11 +128,17 @@ bool jceSSLClient::isConnected()
bool jceSSLClient::sendData(QString str)
{
bool sendDataFlag = false;
int amount = 0;
if (isConnected()) //if connected
{
write(str.toStdString().c_str(),str.length());
if (waitForBytesWritten())
amount = write(str.toStdString().c_str(),str.length());
qDebug() << Q_FUNC_INFO << "lenght send: " << str.length() << "lenght recieved: " << amount;
if (amount == -1)
{
qCritical() << Q_FUNC_INFO << "SendData ended with -1";
sendDataFlag = false;
}
else if (waitForBytesWritten())
sendDataFlag = true;
}
qDebug() << Q_FUNC_INFO << "Sending Data status is: " << sendDataFlag;
@ -143,42 +150,35 @@ bool jceSSLClient::sendData(QString str)
* @param fast true for LOGIN ONLY, false to retrieve all data
* @return true if recieved data bigger than zero
*/
bool jceSSLClient::recieveData(QString &str, bool fast)
bool jceSSLClient::recieveData(QString *str)
{
qDebug() << Q_FUNC_INFO << "Data receiving!";
bool sflag = false; //success on recieving flag
str->clear();
packet = "";
bool sflag = false;
recieveLastPacket = false;
packetSizeRecieved = 0; //counting packet size
readingFlag = true; //to ignore timeout socket error
if (fast) //fast mode connection, good only for login step!!!!
{
qDebug() << Q_FUNC_INFO << "login step receiving";
//loop will exit after first read packet.
//meanwhile packet will gain data. good for small amount of data - fast connection!
connect(this, SIGNAL(readyRead()), &readerLoop, SLOT(quit()));
connect(this, SIGNAL(readyRead()), this, SLOT(readIt()));
readerLoop.exec();
disconnect(this, SIGNAL(readyRead()), &readerLoop, SLOT(quit()));
disconnect(this, SIGNAL(readyRead()), this, SLOT(readIt()));
}
else
{
qDebug() << Q_FUNC_INFO << "normal receiving";
//loop will exit after timeout \ full data
connect(this, SIGNAL(packetHasData()), &readerLoop, SLOT(quit()));
timer.setSingleShot(true);
timer.start(milisTimeOut); //if timer is timeout -> it means the connection takes long time
timer.setSingleShot(true);
connect(&timer, SIGNAL(timeout()), &readerLoop, SLOT(quit()));
connect(this, SIGNAL(readyRead()), this, SLOT(readItAll()));
timer.start(5000);
readerLoop.exec();
connect(this, SIGNAL(readyRead()), this, SLOT(readIt())); //we have something to read
connect(&timer, SIGNAL(timeout()), &readerLoop, SLOT(quit())); //if timer timeout > exiting event
}
str = packet;
qDebug() << Q_FUNC_INFO << "received bytes: " << str.length() ;
if (str.length() > 0)
readerLoop.exec();
disconnect(&timer, SIGNAL(timeout()), &readerLoop, SLOT(quit()));
disconnect(this, SIGNAL(readyRead()), this, SLOT(readIt()));
str->append(packet);
qDebug() << *str;
qDebug() << Q_FUNC_INFO << "packet size: " << packetSizeRecieved << "received data lenght: " << str->length();
if (str->length() > 0)
sflag = true;
qDebug() << Q_FUNC_INFO << "return with flag: " << sflag;
disconnect(this, SIGNAL(readyRead()), this, SLOT(readItAll()));
readingFlag = false;
return sflag;
}
@ -187,33 +187,41 @@ bool jceSSLClient::recieveData(QString &str, bool fast)
*/
void jceSSLClient::readIt()
{
QString p;
do
{
p = readAll();
packet.append(p);
this->progressBar->setValue(this->progressBar->value() + 6);
}while (p.size() > 0);
int packSize = bytesAvailable();
int doTimes=0;
QByteArray tempPacket;
}
void jceSSLClient::readItAll()
{
QString p;
do
{
p = "";
p = read(bytesAvailable());
if (p.contains("</tbody>") == true)
{
//we recieved the end of table. we can stop recieving
timer.setInterval(1000);
}
this->progressBar->setValue(this->progressBar->value() + 6);
packet.append(p);
qDebug() << Q_FUNC_INFO << "packet size" << packSize;
if (doTimes++ > 0) //for debbuging, checking thread looping times
qDebug() << Q_FUNC_INFO << "do loop" << doTimes;
waitForReadyRead(100);
tempPacket = read(packSize);
readerAppendingLocker.lock();
packetSizeRecieved += packSize;
packet.append(tempPacket);
packet.append("\0");
}while (p.size() > 0);
}
readerAppendingLocker.unlock();
progressBar->setValue(this->progressBar->value() + 6);
if (tempPacket.contains("Go_To_system_After_Login.htm") || tempPacket.contains("</html>"))
{
//we have the last packet. (uses only in login first step
recieveLastPacket = true;
timer.setInterval(200);
}
else
{
//just a packet with data
}
}while ((packSize = bytesAvailable()) > 0);
}
void jceSSLClient::setOnlineState(bool isOnline)
{
qWarning() << Q_FUNC_INFO << "isOnline status change: " << isOnline;
@ -246,8 +254,8 @@ void jceSSLClient::setDisconnected()
qDebug() << Q_FUNC_INFO << "connection has been DISCONNECTED";
this->setSocketState(QAbstractSocket::SocketState::UnconnectedState);
packet.clear();
flag = false;
if (reConnection)
loggedIAndConnectedFlag = false;
if (reConnectionFlag)
makeConnect();
@ -260,11 +268,11 @@ void jceSSLClient::setEncrypted()
qDebug() << Q_FUNC_INFO << "connection has been ENCRYPTED";
setReadBufferSize(packetSize);
setSocketOption(QAbstractSocket::KeepAliveOption,true);
flag = true;
loggedIAndConnectedFlag = true;
if (!isConnected())
{
qWarning() << Q_FUNC_INFO << "Connection status didnt change! reseting flag to false";
flag = false;
loggedIAndConnectedFlag = false;
}
}
@ -291,7 +299,7 @@ void jceSSLClient::showIfErrorMsg()
//The remote host closed the connection
if (isConnectedToNetwork()) //we can reconnect
{
reConnection = true;
reConnectionFlag = true;
}
else
relevantError = true;
@ -402,19 +410,23 @@ void jceSSLClient::checkErrors(QAbstractSocket::SocketError a)
qWarning() << Q_FUNC_INFO << "Var Error: " << a;
qWarning() << Q_FUNC_INFO << "Error: " << errorString();
}
else if (!readingFlag)
{
qWarning() << Q_FUNC_INFO << "isConnected?: " << isConnected() << "is timeout?" << timeout;
qWarning() << Q_FUNC_INFO << "isOnline?: " << isConnectedToNetwork() << "state is: " << state();
qWarning() << Q_FUNC_INFO << "Error: " << errorString();
}
else
{
qDebug() << Q_FUNC_INFO << "isConnected?: " << isConnected() << "is timeout?" << timeout;
qWarning() << Q_FUNC_INFO << "isOnline?: " << isConnectedToNetwork();
qWarning() << Q_FUNC_INFO << "state is: " << state();
qWarning() << Q_FUNC_INFO << "Var Error: " << a;
qWarning() << Q_FUNC_INFO << "Error: " << errorString();
//timeout when reading
}
showIfErrorMsg();
}
/** written by KARAN BALKAR
* @brief jceSSLClient::isConnectedToNetwork
*
* @return
*/
bool jceSSLClient::isConnectedToNetwork(){

View file

@ -5,54 +5,66 @@
#include <QSslSocket>
#include <QThread>
#include <QEventLoop>
#include <QTimer>
#include <QMutex>
#include <QMessageBox>
#include <QNetworkConfigurationManager>
#include <QtNetwork/QNetworkInterface>
#include <QTimer>
#include <QProgressBar>
#define packetSize 10000
#define milisTimeOut 4000
#define packetSize 4096 //4k
#define milisTimeOut 5000 //4 seconds
class jceSSLClient : public QSslSocket
{
Q_OBJECT
Q_OBJECT
public:
jceSSLClient(QProgressBar *progressbarPtr);
jceSSLClient(QProgressBar *progressbarPtr);
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);
void showIfErrorMsg();
bool makeConnect(QString server = "yedion.jce.ac.il", int port = 443);
bool makeDiconnect();
bool isConnected();
bool sendData(QString str);
bool recieveData(QString *str);
void showIfErrorMsg();
signals:
void serverDisconnectedbyRemote();
void noInternetLink();
void socketDisconnected();
void packetHasData();
void serverDisconnectedbyRemote();
void noInternetLink();
void socketDisconnected();
private slots:
void checkErrors(QAbstractSocket::SocketError a);
void setConnected();
void setEncrypted();
void setDisconnected();
void readIt();
void readItAll();
void setOnlineState(bool isOnline);
void checkErrors(QAbstractSocket::SocketError a);
void setConnected();
void setEncrypted();
void setDisconnected();
void readIt();
void setOnlineState(bool isOnline);
private:
bool isConnectedToNetwork(); //checking if online
bool flag;
QString packet;
QEventLoop loop; //handle the connection as thread
QEventLoop readerLoop;
QTimer timer;
QNetworkConfigurationManager networkConf; //checking if online
bool reConnection; //used for remote host disconnecting
QProgressBar *progressBar; //
bool isConnectedToNetwork(); //checking if online
bool loggedIAndConnectedFlag;
bool readingFlag;
bool reConnectionFlag; //used for remote host disconnecting
QNetworkConfigurationManager networkConf; //checking if online
QString packet;
bool recieveLastPacket;
int packetSizeRecieved;
QEventLoop loginThreadLoop; //handle the connection as thread
QEventLoop readerLoop;
QMutex readerAppendingLocker; //locking packet when appending
QTimer timer; //uses to check if reading has reached its timeout
QProgressBar *progressBar; //progressbar pointer
};

View file

@ -9,6 +9,7 @@ void CalendarPage::setPage(QString html)
courses = new std::list<calendarCourse*>();
tempHtml = getString(html);
qDebug() << "starting ..";
calendarListInit(tempHtml);
}
@ -18,23 +19,19 @@ void CalendarPage::setPage(QString html)
*/
void CalendarPage::calendarListInit(QString &linesTokinzedString)
{
std::list<QString> stringHolder;
QString temp;
calendarCourse * cTemp = NULL;
char* tok;
char* textToTok = strdup(linesTokinzedString.toStdString().c_str());
tok = strtok(textToTok,"\n");
while (tok != NULL)
QString tempToken;
QStringList holder = linesTokinzedString.split("\n");
QStringList::iterator iterator;
for (iterator = holder.begin(); iterator != holder.end(); ++iterator)
{
temp = tok;
stringHolder.push_back(temp);
tok = strtok(NULL, "\n");
}
for (QString temp: stringHolder)
{
cTemp = lineToCourse(temp);
if (cTemp != NULL)
courses->push_back(cTemp);
tempToken = (*iterator);
if (!tempToken.isEmpty())
{
calendarCourse *cTemp = lineToCourse(tempToken);
if (cTemp != NULL)
this->courses->push_back(cTemp);
}
}
}
@ -51,27 +48,27 @@ calendarCourse *CalendarPage::lineToCourse(QString line)
int serial;
double points,semesterHours;
QString name,type, lecturer,dayAndHour,room;
QString tempS = "";
int i = 0;
char* tok;
char* cLine = strdup(line.toStdString().c_str());
tok = strtok(cLine, "\t");
while(tok != NULL)
{
tempS = QString(tok);
QString tempToken;
int i = 0;
QStringList holder = line.split("\t");
QStringList::iterator iterator;
for (iterator = holder.begin(); iterator != holder.end(); ++iterator)
{
tempToken = (*iterator);
if (i >= 1) //skips on semester character
{
templinearray[i-1] = tempS.trimmed();
templinearray[i] = tempToken.trimmed();
}
i++;
if (i > 8)
break;
tok=strtok(NULL, "\t");
}
if (i >= CALENDAR_COURSE_FIELDS)
break;
}
if (templinearray[0] == "") //empty parsing
return NULL;
return NULL;
serial = templinearray[calendarCourse::CourseScheme::SERIAL].toInt();
name = templinearray[calendarCourse::CourseScheme::NAME];
@ -101,16 +98,16 @@ calendarCourse *CalendarPage::lineToCourse(QString line)
tempC = new calendarCourse(serial,name,type,lecturer,points,semesterHours,dayAndHour,room);
// qDebug() << "serial is: " << tempC->getSerialNum();
// qDebug() << tempC->getName();
// qDebug() << tempC->getType();
// qDebug() << tempC->getLecturer();
// qDebug() << tempC->getPoints();
// qDebug() << tempC->getHourBegin() << ":" << tempC->getMinutesBegin();
// qDebug() << tempC->getHourEnd() << ":" << tempC->getMinutesEnd();
qDebug() << "serial is: " << tempC->getSerialNum();
qDebug() << tempC->getName();
qDebug() << tempC->getType();
qDebug() << tempC->getLecturer();
qDebug() << tempC->getPoints();
qDebug() << tempC->getHourBegin() << ":" << tempC->getMinutesBegin();
qDebug() << tempC->getHourEnd() << ":" << tempC->getMinutesEnd();
// qDebug() << tempC->getDay();
// qDebug() << tempC->getRoom();
qDebug() << tempC->getDay();
qDebug() << tempC->getRoom();
return tempC;
}

View file

@ -59,7 +59,7 @@ int jceLogin::makeConnection()
returnMode = makeSecondVisit();
if (returnMode == true) //siging in the website
{
qDebug() << "jceLogin::makeConnection(); Signed in succeesfully";
qDebug() << Q_FUNC_INFO << "Signed in succeesfully";
status = jceStatus::JCE_YOU_ARE_IN;
setLoginFlag(true);
}
@ -94,7 +94,7 @@ int jceLogin::makeConnection()
status = jceStatus::JCE_NOT_CONNECTED;
//we throw status even if we are IN!
qDebug() << "jceLogin::makeConnection(); return status: " << status;
qDebug() << Q_FUNC_INFO << "return status: " << status;
return status;
}
@ -150,7 +150,7 @@ int jceLogin::makeFirstVisit()
QString psw = jceA->getPassword();
if (JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getFirstValidationStep(*jceA))))
{
if (!JceConnector->recieveData(*recieverPage,true))
if (!JceConnector->recieveData(recieverPage))
return jceLogin::ERROR_ON_GETTING_INFO;
}
else
@ -168,7 +168,7 @@ int jceLogin::makeSecondVisit()
QString pswid=jceA->getHashedPassword();
if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getSecondValidationStep(*jceA)))))
{
if (!(JceConnector->recieveData(*recieverPage,true)))
if (!(JceConnector->recieveData(recieverPage)))
return jceLogin::ERROR_ON_GETTING_INFO;
return true;
@ -188,7 +188,7 @@ int jceLogin::getCalendar(int year, int semester)
{
if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getCalendar(*jceA,year,semester)))))
{
if (!(JceConnector->recieveData(*recieverPage,false)))
if (!(JceConnector->recieveData(recieverPage)))
return jceLogin::ERROR_ON_GETTING_PAGE;
else
return jceLogin::JCE_PAGE_PASSED;
@ -203,7 +203,7 @@ int jceLogin::getExams(int year, int semester)
{
if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getExamSchedule(*jceA,year,semester)))))
{
if (!(JceConnector->recieveData(*recieverPage,false)))
if (!(JceConnector->recieveData(recieverPage)))
return jceLogin::ERROR_ON_GETTING_PAGE;
else
return jceLogin::JCE_PAGE_PASSED;
@ -225,7 +225,7 @@ int jceLogin::getGrades(int fromYear, int toYear, int fromSemester, int toSemest
{
if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getGradesPath(*jceA,fromYear, toYear, fromSemester, toSemester)))))
{
if (!(JceConnector->recieveData(*recieverPage,false)))
if (!(JceConnector->recieveData(recieverPage)))
return jceLogin::ERROR_ON_GETTING_PAGE;
else
return jceLogin::JCE_PAGE_PASSED;