From 09625b22127766ddfbf1f805560002afc93494d0 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Mon, 13 Jan 2014 17:30:29 -0200 Subject: [PATCH] Join multiple dhtget responses. fix #14 --- src/twister.cpp | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/twister.cpp b/src/twister.cpp index 443c07b3..4bc617ee 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -1203,9 +1203,12 @@ Value dhtget(const Array& params, bool fHelp) ses->dht_getData(strUsername, strResource, multi); - Value ret = Array(); + Array ret; + std::set uniqueSigPs; - if( am.wait_for_alert(seconds(20)) ) { + time_duration timeToWait = seconds(10); + int repliesReceived = 0; + while( am.wait_for_alert(timeToWait) ) { std::auto_ptr a(am.get()); dht_reply_data_alert const* rd = alert_cast(&(*a)); @@ -1213,11 +1216,33 @@ Value dhtget(const Array& params, bool fHelp) entry::list_type dhtLst = rd->m_lst; entry::list_type::iterator it; for( it = dhtLst.begin(); it != dhtLst.end(); ++it ) { - hexcapeDht( *it ); + libtorrent::entry &e = *it; + hexcapeDht( e ); + string sig_p = safeGetEntryString(e, "sig_p"); + if( !sig_p.length() ) { + ret.push_back( entryToJson(e) ); + } else { + if( !uniqueSigPs.count(sig_p) ) { + uniqueSigPs.insert(sig_p); + ret.push_back( entryToJson(e) ); + } + } } - ret = entryToJson(dhtLst); + //printf("dhtget: got %zd entries %zd unique\n", dhtLst.size(), uniqueSigPs.size()); } else { // cast failed => dht_reply_data_done_alert => no data + break; + } + + if( multi ) { + if( repliesReceived++ < 3 && uniqueSigPs.size() ) { + timeToWait = milliseconds(100 / repliesReceived); + //printf("dhtget: wait again %d\n", repliesReceived); + } else { + break; + } + } else { + break; } }