]> Some of my projects - anidbudpclient.git/commitdiff
Add semi-useful command models for debugging/statistics.
authorAPTX <marek321@gmail.com>
Mon, 29 Nov 2010 17:01:38 +0000 (18:01 +0100)
committerAPTX <marek321@gmail.com>
Mon, 29 Nov 2010 17:01:38 +0000 (18:01 +0100)
12 files changed:
anidbudpclient.pri
anidbudpclient.pro
anidbudpclient_global.h
client.cpp
client.h
clientcommandmodel.cpp [deleted file]
clientqueuedcommandsmodel.cpp [new file with mode: 0644]
clientqueuedcommandsmodel.h [new file with mode: 0644]
clientsentcommandsmodel.cpp [new file with mode: 0644]
clientsentcommandsmodel.h [moved from clientcommandmodel.h with 76% similarity]
include/AniDBUdpClient/ClientQueuedCommandsModel [new file with mode: 0644]
include/AniDBUdpClient/ClientSentCommandsModel [new file with mode: 0644]

index 40880faa9e1103057c4ac06485c91bae83251cc8..f1500ebec91f9a18cf7b0a3ab859038c63d7d43c 100644 (file)
@@ -25,4 +25,4 @@ QT *= network \
 INCLUDEPATH += $$PWD/include
 DEPENDPATH += $$PWD
 LIBS += -lanidbudpclient
-LIBS += -L$$DESTDIR
+LIBS += -L$$DESTDIR -L$$PWD/build
index 8023a9730e5b94b88d4fa1b972142169831b3a08..5f01f7e4ba4a19b1757da9921a80bb684b744598 100644 (file)
@@ -31,7 +31,8 @@ SOURCES += client.cpp \
     hash.cpp \\r
     hashproducer.cpp \\r
        hashconsumer.cpp \\r
-    clientcommandmodel.cpp\r
+    clientsentcommandsmodel.cpp \\r
+    clientqueuedcommandsmodel.cpp\r
 \r
 HEADERS += client.h \\r
     anidbudpclient_global.h \\r
@@ -49,7 +50,8 @@ HEADERS += client.h \
     hashproducer.h \\r
     hashconsumer.h \\r
        circularbuffer.h \\r
-    clientcommandmodel.h\r
+    clientsentcommandsmodel.h \\r
+    clientqueuedcommandsmodel.h\r
 \r
 CONV_HEADERS += include/AniDBUdpClient/Client \\r
     include/AniDBUdpClient/AbstractCommand \\r
@@ -60,4 +62,6 @@ CONV_HEADERS += include/AniDBUdpClient/Client \
        include/AniDBUdpClient/VoteCommand \\r
        include/AniDBUdpClient/UptimeCommand \\r
        include/AniDBUdpClient/File \\r
-       include/AniDBUdpClient/Hash\r
+       include/AniDBUdpClient/Hash \\r
+       include/AniDBUdpClient/ClientSentCommandsModel \\r
+       include/AniDBUdpClient/ClientQueuedCommandsModel\r
index ff6787c12c89ec930289c16e76c56070171f0d3b..224d266f966e604ed0c60b945b1ca37beee1794e 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef ANIDBUDPCLIENT_GLOBAL_H
 #define ANIDBUDPCLIENT_GLOBAL_H
 
+#include <QtCore/QtGlobal>
 #include <QObject>
 #include <QMetaType>
 #include <QFileInfo>
index 39f0db22dc344204de8c80a22ab9f4c6337513ff..4d120d61c629f533a1d27b6273e7afabfd858703 100644 (file)
@@ -374,7 +374,9 @@ qDebug() << "commandTimer activein sendState";
        if (!controlCommandQueue.isEmpty())
        {
 qDebug() << "Sending Control Command";
-               sendCommand(controlCommandQueue.dequeue(), true);
+               AbstractReply *cmd = controlCommandQueue.dequeue();
+               emit model_queuedCommandRemoved(0);
+               sendCommand(cmd, true);
                emit commandSent();
                return;
        }
@@ -384,7 +386,11 @@ qDebug() << "Sending Control Command";
                emit queueEmpty();
                return;
        }
-       sendCommand(commandQueue.dequeue());
+
+       AbstractReply *cmd = commandQueue.dequeue();
+       emit model_queuedCommandRemoved(controlCommandQueue.size());
+       sendCommand(cmd);
+
        emit commandSent();
 }
 
