From b4708b2b8836f0d35a0e85caf547adecc2972fb4 Mon Sep 17 00:00:00 2001 From: Joe Ferris Date: Fri, 14 Oct 2011 11:22:24 -0400 Subject: [PATCH] Refactor Connection --- Gemfile.lock | 4 +- src/CommandFactory.cpp | 28 ++++++++++++ src/CommandFactory.h | 16 +++++++ src/CommandParser.cpp | 68 +++++++++++++++++++++++++++++ src/CommandParser.h | 29 +++++++++++++ src/Connection.cpp | 97 ++++++------------------------------------ src/Connection.h | 13 +++--- src/webkit_server.pro | 4 ++ 8 files changed, 166 insertions(+), 93 deletions(-) create mode 100644 src/CommandFactory.cpp create mode 100644 src/CommandFactory.h create mode 100644 src/CommandParser.cpp create mode 100644 src/CommandParser.h diff --git a/Gemfile.lock b/Gemfile.lock index db52c9db..7006192e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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/ @@ -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 diff --git a/src/CommandFactory.cpp b/src/CommandFactory.cpp new file mode 100644 index 00000000..7aaa9215 --- /dev/null +++ b/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; +} diff --git a/src/CommandFactory.h b/src/CommandFactory.h new file mode 100644 index 00000000..c52b8d6d --- /dev/null +++ b/src/CommandFactory.h @@ -0,0 +1,16 @@ +#include + +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; +}; + diff --git a/src/CommandParser.cpp b/src/CommandParser.cpp new file mode 100644 index 00000000..bc2520fe --- /dev/null +++ b/src/CommandParser.cpp @@ -0,0 +1,68 @@ +#include "CommandParser.h" + +#include + +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; + } +} diff --git a/src/CommandParser.h b/src/CommandParser.h new file mode 100644 index 00000000..54d43bee --- /dev/null +++ b/src/CommandParser.h @@ -0,0 +1,29 @@ +#include +#include + +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; +}; + diff --git a/src/Connection.cpp b/src/Connection.cpp index ce4ed7ae..f442ae00 100644 --- a/src/Connection.cpp +++ b/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 #include @@ -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 *)), @@ -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) @@ -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; } diff --git a/src/Connection.h b/src/Connection.h index 12f4f537..b39d3553 100644 --- a/src/Connection.h +++ b/src/Connection.h @@ -5,6 +5,8 @@ class QTcpSocket; class WebPage; class Command; class Response; +class CommandParser; +class CommandFactory; class Connection : public QObject { Q_OBJECT @@ -13,16 +15,11 @@ 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); @@ -30,9 +27,9 @@ class Connection : public QObject { 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; }; diff --git a/src/webkit_server.pro b/src/webkit_server.pro index 25acabd4..9b02630c 100644 --- a/src/webkit_server.pro +++ b/src/webkit_server.pro @@ -28,6 +28,8 @@ HEADERS = \ SetCookie.h \ ClearCookies.h \ GetCookies.h \ + CommandParser.h \ + CommandFactory.h \ SOURCES = \ main.cpp \ @@ -57,6 +59,8 @@ SOURCES = \ SetCookie.cpp \ ClearCookies.cpp \ GetCookies.cpp \ + CommandParser.cpp \ + CommandFactory.cpp \ RESOURCES = webkit_server.qrc QT += network webkit