From 285e020f1388d50f6933c67d110dceed2b8e552e Mon Sep 17 00:00:00 2001 From: drbob Date: Wed, 17 Nov 2010 18:10:56 +0000 Subject: [PATCH] a bit of a hack to increase the accuracy of LocalNet accuracy. * periodically query Rnd DhtSpace Peer with APP flag for closest peers. (i.e like own search). git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3810 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libbitdht/src/bitdht/bdmanager.cc | 47 +++++++++++++++++++- libbitdht/src/bitdht/bdmanager.h | 1 + libbitdht/src/bitdht/bdpeer.cc | 73 +++++++++++++++++++++++++++++++ libbitdht/src/bitdht/bdpeer.h | 4 ++ 4 files changed, 124 insertions(+), 1 deletion(-) diff --git a/libbitdht/src/bitdht/bdmanager.cc b/libbitdht/src/bitdht/bdmanager.cc index 114a48cbc..8541b5332 100644 --- a/libbitdht/src/bitdht/bdmanager.cc +++ b/libbitdht/src/bitdht/bdmanager.cc @@ -330,10 +330,18 @@ void bdNodeManager::iteration() updateStore(); #ifdef DEBUG_MGR - std::cerr << "bdNodeManager::iteration(): REFRESH "; + std::cerr << "bdNodeManager::iteration(): Do App Search"; std::cerr << std::endl; #endif + /* run a random search for ourselves, from own App DHT peer */ + QueryRandomLocalNet(); + + +#ifdef DEBUG_MGR + std::cerr << "bdNodeManager::iteration(): REFRESH "; + std::cerr << std::endl; +#endif status(); } @@ -374,6 +382,43 @@ void bdNodeManager::iteration() } } + /* NB: This is a bit of a hack, the code is duplicated from bdnode & bdquery. + * should use fn calls into their functions for good generality + */ +void bdNodeManager::QueryRandomLocalNet() +{ + bdId id; + bdNodeId targetNodeId; + + uint32_t withFlag = BITDHT_PEER_STATUS_DHT_APPL; + if (mNodeSpace.findRandomPeerWithFlag(id, withFlag)) + { + /* calculate mid point */ + mFns->bdRandomMidId(&mOwnId, &(id.id), &targetNodeId); + + /* do standard find_peer message */ + + bdToken transId; + genNewTransId(&transId); + msgout_find_node(&id, &transId, &targetNodeId); + +//#ifdef DEBUG_NODE_MSGS + std::cerr << "bdNodeManager::QueryRandomLocalNet() Querying : "; + mFns->bdPrintId(std::cerr, &id); + std::cerr << " searching for : "; + mFns->bdPrintNodeId(std::cerr, &targetNodeId); + std::cerr << std::endl; +//#endif + } + else + { +//#ifdef DEBUG_NODE_MSGS + std::cerr << "bdNodeManager::QueryRandomLocalNet() No LocalNet Peer Found"; + std::cerr << std::endl; +//#endif + } +} + int bdNodeManager::status() { diff --git a/libbitdht/src/bitdht/bdmanager.h b/libbitdht/src/bitdht/bdmanager.h index 0b9a4587d..6d73435b2 100644 --- a/libbitdht/src/bitdht/bdmanager.h +++ b/libbitdht/src/bitdht/bdmanager.h @@ -139,6 +139,7 @@ int checkStatus(); int checkPingStatus(); int SearchOutOfDate(); void startQueries(); +void QueryRandomLocalNet(); std::map mActivePeers; std::list mCallbacks; diff --git a/libbitdht/src/bitdht/bdpeer.cc b/libbitdht/src/bitdht/bdpeer.cc index e16158eef..5abba60f7 100644 --- a/libbitdht/src/bitdht/bdpeer.cc +++ b/libbitdht/src/bitdht/bdpeer.cc @@ -945,3 +945,76 @@ uint32_t bdSpace::calcSpaceSize() return totalcount; } +uint32_t bdSpace::calcSpaceSizeWithFlag(uint32_t withFlag) +{ + std::vector::iterator it; + + /* little summary */ + uint32_t totalcount = 0; + + it = buckets.begin(); + if (it != buckets.end()) + { + it++; /* skip own bucket! */ + } + for(; it != buckets.end(); it++) + { + int size = 0; + std::list::iterator lit; + for(lit = it->entries.begin(); lit != it->entries.end(); lit++) + { + if (withFlag & lit->mPeerFlags) + { + size++; + } + } + totalcount += size; + } + return totalcount; +} + + /* special function to enable DHT localisation (i.e find peers from own network) */ +bool bdSpace::findRandomPeerWithFlag(bdId &id, uint32_t withFlag) +{ + std::vector::iterator it; + uint32_t totalcount = calcSpaceSizeWithFlag(withFlag); + uint32_t rnd = rand() % totalcount; + uint32_t i = 0; + uint32_t buck = 0; + + std::cerr << "bdSpace::findRandomPeerWithFlag()"; + std::cerr << std::endl; + + it = buckets.begin(); + if (it != buckets.end()) + { + it++; /* skip own bucket! */ + } + for(; it != buckets.end(); it++, buck++) + { + int size = 0; + std::list::iterator lit; + for(lit = it->entries.begin(); lit != it->entries.end(); lit++) + { + if (withFlag & lit->mPeerFlags) + { + if (i == rnd) + { + std::cerr << "bdSpace::findRandomPeerWithFlag() found #" << i; + std::cerr << " in bucket #" << buck; + std::cerr << std::endl; + + /* found */ + id = lit->mPeerId; + return true; + } + i++; + } + } + } + std::cerr << "bdSpace::findRandomPeerWithFlag() failed to find " << rnd << " / " << totalcount; + std::cerr << std::endl; + + return false; +} + diff --git a/libbitdht/src/bitdht/bdpeer.h b/libbitdht/src/bitdht/bdpeer.h index 35dbf345f..7d8f74f34 100644 --- a/libbitdht/src/bitdht/bdpeer.h +++ b/libbitdht/src/bitdht/bdpeer.h @@ -159,6 +159,10 @@ int printDHT(); uint32_t calcNetworkSize(); uint32_t calcNetworkSizeWithFlag(uint32_t withFlag); uint32_t calcSpaceSize(); +uint32_t calcSpaceSizeWithFlag(uint32_t withFlag); + + /* special function to enable DHT localisation (i.e find peers from own network) */ +bool findRandomPeerWithFlag(bdId &id, uint32_t withFlag); /* to add later */ int updateOwnId(bdNodeId *newOwnId);