6 #include <QCoreApplication>
11 #include <QWriteLocker>
20 using namespace swift::misc::network;
21 using namespace swift::core::vatsim;
25 const QStringList &CThreadedReader::getLogCategories()
31 CThreadedReader::CThreadedReader(QObject *owner,
const QString &name) :
CContinuousWorker(owner, name)
33 connect(&
m_updateTimer, &QTimer::timeout,
this, &CThreadedReader::doWork);
41 return CNetworkUtils::lastModifiedMsSinceEpoch(nwReply);
47 return m_updateTimestamp;
52 QWriteLocker lock(&
m_lock);
53 m_updateTimestamp = updateTimestamp;
59 if (dt.isNull() || !dt.isValid()) {
return false; }
60 qint64 delta = QDateTime::currentMSecsSinceEpoch() - dt.toMSecsSinceEpoch();
61 return delta <= timeLastMs;
66 Q_ASSERT(m_initialTime > 0);
89 oldHash = m_contentHash;
91 size_t newHash =
qHash(startPosition < 0 ? content : content.mid(startPosition));
92 if (oldHash == newHash) {
return false; }
95 m_contentHash = newHash;
112 Q_ASSERT_X(QCoreApplication::instance()->thread() != QThread::currentThread(), Q_FUNC_INFO,
113 "Needs to run in own thread");
114 Q_ASSERT_X(this->thread() == QThread::currentThread(), Q_FUNC_INFO,
"Wrong object thread");
119 m_initialTime = initialTime;
120 m_periodicTime = periodicTime;
127 const int delta = m_periodicTime - oldPeriodicTime +
m_updateTimer.remainingTime();
132 void CThreadedReader::doWork()
136 Q_ASSERT(m_periodicTime > 0);
143 if (!this->
isEnabled()) {
return false; }
156 if (!outdatedPendingUrls.
isEmpty())
189 if (!reply) {
return; }
191 m_urlReadLog.
markAsReceived(reply, reply->error() == QNetworkReply::NoError);
201 const QByteArray m(logMsg.
getMessage().toLatin1());
SWIFT_CORE_EXPORT swift::core::CApplication * sApp
Single instance of application object.
QNetworkReply * getFromNetwork(const swift::misc::network::CUrl &url, const CallbackSlot &callback, int maxRedirects=DefaultMaxRedirects)
Request to get network reply.
bool isShuttingDown() const
Is application shutting down?
std::atomic_llong m_networkReplyCurrent
current bytes
QDateTime getUpdateTimestamp() const
Thread safe, get update timestamp.
void pauseReader()
Pauses the reader.
QPair< qint64, qint64 > getNetworkReplyBytes() const
Max./current bytes.
void setUpdateTimestamp(const QDateTime &updateTimestamp=QDateTime::currentDateTimeUtc())
Thread safe, set update timestamp.
virtual void networkReplyProgress(int logId, qint64 current, qint64 max, const QUrl &url)
Network request progress.
static void logInconsistentData(const swift::misc::CStatusMessage &msg, const char *funcInfo=nullptr)
Use this to log inconsistent data.
virtual ~CThreadedReader()
Destructor.
swift::misc::network::CUrlLogList getReadLog() const
Get the read log.
void setMarkedAsFailed(bool failed)
Set marker for read failed.
std::atomic_int m_networkReplyProgress
Progress percentage 0...100.
bool isMarkedAsFailed() const
Is marked as read failed.
void threadAssertCheck() const
Make sure everthing runs correctly in own thread.
void logNetworkReplyReceived(QNetworkReply *reply)
Network reply received, mark in m_urlReadLog.
QReadWriteLock m_lock
lock which can be used from the derived classes
bool hasPendingUrls() const
Has pending URLs?
bool didContentChange(const QString &content, int startPosition=-1)
Stores new content hash and returns if content changed (based on hash value.
swift::misc::network::CUrlLogList getUrlLogList() const
Get the URL log list.
bool updatedWithinLastMs(qint64 timeLastMs)
Was setup read within last xx milliseconds.
void startReader()
Starts the reader.
std::atomic_llong m_networkReplyNax
max bytes
virtual void doWorkImpl()
This method does the actual work in the derived class.
QNetworkReply * getFromNetworkAndLog(const swift::misc::network::CUrl &url, const swift::misc::CSlot< void(QNetworkReply *)> &callback)
Get request from network, and log with m_urlReadLog.
void setInitialAndPeriodicTime(int initialTime, int periodicTime)
Set initial and periodic times.
bool doWorkCheck() const
Still enabled etc.?
qint64 lastModifiedMsSinceEpoch(QNetworkReply *nwReply) const
When was reply last modified, -1 if N/A.
Base class for a long-lived worker object which lives in its own thread.
bool isEnabled() const
Enabled (running)?
QTimer m_updateTimer
timer which can be used by implementing classes
static const QString & worker()
Background task.
static const QString & dataInconsistency()
Data inconsistency.
Class for emitting a log message.
static void preformatted(const CStatusMessage &statusMessage)
Sends a verbatim, preformatted message to the log.
Derived & warning(const char16_t(&format)[N])
Set the severity to warning, providing a format string.
bool isEmpty() const
Message empty.
bool isEmpty() const
Synonym for empty.
Callable wrapper for a member function with function signature F.
Streamable status message, e.g.
QString getMessage() const
Message.
void addCategory(const CLogCategory &category)
Add category, avoids duplicates.
static bool isInThisThread(const QObject *toBeTested)
Is the current thread the object's thread?
bool isAbandoned() const
For the task to check whether it can finish early.
int removeOlderThanNowMinusOffset(qint64 offsetMs)
Remove objects older than seconds.
QString toQString(bool i18n=false) const
Cast as QString.
Value object encapsulating information of a location, kind of simplified CValueObject compliant versi...
Value object encapsulating a list of voice rooms.
bool markAsReceived(int id, bool success)
Mark as received.
CUrlLogList findOutdatedPending(int outdatedOffsetMs) const
Find outdated pending log entries.
bool hasPending() const
Any pending calls.
int addPendingUrl(const CUrl &url, int maxNumber=10)
Add a pending URL.
Backend services of the swift project, like dealing with the network or the simulators.
Free functions in swift::misc.
auto singleShot(int msec, QObject *target, F &&task)
Starts a single-shot timer which will call a task in the thread of the given object when it times out...
#define SWIFT_AUDIT_X(COND, WHERE, WHAT)
A weaker kind of verify.