capybara-webkit/src/Connection.cpp

72 lines
2.0 KiB
C++
Raw Normal View History

2011-02-19 03:53:06 +00:00
#include "Connection.h"
#include "WebPage.h"
2011-10-14 15:22:24 +00:00
#include "CommandParser.h"
#include "CommandFactory.h"
#include "PageLoadingCommand.h"
2011-02-19 03:53:06 +00:00
#include "Command.h"
#include <QTcpSocket>
Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
QObject(parent) {
m_socket = socket;
m_page = page;
2011-10-14 15:22:24 +00:00
m_commandFactory = new CommandFactory(page, this);
m_commandParser = new CommandParser(socket, m_commandFactory, this);
m_pageSuccess = true;
m_commandWaiting = false;
2011-10-14 15:22:24 +00:00
connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext()));
connect(m_commandParser, SIGNAL(commandReady(Command *)), this, SLOT(commandReady(Command *)));
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
2011-02-19 03:53:06 +00:00
}
void Connection::commandReady(Command *command) {
m_queuedCommand = command;
2011-10-14 15:22:24 +00:00
if (m_page->isLoading())
m_commandWaiting = true;
else
startCommand();
}
void Connection::startCommand() {
m_commandWaiting = false;
if (m_pageSuccess) {
m_runningCommand = new PageLoadingCommand(m_queuedCommand, m_page, this);
connect(m_runningCommand, SIGNAL(finished(Response *)), this, SLOT(finishCommand(Response *)));
m_runningCommand->start();
} else {
2012-03-21 23:11:50 +00:00
writePageLoadFailure();
}
}
void Connection::pendingLoadFinished(bool success) {
m_pageSuccess = success;
if (m_commandWaiting)
startCommand();
}
2012-03-21 23:11:50 +00:00
void Connection::writePageLoadFailure() {
2012-03-16 21:53:36 +00:00
m_pageSuccess = true;
QString message = m_page->failureString();
writeResponse(new Response(false, message));
}
void Connection::finishCommand(Response *response) {
m_runningCommand->deleteLater();
writeResponse(response);
2011-02-26 19:03:30 +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
QByteArray messageUtf8 = response->message().toUtf8();
QString messageLength = QString::number(messageUtf8.size()) + "\n";
m_socket->write(messageLength.toAscii());
m_socket->write(messageUtf8);
delete response;
2011-02-19 03:53:06 +00:00
}