File: DHTAbstractNodeLookupTask.h

package info (click to toggle)
aria2 1.18.8-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,392 kB
  • ctags: 16,036
  • sloc: cpp: 115,823; sh: 12,015; ansic: 7,394; makefile: 1,445; ruby: 462; python: 216; xml: 176; asm: 58; sed: 16
file content (231 lines) | stat: -rw-r--r-- 7,220 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2006 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#ifndef D_DHT_ABSTRACT_NODE_LOOKUP_TASK_H
#define D_DHT_ABSTRACT_NODE_LOOKUP_TASK_H

#include "DHTAbstractTask.h"

#include <cstring>
#include <algorithm>
#include <deque>
#include <vector>

#include "DHTConstants.h"
#include "DHTNodeLookupEntry.h"
#include "DHTRoutingTable.h"
#include "DHTMessageDispatcher.h"
#include "DHTMessageFactory.h"
#include "DHTMessage.h"
#include "DHTNode.h"
#include "DHTBucket.h"
#include "LogFactory.h"
#include "Logger.h"
#include "util.h"
#include "DHTIDCloser.h"
#include "a2functional.h"
#include "fmt.h"

namespace aria2 {

class DHTNode;
class DHTMessage;

template<class ResponseMessage>
class DHTAbstractNodeLookupTask:public DHTAbstractTask {
private:
  unsigned char targetID_[DHT_ID_LENGTH];

  std::deque<std::unique_ptr<DHTNodeLookupEntry>> entries_;

  size_t inFlightMessage_;

  template<typename Container>
  void toEntries
  (Container& entries,
   const std::vector<std::shared_ptr<DHTNode>>& nodes) const
  {
    for(auto& node : nodes) {
      entries.push_back(make_unique<DHTNodeLookupEntry>(node));
    }
  }

  void sendMessage()
  {
    for(auto i = std::begin(entries_), eoi = std::end(entries_);
        i != eoi && inFlightMessage_ < ALPHA; ++i) {
      if((*i)->used == false) {
        ++inFlightMessage_;
        (*i)->used = true;
        getMessageDispatcher()->addMessageToQueue
          (createMessage((*i)->node), createCallback());
      }
    }
  }

  void sendMessageAndCheckFinish()
  {
    if(needsAdditionalOutgoingMessage()) {
      sendMessage();
    }
    if(inFlightMessage_ == 0) {
      A2_LOG_DEBUG(fmt("Finished node_lookup for node ID %s",
                       util::toHex(targetID_, DHT_ID_LENGTH).c_str()));
      onFinish();
      updateBucket();
      setFinished(true);
    } else {
      A2_LOG_DEBUG(fmt("%lu in flight message for node ID %s",
                       static_cast<unsigned long>(inFlightMessage_),
                       util::toHex(targetID_, DHT_ID_LENGTH).c_str()));
    }
  }

  void updateBucket() {}
protected:
  const unsigned char* getTargetID() const
  {
    return targetID_;
  }

  const std::deque<std::unique_ptr<DHTNodeLookupEntry>>& getEntries() const
  {
    return entries_;
  }

  virtual void getNodesFromMessage
  (std::vector<std::shared_ptr<DHTNode>>& nodes,
   const ResponseMessage* message) = 0;

  virtual void onReceivedInternal(const ResponseMessage* message) {}

  virtual bool needsAdditionalOutgoingMessage() { return true; }

  virtual void onFinish() {}

  virtual std::unique_ptr<DHTMessage> createMessage
  (const std::shared_ptr<DHTNode>& remoteNode) = 0;

  virtual std::unique_ptr<DHTMessageCallback> createCallback() = 0;
public:
  DHTAbstractNodeLookupTask(const unsigned char* targetID):
    inFlightMessage_(0)
  {
    memcpy(targetID_, targetID, DHT_ID_LENGTH);
  }

  static const size_t ALPHA = 3;

  virtual void startup() CXX11_OVERRIDE
  {
    std::vector<std::shared_ptr<DHTNode>> nodes;
    getRoutingTable()->getClosestKNodes(nodes, targetID_);
    entries_.clear();
    toEntries(entries_, nodes);
    if(entries_.empty()) {
      setFinished(true);
    } else {
      // TODO use RTT here
      inFlightMessage_ = 0;
      sendMessage();
      if(inFlightMessage_ == 0) {
        A2_LOG_DEBUG("No message was sent in this lookup stage. Finished.");
        setFinished(true);
      }
    }
  }

  void onReceived(const ResponseMessage* message)
  {
    --inFlightMessage_;
    // Replace old Node ID with new Node ID.
    for(auto& entry : entries_) {
      if(entry->node->getIPAddress() == message->getRemoteNode()->getIPAddress()
         && entry->node->getPort() == message->getRemoteNode()->getPort()) {
        entry->node = message->getRemoteNode();
      }
    }
    onReceivedInternal(message);
    std::vector<std::shared_ptr<DHTNode>> nodes;
    getNodesFromMessage(nodes, message);
    std::vector<std::unique_ptr<DHTNodeLookupEntry>> newEntries;
    toEntries(newEntries, nodes);

    size_t count = 0;
    for(auto& ne : newEntries) {
      if(memcmp(getLocalNode()->getID(), ne->node->getID(),
                DHT_ID_LENGTH) != 0) {
        A2_LOG_DEBUG(fmt("Received nodes: id=%s, ip=%s",
                         util::toHex(ne->node->getID(),
                                     DHT_ID_LENGTH).c_str(),
                         ne->node->getIPAddress().c_str()));
        entries_.push_front(std::move(ne));
        ++count;
      }
    }
    A2_LOG_DEBUG(fmt("%lu node lookup entries added.",
                     static_cast<unsigned long>(count)));
    std::stable_sort(std::begin(entries_), std::end(entries_),
                     DHTIDCloser(targetID_));
    entries_.erase
      (std::unique(std::begin(entries_), std::end(entries_),
                   DerefEqualTo<std::unique_ptr<DHTNodeLookupEntry>>{}),
       std::end(entries_));
    A2_LOG_DEBUG(fmt("%lu node lookup entries are unique.",
                     static_cast<unsigned long>(entries_.size())));
    if(entries_.size() > DHTBucket::K) {
      entries_.erase(std::begin(entries_)+DHTBucket::K, std::end(entries_));
    }
    sendMessageAndCheckFinish();
  }

  void onTimeout(const std::shared_ptr<DHTNode>& node)
  {
    A2_LOG_DEBUG(fmt("node lookup message timeout for node ID=%s",
                     util::toHex(node->getID(), DHT_ID_LENGTH).c_str()));
    --inFlightMessage_;
    for(auto i = std::begin(entries_), eoi = std::end(entries_);
        i != eoi; ++i) {
      if(*(*i)->node == *node) {
        entries_.erase(i);
        break;
      }
    }
    sendMessageAndCheckFinish();
  }
};

} // namespace aria2

#endif // D_DHT_ABSTRACT_NODE_LOOKUP_TASK_H