1 /*
2 
3 Copyright (c) 2013, Steven Siloti
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 LIBTORRENT_GET_ITEM_HPP
34 #define LIBTORRENT_GET_ITEM_HPP
35 
36 #include <libtorrent/kademlia/find_data.hpp>
37 #include <libtorrent/kademlia/item.hpp>
38 
39 #include <memory>
40 
41 namespace libtorrent { namespace dht {
42 
43 class get_item : public find_data
44 {
45 public:
46 	using data_callback = std::function<void(item const&, bool)>;
47 
48 	void got_data(bdecode_node const& v,
49 		public_key const& pk,
50 		sequence_number seq,
51 		signature const& sig);
52 
53 	// for immutable items
54 	get_item(node& dht_node
55 		, node_id const& target
56 		, data_callback const& dcallback
57 		, nodes_callback const& ncallback);
58 
59 	// for mutable items
60 	get_item(node& dht_node
61 		, public_key const& pk
62 		, span<char const> salt
63 		, data_callback const& dcallback
64 		, nodes_callback const& ncallback);
65 
66 	char const* name() const override;
67 
68 protected:
69 	observer_ptr new_observer(udp::endpoint const& ep
70 		, node_id const& id) override;
71 	bool invoke(observer_ptr o) override;
72 	void done() override;
73 
74 	data_callback m_data_callback;
75 	item m_data;
76 	bool m_immutable;
77 };
78 
79 class get_item_observer : public find_data_observer
80 {
81 public:
get_item_observer(std::shared_ptr<traversal_algorithm> algorithm,udp::endpoint const & ep,node_id const & id)82 	get_item_observer(
83 		std::shared_ptr<traversal_algorithm> algorithm
84 		, udp::endpoint const& ep, node_id const& id)
85 		: find_data_observer(std::move(algorithm), ep, id)
86 	{}
87 
88 	void reply(msg const&) override;
89 };
90 
91 } } // namespace libtorrent::dht
92 
93 #endif // LIBTORRENT_GET_ITEM_HPP
94