From: APTX Date: Mon, 29 Nov 2010 17:01:38 +0000 (+0100) Subject: Add semi-useful command models for debugging/statistics. X-Git-Url: https://gitweb.aptx.org/?a=commitdiff_plain;h=84c29f6166f97f2676f4425c347a373e83cc8522;p=anidbudpclient.git Add semi-useful command models for debugging/statistics. --- diff --git a/anidbudpclient.pri b/anidbudpclient.pri index 40880fa..f1500eb 100644 --- a/anidbudpclient.pri +++ b/anidbudpclient.pri @@ -25,4 +25,4 @@ QT *= network \ INCLUDEPATH += $$PWD/include DEPENDPATH += $$PWD LIBS += -lanidbudpclient -LIBS += -L$$DESTDIR +LIBS += -L$$DESTDIR -L$$PWD/build diff --git a/anidbudpclient.pro b/anidbudpclient.pro index 8023a97..5f01f7e 100644 --- a/anidbudpclient.pro +++ b/anidbudpclient.pro @@ -31,7 +31,8 @@ SOURCES += client.cpp \ hash.cpp \ hashproducer.cpp \ hashconsumer.cpp \ - clientcommandmodel.cpp + clientsentcommandsmodel.cpp \ + clientqueuedcommandsmodel.cpp HEADERS += client.h \ anidbudpclient_global.h \ @@ -49,7 +50,8 @@ HEADERS += client.h \ hashproducer.h \ hashconsumer.h \ circularbuffer.h \ - clientcommandmodel.h + clientsentcommandsmodel.h \ + clientqueuedcommandsmodel.h CONV_HEADERS += include/AniDBUdpClient/Client \ include/AniDBUdpClient/AbstractCommand \ @@ -60,4 +62,6 @@ CONV_HEADERS += include/AniDBUdpClient/Client \ include/AniDBUdpClient/VoteCommand \ include/AniDBUdpClient/UptimeCommand \ include/AniDBUdpClient/File \ - include/AniDBUdpClient/Hash + include/AniDBUdpClient/Hash \ + include/AniDBUdpClient/ClientSentCommandsModel \ + include/AniDBUdpClient/ClientQueuedCommandsModel diff --git a/anidbudpclient_global.h b/anidbudpclient_global.h index ff6787c..224d266 100644 --- a/anidbudpclient_global.h +++ b/anidbudpclient_global.h @@ -1,6 +1,7 @@ #ifndef ANIDBUDPCLIENT_GLOBAL_H #define ANIDBUDPCLIENT_GLOBAL_H +#include #include #include #include diff --git a/client.cpp b/client.cpp index 39f0db2..4d120d6 100644 --- a/client.cpp +++ b/client.cpp @@ -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()) { diff --git a/client.h b/client.h index 087f267..3b1a4ee 100644 --- a/client.h +++ b/client.h @@ -9,7 +9,6 @@ #include #include -#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::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 index 8410a97..0000000 --- a/clientcommandmodel.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include "clientcommandmodel.h" - -#include "client.h" - -namespace AniDBUdpClient { - -ClientCommandModel::ClientCommandModel(Client *client, QObject *parent) : - QAbstractTableModel(parent), m_client(client) -{ -} - -Client *ClientCommandModel::client() const -{ - return m_client; -} - -void ClientCommandModel::setClient(Client *client) -{ - beginResetModel(); - m_client = client; - endResetModel(); -} - -int ClientCommandModel::rowCount(const QModelIndex &/*parent*/) const -{ - if (!m_client) - return 0; - return m_client->sentCommands.count(); -} - -int ClientCommandModel::columnCount(const QModelIndex &/*parent*/) const -{ - return 3; -} - -QVariant ClientCommandModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - if (role != Qt::DisplayRole) - return QVariant(); - - if (orientation == Qt::Vertical) - return section + 1; - - switch (section) - { - case 0: - return tr("Time Sent"); - case 1: - return tr("Command"); - case 2: - return tr("Arguments"); - } - - return QVariant(); -} - -QVariant ClientCommandModel::data(const QModelIndex &index, int role) const -{ - if (role != Qt::DisplayRole) - return QVariant(); - - AbstractReply *currentReply = m_client->sentCommands[m_client->sentCommandOrder[index.row()]]; - - switch (index.column()) - { - case 0: - return currentReply->timeSent(); - case 1: - return currentReply->command().rawCommand().first; - case 2: - return currentReply->command().rawCommand().second; - } - return QVariant(); -} - - -void ClientCommandModel::commandAdded(int index) -{ - beginInsertRows(QModelIndex(), index, index + 1); - endInsertRows(); -} - -void ClientCommandModel::commandRemoved(int index) -{ - beginRemoveRows(QModelIndex(), index, index + 1); - endRemoveRows(); -} - -} // namespace AniDBUdpClient diff --git a/clientqueuedcommandsmodel.cpp b/clientqueuedcommandsmodel.cpp new file mode 100644 index 0000000..92a2f0d --- /dev/null +++ b/clientqueuedcommandsmodel.cpp @@ -0,0 +1,112 @@ +#include "clientqueuedcommandsmodel.h" + +#include "client.h" + +namespace AniDBUdpClient { + +ClientQueuedCommandsModel::ClientQueuedCommandsModel(Client *client, QObject *parent) : + m_client(0), QAbstractTableModel(parent) +{ + setClient(client); +} + +Client *ClientQueuedCommandsModel::client() const +{ + return m_client; +} + +void ClientQueuedCommandsModel::setClient(Client *client) +{ + if (m_client) + disconnect(m_client, 0, this, 0); + + beginResetModel(); + if (client) + { + connect(client, SIGNAL(model_queuedCommandAdded(int)), this, SLOT(commandAdded(int))); + connect(client, SIGNAL(model_queuedCommandRemoved(int)), this, SLOT(commandRemoved(int))); + } + m_client = client; + endResetModel(); +} + +int ClientQueuedCommandsModel::rowCount(const QModelIndex &/*parent*/) const +{ + if (!m_client) + return 0; + return m_client->controlCommandQueue.count() + m_client->commandQueue.count(); +} + +int ClientQueuedCommandsModel::columnCount(const QModelIndex &/*parent*/) const +{ + return 3; +} + +QVariant ClientQueuedCommandsModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + + if (orientation == Qt::Vertical) + { + return section + 1; + } + + switch (section) + { + case 0: + return tr("Control command"); + case 1: + return tr("Command"); + case 2: + return tr("Arguments"); + } + + return QVariant(); +} + +QVariant ClientQueuedCommandsModel::data(const QModelIndex &index, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + + AbstractReply *currentReply; + if (index.row() < m_client->controlCommandQueue.count()) + currentReply = m_client->controlCommandQueue.at(index.row()); + else + currentReply = m_client->commandQueue.at(index.row() - m_client->controlCommandQueue.count()); + + switch (index.column()) + { + case 0: + return currentReply->controlCommand() ? tr("Yes") : tr("No"); + case 1: + return currentReply->command().rawCommand().first; + case 2: + { + Command rawCommand = currentReply->command().rawCommand(); + QString s = QString("(%1) ").arg(rawCommand.second.count()); + for (QVariantMap::const_iterator i = rawCommand.second.constBegin(); i != rawCommand.second.constEnd(); ++i) + { + s += QString("%1=%2; ").arg(i.key(), i.value().toString()); + } + return s; + } + } + return QVariant(); +} + + +void ClientQueuedCommandsModel::commandAdded(int index) +{ + beginInsertRows(QModelIndex(), index, index); + endInsertRows(); +} + +void ClientQueuedCommandsModel::commandRemoved(int index) +{ + beginRemoveRows(QModelIndex(), index, index); + endRemoveRows(); +} + +} // namespace AniDBUdpClient diff --git a/clientqueuedcommandsmodel.h b/clientqueuedcommandsmodel.h new file mode 100644 index 0000000..304ec47 --- /dev/null +++ b/clientqueuedcommandsmodel.h @@ -0,0 +1,40 @@ +#ifndef CLIENTQUEUEDCOMMANDSMODEL_H +#define CLIENTQUEUEDCOMMANDSMODEL_H + +#include "anidbudpclient_global.h" + +#include + +namespace AniDBUdpClient { + +class Client; + +class ANIDBUDPCLIENTSHARED_EXPORT ClientQueuedCommandsModel : public QAbstractTableModel +{ + Q_OBJECT +public: + explicit ClientQueuedCommandsModel(Client *client, QObject *parent = 0); + + Client *client() const; + void setClient(Client *client); + + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &/*parent*/) const; + + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + +signals: + +private slots: + void commandAdded(int index); + void commandRemoved(int index); + +private: + + Client *m_client; +}; + +} // namespace AniDBUdpClient + +#endif // CLIENTQUEUEDCOMMANDSMODEL_H diff --git a/clientsentcommandsmodel.cpp b/clientsentcommandsmodel.cpp new file mode 100644 index 0000000..4603de4 --- /dev/null +++ b/clientsentcommandsmodel.cpp @@ -0,0 +1,108 @@ +#include "clientsentcommandsmodel.h" + +#include "client.h" + +namespace AniDBUdpClient { + +ClientSentCommandsModel::ClientSentCommandsModel(Client *client, QObject *parent) : + m_client(0), QAbstractTableModel(parent) +{ + setClient(client); +} + +Client *ClientSentCommandsModel::client() const +{ + return m_client; +} + +void ClientSentCommandsModel::setClient(Client *client) +{ + if (m_client) + disconnect(m_client, 0, this, 0); + + beginResetModel(); + if (client) + { + connect(client, SIGNAL(model_sentCommandAdded(int)), this, SLOT(commandAdded(int))); + connect(client, SIGNAL(model_sentCommandRemoved(int)), this, SLOT(commandRemoved(int))); + } + m_client = client; + endResetModel(); +} + +int ClientSentCommandsModel::rowCount(const QModelIndex &/*parent*/) const +{ + if (!m_client) + return 0; + return m_client->sentCommandOrder.count(); +} + +int ClientSentCommandsModel::columnCount(const QModelIndex &/*parent*/) const +{ + return 3; +} + +QVariant ClientSentCommandsModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + + if (orientation == Qt::Vertical) + { + return section + 1; + } + + switch (section) + { + case 0: + return tr("Time Sent"); + case 1: + return tr("Command"); + case 2: + return tr("Arguments"); + } + + return QVariant(); +} + +QVariant ClientSentCommandsModel::data(const QModelIndex &index, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + + AbstractReply *currentReply = m_client->sentCommands[m_client->sentCommandOrder[index.row()]]; + + switch (index.column()) + { + case 0: + return currentReply->timeSent(); + case 1: + return currentReply->command().rawCommand().first; + case 2: + { + Command rawCommand = currentReply->command().rawCommand(); + QString s = QString("(%1) ").arg(rawCommand.second.count()); + for (QVariantMap::const_iterator i = rawCommand.second.constBegin(); i != rawCommand.second.constEnd(); ++i) + { + s += QString("%1=%2; ").arg(i.key(), i.value().toString()); + } + return s; + } + } + return QVariant(); +} + + +void ClientSentCommandsModel::commandAdded(int index) +{ + beginInsertRows(QModelIndex(), index, index); + endInsertRows(); +} + +void ClientSentCommandsModel::commandRemoved(int index) +{ + beginRemoveRows(QModelIndex(), index, index); + endRemoveRows(); +} + +} // namespace AniDBUdpClient diff --git a/clientcommandmodel.h b/clientsentcommandsmodel.h similarity index 76% rename from clientcommandmodel.h rename to clientsentcommandsmodel.h index 306743e..73ac46f 100644 --- a/clientcommandmodel.h +++ b/clientsentcommandsmodel.h @@ -9,11 +9,11 @@ namespace AniDBUdpClient { class Client; -class ANIDBUDPCLIENTSHARED_EXPORT ClientCommandModel : public QAbstractTableModel +class ANIDBUDPCLIENTSHARED_EXPORT ClientSentCommandsModel : public QAbstractTableModel { Q_OBJECT public: - explicit ClientCommandModel(Client *client = 0, QObject *parent = 0); + explicit ClientSentCommandsModel(Client *client = 0, QObject *parent = 0); Client *client() const; void setClient(Client *client); @@ -26,12 +26,12 @@ public: signals: -public slots: - -private: +private slots: void commandAdded(int index); void commandRemoved(int index); +private: + Client *m_client; }; diff --git a/include/AniDBUdpClient/ClientQueuedCommandsModel b/include/AniDBUdpClient/ClientQueuedCommandsModel new file mode 100644 index 0000000..978dd22 --- /dev/null +++ b/include/AniDBUdpClient/ClientQueuedCommandsModel @@ -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 index 0000000..97585ef --- /dev/null +++ b/include/AniDBUdpClient/ClientSentCommandsModel @@ -0,0 +1 @@ +#include "../../clientsentcommandsmodel.h" \ No newline at end of file