Modified previous example into a Single Shot Search for RPC support.

Takes 2 - 3 minutes to run at the moment. Got to make it faster!



git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-gxs-b1@5732 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2012-10-28 14:21:58 +00:00
parent 72d3c868e3
commit 1a1e453c7e
6 changed files with 674 additions and 553 deletions

View File

@ -1,16 +1,16 @@
CXXFLAGS = -Wall -g -I..
CXXFLAGS += -arch i386 # OSX
#CXXFLAGS += -arch i386 # OSX
LIBS = -L../lib -lbitdht
LIBS = -L../lib -lbitdht -lpthread
EXEC : bdexample
EXEC : bssdht
EGOBJ = bdhandler.o bdexample.o
EGOBJ = bdhandler.o bssdht.o
bdexample: $(EGOBJ)
$(CXX) $(CXXFLAGS) -o bdexample $(EGOBJ) $(LIBS)
bssdht: $(EGOBJ)
$(CXX) $(CXXFLAGS) -o bssdht $(EGOBJ) $(LIBS)

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -28,8 +28,24 @@
#include <udp/udpbitdht.h>
#include <bitdht/bdstddht.h>
#include <string.h>
#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
*/
@ -110,6 +126,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();
}
@ -162,8 +183,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 ;
}
@ -178,10 +210,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<bdNodeId, BssResult>::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<bdNodeId, BssResult>::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 **************************/
@ -203,6 +275,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<bdNodeId, BssResult>::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)
{
@ -211,6 +297,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:
@ -224,19 +311,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;

View File

@ -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<bdNodeId, BssResult> mSearchNodes;
};

View File

@ -0,0 +1,59 @@
#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 = "bsId";
std::string bootstrapfile = "bdboot.txt";
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 << "bssdht: Found Result:" << std::endl;
std::cerr << "\tId: ";
bdStdPrintId(std::cerr, &resultId);
std::cerr << std::endl;
std::cerr << "\tstatus: " << status;
std::cerr << std::endl;
return 1;
}