some cleanup to dht observers (don't expose internals)

This commit is contained in:
Arvid Norberg 2015-05-22 02:42:26 +00:00
parent 1b93f1e4b8
commit 0052fa6c19
8 changed files with 64 additions and 54 deletions

View File

@ -48,6 +48,7 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent {
namespace dht {
struct dht_observer;
struct observer;
struct msg;
struct traversal_algorithm;
@ -96,13 +97,17 @@ struct observer : boost::noncopyable
// this is called when no reply has been received within
// some timeout
void timeout();
// if this is called the destructor should
// not invoke any new messages, and should
// only clean up. It means the rpc-manager
// is being destructed
void abort();
dht_observer* get_observer() const;
traversal_algorithm* algorithm() const { return m_algorithm.get(); }
time_point sent() const { return m_sent; }
void set_target(udp::endpoint const& ep);
@ -129,13 +134,12 @@ struct observer : boost::noncopyable
flag_done = 128
};
#ifdef TORRENT_DISABLE_LOGGING
// TODO: 3 make this private and unconditional
protected:
#endif
void done();
private:
time_point m_sent;
const boost::intrusive_ptr<traversal_algorithm> m_algorithm;

View File

@ -53,8 +53,8 @@ void find_data_observer::reply(msg const& m)
if (!r)
{
#ifndef TORRENT_DISABLE_LOGGING
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] missing response dict"
, m_algorithm.get());
get_observer()->log(dht_logger::traversal, "[%p] missing response dict"
, algorithm());
#endif
return;
}
@ -63,15 +63,15 @@ void find_data_observer::reply(msg const& m)
if (!id || id.string_length() != 20)
{
#ifndef TORRENT_DISABLE_LOGGING
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] invalid id in response"
, m_algorithm.get());
get_observer()->log(dht_logger::traversal, "[%p] invalid id in response"
, algorithm());
#endif
return;
}
bdecode_node token = r.dict_find_string("token");
if (token)
{
static_cast<find_data*>(m_algorithm.get())->got_write_token(
static_cast<find_data*>(algorithm())->got_write_token(
node_id(id.string_ptr()), token.string_value());
}

View File

@ -258,8 +258,8 @@ void get_item_observer::reply(msg const& m)
if (!r)
{
#ifndef TORRENT_DISABLE_LOGGING
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] missing response dict"
, m_algorithm.get());
get_observer()->log(dht_logger::traversal, "[%p] missing response dict"
, algorithm());
#endif
return;
}
@ -281,7 +281,7 @@ void get_item_observer::reply(msg const& m)
bdecode_node v = r.dict_find("v");
if (v)
{
static_cast<get_item*>(m_algorithm.get())->got_data(v, pk, seq, sig);
static_cast<get_item*>(algorithm())->got_data(v, pk, seq, sig);
}
find_data_observer::reply(m);

View File

