Skip to content

Commit

Permalink
CommandParser accepts a CommandFactory and emits a Command
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Mar 21, 2012
1 parent 3ebe0fa commit 1ed54b1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
18 changes: 13 additions & 5 deletions src/CommandParser.cpp
@@ -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()));
}

Expand Down Expand Up @@ -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;
}
8 changes: 6 additions & 2 deletions src/CommandParser.h
Expand Up @@ -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;
};

9 changes: 4 additions & 5 deletions src/Connection.cpp
Expand Up @@ -6,27 +6,26 @@
#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;
m_commandWaiting = false;
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
Expand Down
2 changes: 1 addition & 1 deletion src/Connection.h
Expand Up @@ -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();
Expand Down

0 comments on commit 1ed54b1

Please sign in to comment.