mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
121 lines
2.7 KiB
C++
121 lines
2.7 KiB
C++
#include "Suppression.h"
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
DECLARE_INSTANCE(Suppression);
|
|
|
|
extern int upTime();
|
|
|
|
Suppression& SuppressionTable::findCreateEntry(const uint256& index, bool& created)
|
|
{
|
|
boost::unordered_map<uint256, Suppression>::iterator fit = mSuppressionMap.find(index);
|
|
|
|
if (fit != mSuppressionMap.end())
|
|
{
|
|
created = false;
|
|
return fit->second;
|
|
}
|
|
created = true;
|
|
|
|
int now = upTime();
|
|
int expireTime = now - mHoldTime;
|
|
|
|
// See if any supressions need to be expired
|
|
std::map< int, std::list<uint256> >::iterator it = mSuppressionTimes.begin();
|
|
if ((it != mSuppressionTimes.end()) && (it->first <= expireTime))
|
|
{
|
|
BOOST_FOREACH(const uint256& lit, it->second)
|
|
mSuppressionMap.erase(lit);
|
|
mSuppressionTimes.erase(it);
|
|
}
|
|
|
|
mSuppressionTimes[now].push_back(index);
|
|
return mSuppressionMap.emplace(index, Suppression()).first->second;
|
|
}
|
|
|
|
bool SuppressionTable::addSuppression(const uint256& index)
|
|
{
|
|
boost::mutex::scoped_lock sl(mSuppressionMutex);
|
|
|
|
bool created;
|
|
findCreateEntry(index, created);
|
|
return created;
|
|
}
|
|
|
|
Suppression SuppressionTable::getEntry(const uint256& index)
|
|
{
|
|
boost::mutex::scoped_lock sl(mSuppressionMutex);
|
|
|
|
bool created;
|
|
return findCreateEntry(index, created);
|
|
}
|
|
|
|
bool SuppressionTable::addSuppressionPeer(const uint256& index, uint64 peer)
|
|
{
|
|
boost::mutex::scoped_lock sl(mSuppressionMutex);
|
|
|
|
bool created;
|
|
findCreateEntry(index, created).addPeer(peer);
|
|
return created;
|
|
}
|
|
|
|
bool SuppressionTable::addSuppressionPeer(const uint256& index, uint64 peer, int& flags)
|
|
{
|
|
boost::mutex::scoped_lock sl(mSuppressionMutex);
|
|
|
|
bool created;
|
|
Suppression &s = findCreateEntry(index, created);
|
|
s.addPeer(peer);
|
|
flags = s.getFlags();
|
|
return created;
|
|
}
|
|
|
|
int SuppressionTable::getFlags(const uint256& index)
|
|
{
|
|
boost::mutex::scoped_lock sl(mSuppressionMutex);
|
|
|
|
bool created;
|
|
return findCreateEntry(index, created).getFlags();
|
|
}
|
|
|
|
bool SuppressionTable::addSuppressionFlags(const uint256& index, int flag)
|
|
{
|
|
boost::mutex::scoped_lock sl(mSuppressionMutex);
|
|
|
|
bool created;
|
|
findCreateEntry(index, created).setFlag(flag);
|
|
return created;
|
|
}
|
|
|
|
bool SuppressionTable::setFlag(const uint256& index, int flag)
|
|
{ // return: true = changed, false = unchanged
|
|
assert(flag != 0);
|
|
|
|
boost::mutex::scoped_lock sl(mSuppressionMutex);
|
|
|
|
bool created;
|
|
Suppression &s = findCreateEntry(index, created);
|
|
|
|
if ((s.getFlags() & flag) == flag)
|
|
return false;
|
|
|
|
s.setFlag(flag);
|
|
return true;
|
|
}
|
|
|
|
bool SuppressionTable::swapSet(const uint256& index, std::set<uint64>& peers, int flag)
|
|
{
|
|
boost::mutex::scoped_lock sl(mSuppressionMutex);
|
|
|
|
bool created;
|
|
Suppression &s = findCreateEntry(index, created);
|
|
|
|
if ((s.getFlags() & flag) == flag)
|
|
return false;
|
|
|
|
s.swapSet(peers);
|
|
s.setFlag(flag);
|
|
|
|
return true;
|
|
}
|