@ -51,8 +51,8 @@ void get_peers_observer::reply(msg const& m)
if (!r)
{
#ifndef TORRENT_DISABLE_LOGGING
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] missing response dict"
, m_algorithm.get());
get_observer()->log(dht_logger::traversal, "[%p] missing response dict"
, algorithm());
#endif
return;
}
@ -72,14 +72,14 @@ void get_peers_observer::reply(msg const& m)
bdecode_node id = r.dict_find_string("id");
if (id && id.string_length() == 20)
{
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] PEERS "
get_observer()->log(dht_logger::traversal, "[%p] PEERS "
"invoke-count: %d branch-factor: %d addr: %s id: %s distance: %d p: %d"
, m_algorithm.get()
, m_algorithm->invoke_count()
, m_algorithm->branch_factor()
, algorithm()
, algorithm()->invoke_count()
, algorithm()->branch_factor()
, print_endpoint(m.addr).c_str()
, to_hex(id.string_value()).c_str()
, distance_exp(m_algorithm->target(), node_id(id.string_ptr()))
, distance_exp(algorithm()->target(), node_id(id.string_ptr()))
, int((end - peers) / 6));
}
#endif
@ -94,19 +94,19 @@ void get_peers_observer::reply(msg const& m)
bdecode_node id = r.dict_find_string("id");
if (id && id.string_length() == 20)
{
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] PEERS "
get_observer()->log(dht_logger::traversal, "[%p] PEERS "
"invoke-count: %d branch-factor: %d addr: %s id: %s distance: %d p: %d"
, m_algorithm.get()
, m_algorithm->invoke_count()
, m_algorithm->branch_factor()
, algorithm()
, algorithm()->invoke_count()
, algorithm()->branch_factor()
, print_endpoint(m.addr).c_str()
, to_hex(id.string_value()).c_str()
, distance_exp(m_algorithm->target(), node_id(id.string_ptr()))
, distance_exp(algorithm()->target(), node_id(id.string_ptr()))
, int(n.list_size()));
}
#endif
}
static_cast<get_peers*>(m_algorithm.get())->got_peers(peer_list);
static_cast<get_peers*>(algorithm())->got_peers(peer_list);
}
find_data_observer::reply(m);
@ -308,8 +308,8 @@ void obfuscated_get_peers_observer::reply(msg const& m)
if (!r)
{
#ifndef TORRENT_DISABLE_LOGGING
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] missing response dict"
, m_algorithm.get());
get_observer()->log(dht_logger::traversal, "[%p] missing response dict"
, algorithm());
#endif
return;
}
@ -318,8 +318,8 @@ void obfuscated_get_peers_observer::reply(msg const& m)
if (!id || id.string_length() != 20)
{
#ifndef TORRENT_DISABLE_LOGGING
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] invalid id in response"
, m_algorithm.get());
get_observer()->log(dht_logger::traversal, "[%p] invalid id in response"
, algorithm());
#endif
return;
}

View File

@ -456,11 +456,11 @@ struct ping_observer : observer
if (!r)
{
#ifndef TORRENT_DISABLE_LOGGING
if (m_algorithm->get_node().observer())
if (get_observer())
{
m_algorithm->get_node().observer()->log(dht_logger::node
get_observer()->log(dht_logger::node
, "[%p] missing response dict"
, m_algorithm.get());
, algorithm());
}
#endif
return;
@ -478,7 +478,7 @@ struct ping_observer : observer
node_id id;
std::copy(nodes, nodes + 20, id.begin());
nodes += 20;
m_algorithm.get()->get_node().m_table.heard_about(id
algorithm()->get_node().m_table.heard_about(id
, detail::read_v4_endpoint<udp::endpoint>(nodes));
}
}

View File

