Browse Source

implement dht_get_data_alert to detect when we are neighbor of a certain request

miguelfreitas
Miguel Freitas 11 years ago
parent
commit
dea237d2ed
  1. 17
      libtorrent/include/libtorrent/alert_types.hpp
  2. 7
      libtorrent/src/alert.cpp
  3. 22
      libtorrent/src/kademlia/node.cpp
  4. 23
      src/twister.cpp

17
libtorrent/include/libtorrent/alert_types.hpp

@ -1163,6 +1163,23 @@ namespace libtorrent
entry::list_type const m_lst; entry::list_type const m_lst;
}; };
struct TORRENT_EXPORT dht_get_data_alert: alert
{
dht_get_data_alert(entry const& target, bool possiblyNeighbor, bool hasData)
: m_target(target), m_possiblyNeighbor(possiblyNeighbor), m_hasData(hasData)
{}
TORRENT_DEFINE_ALERT(dht_get_data_alert);
const static int static_category = alert::dht_notification;
virtual std::string message() const;
entry m_target;
bool m_possiblyNeighbor;
bool m_hasData;
};
struct TORRENT_EXPORT stats_alert: torrent_alert struct TORRENT_EXPORT stats_alert: torrent_alert
{ {
stats_alert(torrent_handle const& h, int interval stats_alert(torrent_handle const& h, int interval

7
libtorrent/src/alert.cpp

@ -358,6 +358,13 @@ namespace libtorrent {
return msg; return msg;
} }
std::string dht_get_data_alert::message() const
{
char msg[200];
snprintf(msg, sizeof(msg), "request of getData received (m_possiblyNeighbor=%d)", m_possiblyNeighbor);
return msg;
}
stats_alert::stats_alert(torrent_handle const& h, int in stats_alert::stats_alert(torrent_handle const& h, int in
, stat const& s) , stat const& s)
: torrent_alert(h) : torrent_alert(h)

22
libtorrent/src/kademlia/node.cpp

@ -1332,9 +1332,11 @@ void node_impl::incoming_request(msg const& m, entry& e)
m_table.find_node(target, n, 0); m_table.find_node(target, n, 0);
write_nodes_entry(reply, n); write_nodes_entry(reply, n);
bool hasData = false;
dht_storage_table_t::iterator i = m_storage_table.find(target); dht_storage_table_t::iterator i = m_storage_table.find(target);
if (i != m_storage_table.end()) if (i != m_storage_table.end())
{ {
hasData = true;
reply["values"] = entry::list_type(); reply["values"] = entry::list_type();
entry::list_type &values = reply["values"].list(); entry::list_type &values = reply["values"].list();
@ -1349,6 +1351,26 @@ void node_impl::incoming_request(msg const& m, entry& e)
values.push_back(v); values.push_back(v);
} }
} }
// check distance between target, nodes and our own id
// n is sorted from closer(begin) to more distant (end)
bool possiblyNeighbor = false;
if( n.size() < m_table.bucket_size() ) {
possiblyNeighbor = true;
} else {
node_id dFarther = distance(n.back().id, target);
node_id dOwn = distance(nid(), target);
if( dOwn < dFarther )
possiblyNeighbor = true;
}
if (m_post_alert)
{
entry eTarget;
eTarget = *msg_keys[mk_target];
alert* a = new dht_get_data_alert(eTarget,possiblyNeighbor,hasData);
if (!m_post_alert->post_alert(a)) delete a;
}
} }
else else
{ {

23
src/twister.cpp

@ -170,6 +170,10 @@ void ThreadWaitExtIP()
, listen_port, listen_port+1, ec.message().c_str()); , listen_port, listen_port+1, ec.message().c_str());
} }
dht_settings dhts;
dhts.restrict_routing_ips = false;
dhts.restrict_search_ips = false;
ses->set_dht_settings(dhts);
ses->start_dht(); ses->start_dht();
//ses->set_settings(settings); //ses->set_settings(settings);
@ -182,12 +186,12 @@ void ThreadMaintainDHTNodes()
RenameThread("maintain-dht-nodes"); RenameThread("maintain-dht-nodes");
while(1) { while(1) {
MilliSleep(5000); MilliSleep(15000);
if( ses ) { if( ses ) {
session_status ss = ses->status(); session_status ss = ses->status();
if( ss.dht_nodes == 0 && vNodes.size() ) { if( ss.dht_nodes < (int)vNodes.size() ) {
printf("ThreadMaintainDHTNodes: no dht_nodes, trying to add some...\n"); printf("ThreadMaintainDHTNodes: too few dht_nodes, trying to add some...\n");
LOCK(cs_vNodes); LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes) { BOOST_FOREACH(CNode* pnode, vNodes) {
BOOST_FOREACH(CAddress const &knownAddr, pnode->setAddrKnown) { BOOST_FOREACH(CAddress const &knownAddr, pnode->setAddrKnown) {
@ -253,6 +257,19 @@ void ThreadSessionAlerts()
continue; continue;
} }
dht_get_data_alert const* gd = alert_cast<dht_get_data_alert>(*i);
if (gd)
{
if( gd->m_possiblyNeighbor ) {
printf("possiblyNeighbor of [%s,%s,%s]\n",
gd->m_target.find_key("n")->string().c_str(),
gd->m_target.find_key("r")->string().c_str(),
gd->m_target.find_key("t")->string().c_str());
}
continue;
}
/* /*
save_resume_data_alert const* rd = alert_cast<save_resume_data_alert>(*i); save_resume_data_alert const* rd = alert_cast<save_resume_data_alert>(*i);
if (rd) { if (rd) {

Loading…
Cancel
Save