blob: dcbcf8480560051978d62d9d113c9b0a330d1686 [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 Afanasyeva557d5a2013-12-28 21:59:03 -0800227Node::processEvents(Milliseconds timeout/* = 0 */)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700228{
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800229 if (timeout > 0)
230 {
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800231 processEventsTimeoutTimer_->expires_from_now(boost::posix_time::milliseconds(timeout));
232 processEventsTimeoutTimer_->async_wait(fireProcessEventsTimeout);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800233 }
234 try
235 {
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800236 ioService_->run();
237 ioService_->reset();
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800238 }
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800239 catch(Node::ProcessEventsTimeout &)
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800240 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800241 // break
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800242 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800243}
244
245void
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800246Node::fireProcessEventsTimeout(const boost::system::error_code& error)
247{
248 if (!error) // can fire for some other reason, e.g., cancelled
249 throw Node::ProcessEventsTimeout();
250}
251
252void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800253Node::checkPitExpire()
254{
Jeff Thompson48917f02013-08-21 17:12:45 -0700255 // Check for PIT entry timeouts. Go backwards through the list so we can erase entries.
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700256 MillisecondsSince1970 nowMilliseconds = ndn_getNowMilliseconds();
Jeff Thompson11095142013-10-01 16:20:28 -0700257 for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) {
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800258 if (pendingInterestTable_[i]->isTimedOut(nowMilliseconds)) {
259 // Save the PendingInterest and remove it from the PIT. Then call the callback.
Jeff Thompsonce115762013-12-18 14:59:56 -0800260 ptr_lib::shared_ptr<PendingInterest> pendingInterest = pendingInterestTable_[i];
Jeff Thompson11095142013-10-01 16:20:28 -0700261 pendingInterestTable_.erase(pendingInterestTable_.begin() + i);
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800262 pendingInterest->callTimeout();
Jeff Thompson48917f02013-08-21 17:12:45 -0700263
264 // Refresh now since the timeout callback might have delayed.
Jeff Thompson9ae4d782013-10-17 10:25:54 -0700265 nowMilliseconds = ndn_getNowMilliseconds();
Jeff Thompson48917f02013-08-21 17:12:45 -0700266 }
267 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800268
Alexander Afanasyevbf082112014-01-09 14:27:55 -0800269 if (!pendingInterestTable_.empty()) {
270 // pitTimeoutCheckTimerActive = true;
271 pitTimeoutCheckTimer_->expires_from_now(boost::posix_time::milliseconds(100));
272 pitTimeoutCheckTimer_->async_wait(func_lib::bind(&Node::checkPitExpire, this));
273 }
274 else {
275 pitTimeoutCheckTimerActive_ = false;
276
277 if (registeredPrefixTable_.empty()) {
278 transport_->close();
279 processEventsTimeoutTimer_->cancel();
280 }
281 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700282}
283
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800284
Jeff Thompson0050abe2013-09-17 12:50:25 -0700285void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800286Node::onReceiveElement(const Block &block)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700287{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800288 if (block.type() == Tlv::Interest)
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800289 {
290 ptr_lib::shared_ptr<Interest> interest(new Interest());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800291 interest->wireDecode(block);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700292
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800293 RegisteredPrefixTable::iterator entry = getEntryForRegisteredPrefix(interest->getName());
294 if (entry != registeredPrefixTable_.end()) {
295 (*entry)->getOnInterest()((*entry)->getPrefix(), interest, *transport_, (*entry)->getRegisteredPrefixId());
296 }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700297 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800298 else if (block.type() == Tlv::Data)
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800299 {
300 ptr_lib::shared_ptr<Data> data(new Data());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800301 data->wireDecode(block);
302
303 PendingInterestTable::iterator entry = getEntryIndexForExpressedInterest(data->getName());
304 if (entry != pendingInterestTable_.end()) {
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800305 // Copy pointers to the needed objects and remove the PIT entry before the calling the callback.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800306 const OnData onData = (*entry)->getOnData();
307 const ptr_lib::shared_ptr<const Interest> interest = (*entry)->getInterest();
308 pendingInterestTable_.erase(entry);
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800309 onData(interest, data);
310 }
311 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700312}
313
Jeff Thompson0050abe2013-09-17 12:50:25 -0700314void
315Node::shutdown()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700316{
317 transport_->close();
Alexander Afanasyevbf082112014-01-09 14:27:55 -0800318 pitTimeoutCheckTimer_->cancel();
319 processEventsTimeoutTimer_->cancel();
320 pitTimeoutCheckTimerActive_ = false;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700321}
322
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800323Node::PendingInterestTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700324Node::getEntryIndexForExpressedInterest(const Name& name)
Jeff Thompson557b81e2013-08-21 15:13:51 -0700325{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800326 for (PendingInterestTable::iterator i = pendingInterestTable_.begin ();
327 i != pendingInterestTable_.end(); ++i)
328 {
329 if ((*i)->getInterest()->matchesName(name))
330 {
331 return i;
332 }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700333 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800334
335 return pendingInterestTable_.end();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700336}
Jeff Thompson86507bc2013-08-23 20:51:38 -0700337
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800338Node::RegisteredPrefixTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700339Node::getEntryForRegisteredPrefix(const Name& name)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700340{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800341 RegisteredPrefixTable::iterator longestPrefix = registeredPrefixTable_.end();
342
343 for (RegisteredPrefixTable::iterator i = registeredPrefixTable_.begin();
344 i != registeredPrefixTable_.end();
345 ++i)
346 {
347 if (longestPrefix == registeredPrefixTable_.end() ||
348 (*i)->getPrefix()->size() > (*longestPrefix)->getPrefix()->size())
349 {
350 longestPrefix = i;
351 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700352 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800353 return longestPrefix;
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700354}
355
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800356Node::PendingInterest::PendingInterest(uint64_t pendingInterestId,
357 const ptr_lib::shared_ptr<const Interest>& interest,
358 const OnData& onData, const OnTimeout& onTimeout)
359: pendingInterestId_(pendingInterestId),
360 interest_(interest),
361 onData_(onData), onTimeout_(onTimeout)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700362{
363 // Set up timeoutTime_.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800364 if (interest_->getInterestLifetime() >= 0)
365 timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + interest_->getInterestLifetime();
Jeff Thompson86507bc2013-08-23 20:51:38 -0700366 else
367 // No timeout.
Alexander Afanasyevb24a68a2013-12-28 16:53:21 -0800368 /**
369 * @todo Set more meaningful default timeout. This timeout MUST exist.
370 */
371 timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + 4000;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700372}
373
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800374void
375Node::PendingInterest::callTimeout()
Jeff Thompson86507bc2013-08-23 20:51:38 -0700376{
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800377 if (onTimeout_) {
378 // Ignore all exceptions.
379 try {
380 onTimeout_(interest_);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700381 }
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800382 catch (...) { }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700383 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700384}
Jeff Thompson557b81e2013-08-21 15:13:51 -0700385
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700386}