diff --git a/src/CommandParser.cpp b/src/CommandParser.cpp index bc2520f..1a0562b 100644 --- a/src/CommandParser.cpp +++ b/src/CommandParser.cpp @@ -1,11 +1,14 @@ #include "CommandParser.h" +#include "CommandFactory.h" +#include "Command.h" #include -CommandParser::CommandParser(QIODevice *device, QObject *parent) : +CommandParser::CommandParser(QIODevice *device, CommandFactory *commandFactory, QObject *parent) : QObject(parent) { m_device = device; m_expectingDataSize = -1; + m_commandFactory = commandFactory; connect(m_device, SIGNAL(readyRead()), this, SLOT(checkNext())); } @@ -60,9 +63,14 @@ void CommandParser::processArgument(const char *data) { } if (m_arguments.length() == m_argumentsExpected) { - emit commandReady(m_commandName, m_arguments); - m_commandName = QString(); - m_arguments.clear(); - m_argumentsExpected = -1; + Command *command = m_commandFactory->createCommand(m_commandName.toAscii().constData(), m_arguments); + emit commandReady(command); + reset(); } } + +void CommandParser::reset() { + m_commandName = QString(); + m_arguments.clear(); + m_argumentsExpected = -1; +} diff --git a/src/CommandParser.h b/src/CommandParser.h index 54d43be..19f7ec8 100644 --- a/src/CommandParser.h +++ b/src/CommandParser.h @@ -2,28 +2,32 @@ #include class QIODevice; +class CommandFactory; +class Command; class CommandParser : public QObject { Q_OBJECT public: - CommandParser(QIODevice *device, QObject *parent = 0); + CommandParser(QIODevice *device, CommandFactory *commandFactory, QObject *parent = 0); public slots: void checkNext(); signals: - void commandReady(QString commandName, QStringList arguments); + void commandReady(Command *command); private: void readLine(); void readDataBlock(); void processNext(const char *line); void processArgument(const char *data); + void reset(); QIODevice *m_device; QString m_commandName; QStringList m_arguments; int m_argumentsExpected; int m_expectingDataSize; + CommandFactory *m_commandFactory; }; diff --git a/src/Connection.cpp b/src/Connection.cpp index f9dbf5b..2e6e114 100644 --- a/src/Connection.cpp +++ b/src/Connection.cpp @@ -6,14 +6,13 @@ #include "Command.h" #include -#include 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_commandParser = new CommandParser(socket, m_commandFactory, this); m_runningCommand = NULL; m_queuedCommand = NULL; m_pageSuccess = true; @@ -21,12 +20,12 @@ Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) : m_pageLoadingFromCommand = false; m_pendingResponse = NULL; connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext())); - connect(m_commandParser, SIGNAL(commandReady(QString, QStringList)), this, SLOT(commandReady(QString, QStringList))); + connect(m_commandParser, SIGNAL(commandReady(Command *)), this, SLOT(commandReady(Command *))); connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool))); } -void Connection::commandReady(QString commandName, QStringList arguments) { - m_queuedCommand = m_commandFactory->createCommand(commandName.toAscii().constData(), arguments); +void Connection::commandReady(Command *command) { + m_queuedCommand = command; if (m_page->isLoading()) m_commandWaiting = true; else diff --git a/src/Connection.h b/src/Connection.h index 07aaec7..56bfb23 100644 --- a/src/Connection.h +++ b/src/Connection.h @@ -15,7 +15,7 @@ class Connection : public QObject { Connection(QTcpSocket *socket, WebPage *page, QObject *parent = 0); public slots: - void commandReady(QString commandName, QStringList arguments); + void commandReady(Command *command); void finishCommand(Response *response); void pendingLoadFinished(bool success); void pageLoadingFromCommand();