From d1a17dc21febca6036a2f92aab5d7502077cb018 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Sat, 15 Feb 2014 17:16:23 -0200 Subject: [PATCH] add timeout parameters to dhtget allowing to wait more to collect multiple replies --- src/bitcoinrpc.cpp | 3 +++ src/twister.cpp | 21 +++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 918dae26..44bb8169 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -1264,6 +1264,9 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector 1) ConvertTo(params[1]); if (strMethod == "dhtput" && n > 3) ConvertToValue(params[3]); if (strMethod == "dhtput" && n > 5) ConvertTo(params[5]); + if (strMethod == "dhtget" && n > 3) ConvertTo(params[3]); + if (strMethod == "dhtget" && n > 4) ConvertTo(params[4]); + if (strMethod == "dhtget" && n > 5) ConvertTo(params[5]); if (strMethod == "newpostmsg" && n > 1) ConvertTo(params[1]); if (strMethod == "newpostmsg" && n > 4) ConvertTo(params[4]); if (strMethod == "newdirectmsg" && n > 1) ConvertTo(params[1]); diff --git a/src/twister.cpp b/src/twister.cpp index 1140bb15..4831e2b9 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -1223,9 +1223,9 @@ Value dhtput(const Array& params, bool fHelp) Value dhtget(const Array& params, bool fHelp) { - if (fHelp || params.size() != 3) + if (fHelp || params.size() < 3 || params.size() > 6) throw runtime_error( - "dhtget \n" + "dhtget [timeout_ms] [timeout_multi_ms] [min_multi]\n" "Get resource from dht network"); if( !ses ) @@ -1234,9 +1234,19 @@ Value dhtget(const Array& params, bool fHelp) string strUsername = params[0].get_str(); string strResource = params[1].get_str(); string strMulti = params[2].get_str(); - bool multi = (strMulti == "m"); + time_duration timeToWait = seconds(10); + time_duration timeToWaitMulti = milliseconds(100); + int minMultiReplies = 3; + + if( params.size() > 3 ) + timeToWait = milliseconds(params[3].get_int()); + if( params.size() > 4 ) + timeToWaitMulti = milliseconds(params[4].get_int()); + if( params.size() > 5 ) + minMultiReplies = params[5].get_int(); + alert_manager am(10, alert::dht_notification); sha1_hash ih = dhtTargetHash(strUsername,strResource,strMulti); @@ -1250,7 +1260,6 @@ Value dhtget(const Array& params, bool fHelp) Array ret; std::set uniqueSigPs; - time_duration timeToWait = seconds(10); int repliesReceived = 0; while( am.wait_for_alert(timeToWait) ) { std::auto_ptr a(am.get()); @@ -1279,8 +1288,8 @@ Value dhtget(const Array& params, bool fHelp) } if( multi ) { - if( repliesReceived++ < 3 && uniqueSigPs.size() ) { - timeToWait = milliseconds(100 / repliesReceived); + if( repliesReceived++ < minMultiReplies && uniqueSigPs.size() ) { + timeToWait = timeToWaitMulti; //printf("dhtget: wait again %d\n", repliesReceived); } else { break;