blob: 5dcbd7b86716eb2b675a89f9a66ba7b4cf72ca5b [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07005 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompsonea141d72013-09-19 14:40:10 -07008#include <stdexcept>
Jeff Thompson9ae4d782013-10-17 10:25:54 -07009#include "c/util/time.h"
Alexander Afanasyev96d914f2014-01-02 22:24:29 -080010
Jeff Thompson25b4e612013-10-10 16:03:24 -070011#include <ndn-cpp/forwarding-entry.hpp>
Alexander Afanasyev5d7db8e2014-01-05 22:43:57 -080012#include <ndn-cpp/face-instance.hpp>
Jeff Thompson25b4e612013-10-10 16:03:24 -070013#include <ndn-cpp/node.hpp>
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070014
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080015#include "util/ndnd-id-fetcher.hpp"
Alexander Afanasyev96d914f2014-01-02 22:24:29 -080016
Alexander Afanasyev6be1a6a2014-01-06 00:08:14 -080017#include <ndn-cpp/security/signature/signature-sha256-with-rsa.hpp>
18#include <ndn-cpp/status-response.hpp>
Alexander Afanasyev18371872014-01-05 23:00:26 -080019
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070020using namespace std;
Alexander Afanasyev6be1a6a2014-01-06 00:08:14 -080021#if NDN_CPP_HAVE_CXX11
22// In the std library, the placeholders are in a different namespace than boost.
23using namespace ndn::func_lib::placeholders;
24#endif
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070025
26namespace ndn {
27
Jeff Thompson62992e42013-10-07 18:50:51 -070028uint64_t Node::PendingInterest::lastPendingInterestId_ = 0;
29uint64_t Node::RegisteredPrefix::lastRegisteredPrefixId_ = 0;
Jeff Thompson11095142013-10-01 16:20:28 -070030
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080031Node::Node(const ptr_lib::shared_ptr<Transport>& transport)
Alexander Afanasyevbf082112014-01-09 14:27:55 -080032 : pitTimeoutCheckTimerActive_(false)
33 , transport_(transport)
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080034 , ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0)
35{
36 ioService_ = ptr_lib::make_shared<boost::asio::io_service>();
37 pitTimeoutCheckTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
38 processEventsTimeoutTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080039}
40
41Node::Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<boost::asio::io_service> &ioService)
42 : ioService_(ioService)
Alexander Afanasyevbf082112014-01-09 14:27:55 -080043 , pitTimeoutCheckTimerActive_(false)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080044 , transport_(transport)
45 , ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0)
Jeff Thompson557b81e2013-08-21 15:13:51 -070046{
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080047 pitTimeoutCheckTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
48 processEventsTimeoutTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
Jeff Thompson557b81e2013-08-21 15:13:51 -070049}
50
Jeff Thompson62992e42013-10-07 18:50:51 -070051uint64_t
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080052Node::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070053{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080054 if (!transport_->isConnected())
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080055 transport_->connect(*ioService_,
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080056 ptr_lib::bind(&Node::onReceiveElement, this, _1));
Jeff Thompson86507bc2013-08-23 20:51:38 -070057
Jeff Thompson62992e42013-10-07 18:50:51 -070058 uint64_t pendingInterestId = PendingInterest::getNextPendingInterestId();
Jeff Thompsonce115762013-12-18 14:59:56 -080059 pendingInterestTable_.push_back(ptr_lib::shared_ptr<PendingInterest>(new PendingInterest
60 (pendingInterestId, ptr_lib::shared_ptr<const Interest>(new Interest(interest)), onData, onTimeout)));
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080061
62 transport_->send(interest.wireEncode());
Alexander Afanasyevbf082112014-01-09 14:27:55 -080063
64 if (!pitTimeoutCheckTimerActive_) {
65 pitTimeoutCheckTimerActive_ = true;
66 pitTimeoutCheckTimer_->expires_from_now(boost::posix_time::milliseconds(100));
67 pitTimeoutCheckTimer_->async_wait(func_lib::bind(&Node::checkPitExpire, this));
68 }
Jeff Thompson11095142013-10-01 16:20:28 -070069
70 return pendingInterestId;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070071}
72
Jeff Thompson11095142013-10-01 16:20:28 -070073void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080074Node::put(const Data &data)
75{
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080076 if (!transport_->isConnected())
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080077 transport_->connect(*ioService_,
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080078 ptr_lib::bind(&Node::onReceiveElement, this, _1));
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080079
80 transport_->send(data.wireEncode());
81}
82
83
84void
Jeff Thompson62992e42013-10-07 18:50:51 -070085Node::removePendingInterest(uint64_t pendingInterestId)
Jeff Thompson11095142013-10-01 16:20:28 -070086{
87 // Go backwards through the list so we can erase entries.
88 // Remove all entries even though pendingInterestId should be unique.
89 for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) {
90 if (pendingInterestTable_[i]->getPendingInterestId() == pendingInterestId)
91 pendingInterestTable_.erase(pendingInterestTable_.begin() + i);
92 }
93}
94
Jeff Thompson62992e42013-10-07 18:50:51 -070095uint64_t
Jeff Thompson590ec232013-09-18 15:55:56 -070096Node::registerPrefix
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080097 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -070098{
Jeff Thompson11095142013-10-01 16:20:28 -070099 // Get the registeredPrefixId now so we can return it to the caller.
Jeff Thompson62992e42013-10-07 18:50:51 -0700100 uint64_t registeredPrefixId = RegisteredPrefix::getNextRegisteredPrefixId();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800101 ptr_lib::shared_ptr<const Name> prefixPtr = ptr_lib::make_shared<const Name>(prefix);
102
Jeff Thompson86507bc2013-08-23 20:51:38 -0700103 if (ndndId_.size() == 0) {
104 // First fetch the ndndId of the connected hub.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800105 NdndIdFetcher fetcher(ndndId_,
106 func_lib::bind(&Node::registerPrefixHelper, this,
107 registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags),
108 func_lib::bind(onRegisterFailed, prefixPtr));
109
110 // @todo: Check if this crash
Jeff Thompsonce115762013-12-18 14:59:56 -0800111 // 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 Afanasyeve2e0d752014-01-03 13:30:30 -0800112 expressInterest(ndndIdFetcherInterest_, fetcher, fetcher);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700113 }
114 else
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800115 registerPrefixHelper(registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags);
Jeff Thompson11095142013-10-01 16:20:28 -0700116
117 return registeredPrefixId;
118}
119
120void
Jeff Thompson62992e42013-10-07 18:50:51 -0700121Node::removeRegisteredPrefix(uint64_t registeredPrefixId)
Jeff Thompson11095142013-10-01 16:20:28 -0700122{
123 // Go backwards through the list so we can erase entries.
124 // Remove all entries even though pendingInterestId should be unique.
125 for (int i = (int)registeredPrefixTable_.size() - 1; i >= 0; --i) {
126 if (registeredPrefixTable_[i]->getRegisteredPrefixId() == registeredPrefixId)
127 registeredPrefixTable_.erase(registeredPrefixTable_.begin() + i);
128 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700129}
130
Jeff Thompson0050abe2013-09-17 12:50:25 -0700131void
Alexander Afanasyev79100492014-01-03 15:35:38 -0800132Node::registerPrefixHelper(uint64_t registeredPrefixId,
133 const ptr_lib::shared_ptr<const Name>& prefix,
134 const OnInterest& onInterest,
135 const OnRegisterFailed& onRegisterFailed,
136 const ForwardingFlags& flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700137{
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700138 // Create a ForwardingEntry.
Alexander Afanasyevfbdfa092013-12-28 20:44:49 -0800139
140 // AlexA: ndnd ignores any freshness that is larger than 3600 sec and sets 300 sec instead
141 // to register "forever" (=2000000000 sec), freshnessPeriod must be omitted
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800142 ForwardingEntry forwardingEntry("selfreg", *prefix, -1, flags, -1);
143 Block content = forwardingEntry.wireEncode();
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700144
145 // Set the ForwardingEntry as the content of a Data packet and sign.
146 Data data;
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700147 data.setContent(content);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700148
Alexander Afanasyev79100492014-01-03 15:35:38 -0800149 // Create an empty signature, since nobody going to verify it for now
150 // @todo In the future, we may require real signatures to do the registration
151 SignatureSha256WithRsa signature;
152 signature.setValue(Block(Tlv::SignatureValue, ptr_lib::make_shared<Buffer>()));
153 data.setSignature(signature);
154
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700155 // Create an interest where the name has the encoded Data packet.
156 Name interestName;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800157 interestName.append("ndnx");
Jeff Thompson3a715632013-10-31 11:36:35 -0700158 interestName.append(ndndId_);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800159 interestName.append("selfreg");
Alexander Afanasyev79100492014-01-03 15:35:38 -0800160 interestName.append(data.wireEncode());
Alexander Afanasyev18371872014-01-05 23:00:26 -0800161
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700162 Interest interest(interestName);
163 interest.setScope(1);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800164 interest.setInterestLifetime(1000);
165
166 expressInterest(interest,
167 func_lib::bind(&Node::registerPrefixFinal, this,
168 registeredPrefixId, prefix, onInterest, onRegisterFailed, _1, _2),
169 func_lib::bind(onRegisterFailed, prefix));
170}
171
172void
173Node::registerPrefixFinal(uint64_t registeredPrefixId,
174 const ptr_lib::shared_ptr<const Name>& prefix,
175 const OnInterest& onInterest,
176 const OnRegisterFailed& onRegisterFailed,
177 const ptr_lib::shared_ptr<const Interest>&, const ptr_lib::shared_ptr<Data>&data)
178{
179 Block content = data->getContent();
180 content.parse();
181
182 if (content.getAll().empty())
183 {
184 onRegisterFailed(prefix);
185 return;
186 }
187
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800188 Block::element_iterator val = content.getAll().begin();
189
190 switch(val->type())
Alexander Afanasyev18371872014-01-05 23:00:26 -0800191 {
192 case Tlv::FaceManagement::ForwardingEntry:
193 {
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800194 ForwardingEntry entry;
195 entry.wireDecode(*val);
196
197 // Save the onInterest callback and send the registration interest.
198 registeredPrefixTable_.push_back(ptr_lib::make_shared<RegisteredPrefix>(registeredPrefixId, prefix, onInterest));
199
200 /// @todo Notify user about successful registration
201
Alexander Afanasyev18371872014-01-05 23:00:26 -0800202 // succeeded
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800203 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800204 }
205 case Tlv::FaceManagement::StatusResponse:
206 {
207 // failed :(
208 StatusResponse resp;
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800209 resp.wireDecode(*val);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800210
211 std::cerr << "StatusReponse: " << resp << std::endl;
212
213 onRegisterFailed(prefix);
214 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800215 }
216 default:
217 {
218 // failed :(
219
220 onRegisterFailed(prefix);
221 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800222 }
223 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700224}
225
Jeff Thompson0050abe2013-09-17 12:50:25 -0700226void
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800227Node::processEvents(Milliseconds timeout/* = 0 */, bool keepThread/* = false*/)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700228{
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800229 try
230 {
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800231 if (timeout < 0)
232 {
233 // do not block if timeout is negative, but process pending events
234 ioService_->poll();
235 return;
236 }
237
238 if (timeout > 0)
239 {
240 processEventsTimeoutTimer_->expires_from_now(boost::posix_time::milliseconds(timeout));
241 processEventsTimeoutTimer_->async_wait(fireProcessEventsTimeout);
242 }
243
244 if (keepThread) {
245 // work will ensure that ioService_ is running until work object exists
246 ioServiceWork_ = ptr_lib::make_shared<boost::asio::io_service::work>(boost::ref(*ioService_));
247 }
248
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800249 ioService_->run();
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800250 }
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800251 catch(Node::ProcessEventsTimeout &)
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800252 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800253 // break
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800254 ioService_->reset();
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800255 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800256}
257
258void
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800259Node::fireProcessEventsTimeout(const boost::system::error_code& error)
260{
261 if (!error) // can fire for some other reason, e.g., cancelled
262 throw Node::ProcessEventsTimeout();
263}
264
265void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800266Node::checkPitExpire()
267{
Jeff Thompson48917f02013-08-21 17:12:45 -0700268 // Check for PIT entry timeouts. Go backwards through the list so we can erase entries.
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700269 MillisecondsSince1970 nowMilliseconds = ndn_getNowMilliseconds();
Jeff Thompson11095142013-10-01 16:20:28 -0700270 for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) {
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800271 if (pendingInterestTable_[i]->isTimedOut(nowMilliseconds)) {
272 // Save the PendingInterest and remove it from the PIT. Then call the callback.
Jeff Thompsonce115762013-12-18 14:59:56 -0800273 ptr_lib::shared_ptr<PendingInterest> pendingInterest = pendingInterestTable_[i];
Jeff Thompson11095142013-10-01 16:20:28 -0700274 pendingInterestTable_.erase(pendingInterestTable_.begin() + i);
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800275 pendingInterest->callTimeout();
Jeff Thompson48917f02013-08-21 17:12:45 -0700276
277 // Refresh now since the timeout callback might have delayed.
Jeff Thompson9ae4d782013-10-17 10:25:54 -0700278 nowMilliseconds = ndn_getNowMilliseconds();
Jeff Thompson48917f02013-08-21 17:12:45 -0700279 }
280 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800281
Alexander Afanasyevbf082112014-01-09 14:27:55 -0800282 if (!pendingInterestTable_.empty()) {
283 // pitTimeoutCheckTimerActive = true;
284 pitTimeoutCheckTimer_->expires_from_now(boost::posix_time::milliseconds(100));
285 pitTimeoutCheckTimer_->async_wait(func_lib::bind(&Node::checkPitExpire, this));
286 }
287 else {
288 pitTimeoutCheckTimerActive_ = false;
289
290 if (registeredPrefixTable_.empty()) {
291 transport_->close();
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800292 if (!ioServiceWork_) {
293 processEventsTimeoutTimer_->cancel();
294 }
Alexander Afanasyevbf082112014-01-09 14:27:55 -0800295 }
296 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700297}
298
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800299
Jeff Thompson0050abe2013-09-17 12:50:25 -0700300void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800301Node::onReceiveElement(const Block &block)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700302{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800303 if (block.type() == Tlv::Interest)
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800304 {
305 ptr_lib::shared_ptr<Interest> interest(new Interest());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800306 interest->wireDecode(block);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700307
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800308 RegisteredPrefixTable::iterator entry = getEntryForRegisteredPrefix(interest->getName());
309 if (entry != registeredPrefixTable_.end()) {
310 (*entry)->getOnInterest()((*entry)->getPrefix(), interest, *transport_, (*entry)->getRegisteredPrefixId());
311 }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700312 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800313 else if (block.type() == Tlv::Data)
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800314 {
315 ptr_lib::shared_ptr<Data> data(new Data());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800316 data->wireDecode(block);
317
318 PendingInterestTable::iterator entry = getEntryIndexForExpressedInterest(data->getName());
319 if (entry != pendingInterestTable_.end()) {
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800320 // Copy pointers to the needed objects and remove the PIT entry before the calling the callback.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800321 const OnData onData = (*entry)->getOnData();
322 const ptr_lib::shared_ptr<const Interest> interest = (*entry)->getInterest();
323 pendingInterestTable_.erase(entry);
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800324 onData(interest, data);
325 }
326 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700327}
328
Jeff Thompson0050abe2013-09-17 12:50:25 -0700329void
330Node::shutdown()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700331{
332 transport_->close();
Alexander Afanasyevbf082112014-01-09 14:27:55 -0800333 pitTimeoutCheckTimer_->cancel();
334 processEventsTimeoutTimer_->cancel();
335 pitTimeoutCheckTimerActive_ = false;
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800336
337 // This will ensure that io_service::work will stop
338 ioServiceWork_.reset();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700339}
340
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800341Node::PendingInterestTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700342Node::getEntryIndexForExpressedInterest(const Name& name)
Jeff Thompson557b81e2013-08-21 15:13:51 -0700343{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800344 for (PendingInterestTable::iterator i = pendingInterestTable_.begin ();
345 i != pendingInterestTable_.end(); ++i)
346 {
347 if ((*i)->getInterest()->matchesName(name))
348 {
349 return i;
350 }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700351 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800352
353 return pendingInterestTable_.end();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700354}
Jeff Thompson86507bc2013-08-23 20:51:38 -0700355
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800356Node::RegisteredPrefixTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700357Node::getEntryForRegisteredPrefix(const Name& name)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700358{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800359 RegisteredPrefixTable::iterator longestPrefix = registeredPrefixTable_.end();
360
361 for (RegisteredPrefixTable::iterator i = registeredPrefixTable_.begin();
362 i != registeredPrefixTable_.end();
363 ++i)
364 {
365 if (longestPrefix == registeredPrefixTable_.end() ||
366 (*i)->getPrefix()->size() > (*longestPrefix)->getPrefix()->size())
367 {
368 longestPrefix = i;
369 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700370 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800371 return longestPrefix;
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700372}
373
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800374Node::PendingInterest::PendingInterest(uint64_t pendingInterestId,
375 const ptr_lib::shared_ptr<const Interest>& interest,
376 const OnData& onData, const OnTimeout& onTimeout)
377: pendingInterestId_(pendingInterestId),
378 interest_(interest),
379 onData_(onData), onTimeout_(onTimeout)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700380{
381 // Set up timeoutTime_.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800382 if (interest_->getInterestLifetime() >= 0)
383 timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + interest_->getInterestLifetime();
Jeff Thompson86507bc2013-08-23 20:51:38 -0700384 else
385 // No timeout.
Alexander Afanasyevb24a68a2013-12-28 16:53:21 -0800386 /**
387 * @todo Set more meaningful default timeout. This timeout MUST exist.
388 */
389 timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + 4000;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700390}
391
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800392void
393Node::PendingInterest::callTimeout()
Jeff Thompson86507bc2013-08-23 20:51:38 -0700394{
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800395 if (onTimeout_) {
396 // Ignore all exceptions.
397 try {
398 onTimeout_(interest_);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700399 }
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800400 catch (...) { }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700401 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700402}
Jeff Thompson557b81e2013-08-21 15:13:51 -0700403
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700404}