OverSim
PeerStorage Class Reference

#include <PeerStorage.h>

Public Member Functions

 ~PeerStorage ()
size_t size ()
const PeerHashMap::iterator find (const IPvXAddress &ip)
const PeerHashMap::iterator begin ()
const PeerHashMap::iterator end ()
std::pair< const
PeerHashMap::iterator, bool > 
insert (const std::pair< IPvXAddress, BootstrapEntry > &element)
void erase (const PeerHashMap::iterator it)
void registerOverlay (const PeerHashMap::iterator it, const NodeHandle &peer, int32_t overlayId)
const PeerHashMap::iterator getRandomNode (int32_t overlayId, int32_t nodeType, bool bootstrappedNeeded, bool inoffensiveNeeded)
void setMalicious (const PeerHashMap::iterator it, bool malicious)
void setBootstrapped (const PeerHashMap::iterator it, int32_t overlayId, bool bootstrapped)
const PeerHashMapgetPeerHashMap ()

Private Types

typedef std::vector
< std::vector
< PeerHashMap::iterator > > 
PeerVector

Private Member Functions

void insertMapIteratorIntoVector (PeerVector &peerVector, PeerHashMap::iterator it)
void removeMapIteratorFromVector (PeerVector &peerVector, PeerHashMap::iterator it)
size_t offsetSize ()
uint8_t calcOffset (bool bootstrapped, bool malicious)

Private Attributes

PeerHashMap peerHashMap
PeerVector globalPeerVector
std::map< int32_t, PeerVectoroverlayPeerVectorMap
std::vector< std::vector
< uint32_t > > 
freeVector

Detailed Description

Author
IngmarBaumgart

Definition at line 116 of file PeerStorage.h.

Member Typedef Documentation

typedef std::vector<std::vector<PeerHashMap::iterator> > PeerStorage::PeerVector
private

Definition at line 143 of file PeerStorage.h.

Constructor & Destructor Documentation

PeerStorage::~PeerStorage ( )

Definition at line 30 of file PeerStorage.cc.

{
PeerHashMap::iterator it;
for (it = peerHashMap.begin(); it != peerHashMap.end(); it++) {
delete it->second.info;
}
}

Member Function Documentation

const PeerHashMap::iterator PeerStorage::begin ( )

Definition at line 48 of file PeerStorage.cc.

Referenced by GlobalNodeList::getAllIps(), and GlobalNodeList::sendNotificationToAllPeers().

{
return peerHashMap.begin();
}
uint8_t PeerStorage::calcOffset ( bool  bootstrapped,
bool  malicious 
)
inlineprivate

Definition at line 63 of file PeerStorage.cc.

Referenced by insertMapIteratorIntoVector(), and removeMapIteratorFromVector().

{
uint8_t offset = 0;
if (bootstrapped) offset += 1<<0;
if (malicious) offset += 1<<1;
return offset;
}
void PeerStorage::erase ( const PeerHashMap::iterator  it)

Definition at line 164 of file PeerStorage.cc.

Referenced by GlobalNodeList::killPeer().

{
delete it->second.info;
peerHashMap.erase(it);
}
const PeerHashMap& PeerStorage::getPeerHashMap ( )
inline

Definition at line 143 of file PeerStorage.h.

Referenced by GlobalNodeList::initialize().

{ return peerHashMap; };
const PeerHashMap::iterator PeerStorage::getRandomNode ( int32_t  overlayId,
int32_t  nodeType,
bool  bootstrappedNeeded,
bool  inoffensiveNeeded 
)

Definition at line 220 of file PeerStorage.cc.

Referenced by GlobalNodeList::getRandomAliveNode(), GlobalNodeList::getRandomNode(), and GlobalNodeList::getRandomPeerInfo().

{
if (peerHashMap.size() == 0) {
std::cout << "getRandomNode: empty!" << std::endl;
return peerHashMap.end();
}
size_t sum = 0;
//std::cout << "getRandomNode(): nodeType: " << nodeType << " boostrapped: "
// << bootstrappedNeeded << " inoffensive: " << inoffensiveNeeded
// << std::endl;
for (uint i = 0; i < peerVector.size(); i++) {
if (((nodeType > -1) && ((uint)nodeType != i/offsetSize())) ||
(bootstrappedNeeded && !(i & 1)) ||
(inoffensiveNeeded && (i & 2))) {
continue;
}
//std::cout << "Using i=" << i << std::endl;
sum += (peerVector[i].size() - freeVector[i].size());
//std::cout << "new sum: " << sum << std::endl;
}
if (sum == 0) {
return peerHashMap.end();
}
size_t random = intuniform(1, sum);
uint i = 0;
while ((i < peerVector.size())) {
if (((nodeType > -1) && ((uint)nodeType != i/offsetSize())) ||
(bootstrappedNeeded && !(i & 1)) ||
(inoffensiveNeeded && (i & 2))) {
i++;
continue;
} else if ((peerVector[i].size() - freeVector[i].size()) < random) {
random -= peerVector[i].size() - freeVector[i].size();
i++;
} else {
break;
}
}
random = intuniform(1, peerVector[i].size());
PeerHashMap::iterator it = peerVector[i][random-1];
while (it == peerHashMap.end()) {
if (random == peerVector[i].size()) {
random = 0;
}
it = peerVector[i][(++random)-1];
}
//std::cout << "Using node from vector i=" << i << " and position=" << random-1 << std::endl;
return it;
}
std::pair< const PeerHashMap::iterator, bool > PeerStorage::insert ( const std::pair< IPvXAddress, BootstrapEntry > &  element)

Definition at line 151 of file PeerStorage.cc.

Referenced by GlobalNodeList::addPeer().

