Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Jeff Thompson | ea141d7 | 2013-09-19 14:40:10 -0700 | [diff] [blame] | 8 | #include <stdexcept> |
Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 9 | #include "c/util/time.h" |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 10 | |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 11 | #include <ndn-cpp/forwarding-entry.hpp> |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 12 | #include <ndn-cpp/node.hpp> |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 13 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 14 | #include "util/ndnd-id-fetcher.hpp" |
Alexander Afanasyev | 7910049 | 2014-01-03 15:35:38 -0800 | [diff] [blame] | 15 | #include "security/signature/signature-sha256-with-rsa.hpp" |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 16 | |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 17 | #include "status-response.hpp" |
| 18 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 19 | using namespace std; |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 20 | |
| 21 | namespace ndn { |
| 22 | |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 23 | uint64_t Node::PendingInterest::lastPendingInterestId_ = 0; |
| 24 | uint64_t Node::RegisteredPrefix::lastRegisteredPrefixId_ = 0; |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 0b688dc | 2013-12-18 16:43:37 -0800 | [diff] [blame] | 26 | Node::Node(const ptr_lib::shared_ptr<Transport>& transport) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 27 | : timer_ (ioService_) |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 28 | , processEventsTimeoutTimer_(ioService_) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 29 | , transport_(transport) |
| 30 | , ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 31 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 32 | timer_.expires_from_now(boost::posix_time::milliseconds(100)); |
| 33 | timer_.async_wait(func_lib::bind(&Node::checkPitExpire, this)); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 36 | uint64_t |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 37 | Node::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 38 | { |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 39 | // TODO: Properly check if we are already connected to the expected host. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 40 | if (!transport_->isConnected()) |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 41 | transport_->connect(ioService_, |
| 42 | ptr_lib::bind(&Node::onReceiveElement, this, _1), |
| 43 | ptr_lib::bind(&Node::onTransportError, this)); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 44 | |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 45 | uint64_t pendingInterestId = PendingInterest::getNextPendingInterestId(); |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 46 | pendingInterestTable_.push_back(ptr_lib::shared_ptr<PendingInterest>(new PendingInterest |
| 47 | (pendingInterestId, ptr_lib::shared_ptr<const Interest>(new Interest(interest)), onData, onTimeout))); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 48 | |
| 49 | transport_->send(interest.wireEncode()); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 50 | |
| 51 | return pendingInterestId; |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 54 | void |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 55 | Node::put(const Data &data) |
| 56 | { |
| 57 | // TODO: Properly check if we are already connected to the expected host. |
| 58 | if (!transport_->isConnected()) |
| 59 | transport_->connect(ioService_, |
| 60 | ptr_lib::bind(&Node::onReceiveElement, this, _1), |
| 61 | ptr_lib::bind(&Node::onTransportError, this)); |
| 62 | |
| 63 | transport_->send(data.wireEncode()); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | void |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 68 | Node::removePendingInterest(uint64_t pendingInterestId) |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 69 | { |
| 70 | // Go backwards through the list so we can erase entries. |
| 71 | // Remove all entries even though pendingInterestId should be unique. |
| 72 | for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) { |
| 73 | if (pendingInterestTable_[i]->getPendingInterestId() == pendingInterestId) |
| 74 | pendingInterestTable_.erase(pendingInterestTable_.begin() + i); |
| 75 | } |
| 76 | } |
| 77 | |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 78 | uint64_t |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame] | 79 | Node::registerPrefix |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 80 | (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 81 | { |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 82 | // Get the registeredPrefixId now so we can return it to the caller. |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 83 | uint64_t registeredPrefixId = RegisteredPrefix::getNextRegisteredPrefixId(); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 84 | ptr_lib::shared_ptr<const Name> prefixPtr = ptr_lib::make_shared<const Name>(prefix); |
| 85 | |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 86 | if (ndndId_.size() == 0) { |
| 87 | // First fetch the ndndId of the connected hub. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 88 | NdndIdFetcher fetcher(ndndId_, |
| 89 | func_lib::bind(&Node::registerPrefixHelper, this, |
| 90 | registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags), |
| 91 | func_lib::bind(onRegisterFailed, prefixPtr)); |
| 92 | |
| 93 | // @todo: Check if this crash |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 94 | // It is OK for func_lib::function make a copy of the function object because the Info is in a ptr_lib::shared_ptr. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 95 | expressInterest(ndndIdFetcherInterest_, fetcher, fetcher); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 96 | } |
| 97 | else |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 98 | registerPrefixHelper(registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 99 | |
| 100 | return registeredPrefixId; |
| 101 | } |
| 102 | |
| 103 | void |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 104 | Node::removeRegisteredPrefix(uint64_t registeredPrefixId) |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 105 | { |
| 106 | // Go backwards through the list so we can erase entries. |
| 107 | // Remove all entries even though pendingInterestId should be unique. |
| 108 | for (int i = (int)registeredPrefixTable_.size() - 1; i >= 0; --i) { |
| 109 | if (registeredPrefixTable_[i]->getRegisteredPrefixId() == registeredPrefixId) |
| 110 | registeredPrefixTable_.erase(registeredPrefixTable_.begin() + i); |
| 111 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 114 | void |
Alexander Afanasyev | 7910049 | 2014-01-03 15:35:38 -0800 | [diff] [blame] | 115 | Node::registerPrefixHelper(uint64_t registeredPrefixId, |
| 116 | const ptr_lib::shared_ptr<const Name>& prefix, |
| 117 | const OnInterest& onInterest, |
| 118 | const OnRegisterFailed& onRegisterFailed, |
| 119 | const ForwardingFlags& flags) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 120 | { |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 121 | // Create a ForwardingEntry. |
Alexander Afanasyev | fbdfa09 | 2013-12-28 20:44:49 -0800 | [diff] [blame] | 122 | |
| 123 | // AlexA: ndnd ignores any freshness that is larger than 3600 sec and sets 300 sec instead |
| 124 | // to register "forever" (=2000000000 sec), freshnessPeriod must be omitted |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 125 | ForwardingEntry forwardingEntry("selfreg", *prefix, -1, flags, -1); |
| 126 | Block content = forwardingEntry.wireEncode(); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 127 | |
| 128 | // Set the ForwardingEntry as the content of a Data packet and sign. |
| 129 | Data data; |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 130 | data.setContent(content); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 131 | |
Alexander Afanasyev | 7910049 | 2014-01-03 15:35:38 -0800 | [diff] [blame] | 132 | // Create an empty signature, since nobody going to verify it for now |
| 133 | // @todo In the future, we may require real signatures to do the registration |
| 134 | SignatureSha256WithRsa signature; |
| 135 | signature.setValue(Block(Tlv::SignatureValue, ptr_lib::make_shared<Buffer>())); |
| 136 | data.setSignature(signature); |
| 137 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 138 | // Create an interest where the name has the encoded Data packet. |
| 139 | Name interestName; |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 140 | interestName.append("ndnx"); |
Jeff Thompson | 3a71563 | 2013-10-31 11:36:35 -0700 | [diff] [blame] | 141 | interestName.append(ndndId_); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 142 | interestName.append("selfreg"); |
Alexander Afanasyev | 7910049 | 2014-01-03 15:35:38 -0800 | [diff] [blame] | 143 | interestName.append(data.wireEncode()); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 144 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 145 | Interest interest(interestName); |
| 146 | interest.setScope(1); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 147 | interest.setInterestLifetime(1000); |
| 148 | |
| 149 | expressInterest(interest, |
| 150 | func_lib::bind(&Node::registerPrefixFinal, this, |
| 151 | registeredPrefixId, prefix, onInterest, onRegisterFailed, _1, _2), |
| 152 | func_lib::bind(onRegisterFailed, prefix)); |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | Node::registerPrefixFinal(uint64_t registeredPrefixId, |
| 157 | const ptr_lib::shared_ptr<const Name>& prefix, |
| 158 | const OnInterest& onInterest, |
| 159 | const OnRegisterFailed& onRegisterFailed, |
| 160 | const ptr_lib::shared_ptr<const Interest>&, const ptr_lib::shared_ptr<Data>&data) |
| 161 | { |
| 162 | Block content = data->getContent(); |
| 163 | content.parse(); |
| 164 | |
| 165 | if (content.getAll().empty()) |
| 166 | { |
| 167 | onRegisterFailed(prefix); |
| 168 | return; |
| 169 | } |
| 170 | |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 171 | Block::element_iterator val = content.getAll().begin(); |
| 172 | |
| 173 | switch(val->type()) |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 174 | { |
| 175 | case Tlv::FaceManagement::ForwardingEntry: |
| 176 | { |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 177 | ForwardingEntry entry; |
| 178 | entry.wireDecode(*val); |
| 179 | |
| 180 | // Save the onInterest callback and send the registration interest. |
| 181 | registeredPrefixTable_.push_back(ptr_lib::make_shared<RegisteredPrefix>(registeredPrefixId, prefix, onInterest)); |
| 182 | |
| 183 | /// @todo Notify user about successful registration |
| 184 | |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 185 | // succeeded |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 186 | return; |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 187 | } |
| 188 | case Tlv::FaceManagement::StatusResponse: |
| 189 | { |
| 190 | // failed :( |
| 191 | StatusResponse resp; |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 192 | resp.wireDecode(*val); |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 193 | |
| 194 | std::cerr << "StatusReponse: " << resp << std::endl; |
| 195 | |
| 196 | onRegisterFailed(prefix); |
| 197 | return; |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 198 | } |
| 199 | default: |
| 200 | { |
| 201 | // failed :( |
| 202 | |
| 203 | onRegisterFailed(prefix); |
| 204 | return; |
Alexander Afanasyev | 1837187 | 2014-01-05 23:00:26 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 209 | void |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 210 | Node::processEvents(Milliseconds timeout/* = 0 */) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 211 | { |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 212 | if (timeout > 0) |
| 213 | { |
| 214 | processEventsTimeoutTimer_.expires_from_now(boost::posix_time::milliseconds(timeout)); |
| 215 | processEventsTimeoutTimer_.async_wait(func_lib::bind(&Node::shutdown, this)); |
| 216 | } |
| 217 | try |
| 218 | { |
| 219 | ioService_.run(); |
| 220 | ioService_.reset(); |
| 221 | } |
| 222 | catch(Node::Error &) |
| 223 | { |
| 224 | ioService_.reset(); // this needed in order to call ioService_.run() again in the future |
| 225 | throw; |
| 226 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void |
| 230 | Node::checkPitExpire() |
| 231 | { |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 232 | // Check for PIT entry timeouts. Go backwards through the list so we can erase entries. |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 233 | MillisecondsSince1970 nowMilliseconds = ndn_getNowMilliseconds(); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 234 | for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) { |
Jeff Thompson | 3b0ed53 | 2013-11-05 13:43:40 -0800 | [diff] [blame] | 235 | if (pendingInterestTable_[i]->isTimedOut(nowMilliseconds)) { |
| 236 | // Save the PendingInterest and remove it from the PIT. Then call the callback. |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 237 | ptr_lib::shared_ptr<PendingInterest> pendingInterest = pendingInterestTable_[i]; |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 238 | pendingInterestTable_.erase(pendingInterestTable_.begin() + i); |
Jeff Thompson | 3b0ed53 | 2013-11-05 13:43:40 -0800 | [diff] [blame] | 239 | pendingInterest->callTimeout(); |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 240 | |
| 241 | // Refresh now since the timeout callback might have delayed. |
Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 242 | nowMilliseconds = ndn_getNowMilliseconds(); |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 245 | |
| 246 | timer_.expires_from_now(boost::posix_time::milliseconds(100)); |
| 247 | timer_.async_wait(func_lib::bind(&Node::checkPitExpire, this)); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 250 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 251 | void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 252 | Node::onReceiveElement(const Block &block) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 253 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 254 | if (block.type() == Tlv::Interest) |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 255 | { |
| 256 | ptr_lib::shared_ptr<Interest> interest(new Interest()); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 257 | interest->wireDecode(block); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 258 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 259 | RegisteredPrefixTable::iterator entry = getEntryForRegisteredPrefix(interest->getName()); |
| 260 | if (entry != registeredPrefixTable_.end()) { |
| 261 | (*entry)->getOnInterest()((*entry)->getPrefix(), interest, *transport_, (*entry)->getRegisteredPrefixId()); |
| 262 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 263 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 264 | else if (block.type() == Tlv::Data) |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 265 | { |
| 266 | ptr_lib::shared_ptr<Data> data(new Data()); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 267 | data->wireDecode(block); |
| 268 | |
| 269 | PendingInterestTable::iterator entry = getEntryIndexForExpressedInterest(data->getName()); |
| 270 | if (entry != pendingInterestTable_.end()) { |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 271 | // Copy pointers to the needed objects and remove the PIT entry before the calling the callback. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 272 | const OnData onData = (*entry)->getOnData(); |
| 273 | const ptr_lib::shared_ptr<const Interest> interest = (*entry)->getInterest(); |
| 274 | pendingInterestTable_.erase(entry); |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 275 | onData(interest, data); |
| 276 | } |
| 277 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 280 | void |
| 281 | Node::onTransportError() |
| 282 | { |
| 283 | /// @todo Set some error code |
| 284 | |
| 285 | ioService_.stop(); |
| 286 | throw Error("TransportError"); |
| 287 | } |
| 288 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 289 | void |
| 290 | Node::shutdown() |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 291 | { |
| 292 | transport_->close(); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 293 | ioService_.stop(); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 296 | Node::PendingInterestTable::iterator |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 297 | Node::getEntryIndexForExpressedInterest(const Name& name) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 298 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 299 | for (PendingInterestTable::iterator i = pendingInterestTable_.begin (); |
| 300 | i != pendingInterestTable_.end(); ++i) |
| 301 | { |
| 302 | if ((*i)->getInterest()->matchesName(name)) |
| 303 | { |
| 304 | return i; |
| 305 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 306 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 307 | |
| 308 | return pendingInterestTable_.end(); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 309 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 310 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 311 | Node::RegisteredPrefixTable::iterator |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 312 | Node::getEntryForRegisteredPrefix(const Name& name) |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 313 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 314 | RegisteredPrefixTable::iterator longestPrefix = registeredPrefixTable_.end(); |
| 315 | |
| 316 | for (RegisteredPrefixTable::iterator i = registeredPrefixTable_.begin(); |
| 317 | i != registeredPrefixTable_.end(); |
| 318 | ++i) |
| 319 | { |
| 320 | if (longestPrefix == registeredPrefixTable_.end() || |
| 321 | (*i)->getPrefix()->size() > (*longestPrefix)->getPrefix()->size()) |
| 322 | { |
| 323 | longestPrefix = i; |
| 324 | } |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 325 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 326 | return longestPrefix; |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 329 | Node::PendingInterest::PendingInterest(uint64_t pendingInterestId, |
| 330 | const ptr_lib::shared_ptr<const Interest>& interest, |
| 331 | const OnData& onData, const OnTimeout& onTimeout) |
| 332 | : pendingInterestId_(pendingInterestId), |
| 333 | interest_(interest), |
| 334 | onData_(onData), onTimeout_(onTimeout) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 335 | { |
| 336 | // Set up timeoutTime_. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 337 | if (interest_->getInterestLifetime() >= 0) |
| 338 | timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + interest_->getInterestLifetime(); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 339 | else |
| 340 | // No timeout. |
Alexander Afanasyev | b24a68a | 2013-12-28 16:53:21 -0800 | [diff] [blame] | 341 | /** |
| 342 | * @todo Set more meaningful default timeout. This timeout MUST exist. |
| 343 | */ |
| 344 | timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + 4000; |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Jeff Thompson | 3b0ed53 | 2013-11-05 13:43:40 -0800 | [diff] [blame] | 347 | void |
| 348 | Node::PendingInterest::callTimeout() |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 349 | { |
Jeff Thompson | 3b0ed53 | 2013-11-05 13:43:40 -0800 | [diff] [blame] | 350 | if (onTimeout_) { |
| 351 | // Ignore all exceptions. |
| 352 | try { |
| 353 | onTimeout_(interest_); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 354 | } |
Jeff Thompson | 3b0ed53 | 2013-11-05 13:43:40 -0800 | [diff] [blame] | 355 | catch (...) { } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 356 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 357 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 358 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 359 | } |