2015-09-22 20:10:57 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2006-2018, Arvid Norberg & Daniel Wallin
|
2015-09-22 20:10:57 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libtorrent/kademlia/put_data.hpp>
|
|
|
|
#include <libtorrent/kademlia/dht_observer.hpp>
|
|
|
|
#include <libtorrent/kademlia/node.hpp>
|
|
|
|
#include <libtorrent/io.hpp>
|
2017-02-22 23:40:14 +01:00
|
|
|
#include <libtorrent/performance_counters.hpp>
|
2015-09-22 20:10:57 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent { namespace dht {
|
2015-09-22 20:10:57 +02:00
|
|
|
|
|
|
|
put_data::put_data(node& dht_node, put_callback const& callback)
|
2018-03-04 19:28:31 +01:00
|
|
|
: traversal_algorithm(dht_node, {})
|
2015-09-22 20:10:57 +02:00
|
|
|
, m_put_callback(callback)
|
2016-09-24 15:10:54 +02:00
|
|
|
{}
|
2015-09-22 20:10:57 +02:00
|
|
|
|
|
|
|
char const* put_data::name() const { return "put_data"; }
|
|
|
|
|
2015-11-23 00:56:27 +01:00
|
|
|
void put_data::start()
|
|
|
|
{
|
|
|
|
// router nodes must not be added to puts
|
|
|
|
init();
|
2016-09-24 15:10:54 +02:00
|
|
|
bool const is_done = add_requests();
|
2015-11-23 00:56:27 +01:00
|
|
|
if (is_done) done();
|
|
|
|
}
|
|
|
|
|
2016-09-03 03:05:11 +02:00
|
|
|
void put_data::set_targets(std::vector<std::pair<node_entry, std::string>> const& targets)
|
2015-09-22 20:10:57 +02:00
|
|
|
{
|
2016-09-03 03:05:11 +02:00
|
|
|
for (auto const& p : targets)
|
2015-09-22 20:10:57 +02:00
|
|
|
{
|
2016-09-03 03:05:11 +02:00
|
|
|
auto o = m_node.m_rpc.allocate_observer<put_data_observer>(self(), p.first.ep()
|
|
|
|
, p.first.id, p.second);
|
|
|
|
if (!o) return;
|
2015-09-22 20:10:57 +02:00
|
|
|
|
2017-09-12 23:10:11 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2015-09-22 20:10:57 +02:00
|
|
|
o->m_in_constructor = false;
|
2017-09-12 23:10:11 +02:00
|
|
|
#endif
|
2016-09-24 15:10:54 +02:00
|
|
|
m_results.push_back(std::move(o));
|
2015-09-22 20:10:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void put_data::done()
|
|
|
|
{
|
|
|
|
m_done = true;
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-12-27 16:12:57 +01:00
|
|
|
get_node().observer()->log(dht_logger::traversal, "[%u] %s DONE, response %d, timeout %d"
|
|
|
|
, id(), name(), num_responses(), num_timeouts());
|
2015-09-22 20:10:57 +02:00
|
|
|
#endif
|
|
|
|
|
2016-10-14 14:03:09 +02:00
|
|
|
m_put_callback(m_data, num_responses());
|
2015-09-22 20:10:57 +02:00
|
|
|
traversal_algorithm::done();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool put_data::invoke(observer_ptr o)
|
|
|
|
{
|
2016-10-14 14:03:09 +02:00
|
|
|
if (m_done) return false;
|
2015-11-23 03:58:32 +01:00
|
|
|
|
2017-02-22 23:40:14 +01:00
|
|
|
// TODO: what if o is not an instance of put_data_observer? This need to be
|
|
|
|
// redesigned for better type safety.
|
2018-01-11 01:35:15 +01:00
|
|
|
auto* po = static_cast<put_data_observer*>(o.get());
|
2015-09-22 20:10:57 +02:00
|
|
|
|
|
|
|
entry e;
|
|
|
|
e["y"] = "q";
|
|
|
|
e["q"] = "put";
|
|
|
|
entry& a = e["a"];
|
|
|
|
a["v"] = m_data.value();
|
|
|
|
a["token"] = po->m_token;
|
|
|
|
if (m_data.is_mutable())
|
|
|
|
{
|
2016-07-24 00:57:04 +02:00
|
|
|
a["k"] = m_data.pk().bytes;
|
|
|
|
a["seq"] = m_data.seq().value;
|
|
|
|
a["sig"] = m_data.sig().bytes;
|
2015-09-22 20:10:57 +02:00
|
|
|
if (!m_data.salt().empty())
|
|
|
|
{
|
|
|
|
a["salt"] = m_data.salt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:40:14 +01:00
|
|
|
m_node.stats_counters().inc_stats_counter(counters::dht_put_out);
|
|
|
|
|
2015-09-22 20:10:57 +02:00
|
|
|
return m_node.m_rpc.invoke(e, o->target_ep(), o);
|
|
|
|
}
|
|
|
|
|
|
|
|
} } // namespace libtorrent::dht
|