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