CommandParser accepts a CommandFactory and emits a Command

This commit is contained in:
Joe Ferris 2012-03-21 19:30:43 -04:00
parent 3ebe0fa577
commit 1ed54b1d59
4 changed files with 24 additions and 13 deletions

View File

@ -1,11 +1,14 @@
#include "CommandParser.h" #include "CommandParser.h"
#include "CommandFactory.h"
#include "Command.h"
#include <QIODevice> #include <QIODevice>
CommandParser::CommandParser(QIODevice *device, QObject *parent) : CommandParser::CommandParser(QIODevice *device, CommandFactory *commandFactory, QObject *parent) :
QObject(parent) { QObject(parent) {
m_device = device; m_device = device;
m_expectingDataSize = -1; m_expectingDataSize = -1;
m_commandFactory = commandFactory;
connect(m_device, SIGNAL(readyRead()), this, SLOT(checkNext())); connect(m_device, SIGNAL(readyRead()), this, SLOT(checkNext()));
} }
@ -60,9 +63,14 @@ void CommandParser::processArgument(const char *data) {
} }
if (m_arguments.length() == m_argumentsExpected) { if (m_arguments.length() == m_argumentsExpected) {
emit commandReady(m_commandName, m_arguments); Command *command = m_commandFactory->createCommand(m_commandName.toAscii().constData(), m_arguments);
m_commandName = QString(); emit commandReady(command);
m_arguments.clear(); reset();
m_argumentsExpected = -1;
} }
} }
void CommandParser::reset() {
m_commandName = QString();
m_arguments.clear();
m_argumentsExpected = -1;
}

View File

@ -2,28 +2,32 @@
#include <QStringList> #include <QStringList>
class QIODevice; class QIODevice;
class CommandFactory;
class Command;
class CommandParser : public QObject { class CommandParser : public QObject {
Q_OBJECT Q_OBJECT
public: public:
CommandParser(QIODevice *device, QObject *parent = 0); CommandParser(QIODevice *device, CommandFactory *commandFactory, QObject *parent = 0);
public slots: public slots:
void checkNext(); void checkNext();
signals: signals:
void commandReady(QString commandName, QStringList arguments); void commandReady(Command *command);
private: private:
void readLine(); void readLine();
void readDataBlock(); void readDataBlock();
void processNext(const char *line); void processNext(const char *line);
void processArgument(const char *data); void processArgument(const char *data);
void reset();
QIODevice *m_device; QIODevice *m_device;
QString m_commandName; QString m_commandName;
QStringList m_arguments; QStringList m_arguments;
int m_argumentsExpected; int m_argumentsExpected;
int m_expectingDataSize; int m_expectingDataSize;
CommandFactory *m_commandFactory;
}; };

View File

@ -6,14 +6,13 @@
#include "Command.h" #include "Command.h"
#include <QTcpSocket> #include <QTcpSocket>
#include <iostream>
Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) : Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
QObject(parent) { QObject(parent) {
m_socket = socket; m_socket = socket;
m_page = page; m_page = page;
m_commandParser = new CommandParser(socket, this);
m_commandFactory = new CommandFactory(page, this); m_commandFactory = new CommandFactory(page, this);
m_commandParser = new CommandParser(socket, m_commandFactory, this);
m_runningCommand = NULL; m_runningCommand = NULL;
m_queuedCommand = NULL; m_queuedCommand = NULL;
m_pageSuccess = true; m_pageSuccess = true;
@ -21,12 +20,12 @@ Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
m_pageLoadingFromCommand = false; m_pageLoadingFromCommand = false;
m_pendingResponse = NULL; m_pendingResponse = NULL;
connect(m_socket, SIGNAL(readyRead()), m_commandParser, 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_commandParser, SIGNAL(commandReady(Command *)), this, SLOT(commandReady(Command *)));
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool))); connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
} }
void Connection::commandReady(QString commandName, QStringList arguments) { void Connection::commandReady(Command *command) {
m_queuedCommand = m_commandFactory->createCommand(commandName.toAscii().constData(), arguments); m_queuedCommand = command;
if (m_page->isLoading()) if (m_page->isLoading())
m_commandWaiting = true; m_commandWaiting = true;
else else

View File

@ -15,7 +15,7 @@ class Connection : public QObject {
Connection(QTcpSocket *socket, WebPage *page, QObject *parent = 0); Connection(QTcpSocket *socket, WebPage *page, QObject *parent = 0);
public slots: public slots:
void commandReady(QString commandName, QStringList arguments); void commandReady(Command *command);
void finishCommand(Response *response); void finishCommand(Response *response);
void pendingLoadFinished(bool success); void pendingLoadFinished(bool success);
void pageLoadingFromCommand(); void pageLoadingFromCommand();