File indexing completed on 2024-05-12 17:15:12

0001 /*
0002    Copyright (C) 2013 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #ifndef EAVESDROPPERMODEL_H
0025 #define EAVESDROPPERMODEL_H
0026 
0027 #include "eavesdropperthread.h"
0028 #include "types.h"
0029 
0030 #include <QAbstractItemModel>
0031 
0032 #include <map>
0033 #include <string>
0034 #include <vector>
0035 
0036 class MainWidget;
0037 class Message;
0038 class MessageSortFilter;
0039 
0040 struct MessageRecord
0041 {
0042     MessageRecord(Message *msg, qint64 time);
0043     MessageRecord();
0044 
0045     QString type() const;
0046     // whether this is a call that should get a reply, with no reply
0047     bool isAwaitingReply() const;
0048     // whether this is a reply that we've seen the call for
0049     bool isReplyToKnownCall() const;
0050     // the serial of the "conversation", i.e. request-response pair
0051     uint32 conversationSerial() const;
0052     // either the method name, or if this is a response the request's method name
0053     QString conversationMethod(const std::vector<MessageRecord> &container) const;
0054     // time unit is nanoseconds
0055     qint64 conversationStartTime(const std::vector<MessageRecord> &container) const;
0056     qint64 roundtripTime(const std::vector<MessageRecord> &container) const;
0057     QString niceSender(const std::vector<MessageRecord> &container) const;
0058     bool couldHaveNicerDestination(const std::vector<MessageRecord> &container) const;
0059     QString niceDestination(const std::vector<MessageRecord> &container) const;
0060 
0061     Message *message;
0062     int otherMessageIndex;
0063     qint64 timestamp;
0064 };
0065 
0066 class EavesdropperModel : public QAbstractItemModel
0067 {
0068     Q_OBJECT
0069 public:
0070     EavesdropperModel(QObject *parent = nullptr);
0071     ~EavesdropperModel() override;
0072 
0073     QVariant data(const QModelIndex &index, int role) const override;
0074     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
0075     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0076     bool hasChildren (const QModelIndex &parent = QModelIndex()) const override;
0077     QModelIndex parent(const QModelIndex &child) const override;
0078     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0079     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0080 
0081 public slots:
0082     void setRecording(bool recording);
0083     void clear();
0084     void saveToFile(const QString &path);
0085     bool loadFromFile(const QString &path);
0086 
0087 private slots:
0088     void addMessage(Message *message, qint64 timestamp);
0089 
0090 private:
0091     void clearInternal();
0092 
0093     // for access to Message pointers to read arguments
0094     friend class MainWidget;
0095     // for direct access to MessageRecords to speed up filtering
0096     friend class MessageSortFilter;
0097 
0098     EavesdropperThread m_worker;
0099 
0100     struct Call {
0101         Call(uint32 s, const std::string &e)
0102            : serial(s), endpoint(e) {}
0103 
0104         bool operator<(const Call &other) const
0105         {
0106             if (serial != other.serial) {
0107                 return serial < other.serial;
0108             }
0109             return endpoint < other.endpoint;
0110         }
0111 
0112         uint32 serial;
0113         std::string endpoint;
0114     };
0115 
0116     bool m_isRecording;
0117     std::map<Call, uint32> m_callsAwaitingResponse; // the value is an index in m_messages
0118     std::vector<MessageRecord> m_messages;
0119 };
0120 
0121 #endif // EAVESDROPPERMODEL_H