Navigation Menu

Skip to content

Commit

Permalink
Refactor Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Oct 14, 2011
1 parent 25ff48a commit b4708b2
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 93 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Expand Up @@ -2,7 +2,7 @@ PATH
remote: .
specs:
capybara-webkit (0.7.2)
capybara (>= 1.0.0, < 1.2)
capybara (< 1.2, >= 1.0.0)

GEM
remote: http://rubygems.org/
Expand Down Expand Up @@ -39,7 +39,7 @@ GEM
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
rubyzip (0.9.4)
selenium-webdriver (2.8.0)
selenium-webdriver (2.7.0)
childprocess (>= 0.2.1)
ffi (>= 1.0.7)
json_pure
Expand Down
28 changes: 28 additions & 0 deletions src/CommandFactory.cpp
@@ -0,0 +1,28 @@
#include "CommandFactory.h"
#include "Visit.h"
#include "Find.h"
#include "Command.h"
#include "Reset.h"
#include "Node.h"
#include "Url.h"
#include "Source.h"
#include "Evaluate.h"
#include "Execute.h"
#include "FrameFocus.h"
#include "Header.h"
#include "Render.h"
#include "Body.h"
#include "Status.h"
#include "Headers.h"
#include "SetCookie.h"
#include "ClearCookies.h"
#include "GetCookies.h"

CommandFactory::CommandFactory(WebPage *page, QObject *parent) : QObject(parent) {
m_page = page;
}

Command *CommandFactory::createCommand(const char *name) {
#include "find_command.h"
return NULL;
}
16 changes: 16 additions & 0 deletions src/CommandFactory.h
@@ -0,0 +1,16 @@
#include <QObject>

class Command;
class WebPage;

class CommandFactory : public QObject {
Q_OBJECT

public:
CommandFactory(WebPage *page, QObject *parent = 0);
Command *createCommand(const char *name);

private:
WebPage *m_page;
};

68 changes: 68 additions & 0 deletions src/CommandParser.cpp
@@ -0,0 +1,68 @@
#include "CommandParser.h"

#include <QIODevice>

CommandParser::CommandParser(QIODevice *device, QObject *parent) :
QObject(parent) {
m_device = device;
m_expectingDataSize = -1;
connect(m_device, SIGNAL(readyRead()), this, SLOT(checkNext()));
}

void CommandParser::checkNext() {
if (m_expectingDataSize == -1) {
if (m_device->canReadLine()) {
readLine();
checkNext();
}
} else {
if (m_device->bytesAvailable() >= m_expectingDataSize) {
readDataBlock();
checkNext();
}
}
}

void CommandParser::readLine() {
char buffer[128];
qint64 lineLength = m_device->readLine(buffer, 128);
if (lineLength != -1) {
buffer[lineLength - 1] = 0;
processNext(buffer);
}
}

void CommandParser::readDataBlock() {
char *buffer = new char[m_expectingDataSize + 1];
m_device->read(buffer, m_expectingDataSize);
buffer[m_expectingDataSize] = 0;
processNext(buffer);
m_expectingDataSize = -1;
delete[] buffer;
}

void CommandParser::processNext(const char *data) {
if (m_commandName.isNull()) {
m_commandName = data;
m_argumentsExpected = -1;
} else {
processArgument(data);
}
}

void CommandParser::processArgument(const char *data) {
if (m_argumentsExpected == -1) {
m_argumentsExpected = QString(data).toInt();
} else if (m_expectingDataSize == -1) {
m_expectingDataSize = QString(data).toInt();
} else {
m_arguments.append(QString::fromUtf8(data));
}

if (m_arguments.length() == m_argumentsExpected) {
emit commandReady(m_commandName, m_arguments);
m_commandName = QString();
m_arguments.clear();
m_argumentsExpected = -1;
}
}
29 changes: 29 additions & 0 deletions src/CommandParser.h
@@ -0,0 +1,29 @@
#include <QObject>
#include <QStringList>

class QIODevice;

class CommandParser : public QObject {
Q_OBJECT

public:
CommandParser(QIODevice *device, QObject *parent = 0);

public slots:
void checkNext();

signals:
void commandReady(QString commandName, QStringList arguments);

private:
void readLine();
void readDataBlock();
void processNext(const char *line);
void processArgument(const char *data);
QIODevice *m_device;
QString m_commandName;
QStringList m_arguments;
int m_argumentsExpected;
int m_expectingDataSize;
};

97 changes: 14 additions & 83 deletions src/Connection.cpp
@@ -1,24 +1,9 @@
#include "Connection.h"
#include "WebPage.h"
#include "UnsupportedContentHandler.h"
#include "Visit.h"
#include "Find.h"
#include "CommandParser.h"
#include "CommandFactory.h"
#include "Command.h"
#include "Reset.h"
#include "Node.h"
#include "Url.h"
#include "Source.h"
#include "Evaluate.h"
#include "Execute.h"
#include "FrameFocus.h"
#include "Header.h"
#include "Render.h"
#include "Body.h"
#include "Status.h"
#include "Headers.h"
#include "SetCookie.h"
#include "ClearCookies.h"
#include "GetCookies.h"

