/*! * \copyright Copyright (c) 2017-2018 Governikus GmbH & Co. KG, Germany */ #include "RemoteDeviceList.h" #include "Env.h" #include "Initializer.h" using namespace governikus; static Initializer::Entry E([] { qRegisterMetaType >("QSharedPointer"); qRegisterMetaType > >("QVector>"); }); namespace governikus { template<> RemoteDeviceList* createNewObject() { return new RemoteDeviceListImpl; } template<> RemoteDeviceList* createNewObject(int&& pCheckInterval, int&& pTimeout) { return new RemoteDeviceListImpl(pCheckInterval, pTimeout); } } /* namespace governikus */ RemoteDeviceListEntry::RemoteDeviceListEntry(const RemoteDeviceDescriptor& pRemoteDeviceDescriptor) : mRemoteDeviceDescriptor(pRemoteDeviceDescriptor) , mLastSeen(QTime::currentTime()) { } void RemoteDeviceListEntry::setLastSeenToNow() { mLastSeen = QTime::currentTime(); } bool RemoteDeviceListEntry::contains(const RemoteDeviceDescriptor& pRemoteDeviceDescriptor) const { return mRemoteDeviceDescriptor == pRemoteDeviceDescriptor; } bool RemoteDeviceListEntry::isEqual(const RemoteDeviceListEntry* const pOther) const { return pOther != nullptr && mRemoteDeviceDescriptor == pOther->mRemoteDeviceDescriptor && mLastSeen == pOther->mLastSeen; } const QTime& RemoteDeviceListEntry::getLastSeen() const { return mLastSeen; } const RemoteDeviceDescriptor& RemoteDeviceListEntry::getRemoteDeviceDescriptor() const { return mRemoteDeviceDescriptor; } RemoteDeviceList::RemoteDeviceList(int, int) { } RemoteDeviceList::~RemoteDeviceList() { } QVector > RemoteDeviceList::getRemoteDevices() const { return QVector >(); } RemoteDeviceListImpl::RemoteDeviceListImpl(int pCheckInterval, int pTimeout) : RemoteDeviceList(pCheckInterval, pTimeout) , mTimer() , mTimeout(pTimeout) , mList() { connect(&mTimer, &QTimer::timeout, this, &RemoteDeviceListImpl::onRemoveUnresponsiveRemoteReaders); mTimer.setInterval(pCheckInterval); } RemoteDeviceListImpl::~RemoteDeviceListImpl() { mTimer.stop(); } void RemoteDeviceListImpl::update(const RemoteDeviceDescriptor& pDescriptor) { for (const QSharedPointer& entry : qAsConst(mList)) { if (entry->contains(pDescriptor)) { entry->setLastSeenToNow(); return; } } const QSharedPointer newDevice(new RemoteDeviceListEntry(pDescriptor)); mList.append(newDevice); if (!mTimer.isActive()) { mTimer.start(); } Q_EMIT fireDeviceAppeared(newDevice); } void RemoteDeviceListImpl::clear() { mList.clear(); } QVector > RemoteDeviceListImpl::getRemoteDevices() const { return mList; } void RemoteDeviceListImpl::onRemoveUnresponsiveRemoteReaders() { const QTime threshold(QTime::currentTime().addMSecs(-mTimeout)); QMutableVectorIterator > i(mList); while (i.hasNext()) { const QSharedPointer& pEntry = i.next(); if (pEntry->getLastSeen() < threshold) { i.remove(); Q_EMIT fireDeviceVanished(pEntry); } } if (mList.isEmpty()) { mTimer.stop(); } }