{
std::pair<PeerHashMap::iterator, bool> ret;
ret = peerHashMap.insert(element);
if (ret.second) {
}
return ret;
}
void PeerStorage::insertMapIteratorIntoVector ( PeerVector peerVector,
PeerHashMap::iterator  it 
)
private

Definition at line 71 of file PeerStorage.cc.

Referenced by insert(), setBootstrapped(), and setMalicious().

{
PeerInfo* peerInfo = it->second.info;
bool bootstrapped = false;
for (AddrPerOverlayVector::iterator oit = it->second.addrVector.begin();
oit != it->second.addrVector.end(); oit++) {
bootstrapped |= oit->bootstrapped;
}
bool malicious = peerInfo->isMalicious();
size_t partition = peerInfo->getTypeID();
size_t offset = calcOffset(bootstrapped, malicious);
size_t partitionIndex = partition*offsetSize()+offset;
#if 0
std::cout << "INSERT " << it->first << " partitionIndex:"
<< partitionIndex
<< " bootstrapped:" << bootstrapped << " malicious:"
<< malicious << std::endl;
#endif
if (peerVector.size() < (partition + 1)*offsetSize()) {
int i = peerVector.size();
peerVector.resize(offsetSize()*(partition + 1));
freeVector.resize(offsetSize()*(partition + 1));
while (i < (int)(peerVector.size())) {
peerVector[i++].reserve(30000);
freeVector[i++].reserve(30000);
}
}
size_t index = -1;
if ((freeVector.size() >= (partition + 1)*offsetSize()) &&
(freeVector[partitionIndex].size())) {
index = freeVector[partitionIndex].back();
freeVector[partitionIndex].pop_back();
peerVector[partitionIndex][index] = it;
//std::cout << "\t REUSING position " << index << std::endl;
} else {
index = peerVector[partitionIndex].size();
peerVector[partitionIndex].push_back(it);
//std::cout << "\t APPENDING at position " << index << std::endl;
}
it->second.peerVectorIndex = index;
}
size_t PeerStorage::offsetSize ( )
inlineprivate

Definition at line 58 of file PeerStorage.cc.

Referenced by getRandomNode(), insertMapIteratorIntoVector(), and removeMapIteratorFromVector().

{
return 1<<2;
}
void PeerStorage::registerOverlay ( const PeerHashMap::iterator  it,
const NodeHandle peer,
int32_t  overlayId 
)

Definition at line 171 of file PeerStorage.cc.

Referenced by GlobalNodeList::registerPeer().

{
it->second.addrVector.setAddrForOverlayId(new NodeHandle(peer),
overlayId);
//if (!overlayPeerVectorMap.count(overlayId)) {
// PeerVector vector;
// overlayPeerVectorMap.insert(std::make_pair(overlayId,
// vector));
//}
// insertMapIteratorIntoVector(overlayPeerVectorMap[overlayId], it);
}
void PeerStorage::removeMapIteratorFromVector ( PeerVector peerVector,
PeerHashMap::iterator  it 
)
private

Definition at line 123 of file PeerStorage.cc.

Referenced by erase(), setBootstrapped(), and setMalicious().

{
PeerInfo* peerInfo = it->second.info;
bool bootstrapped = false;
for (AddrPerOverlayVector::iterator oit = it->second.addrVector.begin();
oit != it->second.addrVector.end(); oit++) {
bootstrapped |= oit->bootstrapped;
}
bool malicious = peerInfo->isMalicious();
size_t partition = peerInfo->getTypeID();
size_t offset = calcOffset(bootstrapped, malicious);
size_t index = it->second.peerVectorIndex;
size_t partitionIndex = partition*offsetSize()+offset;
if (peerVector[partitionIndex].size() == (index + 1)) {
peerVector[partitionIndex].pop_back();
} else {
peerVector[partitionIndex][index] = peerHashMap.end();
freeVector[partitionIndex].push_back(index);
}
//std::cout << "ERASE " << it->first << " partitionIndex:" << partitionIndex
// << " index: " << index << std::endl;
}
void PeerStorage::setBootstrapped ( const PeerHashMap::iterator  it,
int32_t  overlayId,
bool  bootstrapped 
)

Definition at line 197 of file PeerStorage.cc.

Referenced by GlobalNodeList::registerPeer(), and GlobalNodeList::removePeer().

{
if (it == peerHashMap.end()) {
throw cRuntimeError("GlobalNodeList::setBootstrapped(): Node not found!");
}
//std::cout << "setBootstrapped: " << bootstrapped << " overlayId: "
// << overlayId << std::endl;
AddrPerOverlayVector::iterator oit = it->
second.addrVector.getIterForOverlayId(overlayId);
if (oit != it->second.addrVector.end()) {
oit->bootstrapped = bootstrapped;
} else if (bootstrapped) {
throw cRuntimeError("PeerStorage::setBootstrapped: No valid entry found!");
}
}
void PeerStorage::setMalicious ( const PeerHashMap::iterator  it,
bool  malicious 
)

Definition at line 186 of file PeerStorage.cc.

Referenced by GlobalNodeList::setMalicious().

{
if (it == peerHashMap.end()) {
throw cRuntimeError("GlobalNodeList::setMalicious(): Node not found!");
}
it->second.info->setMalicious(malicious);
}

Member Data Documentation

std::vector<std::vector<uint32_t> > PeerStorage::freeVector
private
PeerVector PeerStorage::globalPeerVector
private

Definition at line 158 of file PeerStorage.h.

Referenced by erase(), getRandomNode(), insert(), setBootstrapped(), and setMalicious().

std::map<int32_t, PeerVector> PeerStorage::overlayPeerVectorMap
private

Definition at line 159 of file PeerStorage.h.

PeerHashMap PeerStorage::peerHashMap
private

The documentation for this class was generated from the following files: