Public Member Functions | Private Member Functions | Private Attributes

PeerStorage Class Reference

#include <PeerStorage.h>

List of all members.

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)
const PeerHashMap::iterator getRandomNode (int32_t nodeType, bool bootstrappedNeeded, bool inoffensiveNeeded)
void setMalicious (const PeerHashMap::iterator it, bool malicious)
void setBootstrapped (const PeerHashMap::iterator it, bool bootstrapped)
const PeerHashMapgetPeerHashMap ()

Private Member Functions

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

Private Attributes

PeerHashMap peerHashMap
std::vector< std::vector
< PeerHashMap::iterator > > 
peerVector
std::vector< std::vector
< uint32_t > > 
freeVector

Detailed Description

Author:
IngmarBaumgart

Definition at line 58 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;
        delete it->second.node;
    }
}


Member Function Documentation

const PeerHashMap::iterator PeerStorage::begin (  ) 

Definition at line 49 of file PeerStorage.cc.

Referenced by GlobalNodeList::sendNotificationToAllPeers().

{
    return peerHashMap.begin();
}

uint8_t PeerStorage::calcOffset ( bool  bootstrapped,
bool  malicious 
) [inline, private]

Definition at line 64 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;
}

const PeerHashMap::iterator PeerStorage::end (  ) 
void PeerStorage::erase ( const PeerHashMap::iterator  it  ) 

Definition at line 149 of file PeerStorage.cc.

Referenced by GlobalNodeList::killPeer().

{
    removeMapIteratorFromVector(it);
    delete it->second.info;
    delete it->second.node;
    peerHashMap.erase(it);
}

const PeerHashMap::iterator PeerStorage::find ( const IPvXAddress &  ip  ) 
const PeerHashMap& PeerStorage::getPeerHashMap (  )  [inline]

Definition at line 73 of file PeerStorage.h.

Referenced by GlobalNodeList::initialize().

{ return peerHashMap; };

const PeerHashMap::iterator PeerStorage::getRandomNode ( int32_t  nodeType,
bool  bootstrappedNeeded,
bool  inoffensiveNeeded 
)

Definition at line 180 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 136 of file PeerStorage.cc.

Referenced by GlobalNodeList::addPeer().

{
    std::pair<PeerHashMap::iterator, bool> ret;

    ret = peerHashMap.insert(element);

    if (ret.second) {
        insertMapIteratorIntoVector(ret.first);
    }

    return ret;
}

void PeerStorage::insertMapIteratorIntoVector ( PeerHashMap::iterator  it  )  [private]

Definition at line 72 of file PeerStorage.cc.

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

{
    PeerInfo* peerInfo = it->second.info;
    bool bootstrapped = peerInfo->isBootstrapped();
    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 (  )  [inline, private]

Definition at line 59 of file PeerStorage.cc.

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

{
    return 1<<2;
}

void PeerStorage::removeMapIteratorFromVector ( PeerHashMap::iterator  it  )  [private]

Definition at line 116 of file PeerStorage.cc.

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

{
    PeerInfo* peerInfo = it->second.info;
    bool bootstrapped = peerInfo->isBootstrapped();
    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,
bool  bootstrapped 
)

Definition at line 168 of file PeerStorage.cc.

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

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

    removeMapIteratorFromVector(it);
    //std::cout << "setBootstrapped: " << bootstrapped << std::endl;
    it->second.info->setBootstrapped(bootstrapped);
    insertMapIteratorIntoVector(it);
}

void PeerStorage::setMalicious ( const PeerHashMap::iterator  it,
bool  malicious 
)

Definition at line 157 of file PeerStorage.cc.

Referenced by GlobalNodeList::setMalicious().

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

    removeMapIteratorFromVector(it);
    it->second.info->setMalicious(malicious);
    insertMapIteratorIntoVector(it);
}

size_t PeerStorage::size (  ) 

Member Data Documentation

std::vector<std::vector<uint32_t> > PeerStorage::freeVector [private]
std::vector<std::vector<PeerHashMap::iterator> > PeerStorage::peerVector [private]

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