swift
threadedreader.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_CORE_THREADED_READER_H
7 #define SWIFT_CORE_THREADED_READER_H
8 
9 #include <atomic>
10 
11 #include <QDateTime>
12 #include <QObject>
13 #include <QPair>
14 #include <QReadWriteLock>
15 #include <QString>
16 #include <QtGlobal>
17 
18 #include "core/swiftcoreexport.h"
20 #include "misc/logcategories.h"
22 #include "misc/worker.h"
23 
24 class QNetworkReply;
25 class QTimer;
26 
27 namespace swift::core
28 {
32  {
33  Q_OBJECT
34 
35  public:
37  static const QStringList &getLogCategories();
38 
40  virtual ~CThreadedReader();
41 
44  QDateTime getUpdateTimestamp() const;
45 
48  void setUpdateTimestamp(const QDateTime &updateTimestamp = QDateTime::currentDateTimeUtc());
49 
52  bool updatedWithinLastMs(qint64 timeLastMs);
53 
57  bool isMarkedAsFailed() const;
58 
62  void setMarkedAsFailed(bool failed);
63 
66  swift::misc::network::CUrlLogList getReadLog() const;
67 
70  void startReader();
71 
74  void pauseReader();
75 
78  void markAsUsedInUnitTest() { m_unitTest = true; }
79 
82  bool hasPendingUrls() const;
83 
86  swift::misc::network::CUrlLogList getUrlLogList() const;
87 
90  int getNetworkReplyProgress() const { return m_networkReplyProgress; }
91 
93  QPair<qint64, qint64> getNetworkReplyBytes() const;
94 
95  protected:
96  mutable QReadWriteLock m_lock {
97  QReadWriteLock::Recursive
98  };
99  std::atomic_int m_networkReplyProgress;
100  std::atomic_llong m_networkReplyCurrent;
101  std::atomic_llong m_networkReplyNax;
102 
104  CThreadedReader(QObject *owner, const QString &name);
105 
107  qint64 lastModifiedMsSinceEpoch(QNetworkReply *nwReply) const;
108 
110  void threadAssertCheck() const;
111 
114  bool didContentChange(const QString &content, int startPosition = -1);
115 
117  void setInitialAndPeriodicTime(int initialTime, int periodicTime);
118 
120  virtual void doWorkImpl() {}
121 
124  bool doWorkCheck() const;
125 
128  QNetworkReply *getFromNetworkAndLog(const swift::misc::network::CUrl &url,
129  const swift::misc::CSlot<void(QNetworkReply *)> &callback);
130 
132  virtual void networkReplyProgress(int logId, qint64 current, qint64 max, const QUrl &url);
133 
136  void logNetworkReplyReceived(QNetworkReply *reply);
137 
140  static void logInconsistentData(const swift::misc::CStatusMessage &msg, const char *funcInfo = nullptr);
141 
142  private:
144  void doWork();
145 
146  static constexpr int OutdatedPendingCallMs = 30 * 1000;
147 
148  int m_initialTime = -1;
149  int m_periodicTime = -1;
150  QDateTime m_updateTimestamp;
151  size_t m_contentHash = 0;
152  std::atomic_bool m_markedAsFailed { false };
153  bool m_unitTest { false };
154  swift::misc::network::CUrlLogList m_urlReadLog;
155  };
156 } // namespace swift::core
157 
158 #endif // SWIFT_CORE_THREADED_READER_H
Support for threaded based reading and parsing tasks such as data files via http, or file system and ...
std::atomic_llong m_networkReplyCurrent
current bytes
std::atomic_int m_networkReplyProgress
Progress percentage 0...100.
int getNetworkReplyProgress() const
Progress 0..100.
void markAsUsedInUnitTest()
Used in unit test.
std::atomic_llong m_networkReplyNax
max bytes
virtual void doWorkImpl()
This method does the actual work in the derived class.
Base class for a long-lived worker object which lives in its own thread.
Definition: worker.h:275
Callable wrapper for a member function with function signature F.
Definition: slot.h:62
Streamable status message, e.g.
Value object encapsulating information of a location, kind of simplified CValueObject compliant versi...
Definition: url.h:27
Value object encapsulating a list of voice rooms.
Definition: urlloglist.h:26
Backend services of the swift project, like dealing with the network or the simulators.
Definition: actionbind.cpp:7
#define SWIFT_CORE_EXPORT
Export a class or function from the library.