2011-02-19 03:53:06 +00:00
|
|
|
#include "Connection.h"
|
2011-03-09 05:08:30 +00:00
|
|
|
#include "WebPage.h"
|
2012-03-29 03:05:24 +00:00
|
|
|
#include "WebPageManager.h"
|
2011-10-14 15:22:24 +00:00
|
|
|
#include "CommandParser.h"
|
|
|
|
#include "CommandFactory.h"
|
2012-03-23 20:34:39 +00:00
|
|
|
#include "PageLoadingCommand.h"
|
2012-10-24 11:55:29 +00:00
|
|
|
#include "TimeoutCommand.h"
|
2012-07-08 19:04:30 +00:00
|
|
|
#include "SocketCommand.h"
|
2011-02-19 03:53:06 +00:00
|
|
|
|
|
|
|
#include <QTcpSocket>
|
|
|
|
|
2012-03-29 03:05:24 +00:00
|
|
|
Connection::Connection(QTcpSocket *socket, WebPageManager *manager, QObject *parent) :
|
2011-02-19 03:53:06 +00:00
|
|
|
QObject(parent) {
|
|
|
|
m_socket = socket;
|
2012-03-29 03:05:24 +00:00
|
|
|
m_manager = manager;
|
|
|
|
m_commandFactory = new CommandFactory(m_manager, this);
|
2012-03-21 23:30:43 +00:00
|
|
|
m_commandParser = new CommandParser(socket, m_commandFactory, this);
|
2011-05-05 22:27:04 +00:00
|
|
|
m_pageSuccess = true;
|
2011-10-14 15:22:24 +00:00
|
|
|
connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext()));
|
2012-03-21 23:30:43 +00:00
|
|
|
connect(m_commandParser, SIGNAL(commandReady(Command *)), this, SLOT(commandReady(Command *)));
|
2012-04-04 19:40:18 +00:00
|
|
|
connect(m_manager, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
|
2011-02-19 03:53:06 +00:00
|
|
|
}
|
|
|
|
|
2012-03-21 23:30:43 +00:00
|
|
|
void Connection::commandReady(Command *command) {
|
2012-06-29 16:14:22 +00:00
|
|
|
m_manager->logger() << "Received" << command->toString();
|
2012-10-26 03:45:39 +00:00
|
|
|
startCommand(command);
|
2011-03-09 05:08:30 +00:00
|
|
|
}
|
|
|
|
|
2012-10-26 03:45:39 +00:00
|
|
|
void Connection::startCommand(Command *command) {
|
2011-05-05 22:27:04 +00:00
|
|
|
if (m_pageSuccess) {
|
2012-10-26 03:45:39 +00:00
|
|
|
command = new TimeoutCommand(new PageLoadingCommand(command, m_manager, this), m_manager, this);
|
|
|
|
connect(command, SIGNAL(finished(Response *)), this, SLOT(finishCommand(Response *)));
|
|
|
|
command->start();
|
2011-03-09 05:08:30 +00:00
|
|
|
} else {
|
2012-03-21 23:11:50 +00:00
|
|
|
writePageLoadFailure();
|
2011-03-09 05:08:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::pendingLoadFinished(bool success) {
|
2012-04-26 20:05:19 +00:00
|
|
|
m_pageSuccess = m_pageSuccess && success;
|
2011-02-26 04:29:36 +00:00
|
|
|
}
|
|
|
|
|
2012-03-21 23:11:50 +00:00
|
|
|
void Connection::writePageLoadFailure() {
|
2012-03-16 21:53:36 +00:00
|
|
|
m_pageSuccess = true;
|
2012-03-29 03:05:24 +00:00
|
|
|
QString message = currentPage()->failureString();
|
2012-12-12 06:47:54 +00:00
|
|
|
Response *response = new Response(false, message, this);
|
|
|
|
writeResponse(response);
|
|
|
|
delete response;
|
2012-03-16 21:53:36 +00:00
|
|
|
}
|
|
|
|
|
2011-05-05 21:45:44 +00:00
|
|
|
void Connection::finishCommand(Response *response) {
|
2012-04-04 01:27:34 +00:00
|
|
|
m_pageSuccess = true;
|
2012-03-23 20:34:39 +00:00
|
|
|
writeResponse(response);
|
2012-12-12 06:47:54 +00:00
|
|
|
sender()->deleteLater();
|
2011-02-26 19:03:30 +00:00
|
|
|
}
|
|
|
|
|
2011-05-05 21:45:44 +00:00
|
|
|
void Connection::writeResponse(Response *response) {
|
|
|
|
if (response->isSuccess())
|
2011-02-19 03:53:06 +00:00
|
|
|
m_socket->write("ok\n");
|
2011-02-26 19:03:30 +00:00
|
|
|
else
|
2011-02-19 03:53:06 +00:00
|
|
|
m_socket->write("failure\n");
|
2011-02-26 19:03:30 +00:00
|
|
|
|
2012-06-29 16:14:22 +00:00
|
|
|
m_manager->logger() << "Wrote response" << response->isSuccess() << response->message();
|
|
|
|
|
2012-04-17 16:06:12 +00:00
|
|
|
QByteArray messageUtf8 = response->message();
|
2011-05-05 21:45:44 +00:00
|
|
|
QString messageLength = QString::number(messageUtf8.size()) + "\n";
|
|
|
|
m_socket->write(messageLength.toAscii());
|
|
|
|
m_socket->write(messageUtf8);
|
2011-02-19 03:53:06 +00:00
|
|
|
}
|
2011-03-09 05:08:30 +00:00
|
|
|
|
2012-03-29 03:05:24 +00:00
|
|
|
WebPage *Connection::currentPage() {
|
|
|
|
return m_manager->currentPage();
|
|
|
|
}
|