Ref T184 Moved CTokenBucket from BlackCore to BlackMisc.

This commit is contained in:
Mathew Sutcliffe
2017-11-08 19:01:09 +00:00
parent 7328d48e59
commit dbb67b385b
4 changed files with 17 additions and 15 deletions

View File

@@ -1,59 +0,0 @@
/* Copyright (C) 2014
* 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 "blackcore/tokenbucket.h"
#include "blackmisc/pq/units.h"
#include <QtGlobal>
namespace BlackCore
{
CTokenBucket::CTokenBucket(int capacity, const BlackMisc::PhysicalQuantities::CTime &interval, int numTokensToRefill)
: m_capacity(capacity), m_interval(interval), m_numTokensToRefill(numTokensToRefill) {}
bool CTokenBucket::tryConsume(int numTokens)
{
Q_ASSERT(numTokens > 0 && numTokens < m_capacity);
// Replenish maximal up to capacity
int replenishedTokens = qMin(m_capacity, getTokens());
// Take care of overflows
m_availableTokens = qMin(m_availableTokens + replenishedTokens, m_capacity);
if (numTokens <= m_availableTokens)
{
m_availableTokens -= numTokens;
return true;
}
return false;
}
void CTokenBucket::setNumberOfTokensToRefill(int noTokens)
{
m_numTokensToRefill = noTokens;
}
void CTokenBucket::setCapacity(int capacity)
{
m_capacity = capacity;
}
int CTokenBucket::getTokens()
{
const auto now = QDateTime::currentDateTime();
const auto deltaSeconds = m_lastReplenishmentTime.secsTo(now);
const int numberOfTokens = static_cast<int>(m_numTokensToRefill * deltaSeconds / m_interval.value(BlackMisc::PhysicalQuantities::CTimeUnit::s()));
// Update the time only when replenishment actually took place. We will end up in a infinite loop otherwise.
if (numberOfTokens > 0) { m_lastReplenishmentTime = now; }
return numberOfTokens;
}
} // namespace

View File

@@ -1,56 +0,0 @@
/* Copyright (C) 2014
* 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.
*/
#ifndef BLACKCORE_TOKENBUCKET_H
#define BLACKCORE_TOKENBUCKET_H
#include "blackcore/blackcoreexport.h"
#include "blackmisc/pq/time.h"
#include <QDateTime>
namespace BlackCore
{
/*!
* \brief Token bucket algorithm
* \details This class implements the token bucket algorithm. Tokens as arbitrary unit are added to the bucket at a defined rate.
* Token can be consumsed as long as there are enough available. This class can be used to throttle traffic and packet
* generation. Each time a packet needs to be generated and sent a token is consumed. If no token is available, consumption
* will fail the the packet cannot be sent.
*/
class BLACKCORE_EXPORT CTokenBucket
{
public:
//! Constructor for given replenishment policy
CTokenBucket(int capacity, const BlackMisc::PhysicalQuantities::CTime &interval, int numTokensToRefill);
//! Try to consume a number of tokens
bool tryConsume(int numTokens = 1);
//! Number of tokens to refill
void setNumberOfTokensToRefill(int noTokens);
//! Set the capacity
void setCapacity(int capacity);
private:
//! Get available tokens since last replenishment.
//! \note Note that replenishment is implemented lazy.
//! This means, tokens will not replenished on regular basis via a running timer,
//! but they will be replenished while trying to consume them.
int getTokens();
int m_capacity = 10; //!< Maximum capacity of tokens
int m_availableTokens = 10; //!< Currently available tokens. The initial value is 10
BlackMisc::PhysicalQuantities::CTime m_interval; //!< Refill interval, e.g. every 5 secs
int m_numTokensToRefill; //!< Number of tokens to be refilled each interval
QDateTime m_lastReplenishmentTime = QDateTime::currentDateTime(); //!< Last time
};
} // ns
#endif

View File

@@ -15,7 +15,7 @@
#include "blackcore/blackcoreexport.h"
#include "blackcore/network.h"
#include "blackcore/vatsim/vatsimsettings.h"
#include "blackcore/tokenbucket.h"
#include "blackmisc/tokenbucket.h"
#include "blackmisc/simulation/ownaircraftprovider.h"
#include "blackmisc/simulation/simulatorplugininfo.h"
#include "blackmisc/aviation/aircrafticaocode.h"
@@ -217,7 +217,7 @@ namespace BlackCore
QString m_ownLiveryDescription; //!< "buffered livery", as this must not change when connected
BlackMisc::Aviation::CCallsignSet m_interimPositionReceivers; //!< all aircraft receiving interim positions
BlackMisc::Aviation::CAircraftParts m_sentAircraftConfig; //!< aircraft parts sent
CTokenBucket m_tokenBucket; //!< used with aircraft parts messages
BlackMisc::CTokenBucket m_tokenBucket; //!< used with aircraft parts messages
QTimer m_scheduledConfigUpdate;
QTimer m_processingTimer;