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 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 8 | #include "common.hpp" |
| 9 | |
Alexander Afanasyev | e9fdb80 | 2014-02-05 17:36:51 -0800 | [diff] [blame] | 10 | #include "node.hpp" |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 11 | |
Alexander Afanasyev | e9fdb80 | 2014-02-05 17:36:51 -0800 | [diff] [blame] | 12 | #include "security/signature-sha256-with-rsa.hpp" |
| 13 | |
| 14 | #include "util/time.hpp" |
| 15 | #include "util/random.hpp" |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 16 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 17 | #include "management/ndnd-controller.hpp" |
| 18 | #include "management/nfd-controller.hpp" |
| 19 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 20 | namespace ndn { |
| 21 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 22 | Node::Node(const shared_ptr<Transport>& transport, bool nfdMode/* = false*/) |
Alexander Afanasyev | e1b7a5d | 2013-12-29 16:23:52 -0800 | [diff] [blame] | 23 | { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 24 | construct(transport, make_shared<boost::asio::io_service>(), nfdMode); |
Alexander Afanasyev | e1b7a5d | 2013-12-29 16:23:52 -0800 | [diff] [blame] | 25 | } |
| 26 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 27 | Node::Node(const shared_ptr<Transport>& transport, |
| 28 | const shared_ptr<boost::asio::io_service> &ioService, |
| 29 | bool nfdMode/* = false*/) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 30 | { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 31 | construct(transport, ioService, nfdMode); |
| 32 | } |
| 33 | |
| 34 | void |
| 35 | Node::construct(const shared_ptr<Transport>& transport, |
| 36 | const shared_ptr<boost::asio::io_service> &ioService, |
| 37 | bool nfdMode) |
| 38 | { |
| 39 | pitTimeoutCheckTimerActive_ = false; |
| 40 | transport_ = transport; |
| 41 | ioService_ = ioService; |
| 42 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 43 | pitTimeoutCheckTimer_ = make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_)); |
| 44 | processEventsTimeoutTimer_ = make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_)); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 45 | |
| 46 | if (nfdMode) |
| 47 | m_fwController = make_shared<nfd::Controller>(boost::ref(*this)); |
| 48 | else |
| 49 | m_fwController = make_shared<ndnd::Controller>(boost::ref(*this)); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 52 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 53 | const PendingInterestId* |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 54 | Node::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 55 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 56 | if (!transport_->isConnected()) |
Alexander Afanasyev | e1b7a5d | 2013-12-29 16:23:52 -0800 | [diff] [blame] | 57 | transport_->connect(*ioService_, |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 58 | bind(&Node::onReceiveElement, this, _1)); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 59 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 60 | shared_ptr<const Interest> interestToExpress(new Interest(interest)); |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 62 | ioService_->post(bind(&Node::asyncExpressInterest, this, interestToExpress, onData, onTimeout)); |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 63 | |
| 64 | return reinterpret_cast<const PendingInterestId*>(interestToExpress.get()); |
| 65 | } |
| 66 | |
| 67 | void |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 68 | Node::asyncExpressInterest(const shared_ptr<const Interest> &interest, |
| 69 | const OnData& onData, const OnTimeout& onTimeout) |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 70 | { |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 71 | pendingInterestTable_.push_back(shared_ptr<PendingInterest>(new PendingInterest |
| 72 | (interest, onData, onTimeout))); |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 73 | |
| 74 | transport_->send(interest->wireEncode()); |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 75 | |
| 76 | if (!pitTimeoutCheckTimerActive_) { |
| 77 | pitTimeoutCheckTimerActive_ = true; |
| 78 | pitTimeoutCheckTimer_->expires_from_now(boost::posix_time::milliseconds(100)); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 79 | pitTimeoutCheckTimer_->async_wait(bind(&Node::checkPitExpire, this)); |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 80 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 81 | } |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 82 | |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 83 | void |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 84 | Node::put(const Data &data) |
| 85 | { |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 86 | if (!transport_->isConnected()) |
Alexander Afanasyev | e1b7a5d | 2013-12-29 16:23:52 -0800 | [diff] [blame] | 87 | transport_->connect(*ioService_, |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 88 | bind(&Node::onReceiveElement, this, _1)); |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 89 | |
| 90 | transport_->send(data.wireEncode()); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | void |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 95 | Node::removePendingInterest(const PendingInterestId *pendingInterestId) |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 96 | { |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 97 | ioService_->post(bind(&Node::asyncRemovePendingInterest, this, pendingInterestId)); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 100 | |
| 101 | void |
| 102 | Node::asyncRemovePendingInterest(const PendingInterestId *pendingInterestId) |
| 103 | { |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 104 | pendingInterestTable_.remove_if(MatchPendingInterestId(pendingInterestId)); |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | const RegisteredPrefixId* |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 108 | Node::setInterestFilter(const Name& prefix, |
| 109 | const OnInterest& onInterest, |
| 110 | const OnSetInterestFilterFailed& onSetInterestFilterFailed) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 111 | { |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 112 | shared_ptr<RegisteredPrefix> prefixToRegister(new RegisteredPrefix(prefix, onInterest)); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 114 | m_fwController->selfRegisterPrefix(prefixToRegister->getPrefix(), |
| 115 | bind(&RegisteredPrefixTable::push_back, ®isteredPrefixTable_, prefixToRegister), |
| 116 | bind(onSetInterestFilterFailed, prefixToRegister->getPrefix(), _1)); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 117 | |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 118 | return reinterpret_cast<const RegisteredPrefixId*>(prefixToRegister.get()); |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 122 | Node::unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId) |
| 123 | { |
| 124 | ioService_->post(bind(&Node::asyncUnsetInterestFilter, this, registeredPrefixId)); |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | Node::asyncUnsetInterestFilter(const RegisteredPrefixId *registeredPrefixId) |
Jeff Thompson | 1109514 | 2013-10-01 16:20:28 -0700 | [diff] [blame] | 129 | { |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 130 | RegisteredPrefixTable::iterator i = std::find_if(registeredPrefixTable_.begin(), registeredPrefixTable_.end(), |
| 131 | MatchRegisteredPrefixId(registeredPrefixId)); |
| 132 | if (i != registeredPrefixTable_.end()) |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 133 | { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame^] | 134 | m_fwController->selfDeregisterPrefix((*i)->getPrefix(), |
| 135 | bind(&RegisteredPrefixTable::erase, ®isteredPrefixTable_, i), |
| 136 | Controller::FailCallback()); |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 137 | } |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 138 | |
| 139 | // there cannot be two registered prefixes with the same id. if there are, then something is broken |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 142 | void |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 143 | Node::processEvents(Milliseconds timeout/* = 0 */, bool keepThread/* = false*/) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 144 | { |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 145 | try |
| 146 | { |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 147 | if (timeout < 0) |
| 148 | { |
| 149 | // do not block if timeout is negative, but process pending events |
| 150 | ioService_->poll(); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | if (timeout > 0) |
| 155 | { |
| 156 | processEventsTimeoutTimer_->expires_from_now(boost::posix_time::milliseconds(timeout)); |
Jeff Thompson | 6e9fc18 | 2014-01-23 13:04:08 -0800 | [diff] [blame] | 157 | processEventsTimeoutTimer_->async_wait(&fireProcessEventsTimeout); |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | if (keepThread) { |
| 161 | // work will ensure that ioService_ is running until work object exists |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 162 | ioServiceWork_ = make_shared<boost::asio::io_service::work>(boost::ref(*ioService_)); |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Alexander Afanasyev | e1b7a5d | 2013-12-29 16:23:52 -0800 | [diff] [blame] | 165 | ioService_->run(); |
Alexander Afanasyev | 8995f54 | 2014-01-17 15:33:44 -0800 | [diff] [blame] | 166 | ioService_->reset(); // so it is possible to run processEvents again (if necessary) |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 167 | } |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 168 | catch(Node::ProcessEventsTimeout &) |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 169 | { |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 170 | // break |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 171 | ioService_->reset(); |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 172 | } |
Alexander Afanasyev | 8995f54 | 2014-01-17 15:33:44 -0800 | [diff] [blame] | 173 | catch(const std::exception &) |
| 174 | { |
| 175 | ioService_->reset(); |
| 176 | pendingInterestTable_.clear(); |
| 177 | registeredPrefixTable_.clear(); |
| 178 | throw; |
| 179 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 182 | void |
| 183 | Node::shutdown() |
| 184 | { |
| 185 | pendingInterestTable_.clear(); |
| 186 | registeredPrefixTable_.clear(); |
| 187 | |
| 188 | transport_->close(); |
| 189 | pitTimeoutCheckTimer_->cancel(); |
| 190 | processEventsTimeoutTimer_->cancel(); |
| 191 | pitTimeoutCheckTimerActive_ = false; |
| 192 | } |
| 193 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 194 | void |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 195 | Node::fireProcessEventsTimeout(const boost::system::error_code& error) |
| 196 | { |
| 197 | if (!error) // can fire for some other reason, e.g., cancelled |
| 198 | throw Node::ProcessEventsTimeout(); |
| 199 | } |
| 200 | |
| 201 | void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 202 | Node::checkPitExpire() |
| 203 | { |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 204 | // Check for PIT entry timeouts. Go backwards through the list so we can erase entries. |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 205 | MillisecondsSince1970 nowMilliseconds = getNowMilliseconds(); |
| 206 | |
| 207 | PendingInterestTable::iterator i = pendingInterestTable_.begin(); |
| 208 | while (i != pendingInterestTable_.end()) |
| 209 | { |
| 210 | if ((*i)->isTimedOut(nowMilliseconds)) |
| 211 | { |
| 212 | // Save the PendingInterest and remove it from the PIT. Then call the callback. |
| 213 | shared_ptr<PendingInterest> pendingInterest = *i; |
| 214 | |
| 215 | i = pendingInterestTable_.erase(i); |
| 216 | |
| 217 | pendingInterest->callTimeout(); |
| 218 | } |
| 219 | else |
| 220 | ++i; |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 221 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 222 | |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 223 | if (!pendingInterestTable_.empty()) { |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 224 | pitTimeoutCheckTimerActive_ = true; |
| 225 | |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 226 | pitTimeoutCheckTimer_->expires_from_now(boost::posix_time::milliseconds(100)); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 227 | pitTimeoutCheckTimer_->async_wait(bind(&Node::checkPitExpire, this)); |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 228 | } |
| 229 | else { |
| 230 | pitTimeoutCheckTimerActive_ = false; |
| 231 | |
| 232 | if (registeredPrefixTable_.empty()) { |
| 233 | transport_->close(); |
Alexander Afanasyev | f75a0aa | 2014-01-09 14:29:22 -0800 | [diff] [blame] | 234 | if (!ioServiceWork_) { |
| 235 | processEventsTimeoutTimer_->cancel(); |
| 236 | } |
Alexander Afanasyev | bf08211 | 2014-01-09 14:27:55 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 241 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 242 | void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 243 | Node::onReceiveElement(const Block &block) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 244 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 245 | if (block.type() == Tlv::Interest) |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 246 | { |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 247 | shared_ptr<Interest> interest(new Interest()); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 248 | interest->wireDecode(block); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 249 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 250 | RegisteredPrefixTable::iterator entry = getEntryForRegisteredPrefix(interest->getName()); |
| 251 | if (entry != registeredPrefixTable_.end()) { |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 252 | (*entry)->getOnInterest()((*entry)->getPrefix().shared_from_this(), interest); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 253 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 254 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 255 | else if (block.type() == Tlv::Data) |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 256 | { |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 257 | shared_ptr<Data> data(new Data()); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 258 | data->wireDecode(block); |
| 259 | |
| 260 | PendingInterestTable::iterator entry = getEntryIndexForExpressedInterest(data->getName()); |
| 261 | if (entry != pendingInterestTable_.end()) { |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 262 | // 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] | 263 | const OnData onData = (*entry)->getOnData(); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 264 | const shared_ptr<const Interest> interest = (*entry)->getInterest(); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 265 | pendingInterestTable_.erase(entry); |
Alexander Afanasyev | bc343ef | 2014-01-09 22:36:20 -0800 | [diff] [blame] | 266 | |
| 267 | if (onData) { |
| 268 | onData(interest, data); |
| 269 | } |
| 270 | |
| 271 | if (pendingInterestTable_.empty()) { |
| 272 | pitTimeoutCheckTimer_->cancel(); // this will cause checkPitExpire invocation |
| 273 | } |
Alexander Afanasyev | 96d914f | 2014-01-02 22:24:29 -0800 | [diff] [blame] | 274 | } |
| 275 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 278 | Node::PendingInterestTable::iterator |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 279 | Node::getEntryIndexForExpressedInterest(const Name& name) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 280 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 281 | for (PendingInterestTable::iterator i = pendingInterestTable_.begin (); |
| 282 | i != pendingInterestTable_.end(); ++i) |
| 283 | { |
| 284 | if ((*i)->getInterest()->matchesName(name)) |
| 285 | { |
| 286 | return i; |
| 287 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 288 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 289 | |
| 290 | return pendingInterestTable_.end(); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 291 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 292 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 293 | Node::RegisteredPrefixTable::iterator |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 294 | Node::getEntryForRegisteredPrefix(const Name& name) |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 295 | { |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 296 | RegisteredPrefixTable::iterator longestPrefix = registeredPrefixTable_.end(); |
| 297 | |
| 298 | for (RegisteredPrefixTable::iterator i = registeredPrefixTable_.begin(); |
| 299 | i != registeredPrefixTable_.end(); |
| 300 | ++i) |
| 301 | { |
| 302 | if (longestPrefix == registeredPrefixTable_.end() || |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 303 | (*i)->getPrefix().size() > (*longestPrefix)->getPrefix().size()) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 304 | { |
| 305 | longestPrefix = i; |
| 306 | } |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 307 | } |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 308 | return longestPrefix; |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 311 | } // namespace ndn |