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; }
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; } }
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; }