@@ -548,6 +554,7 @@ qDebug() << QString("Sending reply to command with id: %1")
                if (sentCommandOrder.head() == commandId)
                {
                        sentCommandOrder.dequeue();
+                       emit model_sentCommandRemoved(0);
 
                        if (sentCommandOrder.isEmpty())
                        {
@@ -568,7 +575,12 @@ qDebug() << "Starting replyTimeoutTimer" << newTimeout;
                        // If the reply is not for the first command sent,
                        // then there is no need to change the replyTimeoutTimer
                        // NOTE: even though this is linear search, the command should always be among the first few.
-                       sentCommandOrder.removeOne(commandId);
+                       int index = sentCommandOrder.indexOf(commandId);
+                       if (index != -1)
+                       {
+                               sentCommandOrder.removeAt(index);
+                               emit model_sentCommandRemoved(index);
+                       }
                }
 
                bool controlCommand = cmd->controlCommand();
@@ -762,6 +774,8 @@ void Client::commandTimeout()
        Q_ASSERT(!sentCommandOrder.isEmpty());
 
        QByteArray commandId = sentCommandOrder.dequeue();
+       emit model_sentCommandRemoved(0);
+
 qDebug() << commandId << "timed out";
 
        if (!sentCommandOrder.isEmpty())
@@ -791,10 +805,12 @@ void Client::enqueueCommand(AbstractReply *command, bool first)
        if (first)
        {
                commandQueue.push_front(command);
+               emit model_queuedCommandAdded(controlCommandQueue.size());
        }
        else
        {
                commandQueue.enqueue(command);
+               emit model_queuedCommandAdded(controlCommandQueue.size() + commandQueue.size() - 1);
        }
 
        emit startSending();
@@ -805,10 +821,12 @@ void Client::enqueueControlCommand(AbstractReply *command, bool first)
        if (first)
        {
                controlCommandQueue.push_front(command);
+               emit model_queuedCommandAdded(0);
        }
        else
        {
                controlCommandQueue.enqueue(command);
+               emit model_queuedCommandAdded(controlCommandQueue.size() - 1);
        }
 
        emit startSending();
@@ -843,6 +861,7 @@ void Client::sendCommand(AbstractReply *command, bool controlCommand)
 
        sentCommands[commandId] = command;
        sentCommandOrder.enqueue(commandId);
+       emit model_sentCommandAdded(sentCommandOrder.size() - 1);
 
        if (!replyTimeoutTimer->isActive())
        {
index 087f2677bbcee0f0c1e21a8789935665d7a488b0..3b1a4eefe0b6f74abcb416170e377b65c00fc884 100644 (file)
--- a/client.h
+++ b/client.h
@@ -9,7 +9,6 @@
 #include <QHostInfo>
 #include <QVariantMap>
 
-#include "anidbudpclient_global.h"
 #include "authcommand.h"
 
 class QStateMachine;
@@ -28,7 +27,8 @@ class UptimeReply;
 class ANIDBUDPCLIENTSHARED_EXPORT Client : public QObject
 {
        friend class AbstractReply;
-       friend class ClientCommandModel;
+       friend class ClientQueuedCommandsModel;
+       friend class ClientSentCommandsModel;
 
        Q_OBJECT
        Q_ENUMS(AniDBUdpClient::State AniDBUdpClient::Error AniDBUdpClient::IdlePolicy AniDBUdpClient::ReplyCode);
@@ -110,7 +110,7 @@ public:
 private:
        template<typename T> typename T::ReplyType *createReply(const T &command, QObject *parent = 0)
        {
-               return new T::ReplyType(command, nextCommandId(), this, parent);
+               return new typename T::ReplyType(command, nextCommandId(), this, parent);
        }
 public slots:
 
@@ -139,6 +139,11 @@ signals:
 
        void connectionError();
 
+       void model_queuedCommandAdded(int index);
+       void model_queuedCommandRemoved(int index);
+       void model_sentCommandAdded(int index);
+       void model_sentCommandRemoved(int index);
+
 private slots:
        void enterErrorState();
        void enterDisconnectedState();
diff --git a/clientcommandmodel.cpp b/clientcommandmodel.cpp
deleted file mode 100644 (file)
index 8410a97..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
-#include "clientcommandmodel.h"\r
-\r
-#include "client.h"\r
-\r
-namespace AniDBUdpClient {\r
-\r
-ClientCommandModel::ClientCommandModel(Client *client, QObject *parent) :\r
-       QAbstractTableModel(parent), m_client(client)\r
-{\r
-}\r
-\r
-Client *ClientCommandModel::client() const\r
-{\r
-       return m_client;\r
-}\r
-\r
-void ClientCommandModel::setClient(Client *client)\r
-{\r
-       beginResetModel();\r
-       m_client = client;\r
-       endResetModel();\r
-}\r
-\r
-int ClientCommandModel::rowCount(const QModelIndex &/*parent*/) const\r
-{\r
-       if (!m_client)\r
-               return 0;\r
-       return m_client->sentCommands.count();\r
-}\r
-\r
-int ClientCommandModel::columnCount(const QModelIndex &/*parent*/) const\r
-{\r
-       return 3;\r
-}\r
-\r
-QVariant ClientCommandModel::headerData(int section, Qt::Orientation orientation, int role) const\r
-{\r
-       if (role != Qt::DisplayRole)\r
-               return QVariant();\r
-\r
-       if (orientation == Qt::Vertical)\r
-               return section + 1;\r
-\r
-       switch (section)\r
-       {\r
-               case 0:\r
-                       return tr("Time Sent");\r
-               case 1:\r
-                       return tr("Command");\r
-               case 2:\r
-                       return tr("Arguments");\r
-       }\r
-\r
-       return QVariant();\r
-}\r
-\r
-QVariant ClientCommandModel::data(const QModelIndex &index, int role) const\r
-{\r
-       if (role != Qt::DisplayRole)\r
-               return QVariant();\r
-\r
-       AbstractReply *currentReply = m_client->sentCommands[m_client->sentCommandOrder[index.row()]];\r
-\r
-       switch (index.column())\r
-       {\r
-               case 0:\r
-                       return currentReply->timeSent();\r
-               case 1:\r
-                       return currentReply->command().rawCommand().first;\r
-               case 2:\r
-                       return currentReply->command().rawCommand().second;\r
-       }\r
-       return QVariant();\r
-}\r
-\r
-\r
-void ClientCommandModel::commandAdded(int index)\r
-{\r
-       beginInsertRows(QModelIndex(), index, index + 1);\r
-       endInsertRows();\r
-}\r
-\r
-void ClientCommandModel::commandRemoved(int index)\r
-{\r
-       beginRemoveRows(QModelIndex(), index, index + 1);\r
-       endRemoveRows();\r
-}\r
-\r
-} // namespace AniDBUdpClient\r
diff --git a/clientqueuedcommandsmodel.cpp b/clientqueuedcommandsmodel.cpp
new file mode 100644 (file)
index 0000000..92a2f0d
--- /dev/null
@@ -0,0 +1,112 @@
+#include "clientqueuedcommandsmodel.h"\r
+\r
+#include "client.h"\r
+\r
+namespace AniDBUdpClient {\r
+\r
+ClientQueuedCommandsModel::ClientQueuedCommandsModel(Client *client, QObject *parent) :\r
+       m_client(0), QAbstractTableModel(parent)\r
+{\r
+       setClient(client);\r
+}\r
+\r
+Client *ClientQueuedCommandsModel::client() const\r
+{\r
+       return m_client;\r
+}\r
+\r
+void ClientQueuedCommandsModel::setClient(Client *client)\r
+{\r
+       if (m_client)\r
+               disconnect(m_client, 0, this, 0);\r
+\r
+       beginResetModel();\r
+       if (client)\r
+       {\r
+               connect(client, SIGNAL(model_queuedCommandAdded(int)), this, SLOT(commandAdded(int)));\r
+               connect(client, SIGNAL(model_queuedCommandRemoved(int)), this, SLOT(commandRemoved(int)));\r
+       }\r
+       m_client = client;\r
+       endResetModel();\r
+}\r
+\r
+int ClientQueuedCommandsModel::rowCount(const QModelIndex &/*parent*/) const\r
+{\r
+       if (!m_client)\r
+               return 0;\r
+       return m_client->controlCommandQueue.count() + m_client->commandQueue.count();\r
+}\r
+\r
+int ClientQueuedCommandsModel::columnCount(const QModelIndex &/*parent*/) const\r
+{\r
+       return 3;\r
+}\r
+\r
+QVariant ClientQueuedCommandsModel::headerData(int section, Qt::Orientation orientation, int role) const\r
+{\r
+       if (role != Qt::DisplayRole)\r
+               return QVariant();\r
+\r
+       if (orientation == Qt::Vertical)\r
+       {\r
+               return section + 1;\r
+       }\r
+\r
+       switch (section)\r
+       {\r
+               case 0:\r
+                       return tr("Control command");\r
+               case 1:\r
+                       return tr("Command");\r
+               case 2:\r
+                       return tr("Arguments");\r
+       }\r
+\r
+       return QVariant();\r
+}\r
+\r
+QVariant ClientQueuedCommandsModel::data(const QModelIndex &index, int role) const\r
+{\r
+       if (role != Qt::DisplayRole)\r
+               return QVariant();\r
+\r
+       AbstractReply *currentReply;\r
+       if (index.row() < m_client->controlCommandQueue.count())\r
+               currentReply = m_client->controlCommandQueue.at(index.row());\r
+       else\r
+               currentReply = m_client->commandQueue.at(index.row() - m_client->controlCommandQueue.count());\r
+\r
+       switch (index.column())\r
+       {\r
+               case 0:\r
+                       return currentReply->controlCommand() ? tr("Yes") : tr("No");\r
+               case 1:\r
+                       return currentReply->command().rawCommand().first;\r
+               case 2:\r
+               {\r
+                       Command rawCommand = currentReply->command().rawCommand();\r
+                       QString s = QString("(%1) ").arg(rawCommand.second.count());\r
+                       for (QVariantMap::const_iterator i = rawCommand.second.constBegin(); i != rawCommand.second.constEnd(); ++i)\r
+                       {\r
+                               s += QString("%1=%2; ").arg(i.key(), i.value().toString());\r
+                       }\r
+                       return s;\r
+               }\r
+       }\r
+       return QVariant();\r
+}\r
+\r
+\r
+void ClientQueuedCommandsModel::commandAdded(int index)\r
+{\r
+       beginInsertRows(QModelIndex(), index, index);\r
+       endInsertRows();\r
+}\r
+\r
+void ClientQueuedCommandsModel::commandRemoved(int index)\r
+{\r
+       beginRemoveRows(QModelIndex(), index, index);\r
+       endRemoveRows();\r
+}\r
+\r
+} // namespace AniDBUdpClient\r
diff --git a/clientqueuedcommandsmodel.h b/clientqueuedcommandsmodel.h
new file mode 100644 (file)
index 0000000..304ec47
--- /dev/null
@@ -0,0 +1,40 @@
+#ifndef CLIENTQUEUEDCOMMANDSMODEL_H\r
+#define CLIENTQUEUEDCOMMANDSMODEL_H\r
+\r
+#include "anidbudpclient_global.h"\r
+\r
+#include <QAbstractTableModel>\r
+\r
+namespace AniDBUdpClient {\r
+\r
+class Client;\r
+\r
+class ANIDBUDPCLIENTSHARED_EXPORT ClientQueuedCommandsModel : public QAbstractTableModel\r
+{\r
+    Q_OBJECT\r
+public:\r
+       explicit ClientQueuedCommandsModel(Client *client, QObject *parent = 0);\r
+\r
+       Client *client() const;\r
+       void setClient(Client *client);\r
+\r
+       int rowCount(const QModelIndex &parent = QModelIndex()) const;\r
+       int columnCount(const QModelIndex &/*parent*/) const;\r
+\r
+       QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;\r
+       QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;\r
+\r
+signals:\r
+\r
+private slots:\r
+       void commandAdded(int index);\r
+       void commandRemoved(int index);\r
+\r
+private:\r
+\r
+       Client *m_client;\r
+};\r
+\r
+} // namespace AniDBUdpClient\r
+\r
+#endif // CLIENTQUEUEDCOMMANDSMODEL_H\r
diff --git a/clientsentcommandsmodel.cpp b/clientsentcommandsmodel.cpp
new file mode 100644 (file)
index 0000000..4603de4
--- /dev/null
@@ -0,0 +1,108 @@
+#include "clientsentcommandsmodel.h"\r
+\r
+#include "client.h"\r
+\r
+namespace AniDBUdpClient {\r
+\r
+ClientSentCommandsModel::ClientSentCommandsModel(Client *client, QObject *parent) :\r
+       m_client(0), QAbstractTableModel(parent)\r
+{\r
+       setClient(client);\r
+}\r
+\r
+Client *ClientSentCommandsModel::client() const\r
+{\r
+       return m_client;\r
+}\r
+\r
+void ClientSentCommandsModel::setClient(Client *client)\r
+{\r
+       if (m_client)\r
+               disconnect(m_client, 0, this, 0);\r
+\r
+       beginResetModel();\r
+       if (client)\r
+       {\r
+               connect(client, SIGNAL(model_sentCommandAdded(int)), this, SLOT(commandAdded(int)));\r
+               connect(client, SIGNAL(model_sentCommandRemoved(int)), this, SLOT(commandRemoved(int)));\r
+       }\r
+       m_client = client;\r
+       endResetModel();\r
+}\r
+\r
+int ClientSentCommandsModel::rowCount(const QModelIndex &/*parent*/) const\r
+{\r
+       if (!m_client)\r
+               return 0;\r
+       return m_client->sentCommandOrder.count();\r
+}\r
+\r
+int ClientSentCommandsModel::columnCount(const QModelIndex &/*parent*/) const\r
+{\r
+       return 3;\r
+}\r
+\r
+QVariant ClientSentCommandsModel::headerData(int section, Qt::Orientation orientation, int role) const\r
+{\r
+       if (role != Qt::DisplayRole)\r
+               return QVariant();\r
+\r
+       if (orientation == Qt::Vertical)\r
+       {\r
+               return section + 1;\r
+       }\r
+\r
+       switch (section)\r
+       {\r
+               case 0:\r
+                       return tr("Time Sent");\r
+               case 1:\r
+                       return tr("Command");\r
+               case 2:\r
+                       return tr("Arguments");\r
+       }\r
+\r
+       return QVariant();\r
+}\r
+\r
+QVariant ClientSentCommandsModel::data(const QModelIndex &index, int role) const\r
+{\r
+       if (role != Qt::DisplayRole)\r
+               return QVariant();\r
+\r
+       AbstractReply *currentReply = m_client->sentCommands[m_client->sentCommandOrder[index.row()]];\r
+\r
+       switch (index.column())\r
+       {\r
+               case 0:\r
+                       return currentReply->timeSent();\r
+               case 1:\r
+                       return currentReply->command().rawCommand().first;\r
+               case 2:\r
+               {\r
+                       Command rawCommand = currentReply->command().rawCommand();\r
+                       QString s = QString("(%1) ").arg(rawCommand.second.count());\r
+                       for (QVariantMap::const_iterator i = rawCommand.second.constBegin(); i != rawCommand.second.constEnd(); ++i)\r
+                       {\r
+                               s += QString("%1=%2; ").arg(i.key(), i.value().toString());\r
+                       }\r
+                       return s;\r
+               }\r
+       }\r
+       return QVariant();\r
+}\r
+\r
+\r
+void ClientSentCommandsModel::commandAdded(int index)\r
+{\r
+       beginInsertRows(QModelIndex(), index, index);\r
+       endInsertRows();\r
+}\r
+\r
+void ClientSentCommandsModel::commandRemoved(int index)\r
+{\r
+       beginRemoveRows(QModelIndex(), index, index);\r
+       endRemoveRows();\r
+}\r
+\r
+} // namespace AniDBUdpClient\r
similarity index 76%
rename from clientcommandmodel.h
rename to clientsentcommandsmodel.h
index 306743efd489b7b6e7ea82426e6bc73e9a2bee1e..73ac46fa458422d023902a3d6b4fd1cd9479f151 100644 (file)
@@ -9,11 +9,11 @@ namespace AniDBUdpClient {
 \r
 class Client;\r
 \r
-class ANIDBUDPCLIENTSHARED_EXPORT ClientCommandModel : public QAbstractTableModel\r
+class ANIDBUDPCLIENTSHARED_EXPORT ClientSentCommandsModel : public QAbstractTableModel\r
 {\r
     Q_OBJECT\r
 public:\r
-       explicit ClientCommandModel(Client *client = 0, QObject *parent = 0);\r
+       explicit ClientSentCommandsModel(Client *client = 0, QObject *parent = 0);\r
 \r
        Client *client() const;\r
        void setClient(Client *client);\r
@@ -26,12 +26,12 @@ public:
 \r
 signals:\r
 \r
-public slots:\r
-\r
-private:\r
+private slots:\r
        void commandAdded(int index);\r
        void commandRemoved(int index);\r
 \r
+private:\r
+\r
        Client *m_client;\r
 };\r
 \r
diff --git a/include/AniDBUdpClient/ClientQueuedCommandsModel b/include/AniDBUdpClient/ClientQueuedCommandsModel
new file mode 100644 (file)
index 0000000..978dd22
--- /dev/null
@@ -0,0 +1 @@
+#include "../../clientqueuedcommandsmodel.h"
\ No newline at end of file
diff --git a/include/AniDBUdpClient/ClientSentCommandsModel b/include/AniDBUdpClient/ClientSentCommandsModel
new file mode 100644 (file)
index 0000000..97585ef
--- /dev/null
@@ -0,0 +1 @@
+#include "../../clientsentcommandsmodel.h"
\ No newline at end of file