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" |
| 15 | |
| 16 | #include <ndn-cpp/encoding/block.hpp> |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 17 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 18 | using namespace std; |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 19 | |
| 20 | namespace ndn { |
| 21 | |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 22 | uint64_t Node::PendingInterest::lastPendingInterestId_ = 0; |
| 23 | uint64_t Node::RegisteredPrefix::lastRegisteredPrefixId_ = 0; |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 0b688dc | 2013-12-18 16:43:37 -0800 | [diff] [blame] | 25 | Node::Node(const ptr_lib::shared_ptr<Transport>& transport) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 26 | : timer_ (ioService_) |
| 27 | , transport_(transport) |
| 28 | , 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] | 29 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 30 | timer_.expires_from_now(boost::posix_time::milliseconds(100)); |
| 31 | timer_.async_wait(func_lib::bind(&Node::checkPitExpire, this)); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 34 | uint64_t |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 35 | Node::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 36 | { |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 37 | // TODO: Properly check if we are already connected to the expected host. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 38 | if (!transport_->isConnected()) |
| 39 | transport_->connect(ioService_, ptr_lib::bind(&Node::onReceiveElement, this, _1)); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 40 | |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 41 | uint64_t pendingInterestId = PendingInterest::getNextPendingInterestId(); |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 42 | pendingInterestTable_.push_back(ptr_lib::shared_ptr<PendingInterest>(new PendingInterest |
| 43 | (pendingInterestId, ptr_lib::shared_ptr<const Interest>(new Interest(interest)), onData, onTimeout))); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 44 | |
| 45 | transport_->send(interest.wireEncode()); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 46 | |
| 47 | return pendingInterestId; |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 50 | void |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 51 | Node::removePendingInterest(uint64_t pendingInterestId) |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 52 | { |
| 53 | // Go backwards through the list so we can erase entries. |
| 54 | // Remove all entries even though pendingInterestId should be unique. |
| 55 | for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) { |
| 56 | if (pendingInterestTable_[i]->getPendingInterestId() == pendingInterestId) |
| 57 | pendingInterestTable_.erase(pendingInterestTable_.begin() + i); |
| 58 | } |
| 59 | } |
| 60 | |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 61 | uint64_t |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame] | 62 | Node::registerPrefix |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 63 | (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 64 | { |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 65 | // Get the registeredPrefixId now so we can return it to the caller. |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 66 | uint64_t registeredPrefixId = RegisteredPrefix::getNextRegisteredPrefixId(); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 67 | ptr_lib::shared_ptr<const Name> prefixPtr = ptr_lib::make_shared<const Name>(prefix); |
| 68 | |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 69 | if (ndndId_.size() == 0) { |
| 70 | // First fetch the ndndId of the connected hub. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 71 | NdndIdFetcher fetcher(ndndId_, |
| 72 | func_lib::bind(&Node::registerPrefixHelper, this, |
| 73 | registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags), |
| 74 | func_lib::bind(onRegisterFailed, prefixPtr)); |
| 75 | |
| 76 | // @todo: Check if this crash |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 77 | // 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^] | 78 | expressInterest(ndndIdFetcherInterest_, fetcher, fetcher); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 79 | } |
| 80 | else |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 81 | registerPrefixHelper(registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 82 | |
| 83 | return registeredPrefixId; |
| 84 | } |
| 85 | |
| 86 | void |
Jeff Thompson | 62992e4 | 2013-10-07 18:50:51 -0700 | [diff] [blame] | 87 | Node::removeRegisteredPrefix(uint64_t registeredPrefixId) |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 88 | { |
| 89 | // Go backwards through the list so we can erase entries. |
| 90 | // Remove all entries even though pendingInterestId should be unique. |
| 91 | for (int i = (int)registeredPrefixTable_.size() - 1; i >= 0; --i) { |
| 92 | if (registeredPrefixTable_[i]->getRegisteredPrefixId() == registeredPrefixId) |
| 93 | registeredPrefixTable_.erase(registeredPrefixTable_.begin() + i); |
| 94 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 97 | void |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame] | 98 | Node::registerPrefixHelper |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 99 | (uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 100 | const ForwardingFlags& flags) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 101 | { |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 102 | // Create a ForwardingEntry. |
Alexander Afanasyev | fbdfa09 | 2013-12-28 20:44:49 -0800 | [diff] [blame] | 103 | |
| 104 | // AlexA: ndnd ignores any freshness that is larger than 3600 sec and sets 300 sec instead |
| 105 | // to register "forever" (=2000000000 sec), freshnessPeriod must be omitted |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 106 | ForwardingEntry forwardingEntry("selfreg", *prefix, -1, flags, -1); |
| 107 | Block content = forwardingEntry.wireEncode(); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 108 | |
| 109 | // Set the ForwardingEntry as the content of a Data packet and sign. |
| 110 | Data data; |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 111 | data.setContent(content); |
Jeff Thompson | ea141d7 | 2013-09-19 14:40:10 -0700 | [diff] [blame] | 112 | // For now, self sign with an arbirary key. In the future, we may not require a signature to register. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 113 | // selfregSign(data); |
| 114 | const Block &encodedData = data.wireEncode(); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 115 | |
| 116 | // Create an interest where the name has the encoded Data packet. |
| 117 | Name interestName; |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 118 | const uint8_t component0[] = "ndnx"; |
| 119 | const uint8_t component2[] = "selfreg"; |
Jeff Thompson | 3a71563 | 2013-10-31 11:36:35 -0700 | [diff] [blame] | 120 | interestName.append(component0, sizeof(component0) - 1); |
| 121 | interestName.append(ndndId_); |
| 122 | interestName.append(component2, sizeof(component2) - 1); |
| 123 | interestName.append(encodedData); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 124 | |
| 125 | Interest interest(interestName); |
| 126 | interest.setScope(1); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 127 | |
| 128 | // Save the onInterest callback and send the registration interest. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 129 | registeredPrefixTable_.push_back(ptr_lib::make_shared<RegisteredPrefix>(registeredPrefixId, prefix, onInterest)); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 130 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 131 | transport_->send(interest.wireEncode()); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 134 | void |
| 135 | Node::processEvents() |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 136 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 137 | ioService_.run(); |
| 138 | |
| 139 | // auto_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(ioService_)); |
| 140 | // work.reset(); // Allow run() to exit. |
| 141 | } |
| 142 | |
| 143 | void |
| 144 | Node::checkPitExpire() |
| 145 | { |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 146 | // 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] | 147 | MillisecondsSince1970 nowMilliseconds = ndn_getNowMilliseconds(); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 148 | for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) { |
Jeff Thompson | 3b0ed53 | 2013-11-05 13:43:40 -0800 | [diff] [blame] | 149 | if (pendingInterestTable_[i]->isTimedOut(nowMilliseconds)) { |
| 150 | // 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] | 151 | ptr_lib::shared_ptr<PendingInterest> pendingInterest = pendingInterestTable_[i]; |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 152 | pendingInterestTable_.erase(pendingInterestTable_.begin() + i); |
Jeff Thompson | 3b0ed53 | 2013-11-05 13:43:40 -0800 | [diff] [blame] | 153 | pendingInterest->callTimeout(); |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 154 | |
| 155 | // Refresh now since the timeout callback might have delayed. |
Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 156 | nowMilliseconds = ndn_getNowMilliseconds(); |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 159 | |
| 160 | timer_.expires_from_now(boost::posix_time::milliseconds(100)); |
| 161 | timer_.async_wait(func_lib::bind(&Node::checkPitExpire, this)); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 164 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 165 | void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 166 | Node::onReceiveElement(const Block &block) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 167 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 168 | if (block.type() == Tlv::Interest) |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 169 | { |
| 170 | ptr_lib::shared_ptr<Interest> interest(new Interest()); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 171 | interest->wireDecode(block); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 172 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 173 | RegisteredPrefixTable::iterator entry = getEntryForRegisteredPrefix(interest->getName()); |
| 174 | if (entry != registeredPrefixTable_.end()) { |
| 175 | (*entry)->getOnInterest()((*entry)->getPrefix(), interest, *transport_, (*entry)->getRegisteredPrefixId()); |
| 176 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 177 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 178 | else if (block.type() == Tlv::Data) |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 179 | { |
| 180 | ptr_lib::shared_ptr<Data> data(new Data()); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 181 | data->wireDecode(block); |
| 182 | |
| 183 | PendingInterestTable::iterator entry = getEntryIndexForExpressedInterest(data->getName()); |
| 184 | if (entry != pendingInterestTable_.end()) { |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 185 | // 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^] | 186 | const OnData onData = (*entry)->getOnData(); |
| 187 | const ptr_lib::shared_ptr<const Interest> interest = (*entry)->getInterest(); |
| 188 | pendingInterestTable_.erase(entry); |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 189 | onData(interest, data); |
| 190 | } |
| 191 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 194 | void |
| 195 | Node::shutdown() |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 196 | { |
| 197 | transport_->close(); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 198 | ioService_.stop(); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 201 | Node::PendingInterestTable::iterator |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 202 | Node::getEntryIndexForExpressedInterest(const Name& name) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 203 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 204 | for (PendingInterestTable::iterator i = pendingInterestTable_.begin (); |
| 205 | i != pendingInterestTable_.end(); ++i) |
| 206 | { |
| 207 | if ((*i)->getInterest()->matchesName(name)) |
| 208 | { |
| 209 | return i; |
| 210 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 211 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 212 | |
| 213 | return pendingInterestTable_.end(); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 214 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 215 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 216 | Node::RegisteredPrefixTable::iterator |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 217 | Node::getEntryForRegisteredPrefix(const Name& name) |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 218 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 219 | RegisteredPrefixTable::iterator longestPrefix = registeredPrefixTable_.end(); |
| 220 | |
| 221 | for (RegisteredPrefixTable::iterator i = registeredPrefixTable_.begin(); |
| 222 | i != registeredPrefixTable_.end(); |
| 223 | ++i) |
| 224 | { |
| 225 | if (longestPrefix == registeredPrefixTable_.end() || |
| 226 | (*i)->getPrefix()->size() > (*longestPrefix)->getPrefix()->size()) |
| 227 | { |
| 228 | longestPrefix = i; |
| 229 | } |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 230 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 231 | return longestPrefix; |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 234 | Node::PendingInterest::PendingInterest(uint64_t pendingInterestId, |
| 235 | const ptr_lib::shared_ptr<const Interest>& interest, |
| 236 | const OnData& onData, const OnTimeout& onTimeout) |
| 237 | : pendingInterestId_(pendingInterestId), |
| 238 | interest_(interest), |
| 239 | onData_(onData), onTimeout_(onTimeout) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 240 | { |
| 241 | // Set up timeoutTime_. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 242 | if (interest_->getInterestLifetime() >= 0) |
| 243 | timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + interest_->getInterestLifetime(); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 244 | else |
| 245 | // No timeout. |
Alexander Afanasyev | ee94031 | 2013-12-24 19:43:15 -0800 | [diff] [blame] | 246 | timeoutTimeMilliseconds_ = -1; |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Jeff Thompson | 3b0ed53 | 2013-11-05 13:43:40 -0800 | [diff] [blame] | 249 | void |
| 250 | Node::PendingInterest::callTimeout() |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 251 | { |
Jeff Thompson | 3b0ed53 | 2013-11-05 13:43:40 -0800 | [diff] [blame] | 252 | if (onTimeout_) { |
| 253 | // Ignore all exceptions. |
| 254 | try { |
| 255 | onTimeout_(interest_); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 256 | } |
Jeff Thompson | 3b0ed53 | 2013-11-05 13:43:40 -0800 | [diff] [blame] | 257 | catch (...) { } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 258 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 259 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 260 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 261 | } |