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