1 /*
2 
3 Copyright (c) 2006-2018, Arvid Norberg & Daniel Wallin
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 
10     * Redistributions of source code must retain the above copyright
11       notice, this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in
14       the documentation and/or other materials provided with the distribution.
15     * Neither the name of the author nor the names of its
16       contributors may be used to endorse or promote products derived
17       from this software without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
30 
31 */
32 
33 #ifndef FIND_DATA_050323_HPP
34 #define FIND_DATA_050323_HPP
35 
36 #include <libtorrent/kademlia/traversal_algorithm.hpp>
37 #include <libtorrent/kademlia/node_id.hpp>
38 #include <libtorrent/kademlia/observer.hpp>
39 #include <libtorrent/kademlia/msg.hpp>
40 
41 #include <vector>
42 #include <map>
43 
44 namespace libtorrent { namespace dht {
45 
46 class node;
47 
48 // -------- find data -----------
49 
50 struct find_data : traversal_algorithm
51 {
52 	using nodes_callback = std::function<void(std::vector<std::pair<node_entry, std::string>> const&)>;
53 
54 	find_data(node& dht_node, node_id const& target
55 		, nodes_callback const& ncallback);
56 
57 	void got_write_token(node_id const& n, std::string write_token);
58 
59 	void start() override;
60 
61 	char const* name() const override;
62 
63 protected:
64 
65 	void done() override;
66 	observer_ptr new_observer(udp::endpoint const& ep
67 		, node_id const& id) override;
68 
69 	nodes_callback m_nodes_callback;
70 	std::map<node_id, std::string> m_write_tokens;
71 	bool m_done;
72 };
73 
74 struct find_data_observer : traversal_observer
75 {
find_data_observerlibtorrent::dht::find_data_observer76 	find_data_observer(
77 		std::shared_ptr<traversal_algorithm> algorithm
78 		, udp::endpoint const& ep, node_id const& id)
79 		: traversal_observer(std::move(algorithm), ep, id)
80 	{}
81 
82 	void reply(msg const&) override;
83 };
84 
85 } } // namespace libtorrent::dht
86 
87 #endif // FIND_DATA_050323_HPP
88