@ -79,12 +79,18 @@ void intrusive_ptr_release(observer const* o)
TORRENT_ASSERT(o->m_refs > 0);
if (--o->m_refs == 0)
{
boost::intrusive_ptr<traversal_algorithm> ta = o->m_algorithm;
boost::intrusive_ptr<traversal_algorithm> ta = o->algorithm();
(const_cast<observer*>(o))->~observer();
ta->free_observer(const_cast<observer*>(o));
}
}
// TODO: 3 move this into it's own .cpp file
dht_observer* observer::get_observer() const
{
return m_algorithm->get_node().observer();
}
void observer::set_target(udp::endpoint const& ep)
{
m_sent = clock_type::now();
@ -336,7 +342,7 @@ bool rpc_manager::incoming(msg const& m, node_id* id
#ifndef TORRENT_DISABLE_LOGGING
m_log->log(dht_logger::rpc_manager, "[%p] reply with transaction id: %d from %s"
, o->m_algorithm.get(), int(transaction_id.size())
, o->algorithm(), int(transaction_id.size())
, print_endpoint(m.addr).c_str());
#endif
o->reply(m);
@ -376,7 +382,7 @@ time_duration rpc_manager::tick()
{
#ifndef TORRENT_DISABLE_LOGGING
m_log->log(dht_logger::rpc_manager, "[%p] timing out transaction id: %d from: %s"
, o->m_algorithm.get(), o->transaction_id()
, o->algorithm(), o->transaction_id()
, print_endpoint(o->target_ep()).c_str());
#endif
m_transactions.erase(i++);
@ -390,7 +396,7 @@ time_duration rpc_manager::tick()
{
#ifndef TORRENT_DISABLE_LOGGING
m_log->log(dht_logger::rpc_manager, "[%p] short-timing out transaction id: %d from: %s"
, o->m_algorithm.get(), o->transaction_id()
, o->algorithm(), o->transaction_id()
, print_endpoint(o->target_ep()).c_str());
#endif
++i;
@ -402,10 +408,10 @@ time_duration rpc_manager::tick()
ret = std::min(seconds(timeout) - diff, ret);
++i;
}
std::for_each(timeouts.begin(), timeouts.end(), boost::bind(&observer::timeout, _1));
std::for_each(short_timeouts.begin(), short_timeouts.end(), boost::bind(&observer::short_timeout, _1));
return ret;
}
@ -431,13 +437,13 @@ bool rpc_manager::invoke(entry& e, udp::endpoint target_addr
int tid = (random() ^ (random() << 5)) & 0xffff;
io::write_uint16(tid, out);
e["t"] = transaction_id;
o->set_target(target_addr);
o->set_transaction_id(tid);
#ifndef TORRENT_DISABLE_LOGGING
m_log->log(dht_logger::rpc_manager, "[%p] invoking %s -> %s"
, o->m_algorithm.get(), e["q"].string().c_str()
, o->algorithm(), e["q"].string().c_str()
, print_endpoint(target_addr).c_str());
#endif

View File

@ -552,26 +552,26 @@ void traversal_observer::reply(msg const& m)
if (!r)
{
#ifndef TORRENT_DISABLE_LOGGING
if (m_algorithm->get_node().observer())
if (get_observer())
{
m_algorithm->get_node().observer()->log(dht_logger::traversal
get_observer()->log(dht_logger::traversal
, "[%p] missing response dict"
, m_algorithm.get());
, algorithm());
}
#endif
return;
}
#ifndef TORRENT_DISABLE_LOGGING
if (m_algorithm->get_node().observer())
if (get_observer())
{
bdecode_node nid = r.dict_find_string("id");
char hex_id[41];
to_hex(nid.string_ptr(), 20, hex_id);
m_algorithm->get_node().observer()->log(dht_logger::traversal
get_observer()->log(dht_logger::traversal
, "[%p] RESPONSE id: %s invoke-count: %d addr: %s type: %s"
, m_algorithm.get(), hex_id, m_algorithm->invoke_count()
, print_endpoint(target_ep()).c_str(), m_algorithm->name());
, algorithm(), hex_id, algorithm()->invoke_count()
, print_endpoint(target_ep()).c_str(), algorithm()->name());
}
#endif
// look for nodes
@ -586,7 +586,7 @@ void traversal_observer::reply(msg const& m)
node_id id;
std::copy(nodes, nodes + 20, id.begin());
nodes += 20;
m_algorithm->traverse(id, read_v4_endpoint<udp::endpoint>(nodes));
algorithm()->traverse(id, read_v4_endpoint<udp::endpoint>(nodes));
}
}
@ -594,10 +594,10 @@ void traversal_observer::reply(msg const& m)
if (!id || id.string_length() != 20)
{
#ifndef TORRENT_DISABLE_LOGGING
if (m_algorithm->get_node().observer())
if (get_observer())
{
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] invalid id in response"
, m_algorithm.get());
get_observer()->log(dht_logger::traversal, "[%p] invalid id in response"
, algorithm());
}
#endif
return;

View File

@ -2344,7 +2344,7 @@ retry:
}
// to test SSL connections, one can use this openssl command template:
//
//
// openssl s_client -cert <client-cert>.pem -key <client-private-key>.pem
// -CAfile <torrent-cert>.pem -debug -connect 127.0.0.1:4433 -tls1
// -servername <hex-encoded-info-hash>
@ -2851,7 +2851,7 @@ retry:
#endif
error_code ec;
m_timer.expires_at(now + milliseconds(m_settings.get_int(settings_pack::tick_interval)), ec);
m_timer.async_wait(bind(&session_impl::on_tick, this, _1));
m_timer.async_wait(boost::bind(&session_impl::on_tick, this, _1));
m_download_rate.update_quotas(now - m_last_tick);
m_upload_rate.update_quotas(now - m_last_tick);