diff --git a/src/bitdht/bdmanager.cc b/src/bitdht/bdmanager.cc index 0803899..4659486 100644 --- a/src/bitdht/bdmanager.cc +++ b/src/bitdht/bdmanager.cc @@ -87,6 +87,8 @@ bdNodeManager::bdNodeManager(bdNodeId *id, std::string dhtVersion, std::string b mFns->bdPrintNodeId(std::cerr, id); std::cerr << std::endl; #endif + + mLocalNetEnhancements = true; } int bdNodeManager::stopDht() @@ -400,34 +402,39 @@ void bdNodeManager::iteration() std::cerr << std::endl; #endif - /* run a random search for ourselves, from own App DHT peer */ - QueryRandomLocalNet(); + /* This stuff is only important for "LocalNet based Features */ + if (mLocalNetEnhancements) + { + /* run a random search for ourselves, from own App DHT peer */ + QueryRandomLocalNet(); + #define SEARCH_MAX_SIZE 10 - if (mBdNetworkSize < SEARCH_MAX_SIZE) - { -#ifdef DEBUG_MGR - std::cerr << "Local Netsize: " << mBdNetworkSize << " to small...searching"; - std::cerr << std::endl; -#endif - - /* if the network size is very small */ - SearchForLocalNet(); - mSearchingDone = false; - } - else - { - if (!mSearchingDone) + if (mBdNetworkSize < SEARCH_MAX_SIZE) { - mSearchingDone = true; - mSearchTS = now; #ifdef DEBUG_MGR - std::cerr << "Completed LocalNet Search in : " << mSearchTS-mStartTS; + std::cerr << "Local Netsize: " << mBdNetworkSize << " to small...searching"; std::cerr << std::endl; #endif + + /* if the network size is very small */ + SearchForLocalNet(); + mSearchingDone = false; + } + else + { + if (!mSearchingDone) + { + mSearchingDone = true; + mSearchTS = now; +#ifdef DEBUG_MGR + std::cerr << "Completed LocalNet Search in : " << mSearchTS-mStartTS; + std::cerr << std::endl; +#endif + } } } - + #ifdef DEBUG_MGR std::cerr << "bdNodeManager::iteration(): REFRESH "; std::cerr << std::endl; diff --git a/src/bitdht/bdmanager.h b/src/bitdht/bdmanager.h index 635a5eb..a43a883 100644 --- a/src/bitdht/bdmanager.h +++ b/src/bitdht/bdmanager.h @@ -187,6 +187,8 @@ void SearchForLocalNet(); bdBloom mBloomFilter; + bool mLocalNetEnhancements; + /* future node functions */ //addPeerPing(foundId); //clearPing(it->first); diff --git a/src/example/Makefile b/src/example/Makefile index 2585c28..447407b 100644 --- a/src/example/Makefile +++ b/src/example/Makefile @@ -6,11 +6,11 @@ LIBS = -L../lib -lbitdht -lpthread -EXEC : bdexample +EXEC : bssdht -EGOBJ = bdhandler.o bdexample.o +EGOBJ = bdhandler.o bssdht.o bootstrap_fn.o -bdexample: $(EGOBJ) - $(CXX) $(CXXFLAGS) -o bdexample $(EGOBJ) $(LIBS) +bssdht: $(EGOBJ) + $(CXX) $(CXXFLAGS) -o bssdht $(EGOBJ) $(LIBS) diff --git a/src/example/bdexample.cc b/src/example/bdexample.cc deleted file mode 100644 index 0c965ea..0000000 --- a/src/example/bdexample.cc +++ /dev/null @@ -1,42 +0,0 @@ - - -#include "bitdht/bdiface.h" -#include "bitdht/bdstddht.h" -#include "bdhandler.h" - -int main(int argc, char **argv) -{ - - /* startup dht : with a random id! */ - bdNodeId ownId; - bdStdRandomNodeId(&ownId); - - uint16_t port = 6775; - std::string appId = "exId"; - std::string bootstrapfile = "bdboot.txt"; - - BitDhtHandler dht(&ownId, port, appId, bootstrapfile); - - /* run your program */ - while(1) - { - bdNodeId searchId; - bdStdRandomNodeId(&searchId); - - dht.FindNode(&searchId); - - sleep(180); - - dht.DropNode(&searchId); - } - - return 1; -} - - - - - - - - diff --git a/src/example/bdhandler.cc b/src/example/bdhandler.cc index 21a3b62..0b8766a 100644 --- a/src/example/bdhandler.cc +++ b/src/example/bdhandler.cc @@ -29,8 +29,24 @@ #include #include +#include + #include "bdhandler.h" + +/**** + * This example bitdht app is designed to perform a single shot DHT search. + * Ww want to minimise the dht work, and number of UDP packets sent. + * + * This means we need to add: + * - don't search for App network. (libbitdht option) + * - don't bother filling up Space. (libbitdht option) + * - Programmatically add bootstrap peers. (libbitdht option) + * + */ + + + /* This is a conversion callback class */ @@ -111,6 +127,11 @@ BitDhtHandler::BitDhtHandler(bdNodeId *ownId, uint16_t port, std::string appId, mUdpBitDht->start(); /* starts up the bitdht thread */ + /* setup best mode for quick search */ + uint32_t dhtFlags = BITDHT_MODE_TRAFFIC_MED | BITDHT_MODE_RELAYSERVERS_IGNORED; + mUdpBitDht->setDhtMode(dhtFlags); + mUdpBitDht->setAttachMode(false); + /* switch on the dht too */ mUdpBitDht->startDht(); } @@ -163,8 +184,19 @@ bool BitDhtHandler::FindNode(bdNodeId *peerId) bdStdPrintNodeId(std::cerr, peerId); std::cerr << ")" << std::endl; + + BssResult res; + res.id.id = *peerId; + res.mode = BSS_SINGLE_SHOT; + res.status = 0; + + { + bdStackMutex stack(resultsMtx); /********** MUTEX LOCKED *************/ + mSearchNodes[*peerId] = res; + } + /* add in peer */ - mUdpBitDht->addFindNode(peerId, BITDHT_QFLAGS_DO_IDLE); + mUdpBitDht->addFindNode(peerId, BITDHT_QFLAGS_DISGUISE); return true ; } @@ -179,10 +211,50 @@ bool BitDhtHandler::DropNode(bdNodeId *peerId) /* remove in peer */ mUdpBitDht->removeFindNode(peerId); + bdStackMutex stack(resultsMtx); /********** MUTEX LOCKED *************/ + + /* find the node from our list */ + std::map::iterator it; + it = mSearchNodes.find(*peerId); + if (it != mSearchNodes.end()) + { + std::cerr << "BitDhtHandler::DropNode() Found NodeId, removing"; + std::cerr << std::endl; + + mSearchNodes.erase(it); + } return true ; } +bool BitDhtHandler::SearchResult(bdId *id, uint32_t &status) +{ + bdStackMutex stack(resultsMtx); /********** MUTEX LOCKED *************/ + + /* find the node from our list */ + std::map::iterator it; + it = mSearchNodes.find(id->id); + if (it != mSearchNodes.end()) + { + if (it->second.status != 0) + { + std::cerr << "BitDhtHandler::SearchResults() Found Results"; + std::cerr << std::endl; + status = it->second.status; + *id = it->second.id; + return true; + } + + std::cerr << "BitDhtHandler::SearchResults() No Results Yet"; + std::cerr << std::endl; + return false; + } + + std::cerr << "BitDhtHandler::SearchResults() ERROR: No Search Entry"; + std::cerr << std::endl; + return false; +} + /********************** Callback Functions **************************/ @@ -204,6 +276,20 @@ int BitDhtHandler::PeerCallback(const bdId *id, uint32_t status) bdStdPrintId(std::cerr, id); std::cerr << std::endl; + bdStackMutex stack(resultsMtx); /********** MUTEX LOCKED *************/ + + /* find the node from our list */ + std::map::iterator it; + it = mSearchNodes.find(id->id); + if (it == mSearchNodes.end()) + { + std::cerr << "BitDhtHandler::PeerCallback() Unknown NodeId !!! "; + std::cerr << std::endl; + + return 1; + } + it->second.status = status; + bool connect = false; switch(status) { @@ -212,6 +298,7 @@ int BitDhtHandler::PeerCallback(const bdId *id, uint32_t status) std::cerr << "BitDhtHandler::PeerCallback() QUERY FAILURE ... do nothin "; std::cerr << std::endl; + break; case BITDHT_MGR_QUERY_PEER_OFFLINE: @@ -225,19 +312,19 @@ int BitDhtHandler::PeerCallback(const bdId *id, uint32_t status) case BITDHT_MGR_QUERY_PEER_UNREACHABLE: /* do nothing */ - std::cerr << "BitDhtHandler::PeerCallback() QUERY PEER UNREACHABLE ... flag? / do nothin "; + std::cerr << "BitDhtHandler::PeerCallback() QUERY PEER UNREACHABLE ... saving address "; std::cerr << std::endl; - + it->second.id = *id; break; case BITDHT_MGR_QUERY_PEER_ONLINE: /* do something */ - std::cerr << "BitDhtHandler::PeerCallback() QUERY PEER ONLINE ... try udp connection"; + std::cerr << "BitDhtHandler::PeerCallback() QUERY PEER ONLINE ... saving address"; std::cerr << std::endl; - connect = true; + it->second.id = *id; break; } return 1; diff --git a/src/example/bdhandler.h b/src/example/bdhandler.h index e2ad55d..c0c942d 100644 --- a/src/example/bdhandler.h +++ b/src/example/bdhandler.h @@ -37,6 +37,18 @@ /*** This class can be overloaded to use the XXXXCallback() Functions *****/ class BitDhtIntCallback; + +class BssResult +{ + public: + bdId id; + uint32_t mode; // single shot + uint32_t status; // SEARCHING, FAILURE, FOUND, MULTIPLE HITS. +}; + +#define BSS_SINGLE_SHOT 0x0001 + + class BitDhtHandler { @@ -58,12 +70,17 @@ virtual int NodeCallback(const bdId *id, uint32_t peerflags); virtual int PeerCallback(const bdId *id, uint32_t status); virtual int ValueCallback(const bdNodeId *id, std::string key, uint32_t status); +bool SearchResult(bdId *id, uint32_t &status); + private: /* real DHT classes */ UdpStack *mStack; UdpBitDht *mUdpBitDht; + bdMutex resultsMtx; /* for all class data (below) */ + + std::map mSearchNodes; }; diff --git a/src/example/bootstrap_fn.cc b/src/example/bootstrap_fn.cc new file mode 100644 index 0000000..e58d9a1 --- /dev/null +++ b/src/example/bootstrap_fn.cc @@ -0,0 +1,60 @@ + + +#include "bitdht/bdiface.h" +#include "bitdht/bdstddht.h" +#include "bdhandler.h" + +#include "bootstrap_fn.h" + +bool bdSingleShotFindPeer(const std::string bootstrapfile, const std::string peerId, std::string &peer_ip, uint16_t &peer_port) +{ + /* startup dht : with a random id! */ + bdNodeId ownId; + bdStdRandomNodeId(&ownId); + + uint16_t port = 6775; + std::string appId = "bsId"; + BitDhtHandler dht(&ownId, port, appId, bootstrapfile); + + /* install search node */ + bdNodeId searchId; + bdStdRandomNodeId(&searchId); + + std::cerr << "bssdht: searching for Id: "; + bdStdPrintNodeId(std::cerr, &searchId); + std::cerr << std::endl; + + dht.FindNode(&searchId); + + /* run your program */ + bdId resultId; + uint32_t status; + + resultId.id = searchId; + + while(false == dht.SearchResult(&resultId, status)) + { + sleep(10); + } + + std::cerr << "bdSingleShotFindPeer(): Found Result:" << std::endl; + + std::cerr << "\tId: "; + bdStdPrintId(std::cerr, &resultId); + std::cerr << std::endl; + + std::cerr << "\tstatus: " << status; + std::cerr << std::endl; + + dht.shutdown(); + + return true; +} + + + + + + + + diff --git a/src/example/bootstrap_fn.h b/src/example/bootstrap_fn.h new file mode 100644 index 0000000..19ff6d7 --- /dev/null +++ b/src/example/bootstrap_fn.h @@ -0,0 +1,17 @@ + +#include +#include + + +/* NOTE. At the moment only the bootstrapfile is actually used. + * peerId is ignored (a random peerId is searched for). ip & port are not filled in either. + * + * This is mainly to finish testing. + * + * Once the best form of the return functions is decided (ipv4 structure, or strings). + * this can be finished off. + * + */ + +bool bdSingleShotFindPeer(const std::string bootstrapfile, const std::string peerId, std::string &ip, uint16_t &port); + diff --git a/src/example/bssdht.cc b/src/example/bssdht.cc new file mode 100644 index 0000000..9d5f0e4 --- /dev/null +++ b/src/example/bssdht.cc @@ -0,0 +1,31 @@ + +#include "bootstrap_fn.h" +#include +#include + +int main(int argc, char **argv) +{ + + std::string bootstrapfile = "bdboot.txt"; + std::string peerId; + std::string ip; + uint16_t port; + + std::cerr << "bssdht: starting up"; + std::cerr << std::endl; + + bdSingleShotFindPeer(bootstrapfile, peerId, ip, port); + + std::cerr << "bssdht: finished"; + std::cerr << std::endl; + + return 1; +} + + + + + + + +