Files
pilotclient/src/blackmisc/threadedreader.cpp
Klaus Basan d131cd2d33 refs #507, changed setup reader and simplied global setup and setup reader
* a single base URL (shared), derived URLs by appended path
* simplified dir structure shared with sub directories
* renamed functions
* automatically synchronize setup with DB when initialized
* trigger download info loading when setup is completed
* allow to automatically read after setup data have been synchronized
* read DB data when setup has been loaded
* allow to reload from threaded reader
* improved handling (log messages, skip reading) when data are not available
2015-11-19 21:07:58 +00:00

108 lines
3.0 KiB
C++

/* 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 "threadedreader.h"
using namespace BlackMisc;
using namespace BlackMisc::Network;
namespace BlackMisc
{
CThreadedReader::CThreadedReader(QObject *owner, const QString &name) :
CContinuousWorker(owner, name),
m_updateTimer(new QTimer(this))
{ }
qint64 CThreadedReader::lastModifiedMsSinceEpoch(QNetworkReply *nwReply) const
{
if (nwReply)
{
QVariant lastModifiedQv = nwReply->header(QNetworkRequest::LastModifiedHeader);
if (lastModifiedQv.isValid() && lastModifiedQv.canConvert<QDateTime>())
{
return lastModifiedQv.value<QDateTime>().toMSecsSinceEpoch();
}
}
return -1;
}
bool CThreadedReader::isFinishedOrShutdown() const
{
return m_shutdown || isFinished();
}
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;
}
void CThreadedReader::requestStop()
{
QMetaObject::invokeMethod(m_updateTimer, "stop");
}
void CThreadedReader::requestReload()
{
// default implementation, subclasses shall override as required
this->initialize();
}
void CThreadedReader::gracefulShutdown()
{
this->m_shutdown = true;
this->requestStop();
this->quit();
}
CThreadedReader::~CThreadedReader()
{
cleanup();
}
void CThreadedReader::cleanup()
{
// cleanup code would go here
}
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