Skip to content
This repository
Browse code

CommandParser accepts a CommandFactory and emits a Command

  • Loading branch information...
commit 1ed54b1d59e3e23edc181d8a6606f70a8b2a6385 1 parent 3ebe0fa
Joe Ferris authored March 21, 2012
18  src/CommandParser.cpp
... ...
@@ -1,11 +1,14 @@
1 1
 #include "CommandParser.h"
  2
+#include "CommandFactory.h"
  3
+#include "Command.h"
2 4
 
3 5
 #include <QIODevice>
4 6
 
5  
-CommandParser::CommandParser(QIODevice *device, QObject *parent) :
  7
+CommandParser::CommandParser(QIODevice *device, CommandFactory *commandFactory, QObject *parent) :
6 8
     QObject(parent) {
7 9
   m_device = device;
8 10
   m_expectingDataSize = -1;
  11
+  m_commandFactory = commandFactory;
9 12
   connect(m_device, SIGNAL(readyRead()), this, SLOT(checkNext()));
10 13
 }
11 14
 
@@ -60,9 +63,14 @@ void CommandParser::processArgument(const char *data) {
60 63
   }
61 64
 
62 65
   if (m_arguments.length() == m_argumentsExpected) {
63  
-    emit commandReady(m_commandName, m_arguments);
64  
-    m_commandName = QString();
65  
-    m_arguments.clear();
66  
-    m_argumentsExpected = -1;
  66
+    Command *command = m_commandFactory->createCommand(m_commandName.toAscii().constData(), m_arguments);
  67
+    emit commandReady(command);
  68
+    reset();
67 69
   }
68 70
 }
  71
+
  72
+void CommandParser::reset() {
  73
+  m_commandName = QString();
  74
+  m_arguments.clear();
  75
+  m_argumentsExpected = -1;
  76
+}
8  src/CommandParser.h
@@ -2,28 +2,32 @@
2 2
 #include <QStringList>
3 3
 
4 4
 class QIODevice;
  5
+class CommandFactory;
  6
+class Command;
5 7
 
6 8
 class CommandParser : public QObject {
7 9
   Q_OBJECT
8 10
 
9 11
   public:
10  
-    CommandParser(QIODevice *device, QObject *parent = 0);
  12
+    CommandParser(QIODevice *device, CommandFactory *commandFactory, QObject *parent = 0);
11 13
 
12 14
   public slots:
13 15
     void checkNext();
14 16
 
15 17
   signals:
16  
-    void commandReady(QString commandName, QStringList arguments);
  18
+    void commandReady(Command *command);
17 19
 
18 20
   private:
19 21
     void readLine();
20 22
     void readDataBlock();
21 23
     void processNext(const char *line);
22 24
     void processArgument(const char *data);
  25
+    void reset();
23 26
     QIODevice *m_device;
24 27
     QString m_commandName;
25 28
     QStringList m_arguments;
26 29
     int m_argumentsExpected;
27 30
     int m_expectingDataSize;
  31
+    CommandFactory *m_commandFactory;
28 32
 };
29 33
 
9  src/Connection.cpp
@@ -6,14 +6,13 @@
6 6
 #include "Command.h"
7 7
 
8 8
 #include <QTcpSocket>
9  
-#include <iostream>
10 9
 
11 10
 Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
12 11
     QObject(parent) {
13 12
   m_socket = socket;
14 13
   m_page = page;
15  
-  m_commandParser = new CommandParser(socket, this);
16 14
   m_commandFactory = new CommandFactory(page, this);
  15
+  m_commandParser = new CommandParser(socket, m_commandFactory, this);
17 16
   m_runningCommand = NULL;
18 17
   m_queuedCommand = NULL;
19 18
   m_pageSuccess = true;
@@ -21,12 +20,12 @@ Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
21 20
   m_pageLoadingFromCommand = false;
22 21
   m_pendingResponse = NULL;
23 22
   connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext()));
24  
-  connect(m_commandParser, SIGNAL(commandReady(QString, QStringList)), this, SLOT(commandReady(QString, QStringList)));
  23
+  connect(m_commandParser, SIGNAL(commandReady(Command *)), this, SLOT(commandReady(Command *)));
25 24
   connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
26 25
 }
27 26
 
28  
-void Connection::commandReady(QString commandName, QStringList arguments) {
29  
-  m_queuedCommand = m_commandFactory->createCommand(commandName.toAscii().constData(), arguments);
  27
+void Connection::commandReady(Command *command) {
  28
+  m_queuedCommand = command;
30 29
   if (m_page->isLoading())
31 30
     m_commandWaiting = true;
32 31
   else
2  src/Connection.h
@@ -15,7 +15,7 @@ class Connection : public QObject {
15 15
     Connection(QTcpSocket *socket, WebPage *page, QObject *parent = 0);
16 16
 
17 17
   public slots:
18  
-    void commandReady(QString commandName, QStringList arguments);
  18
+    void commandReady(Command *command);
19 19
     void finishCommand(Response *response);
20 20
     void pendingLoadFinished(bool success);
21 21
     void pageLoadingFromCommand();

0 notes on commit 1ed54b1

Please sign in to comment.
Something went wrong with that request. Please try again.