2011-02-18 22:53:06 -05:00
|
|
|
#include "Connection.h"
|
2011-03-09 00:08:30 -05:00
|
|
|
#include "WebPage.h"
|
2011-02-18 22:53:06 -05:00
|
|
|
#include "Visit.h"
|
|
|
|
#include "Find.h"
|
|
|
|
#include "Command.h"
|
|
|
|
#include "Reset.h"
|
2011-02-25 23:39:29 -05:00
|
|
|
#include "Node.h"
|
2011-02-25 17:53:36 -05:00
|
|
|
#include "Url.h"
|
2011-02-25 18:04:23 -05:00
|
|
|
#include "Source.h"
|
2011-02-26 13:02:43 -05:00
|
|
|
#include "Evaluate.h"
|
2011-02-26 14:03:30 -05:00
|
|
|
#include "Execute.h"
|
2011-04-19 03:16:25 -04:00
|
|
|
#include "FrameFocus.h"
|
2011-02-18 22:53:06 -05:00
|
|
|
|
|
|
|
#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;
|
|
|
|
m_command = NULL;
|
2011-02-26 13:38:10 -05:00
|
|
|
m_expectingDataSize = -1;
|
2011-05-05 18:27:04 -04:00
|
|
|
m_pageSuccess = true;
|
|
|
|
m_commandWaiting = false;
|
2011-02-18 22:53:06 -05:00
|
|
|
connect(m_socket, SIGNAL(readyRead()), this, SLOT(checkNext()));
|
2011-05-05 18:27:04 -04:00
|
|
|
connect(m_page, SIGNAL(loadFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
|
2011-02-18 22:53:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::checkNext() {
|
2011-02-26 13:38:10 -05:00
|
|
|
if (m_expectingDataSize == -1) {
|
|
|
|
if (m_socket->canReadLine()) {
|
|
|
|
readLine();
|
|
|
|
checkNext();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (m_socket->bytesAvailable() >= m_expectingDataSize) {
|
|
|
|
readDataBlock();
|
|
|
|
checkNext();
|
|
|
|
}
|
2011-02-18 22:53:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-26 13:38:10 -05:00
|
|
|
void Connection::readLine() {
|
|
|
|
char buffer[128];
|
|
|
|
qint64 lineLength = m_socket->readLine(buffer, 128);
|
2011-02-18 22:53:06 -05:00
|
|
|
if (lineLength != -1) {
|
|
|
|
buffer[lineLength - 1] = 0;
|
2011-02-26 13:38:10 -05:00
|
|
|
processNext(buffer);
|
2011-02-25 23:29:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-26 13:38:10 -05:00
|
|
|
void Connection::readDataBlock() {
|
|
|
|
char *buffer = new char[m_expectingDataSize + 1];
|
|
|
|
m_socket->read(buffer, m_expectingDataSize);
|
|
|
|
buffer[m_expectingDataSize] = 0;
|
|
|
|
processNext(buffer);
|
|
|
|
m_expectingDataSize = -1;
|
|
|
|
delete buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::processNext(const char *data) {
|
2011-03-09 00:08:30 -05:00
|
|
|
if (m_commandName.isNull()) {
|
|
|
|
m_commandName = data;
|
|
|
|
m_argumentsExpected = -1;
|
2011-02-25 23:29:36 -05:00
|
|
|
} else {
|
2011-03-09 00:08:30 -05:00
|
|
|
processArgument(data);
|
2011-02-18 22:53:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-09 00:08:30 -05:00
|
|
|
void Connection::processArgument(const char *data) {
|
2011-02-25 23:29:36 -05:00
|
|
|
if (m_argumentsExpected == -1) {
|
2011-02-26 13:38:10 -05:00
|
|
|
m_argumentsExpected = QString(data).toInt();
|
|
|
|
} else if (m_expectingDataSize == -1) {
|
|
|
|
m_expectingDataSize = QString(data).toInt();
|
2011-02-25 23:29:36 -05:00
|
|
|
} else {
|
2011-04-19 07:51:38 -04:00
|
|
|
m_arguments.append(QString::fromUtf8(data));
|
2011-02-25 23:29:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_arguments.length() == m_argumentsExpected) {
|
2011-03-09 00:08:30 -05:00
|
|
|
if (m_page->isLoading())
|
2011-05-05 18:27:04 -04:00
|
|
|
m_commandWaiting = true;
|
2011-03-09 00:08:30 -05:00
|
|
|
else
|
|
|
|
startCommand();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::startCommand() {
|
2011-05-05 18:27:04 -04:00
|
|
|
m_commandWaiting = false;
|
|
|
|
if (m_pageSuccess) {
|
|
|
|
m_command = createCommand(m_commandName.toAscii().constData());
|
|
|
|
if (m_command) {
|
|
|
|
connect(m_command,
|
|
|
|
SIGNAL(finished(Response *)),
|
|
|
|
this,
|
|
|
|
SLOT(finishCommand(Response *)));
|
|
|
|
m_command->start(m_arguments);
|
|
|
|
} else {
|
|
|
|
QString failure = QString("Unknown command: ") + m_commandName + "\n";
|
|
|
|
writeResponse(new Response(false, failure));
|
|
|
|
}
|
|
|
|
m_commandName = QString();
|
2011-03-09 00:08:30 -05:00
|
|
|
} else {
|
2011-05-05 18:27:04 -04:00
|
|
|
m_pageSuccess = true;
|
|
|
|
QString message = m_page->failureString();
|
|
|
|
writeResponse(new Response(false, message));
|
2011-03-09 00:08:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Command *Connection::createCommand(const char *name) {
|
|
|
|
#include "find_command.h"
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2011-02-25 23:29:36 -05:00
|
|
|
}
|
|
|
|
|
2011-05-05 17:45:44 -04:00
|
|
|
void Connection::finishCommand(Response *response) {
|
2011-02-18 22:53:06 -05:00
|
|
|
m_command->deleteLater();
|
|
|
|
m_command = NULL;
|
2011-05-05 17:45:44 -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
|
|
|
|
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;
|
|
|
|
|
2011-05-05 09:19:46 -04:00
|
|
|
m_arguments.clear();
|
|
|
|
m_commandName = QString();
|
|
|
|
m_argumentsExpected = -1;
|
2011-02-18 22:53:06 -05:00
|
|
|
}
|
2011-03-09 00:08:30 -05:00
|
|
|
|