#include <QTcpSocket>
#include <iostream>
Expand All @@ -27,76 +12,31 @@ Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
QObject(parent) {
m_socket = socket;
m_page = page;
m_commandParser = new CommandParser(socket, this);
m_commandFactory = new CommandFactory(page, this);
m_command = NULL;
m_expectingDataSize = -1;
m_pageSuccess = true;
m_commandWaiting = false;
connect(m_socket, SIGNAL(readyRead()), this, SLOT(checkNext()));
connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext()));
connect(m_commandParser, SIGNAL(commandReady(QString, QStringList)), this, SLOT(commandReady(QString, QStringList)));
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
}

void Connection::checkNext() {
if (m_expectingDataSize == -1) {
if (m_socket->canReadLine()) {
readLine();
checkNext();
}
} else {
if (m_socket->bytesAvailable() >= m_expectingDataSize) {
readDataBlock();
checkNext();
}
}
}

void Connection::readLine() {
char buffer[128];
qint64 lineLength = m_socket->readLine(buffer, 128);
if (lineLength != -1) {
buffer[lineLength - 1] = 0;
processNext(buffer);
}
}

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) {
if (m_commandName.isNull()) {
m_commandName = data;
m_argumentsExpected = -1;
} else {
processArgument(data);
}
}

void Connection::processArgument(const char *data) {
if (m_argumentsExpected == -1) {
m_argumentsExpected = QString(data).toInt();
} else if (m_expectingDataSize == -1) {
m_expectingDataSize = QString(data).toInt();
} else {
m_arguments.append(QString::fromUtf8(data));
}
void Connection::commandReady(QString commandName, QStringList arguments) {
m_commandName = commandName;
m_arguments = arguments;

if (m_arguments.length() == m_argumentsExpected) {
if (m_page->isLoading())
m_commandWaiting = true;
else
startCommand();
}
if (m_page->isLoading())
m_commandWaiting = true;
else
startCommand();
}

void Connection::startCommand() {
m_commandWaiting = false;
if (m_pageSuccess) {
m_command = createCommand(m_commandName.toAscii().constData());
m_command = m_commandFactory->createCommand(m_commandName.toAscii().constData());
if (m_command) {
connect(m_command,
SIGNAL(finished(Response *)),
Expand All @@ -115,11 +55,6 @@ void Connection::startCommand() {
}
}

Command *Connection::createCommand(const char *name) {
#include "find_command.h"
return NULL;
}

void Connection::pendingLoadFinished(bool success) {
m_pageSuccess = success;
if (m_commandWaiting)
Expand All @@ -143,9 +78,5 @@ void Connection::writeResponse(Response *response) {
m_socket->write(messageLength.toAscii());
m_socket->write(messageUtf8);
delete response;

m_arguments.clear();
m_commandName = QString();
m_argumentsExpected = -1;
}

13 changes: 5 additions & 8 deletions src/Connection.h
Expand Up @@ -5,6 +5,8 @@ class QTcpSocket;
class WebPage;
class Command;
class Response;
class CommandParser;
class CommandFactory;

class Connection : public QObject {
Q_OBJECT
Expand All @@ -13,26 +15,21 @@ class Connection : public QObject {
Connection(QTcpSocket *socket, WebPage *page, QObject *parent = 0);

public slots:
void checkNext();
void commandReady(QString commandName, QStringList arguments);
void finishCommand(Response *response);
void pendingLoadFinished(bool success);

private:
void readLine();
void readDataBlock();
void processNext(const char *line);
Command *createCommand(const char *name);
void processArgument(const char *line);
void startCommand();
void writeResponse(Response *response);

QTcpSocket *m_socket;
QString m_commandName;
Command *m_command;
QStringList m_arguments;
int m_argumentsExpected;
WebPage *m_page;
int m_expectingDataSize;
CommandParser *m_commandParser;
CommandFactory *m_commandFactory;
bool m_pageSuccess;
bool m_commandWaiting;
};
Expand Down
4 changes: 4 additions & 0 deletions src/webkit_server.pro
Expand Up @@ -28,6 +28,8 @@ HEADERS = \
SetCookie.h \
ClearCookies.h \
GetCookies.h \
CommandParser.h \
CommandFactory.h \

SOURCES = \
main.cpp \
Expand Down Expand Up @@ -57,6 +59,8 @@ SOURCES = \
SetCookie.cpp \
ClearCookies.cpp \
GetCookies.cpp \
CommandParser.cpp \
CommandFactory.cpp \

RESOURCES = webkit_server.qrc
QT += network webkit
Expand Down

0 comments on commit b4708b2

Please sign in to comment.