int TopologyWorldState::updateIndirectProbabilities (const char *pszNeighborNodeId, StringHashtable<StringFloatHashtable> *pProbabilitiesTable)
{
    // Updates indirect probabilities after receiving a probabilities message from pszNeighborNodeId
    _m.lock (140);
    ageProbabilities();
    for (StringHashtable<StringFloatHashtable>::Iterator iterator = pProbabilitiesTable->getAllElements(); !iterator.end(); iterator.nextElement()) {
        if ( 0 != stricmp(iterator.getKey(), _pDisService->getNodeId()) ) { // Info are not about localnode
            RemoteNodeInfo *pRNI = (RemoteNodeInfo *) retrieveNodeInfo (iterator.getKey());
            if (pRNI) {
                // Choose the best indirect probability for node pRNI
                StringFloatHashtable *pIndProb = iterator.getValue();
                float fMaxProb = 0.0f;
                for (StringFloatHashtable::Iterator i = pIndProb->getAllElements(); !i.end(); i.nextElement()) {
                    if ( 0 != stricmp (i.getKey(), _pDisService->getNodeId()) ) { // Localnode is not the gateway node
                        if ( *(i.getValue()) > fMaxProb ) { // Prob is higher than maxProb
                            fMaxProb = *(i.getValue());
                        }
                    }
                }
                if (fMaxProb != 0.0f) {
                    pRNI->addIndirectProbability (pszNeighborNodeId, fMaxProb, _fAddParam, _fProbThreshold);
                }
            }
        }
    }
    _m.unlock (140);
    return 0;
}
const char * TopologyWorldState::getBestGateway (const char *pszTargetNodeId)
{
    // Returns the best gateway node to reach node pszTargetNodeId
    _m.lock (144);
    RemoteNodeInfo * pTRNI = (RemoteNodeInfo*) retrieveNodeInfo (pszTargetNodeId);
    if (pTRNI == NULL) {
        _m.unlock (144);
        return 0;
    }
    float fMaxProb = 0.0;
    const char *pszBestGWNodeID = NULL;
    if (pTRNI->getIndirectProbabilities()) {
        for (StringFloatHashtable::Iterator iGWNodes = pTRNI->getIndirectProbabilities()->getAllElements(); !iGWNodes.end(); iGWNodes.nextElement()) {
            const char *pszGWNodeId = iGWNodes.getKey();
            RemoteNodeInfo *pGWRNI = (RemoteNodeInfo*) retrieveNodeInfo (pszGWNodeId);
            if (pGWRNI) { 
                if (isActiveNeighbor (pszGWNodeId)) {
                    float fProb = *(iGWNodes.getValue()); // Prob from GWnode to targetNode
                    if (fProb > fMaxProb) {
                        fMaxProb = fProb;
                        pszBestGWNodeID = pszGWNodeId;
                    }
                }
            }
        }
    }
    _m.unlock (144);
    return pszBestGWNodeID;
}
StringFloatHashtable * TopologyWorldState::sendProbabilitiesInternal (RemoteNodeInfo *pRNI)
{
    // Create a probabilities table with the info on how to reach remote node pRNI
    StringFloatHashtable *pIndProb = NULL;
    if (isPeerAlive (pRNI)) { // Alive node
        pIndProb = new StringFloatHashtable();
        float fHigh1 = (float) _fProbContact;
        float *pHigh1 = &fHigh1;
        pIndProb->put (_pDisService->getNodeId(), pHigh1);
    }
    else { // Dead node
        if (pRNI->getIndirectProbabilities()) {
            float fHigh1 = 0.0f;
            const char *pszHigh1 = NULL;
            float fHigh2 = 0.0f;
            const char *pszHigh2 = NULL;
            for (StringFloatHashtable::Iterator iNodes = pRNI->getIndirectProbabilities()->getAllElements(); !iNodes.end(); iNodes.nextElement()) {
                if (isActiveNeighbor (iNodes.getKey())) {
                    float prob = *(iNodes.getValue());
                    if (prob > fHigh1 && prob > fHigh2) {
                        fHigh2 = fHigh1;
                        pszHigh2 = pszHigh1;
                        fHigh1 = prob;
                        pszHigh1 = iNodes.getKey();
                    }
                    else if (prob > fHigh2) {
                        fHigh2 = prob;
                        pszHigh2 = iNodes.getKey();
                    }
                }
            }
            if (fHigh1 > 0.0f) { 
                pIndProb = new StringFloatHashtable();
                float *pHigh1 = &fHigh1;
                pIndProb->put (pszHigh1, pHigh1);
                if (fHigh2 > 0.0f) {
                    float *pHigh2 = &fHigh2;
                    pIndProb->put (pszHigh2, pHigh2);
                }
            }
        }
    }
    return pIndProb;
}
Example #4
0
void RemoteNodeInfo::ageIndirectProbabilities (float fAgeParam, float fProbThreshold, int iTimeSinceLastAging)
{
    // Ages the indirect probabilities
    // TODO: find a better way, instead of creating a temporary table
    // _pIndirectProbabilities: gatewayNodeId->probValue
    if (_pIndirectProbabilities) {
        StringFloatHashtable * pIndProbTemp = new StringFloatHashtable();
        for (StringFloatHashtable::Iterator iNodes = _pIndirectProbabilities->getAllElements(); !iNodes.end(); iNodes.nextElement()) {
            float fProb = *(iNodes.getValue()) * pow (fAgeParam, iTimeSinceLastAging);
            if (fProb > fProbThreshold) {
                float *pProb = &fProb;
                pIndProbTemp->put (iNodes.getKey(), pProb);
            }
        }
        delete _pIndirectProbabilities;
        _pIndirectProbabilities = pIndProbTemp;
    }
}
Example #5
0
void RemoteNodeInfo::printIndirectProbabilities (void)
{
    if (_pIndirectProbabilities) {
        for (StringFloatHashtable::Iterator iNodes = _pIndirectProbabilities->getAllElements(); !iNodes.end(); iNodes.nextElement()) {
            checkAndLogMsg ("RemoteNodeInfo::printIndirectProbabilities", Logger::L_Info, "       Through gateway node: %s --> probability %f\n", iNodes.getKey(), *(iNodes.getValue()));
        }
    }
}