Browse Source

don't assume the fastest dht node to be the most up-to-date on a given resource.

that is, now we also enable multiple replies collecting for non-multi entries.
default parameter is a very small timeout for additional replies: that's a tradeoff between probability of getting an older value x slowing the UI.
miguelfreitas
Miguel Freitas 11 years ago
parent
commit
5eb40d9ffd
  1. 24
      src/twister.cpp
  2. 23
      src/twister_utils.cpp
  3. 4
      src/twister_utils.h

24
src/twister.cpp

@ -1339,6 +1339,7 @@ Value dhtget(const Array& params, bool fHelp) @@ -1339,6 +1339,7 @@ Value dhtget(const Array& params, bool fHelp)
time_duration timeToWait = seconds(10);
time_duration timeToWaitMulti = milliseconds(100);
int minMultiReplies = 3;
int lastSeq = -1;
if( params.size() > 3 )
timeToWait = milliseconds(params[3].get_int());
@ -1372,12 +1373,17 @@ Value dhtget(const Array& params, bool fHelp) @@ -1372,12 +1373,17 @@ Value dhtget(const Array& params, bool fHelp)
libtorrent::entry &e = *it;
hexcapeDht( e );
string sig_p = safeGetEntryString(e, "sig_p");
if( !sig_p.length() ) {
int seq = (multi) ? 0 : safeGetEntryInt( safeGetEntryDict(e,"p"), "seq" );
bool acceptEntry = (multi) ? (!sig_p.length() || !uniqueSigPs.count(sig_p)) :
(seq > lastSeq);
if( acceptEntry ) {
if( !multi) {
ret.clear();
}
ret.push_back( entryToJson(e) );
} else {
if( !uniqueSigPs.count(sig_p) ) {
lastSeq = seq;
if( sig_p.length() ) {
uniqueSigPs.insert(sig_p);
ret.push_back( entryToJson(e) );
}
}
}
@ -1387,13 +1393,9 @@ Value dhtget(const Array& params, bool fHelp) @@ -1387,13 +1393,9 @@ Value dhtget(const Array& params, bool fHelp)
break;
}
if( multi ) {
if( repliesReceived++ < minMultiReplies && uniqueSigPs.size() ) {
timeToWait = timeToWaitMulti;
//printf("dhtget: wait again %d\n", repliesReceived);
} else {
break;
}
if( repliesReceived++ < minMultiReplies ) {
timeToWait = timeToWaitMulti;
//printf("dhtget: wait again repliesReceived=%d lastSeq=%d\n", repliesReceived, lastSeq);
} else {
break;
}

23
src/twister_utils.cpp

@ -329,7 +329,7 @@ void unHexcapeDht(libtorrent::entry &e) @@ -329,7 +329,7 @@ void unHexcapeDht(libtorrent::entry &e)
}
}
std::string safeGetEntryString(libtorrent::entry &e, std::string const& key)
std::string safeGetEntryString(libtorrent::entry const &e, std::string const& key)
{
if( e.type() == libtorrent::entry::dictionary_t &&
e.find_key(key) && e[key].type() == libtorrent::entry::string_t ) {
@ -339,3 +339,24 @@ std::string safeGetEntryString(libtorrent::entry &e, std::string const& key) @@ -339,3 +339,24 @@ std::string safeGetEntryString(libtorrent::entry &e, std::string const& key)
}
}
int safeGetEntryInt(libtorrent::entry const &e, std::string const& key)
{
if( e.type() == libtorrent::entry::dictionary_t &&
e.find_key(key) && e[key].type() == libtorrent::entry::int_t ) {
return e[key].integer();
} else {
return 0;
}
}
libtorrent::entry safeGetEntryDict(libtorrent::entry const &e, std::string const& key)
{
static libtorrent::entry::dictionary_type dummy;
if( e.type() == libtorrent::entry::dictionary_t &&
e.find_key(key) && e[key].type() == libtorrent::entry::dictionary_t ) {
return e[key].dict();
} else {
return dummy;
}
}

4
src/twister_utils.h

@ -44,6 +44,8 @@ void unHexcapePost(libtorrent::entry &e); @@ -44,6 +44,8 @@ void unHexcapePost(libtorrent::entry &e);
void hexcapeDht(libtorrent::entry &e);
void unHexcapeDht(libtorrent::entry &e);
std::string safeGetEntryString(libtorrent::entry &e, std::string const& key);
std::string safeGetEntryString(libtorrent::entry const &e, std::string const& key);
int safeGetEntryInt(libtorrent::entry const &e, std::string const& key);
libtorrent::entry safeGetEntryDict(libtorrent::entry const &e, std::string const& key);
#endif // TWISTER_UTILS_H

Loading…
Cancel
Save