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. 22
      src/twister.cpp
  2. 23
      src/twister_utils.cpp
  3. 4
      src/twister_utils.h

22
src/twister.cpp

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

23
src/twister_utils.cpp

@ -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 && if( e.type() == libtorrent::entry::dictionary_t &&
e.find_key(key) && e[key].type() == libtorrent::entry::string_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)
} }
} }
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);
void hexcapeDht(libtorrent::entry &e); void hexcapeDht(libtorrent::entry &e);
void unHexcapeDht(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 #endif // TWISTER_UTILS_H

Loading…
Cancel
Save