Browse Source

add support to detect when our node is neighbor of a special resource (eg. torrent tracker)

miguelfreitas
Miguel Freitas 11 years ago
parent
commit
4154a37a14
  1. 2
      libtorrent/include/libtorrent/kademlia/dht_get.hpp
  2. 2
      libtorrent/src/kademlia/dht_get.cpp
  3. 16
      libtorrent/src/kademlia/node.cpp
  4. 30
      src/twister.cpp

2
libtorrent/include/libtorrent/kademlia/dht_get.hpp

@ -64,7 +64,7 @@ public:
typedef boost::function<void(entry::list_type const&)> data_callback; typedef boost::function<void(entry::list_type const&)> data_callback;
// callback to receive all write tokens // callback to receive all write tokens
typedef boost::function<void(std::vector<std::pair<node_entry, std::string> > const&, bool)> nodes_callback; typedef boost::function<void(std::vector<std::pair<node_entry, std::string> > const&, bool, node_id)> nodes_callback;
void got_data(entry::list_type const& values_list); void got_data(entry::list_type const& values_list);
void got_write_token(node_id const& n, std::string const& write_token) void got_write_token(node_id const& n, std::string const& write_token)

2
libtorrent/src/kademlia/dht_get.cpp

@ -269,7 +269,7 @@ void dht_get::done()
results.push_back(std::make_pair(node_entry(o->id(), o->target_ep()), j->second)); results.push_back(std::make_pair(node_entry(o->id(), o->target_ep()), j->second));
--num_results; --num_results;
} }
m_nodes_callback(results, m_got_data); m_nodes_callback(results, m_got_data, target());
traversal_algorithm::done(); traversal_algorithm::done();
} }

16
libtorrent/src/kademlia/node.cpp

@ -367,10 +367,22 @@ namespace
} }
void getDataDone_fun(std::vector<std::pair<node_entry, std::string> > const& node_results, void getDataDone_fun(std::vector<std::pair<node_entry, std::string> > const& node_results,
bool got_data, node_impl& node, bool got_data, node_id target, node_impl& node,
boost::function<void(bool, bool)> fdone) boost::function<void(bool, bool)> fdone)
{ {
bool is_neighbor = false; bool is_neighbor = false;
// check distance between target, nodes and our own id
// n is sorted from closer(begin) to more distant (back)
if( node_results.size() < node.m_table.bucket_size() ) {
is_neighbor = true;
} else {
node_id dFarther = distance(node_results.back().first.id, target);
node_id dOwn = distance(node.nid(), target);
if( dOwn < dFarther )
is_neighbor = true;
}
fdone(is_neighbor, got_data); fdone(is_neighbor, got_data);
} }
} }
@ -449,7 +461,7 @@ void node_impl::getData(std::string const &username, std::string const &resource
// for info-hash id. callback is used to return data. // for info-hash id. callback is used to return data.
boost::intrusive_ptr<dht_get> ta(new dht_get(*this, username, resource, multi, boost::intrusive_ptr<dht_get> ta(new dht_get(*this, username, resource, multi,
fdata, fdata,
boost::bind(&getDataDone_fun, _1, _2, boost::ref(*this), fdone), false)); boost::bind(&getDataDone_fun, _1, _2, _3, boost::ref(*this), fdone), false));
ta->start(); ta->start();
} }

30
src/twister.cpp

@ -31,7 +31,7 @@ static session *ses = NULL;
static CCriticalSection cs_dhtgetMap; static CCriticalSection cs_dhtgetMap;
static map<uint256, alert_manager*> m_dhtgetMap; static map<uint256, alert_manager*> m_dhtgetMap;
static map<std::string, bool> m_specialResources;
uint256 dhtTargetHash(std::string const &username, std::string const &resource, std::string const &type) uint256 dhtTargetHash(std::string const &username, std::string const &resource, std::string const &type)
{ {
@ -281,17 +281,21 @@ void ThreadSessionAlerts()
r && r->type() == entry::string_t && r && r->type() == entry::string_t &&
t && t->type() == entry::string_t) { t && t->type() == entry::string_t) {
// now we do our own search to make sure we are really close to this target // if this is a special resource then start another dhtget to make
uint256 th = dhtTargetHash(n->string(), r->string(), t->string()); // sure we are really its neighbor. don't do it needless.
if( m_specialResources.count(r->string()) ) {
// now we do our own search to make sure we are really close to this target
uint256 th = dhtTargetHash(n->string(), r->string(), t->string());
if( !neighborCheck.count(th) ) { if( !neighborCheck.count(th) ) {
printf("possiblyNeighbor of [%s,%s,%s]\n", printf("possiblyNeighbor of [%s,%s,%s] - starting a new dhtget to be sure\n",
n->string().c_str(), n->string().c_str(),
r->string().c_str(), r->string().c_str(),
t->string().c_str()); t->string().c_str());
neighborCheck[th] = gd->m_target; neighborCheck[th] = gd->m_target;
ses->dht_getData(n->string(), r->string(), t->string() == "m"); ses->dht_getData(n->string(), r->string(), t->string() == "m");
}
} }
} }
@ -318,6 +322,10 @@ void ThreadSessionAlerts()
} }
} }
if( dd->m_is_neighbor && m_specialResources.count(dd->m_resource) ) {
// Do something!
printf("Neighbor of special resource - do something!\n");
}
continue; continue;
} }
@ -342,6 +350,8 @@ void startSessionTorrent(boost::thread_group& threadGroup)
{ {
printf("startSessionTorrent (waiting for external IP)\n"); printf("startSessionTorrent (waiting for external IP)\n");
m_specialResources["tracker"] = true;
threadGroup.create_thread(boost::bind(&ThreadWaitExtIP)); threadGroup.create_thread(boost::bind(&ThreadWaitExtIP));
threadGroup.create_thread(boost::bind(&ThreadMaintainDHTNodes)); threadGroup.create_thread(boost::bind(&ThreadMaintainDHTNodes));
threadGroup.create_thread(boost::bind(&ThreadSessionAlerts)); threadGroup.create_thread(boost::bind(&ThreadSessionAlerts));

Loading…
Cancel
Save