~cmiller/ubuntu/quantal/deluge/fix-parameter-move-storage

« back to all changes in this revision

Viewing changes to libtorrent/src/kademlia/refresh.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2009-11-13 02:39:45 UTC
  • mfrom: (4.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091113023945-te1bybo2912ejzuc
Tags: 1.2.0~rc3-4
* debian/control: bump build-dep on python-setuptools to (>= 0.6c9).
* debian/patches:
  - 25_r5921_fastresume_files.patch
    new, should fix problems with fresh configs;
  - 30_r5931_ipc_lockfile.patch:
    new, should fix an issue where Deluge will fail to start if there is a
    stale ipc lockfile. (Closes: #555849)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
Copyright (c) 2006, 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
 
#include "libtorrent/pch.hpp"
34
 
 
35
 
#include <libtorrent/kademlia/refresh.hpp>
36
 
#include <libtorrent/kademlia/routing_table.hpp>
37
 
#include <libtorrent/kademlia/rpc_manager.hpp>
38
 
#include <libtorrent/kademlia/logging.hpp>
39
 
#include <libtorrent/kademlia/msg.hpp>
40
 
 
41
 
#include <libtorrent/io.hpp>
42
 
 
43
 
#include <boost/bind.hpp>
44
 
 
45
 
using boost::bind;
46
 
 
47
 
namespace libtorrent { namespace dht
48
 
{
49
 
 
50
 
#ifdef TORRENT_DHT_VERBOSE_LOGGING
51
 
TORRENT_DEFINE_LOG(refresh)
52
 
#endif
53
 
 
54
 
refresh_observer::~refresh_observer()
55
 
{
56
 
        if (m_algorithm) m_algorithm->failed(m_self, true);
57
 
}
58
 
 
59
 
void refresh_observer::reply(msg const& in)
60
 
{
61
 
        if (!m_algorithm) return;
62
 
 
63
 
        if (!in.nodes.empty())
64
 
        {
65
 
                for (msg::nodes_t::const_iterator i = in.nodes.begin()
66
 
                        , end(in.nodes.end()); i != end; ++i)
67
 
                {
68
 
                        m_algorithm->traverse(i->id, i->addr);
69
 
                }
70
 
        }
71
 
        m_algorithm->finished(m_self);
72
 
        m_algorithm = 0;
73
 
}
74
 
 
75
 
void refresh_observer::timeout()
76
 
{
77
 
        if (!m_algorithm) return;
78
 
        m_algorithm->failed(m_self);
79
 
        m_algorithm = 0;
80
 
}
81
 
 
82
 
ping_observer::~ping_observer()
83
 
{
84
 
        if (m_algorithm) m_algorithm->ping_timeout(m_self, true);
85
 
}
86
 
 
87
 
void ping_observer::reply(msg const& m)
88
 
{
89
 
        if (!m_algorithm) return;
90
 
        
91
 
        m_algorithm->ping_reply(m_self);
92
 
        m_algorithm = 0;
93
 
}
94
 
 
95
 
void ping_observer::timeout()
96
 
{
97
 
        if (!m_algorithm) return;
98
 
        m_algorithm->ping_timeout(m_self);
99
 
        m_algorithm = 0;
100
 
}
101
 
 
102
 
void refresh::invoke(node_id const& nid, udp::endpoint addr)
103
 
{
104
 
        TORRENT_ASSERT(m_rpc.allocation_size() >= sizeof(refresh_observer));
105
 
        observer_ptr o(new (m_rpc.allocator().malloc()) refresh_observer(
106
 
                this, nid, m_target));
107
 
#ifdef TORRENT_DEBUG
108
 
        o->m_in_constructor = false;
109
 
#endif
110
 
 
111
 
        m_rpc.invoke(messages::find_node, addr, o);
112
 
}
113
 
 
114
 
void refresh::done()
115
 
{
116
 
        m_leftover_nodes_iterator = (int)m_results.size() > m_max_results ?
117
 
                m_results.begin() + m_max_results : m_results.end();
118
 
 
119
 
        invoke_pings_or_finish();
120
 
}
121
 
 
122
 
void refresh::ping_reply(node_id nid)
123
 
{
124
 
        m_active_pings--;
125
 
        invoke_pings_or_finish();
126
 
}
127
 
 
128
 
void refresh::ping_timeout(node_id nid, bool prevent_request)
129
 
{
130
 
        m_active_pings--;
131
 
        invoke_pings_or_finish(prevent_request);
132
 
}
133
 
 
134
 
void refresh::invoke_pings_or_finish(bool prevent_request)
135
 
{
136
 
        if (prevent_request)
137
 
        {
138
 
                --m_max_active_pings;
139
 
                if (m_max_active_pings <= 0)
140
 
                        m_max_active_pings = 1;
141
 
        }
142
 
        else
143
 
        {
144
 
                while (m_active_pings < m_max_active_pings)
145
 
                {
146
 
                        if (m_leftover_nodes_iterator == m_results.end()) break;
147
 
 
148
 
                        result const& node = *m_leftover_nodes_iterator;
149
 
 
150
 
                        // Skip initial nodes
151
 
                        if (node.flags & result::initial)
152
 
                        {
153
 
                                ++m_leftover_nodes_iterator;
154
 
                                continue;
155
 
                        }
156
 
 
157
 
                        try
158
 
                        {
159
 
                                TORRENT_ASSERT(m_rpc.allocation_size() >= sizeof(ping_observer));
160
 
                                observer_ptr o(new (m_rpc.allocator().malloc()) ping_observer(
161
 
                                        this, node.id));
162
 
#ifdef TORRENT_DEBUG
163
 
                                o->m_in_constructor = false;
164
 
#endif
165
 
                                m_rpc.invoke(messages::ping, node.addr, o);
166
 
                                ++m_active_pings;
167
 
                                ++m_leftover_nodes_iterator;
168
 
                        }
169
 
                        catch (std::exception& e) {}
170
 
                }
171
 
        }
172
 
 
173
 
        if (m_active_pings == 0)
174
 
        {
175
 
                m_done_callback();
176
 
        }
177
 
}
178
 
 
179
 
} } // namespace libtorrent::dht
180