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