2011-02-18 22:53:06 -05:00
|
|
|
#include "Connection.h"
|
2011-03-09 00:08:30 -05:00
|
|
|
#include "WebPage.h"
|
2011-09-15 11:22:10 -04:00
|
|
|
#include "UnsupportedContentHandler.h"
|
2011-10-14 11:22:24 -04:00
|
|
|
#include "CommandParser.h"
|
|
|
|
#include "CommandFactory.h"
|
2011-02-18 22:53:06 -05:00
|
|
|
#include "Command.h"
|
|
|
|
|
|
|
|
#include <QTcpSocket>
|
2011-02-26 13:02:43 -05:00
|
|
|
#include <iostream>
|
2011-02-18 22:53:06 -05:00
|
|
|
|
|
|
|
Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
|
|
|
|
QObject(parent) {
|
|
|
|
m_socket = socket;
|
|
|
|
m_page = page;
|
2011-10-14 11:22:24 -04:00
|
|
|
m_commandParser = new CommandParser(socket, this);
|
|
|
|
m_commandFactory = new CommandFactory(page, this);
|
2012-03-21 18:56:01 -04:00
|
|
|
m_runningCommand = NULL;
|
|
|
|
m_queuedCommand = NULL;
|
2011-05-05 18:27:04 -04:00
|
|
|
m_pageSuccess = true;
|
|
|
|
m_commandWaiting = false;
|
2012-01-31 16:57:57 -05:00
|
|
|
m_pageLoadingFromCommand = false;
|
|
|
|
m_pendingResponse = NULL;
|
2011-10-14 11:22:24 -04:00
|
|
|
connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext()));
|
|
|
|
connect(m_commandParser, SIGNAL(commandReady(QString, QStringList)), this, SLOT(commandReady(QString, QStringList)));
|
2011-09-15 11:22:10 -04:00
|
|
|
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
|
2011-02-18 22:53:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-14 11:22:24 -04:00
|
|
|
void Connection::commandReady(QString commandName, QStringList arguments) {
|
2012-03-21 18:56:01 -04:00
|
|
|
m_queuedCommand = m_commandFactory->createCommand(commandName.toAscii().constData(), arguments);
|
2011-10-14 11:22:24 -04:00
|
|
|
if (m_page->isLoading())
|
|
|
|
m_commandWaiting = true;
|
|
|
|
else
|
|
|
|
startCommand();
|
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-03-21 18:56:01 -04:00
|
|
|
m_runningCommand = m_queuedCommand;
|
|
|
|
m_queuedCommand = NULL;
|
2012-03-21 18:50:55 -04:00
|
|
|
connect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
|
2012-03-21 18:56:01 -04:00
|
|
|
connect(m_runningCommand,
|
2012-03-21 18:50:55 -04:00
|
|
|
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-16 17:53:36 -04:00
|
|
|
pageLoadFailed();
|
2011-03-09 00:08:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 16:57:57 -05:00
|
|
|
void Connection::pageLoadingFromCommand() {
|
|
|
|
m_pageLoadingFromCommand = true;
|
|
|
|
}
|
|
|
|
|
2011-03-09 00:08:30 -05:00
|
|
|
void Connection::pendingLoadFinished(bool success) {
|
2011-05-05 18:27:04 -04:00
|
|
|
m_pageSuccess = success;
|
|
|
|
if (m_commandWaiting)
|
2011-03-09 00:08:30 -05:00
|
|
|
startCommand();
|
2012-01-31 16:57:57 -05:00
|
|
|
if (m_pageLoadingFromCommand) {
|
|
|
|
m_pageLoadingFromCommand = false;
|
|
|
|
if (m_pendingResponse) {
|
2012-03-16 17:53:36 -04:00
|
|
|
if (m_pageSuccess) {
|
|
|
|
writeResponse(m_pendingResponse);
|
|
|
|
} else {
|
|
|
|
pageLoadFailed();
|
|
|
|
}
|
2012-01-31 16:57:57 -05:00
|
|
|
}
|
|
|
|
}
|
2011-02-25 23:29:36 -05:00
|
|
|
}
|
|
|
|
|
2012-03-16 17:53:36 -04:00
|
|
|
void Connection::pageLoadFailed() {
|
|
|
|
m_pageSuccess = true;
|
|
|
|
QString message = m_page->failureString();
|
|
|
|
writeResponse(new Response(false, message));
|
|
|
|
}
|
|
|
|
|
2011-05-05 17:45:44 -04:00
|
|
|
void Connection::finishCommand(Response *response) {
|
2012-01-31 16:57:57 -05:00
|
|
|
disconnect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
|
2012-03-21 18:56:01 -04:00
|
|
|
m_runningCommand->deleteLater();
|
|
|
|
m_runningCommand = NULL;
|
|
|
|
delete m_queuedCommand;
|
|
|
|
m_queuedCommand = NULL;
|
2012-01-31 16:57:57 -05:00
|
|
|
if (m_pageLoadingFromCommand)
|
|
|
|
m_pendingResponse = response;
|
|
|
|
else
|
|
|
|
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
|
|
|
|
2011-05-05 17:45:44 -04:00
|
|
|
QByteArray messageUtf8 = response->message().toUtf8();
|
|
|
|
QString messageLength = QString::number(messageUtf8.size()) + "\n";
|
|
|
|
m_socket->write(messageLength.toAscii());
|
|
|
|
m_socket->write(messageUtf8);
|
|
|
|
delete response;
|
2012-03-16 17:53:36 -04:00
|
|
|
m_pendingResponse = NULL;
|
2011-02-18 22:53:06 -05:00
|
|
|
}
|
2011-03-09 00:08:30 -05:00
|
|
|
|