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 "CommandFactory.h"
#include "Command.h"
#include <QIODevice>
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;
}

View File

@ -2,28 +2,32 @@
#include <QStringList>
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;
};

View File

@ -6,14 +6,13 @@
#include "Command.h"
#include <QTcpSocket>
#include <iostream>
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

View File

@ -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();