capybara-webkit/src/Connection.cpp

85 lines
1.8 KiB
C++
Raw Normal View History

2011-02-19 03:53:06 +00:00
#include "Connection.h"
#include "Visit.h"
#include "Find.h"
#include "Command.h"
#include "Reset.h"
2011-02-25 05:15:08 +00:00
#include "Attribute.h"
2011-02-19 03:53:06 +00:00
#include <QTcpSocket>
Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
QObject(parent) {
m_socket = socket;
m_page = page;
m_command = NULL;
connect(m_socket, SIGNAL(readyRead()), this, SLOT(checkNext()));
}
void Connection::checkNext() {
while (m_socket->canReadLine()) {
readNext();
}
}
void Connection::readNext() {
char buffer[1024];
qint64 lineLength = m_socket->readLine(buffer, 1024);
if (lineLength != -1) {
buffer[lineLength - 1] = 0;
processLine(buffer);
}
}
void Connection::processLine(const char *line) {
if (m_command) {
continueCommand(line);
} else {
m_command = createCommand(line);
2011-02-19 03:53:06 +00:00
if (m_command) {
startCommand();
2011-02-19 03:53:06 +00:00
} else {
m_socket->write("bad command\n");
2011-02-19 03:53:06 +00:00
}
}
}
Command *Connection::createCommand(const char *name) {
#include "find_command.h"
return NULL;
2011-02-19 03:53:06 +00:00
}
void Connection::startCommand() {
m_argumentsExpected = -1;
connect(m_command,
SIGNAL(finished(bool, QString &)),
this,
SLOT(finishCommand(bool, QString &)));
}
void Connection::continueCommand(const char *line) {
if (m_argumentsExpected == -1) {
m_argumentsExpected = QString(line).toInt();
} else {
m_arguments.append(line);
}
if (m_arguments.length() == m_argumentsExpected) {
m_command->start(m_arguments);
}
}
2011-02-19 03:53:06 +00:00
void Connection::finishCommand(bool success, QString &response) {
m_command->deleteLater();
m_command = NULL;
m_arguments.clear();
2011-02-19 03:53:06 +00:00
if (success) {
m_socket->write("ok\n");
} else {
m_socket->write("failure\n");
}
QString responseLength = QString::number(response.size()) + "\n";
m_socket->write(responseLength.toAscii());
m_socket->write(response.toAscii());
2011-02-19 03:53:06 +00:00
}