#refs 686, used settings in readers

* moved threaded reader to BlackCore (settings are normally BlackCore aware)
* created ns/subfolder VATSIM
* prepared settings for the VATSIM readers
This commit is contained in:
Klaus Basan
2016-06-24 17:23:48 +02:00
parent 056841f9b1
commit 488d437a2a
24 changed files with 1472 additions and 1362 deletions

View File

@@ -1,105 +0,0 @@
/* Copyright (C) 2013
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "blackmisc/network/networkutils.h"
#include "blackmisc/threadedreader.h"
#include "blackmisc/threadutils.h"
#include <QCoreApplication>
#include <QMetaObject>
#include <QReadLocker>
#include <QThread>
#include <QTimer>
#include <QWriteLocker>
using namespace BlackMisc;
using namespace BlackMisc::Network;
namespace BlackMisc
{
CThreadedReader::CThreadedReader(QObject *owner, const QString &name) :
CContinuousWorker(owner, name),
m_updateTimer(new QTimer(this))
{
// void
}
qint64 CThreadedReader::lastModifiedMsSinceEpoch(QNetworkReply *nwReply) const
{
return CNetworkUtils::lastModifiedMsSinceEpoch(nwReply);
}
QDateTime CThreadedReader::getUpdateTimestamp() const
{
QReadLocker lock(&this->m_lock);
return this->m_updateTimestamp;
}
void CThreadedReader::setUpdateTimestamp(const QDateTime &updateTimestamp)
{
QWriteLocker lock(&this->m_lock);
this->m_updateTimestamp = updateTimestamp;
}
bool CThreadedReader::updatedWithinLastMs(qint64 timeLastMs)
{
QDateTime dt(getUpdateTimestamp());
if (dt.isNull() || !dt.isValid()) { return false; }
qint64 delta = QDateTime::currentMSecsSinceEpoch() - dt.toMSecsSinceEpoch();
return delta <= timeLastMs;
}
void CThreadedReader::requestReload()
{
// default implementation, subclasses shall override as required
this->initialize();
}
void CThreadedReader::gracefulShutdown()
{
// if not in main thread stop, otherwise it makes no sense to abandon
if (!CThreadUtils::isCurrentThreadObjectThread(this))
{
this->abandonAndWait();
}
}
CThreadedReader::~CThreadedReader()
{
gracefulShutdown();
}
void CThreadedReader::setInterval(int updatePeriodMs)
{
Q_ASSERT(this->m_updateTimer);
bool s;
if (updatePeriodMs < 1)
{
s = QMetaObject::invokeMethod(m_updateTimer, "stop");
}
else
{
s = QMetaObject::invokeMethod(m_updateTimer, "start", Q_ARG(int, updatePeriodMs));
}
Q_ASSERT_X(s, Q_FUNC_INFO, "Failed invoke");
Q_UNUSED(s);
}
int CThreadedReader::interval() const
{
QReadLocker rl(&this->m_lock);
return this->m_updateTimer->interval();
}
void CThreadedReader::threadAssertCheck() const
{
Q_ASSERT_X(QCoreApplication::instance()->thread() != QThread::currentThread(), Q_FUNC_INFO, "Needs to run in own thread");
Q_ASSERT_X(QObject::thread() == QThread::currentThread(), Q_FUNC_INFO, "Wrong object thread");
}
} // namespace

View File

@@ -1,85 +0,0 @@
/* Copyright (C) 2013
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKMISC_THREADED_READER_H
#define BLACKMISC_THREADED_READER_H
#include "blackmisc/blackmiscexport.h"
#include "blackmisc/worker.h"
#include <QDateTime>
#include <QObject>
#include <QReadWriteLock>
#include <QString>
#include <QtGlobal>
class QNetworkReply;
class QTimer;
namespace BlackMisc
{
//! Support for threaded based reading and parsing tasks such
//! as data files via http, or file system and parsing (such as FSX models)
class BLACKMISC_EXPORT CThreadedReader : public CContinuousWorker
{
public:
//! Destructor
virtual ~CThreadedReader();
//! Thread safe, set update timestamp
//! \threadsafe
QDateTime getUpdateTimestamp() const;
//! Thread safe, set update timestamp
//! \threadsafe
void setUpdateTimestamp(const QDateTime &updateTimestamp = QDateTime::currentDateTimeUtc());
//! Was setup read within last xx milliseconds
//! \threadsafe
bool updatedWithinLastMs(qint64 timeLastMs);
//! Request new reading
//! \note override as required, default is to call initialize()
virtual void requestReload();
//! Set the update time
//! \param updatePeriodMs <=0 stops the timer
//! \threadsafe
void setInterval(int updatePeriodMs);
//! Get the timer interval (ms)
//! \threadsafe
int interval() const;
public slots:
//! Graceful shutdown
//! \threadsafe
void gracefulShutdown();
protected:
QTimer *m_updateTimer = nullptr; //!< update timer
mutable QReadWriteLock m_lock {QReadWriteLock::Recursive}; //!< lock which can be used from the derived classes
//! Constructor
CThreadedReader(QObject *owner, const QString &name);
//! When was reply last modified, -1 if N/A
qint64 lastModifiedMsSinceEpoch(QNetworkReply *nwReply) const;
//! Make sure everthing runs correctly in own thread
void threadAssertCheck() const;
private:
QDateTime m_updateTimestamp; //!< when file/resource was read
};
} // namespace
#endif // guard