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-26 04:39:29 +00:00
|
|
|
#include "Node.h"
|
2011-02-25 22:53:36 +00:00
|
|
|
#include "Url.h"
|
2011-02-25 23:04:23 +00:00
|
|
|
#include "Source.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;
|
2011-02-26 04:29:36 +00:00
|
|
|
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) {
|
2011-02-26 04:29:36 +00:00
|
|
|
startCommand();
|
2011-02-19 03:53:06 +00:00
|
|
|
} else {
|
2011-02-26 04:29:36 +00:00
|
|
|
m_socket->write("bad command\n");
|
2011-02-19 03:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-26 04:29:36 +00:00
|
|
|
Command *Connection::createCommand(const char *name) {
|
2011-02-25 04:48:18 +00:00
|
|
|
#include "find_command.h"
|
|
|
|
return NULL;
|
2011-02-19 03:53:06 +00:00
|
|
|
}
|
|
|
|
|
2011-02-26 04:29:36 +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;
|
2011-02-26 04:29:36 +00:00
|
|
|
m_arguments.clear();
|
2011-02-19 03:53:06 +00:00
|
|
|
if (success) {
|
|
|
|
m_socket->write("ok\n");
|
|
|
|
} else {
|
|
|
|
m_socket->write("failure\n");
|
|
|
|
}
|
2011-02-25 04:22:56 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|