blob: 1ae1d3b254da06a8b023caeb12d8979cbe9aa587 [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 Afanasyeve1b7a5d2013-12-29 16:23:52 -080032 : transport_(transport)
33 , ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0)
34{
35 ioService_ = ptr_lib::make_shared<boost::asio::io_service>();
36 pitTimeoutCheckTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
37 processEventsTimeoutTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
38
39 pitTimeoutCheckTimer_->expires_from_now(boost::posix_time::milliseconds(100));
40 pitTimeoutCheckTimer_->async_wait(func_lib::bind(&Node::checkPitExpire, this));
41}
42
43Node::Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<boost::asio::io_service> &ioService)
44 : ioService_(ioService)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080045 , transport_(transport)
46 , ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0)
Jeff Thompson557b81e2013-08-21 15:13:51 -070047{
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080048 pitTimeoutCheckTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
49 processEventsTimeoutTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
50
51 pitTimeoutCheckTimer_->expires_from_now(boost::posix_time::milliseconds(100));
52 pitTimeoutCheckTimer_->async_wait(func_lib::bind(&Node::checkPitExpire, this));
Jeff Thompson557b81e2013-08-21 15:13:51 -070053}
54
Jeff Thompson62992e42013-10-07 18:50:51 -070055uint64_t
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080056Node::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070057{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080058 if (!transport_->isConnected())
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080059 transport_->connect(*ioService_,
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080060 ptr_lib::bind(&Node::onReceiveElement, this, _1));
Jeff Thompson86507bc2013-08-23 20:51:38 -070061
Jeff Thompson62992e42013-10-07 18:50:51 -070062 uint64_t pendingInterestId = PendingInterest::getNextPendingInterestId();
Jeff Thompsonce115762013-12-18 14:59:56 -080063 pendingInterestTable_.push_back(ptr_lib::shared_ptr<PendingInterest>(new PendingInterest
64 (pendingInterestId, ptr_lib::shared_ptr<const Interest>(new Interest(interest)), onData, onTimeout)));
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080065
66 transport_->send(interest.wireEncode());
Jeff Thompson11095142013-10-01 16:20:28 -070067
68 return pendingInterestId;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070069}
70
Jeff Thompson11095142013-10-01 16:20:28 -070071void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080072Node::put(const Data &data)
73{
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080074 if (!transport_->isConnected())
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080075 transport_->connect(*ioService_,
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080076 ptr_lib::bind(&Node::onReceiveElement, this, _1));
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080077
78 transport_->send(data.wireEncode());
79}
80
81
82void
Jeff Thompson62992e42013-10-07 18:50:51 -070083Node::removePendingInterest(uint64_t pendingInterestId)
Jeff Thompson11095142013-10-01 16:20:28 -070084{
85 // Go backwards through the list so we can erase entries.
86 // Remove all entries even though pendingInterestId should be unique.
87 for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) {
88 if (pendingInterestTable_[i]->getPendingInterestId() == pendingInterestId)
89 pendingInterestTable_.erase(pendingInterestTable_.begin() + i);
90 }
91}
92
Jeff Thompson62992e42013-10-07 18:50:51 -070093uint64_t
Jeff Thompson590ec232013-09-18 15:55:56 -070094Node::registerPrefix
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080095 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -070096{
Jeff Thompson11095142013-10-01 16:20:28 -070097 // Get the registeredPrefixId now so we can return it to the caller.
Jeff Thompson62992e42013-10-07 18:50:51 -070098 uint64_t registeredPrefixId = RegisteredPrefix::getNextRegisteredPrefixId();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080099 ptr_lib::shared_ptr<const Name> prefixPtr = ptr_lib::make_shared<const Name>(prefix);
100
Jeff Thompson86507bc2013-08-23 20:51:38 -0700101 if (ndndId_.size() == 0) {
102 // First fetch the ndndId of the connected hub.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800103 NdndIdFetcher fetcher(ndndId_,
104 func_lib::bind(&Node::registerPrefixHelper, this,
105 registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags),
106 func_lib::bind(onRegisterFailed, prefixPtr));
107
108 // @todo: Check if this crash
Jeff Thompsonce115762013-12-18 14:59:56 -0800109 // 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 -0800110 expressInterest(ndndIdFetcherInterest_, fetcher, fetcher);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700111 }
112 else
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800113 registerPrefixHelper(registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags);
Jeff Thompson11095142013-10-01 16:20:28 -0700114
115 return registeredPrefixId;
116}
117
118void
Jeff Thompson62992e42013-10-07 18:50:51 -0700119Node::removeRegisteredPrefix(uint64_t registeredPrefixId)
Jeff Thompson11095142013-10-01 16:20:28 -0700120{
121 // Go backwards through the list so we can erase entries.
122 // Remove all entries even though pendingInterestId should be unique.
123 for (int i = (int)registeredPrefixTable_.size() - 1; i >= 0; --i) {
124 if (registeredPrefixTable_[i]->getRegisteredPrefixId() == registeredPrefixId)
125 registeredPrefixTable_.erase(registeredPrefixTable_.begin() + i);
126 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700127}
128
Jeff Thompson0050abe2013-09-17 12:50:25 -0700129void
Alexander Afanasyev79100492014-01-03 15:35:38 -0800130Node::registerPrefixHelper(uint64_t registeredPrefixId,
131 const ptr_lib::shared_ptr<const Name>& prefix,
132 const OnInterest& onInterest,
133 const OnRegisterFailed& onRegisterFailed,
134 const ForwardingFlags& flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700135{
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700136 // Create a ForwardingEntry.
Alexander Afanasyevfbdfa092013-12-28 20:44:49 -0800137
138 // AlexA: ndnd ignores any freshness that is larger than 3600 sec and sets 300 sec instead
139 // to register "forever" (=2000000000 sec), freshnessPeriod must be omitted
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800140 ForwardingEntry forwardingEntry("selfreg", *prefix, -1, flags, -1);
141 Block content = forwardingEntry.wireEncode();
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700142
143 // Set the ForwardingEntry as the content of a Data packet and sign.
144 Data data;
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700145 data.setContent(content);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700146
Alexander Afanasyev79100492014-01-03 15:35:38 -0800147 // Create an empty signature, since nobody going to verify it for now
148 // @todo In the future, we may require real signatures to do the registration
149 SignatureSha256WithRsa signature;
150 signature.setValue(Block(Tlv::SignatureValue, ptr_lib::make_shared<Buffer>()));
151 data.setSignature(signature);
152
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700153 // Create an interest where the name has the encoded Data packet.
154 Name interestName;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800155 interestName.append("ndnx");
Jeff Thompson3a715632013-10-31 11:36:35 -0700156 interestName.append(ndndId_);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800157 interestName.append("selfreg");
Alexander Afanasyev79100492014-01-03 15:35:38 -0800158 interestName.append(data.wireEncode());
Alexander Afanasyev18371872014-01-05 23:00:26 -0800159
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700160 Interest interest(interestName);
161 interest.setScope(1);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800162 interest.setInterestLifetime(1000);
163
164 expressInterest(interest,
165 func_lib::bind(&Node::registerPrefixFinal, this,
166 registeredPrefixId, prefix, onInterest, onRegisterFailed, _1, _2),
167 func_lib::bind(onRegisterFailed, prefix));
168}
169
170void
171Node::registerPrefixFinal(uint64_t registeredPrefixId,
172 const ptr_lib::shared_ptr<const Name>& prefix,
173 const OnInterest& onInterest,
174 const OnRegisterFailed& onRegisterFailed,
175 const ptr_lib::shared_ptr<const Interest>&, const ptr_lib::shared_ptr<Data>&data)
176{
177 Block content = data->getContent();
178 content.parse();
179
180 if (content.getAll().empty())
181 {
182 onRegisterFailed(prefix);
183 return;
184 }
185
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800186 Block::element_iterator val = content.getAll().begin();
187
188 switch(val->type())
Alexander Afanasyev18371872014-01-05 23:00:26 -0800189 {
190 case Tlv::FaceManagement::ForwardingEntry:
191 {
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800192 ForwardingEntry entry;
193 entry.wireDecode(*val);
194
195 // Save the onInterest callback and send the registration interest.
196 registeredPrefixTable_.push_back(ptr_lib::make_shared<RegisteredPrefix>(registeredPrefixId, prefix, onInterest));
197
198 /// @todo Notify user about successful registration
199
Alexander Afanasyev18371872014-01-05 23:00:26 -0800200 // succeeded
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800201 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800202 }
203 case Tlv::FaceManagement::StatusResponse:
204 {
205 // failed :(
206 StatusResponse resp;
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800207 resp.wireDecode(*val);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800208
209 std::cerr << "StatusReponse: " << resp << std::endl;
210
211 onRegisterFailed(prefix);
212 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800213 }
214 default:
215 {
216 // failed :(
217
218 onRegisterFailed(prefix);
219 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800220 }
221 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700222}
223
Jeff Thompson0050abe2013-09-17 12:50:25 -0700224void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800225Node::processEvents(Milliseconds timeout/* = 0 */)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700226{
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800227 if (timeout > 0)
228 {
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800229 processEventsTimeoutTimer_->expires_from_now(boost::posix_time::milliseconds(timeout));
230 processEventsTimeoutTimer_->async_wait(fireProcessEventsTimeout);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800231 }
232 try
233 {
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800234 ioService_->run();
235 ioService_->reset();
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800236 }
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800237 catch(Node::ProcessEventsTimeout &)
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800238 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800239 // break
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800240 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800241}
242
243void
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800244Node::fireProcessEventsTimeout(const boost::system::error_code& error)
245{
246 if (!error) // can fire for some other reason, e.g., cancelled
247 throw Node::ProcessEventsTimeout();
248}
249
250void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800251Node::checkPitExpire()
252{
Jeff Thompson48917f02013-08-21 17:12:45 -0700253 // Check for PIT entry timeouts. Go backwards through the list so we can erase entries.
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700254 MillisecondsSince1970 nowMilliseconds = ndn_getNowMilliseconds();
Jeff Thompson11095142013-10-01 16:20:28 -0700255 for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) {
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800256 if (pendingInterestTable_[i]->isTimedOut(nowMilliseconds)) {
257 // Save the PendingInterest and remove it from the PIT. Then call the callback.
Jeff Thompsonce115762013-12-18 14:59:56 -0800258 ptr_lib::shared_ptr<PendingInterest> pendingInterest = pendingInterestTable_[i];
Jeff Thompson11095142013-10-01 16:20:28 -0700259 pendingInterestTable_.erase(pendingInterestTable_.begin() + i);
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800260 pendingInterest->callTimeout();
Jeff Thompson48917f02013-08-21 17:12:45 -0700261
262 // Refresh now since the timeout callback might have delayed.
Jeff Thompson9ae4d782013-10-17 10:25:54 -0700263 nowMilliseconds = ndn_getNowMilliseconds();
Jeff Thompson48917f02013-08-21 17:12:45 -0700264 }
265 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800266
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800267 pitTimeoutCheckTimer_->expires_from_now(boost::posix_time::milliseconds(100));
268 pitTimeoutCheckTimer_->async_wait(func_lib::bind(&Node::checkPitExpire, this));
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700269}
270
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800271
Jeff Thompson0050abe2013-09-17 12:50:25 -0700272void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800273Node::onReceiveElement(const Block &block)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700274{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800275 if (block.type() == Tlv::Interest)
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800276 {
277 ptr_lib::shared_ptr<Interest> interest(new Interest());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800278 interest->wireDecode(block);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700279
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800280 RegisteredPrefixTable::iterator entry = getEntryForRegisteredPrefix(interest->getName());
281 if (entry != registeredPrefixTable_.end()) {
282 (*entry)->getOnInterest()((*entry)->getPrefix(), interest, *transport_, (*entry)->getRegisteredPrefixId());
283 }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700284 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800285 else if (block.type() == Tlv::Data)
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800286 {
287 ptr_lib::shared_ptr<Data> data(new Data());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800288 data->wireDecode(block);
289
290 PendingInterestTable::iterator entry = getEntryIndexForExpressedInterest(data->getName());
291 if (entry != pendingInterestTable_.end()) {
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800292 // Copy pointers to the needed objects and remove the PIT entry before the calling the callback.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800293 const OnData onData = (*entry)->getOnData();
294 const ptr_lib::shared_ptr<const Interest> interest = (*entry)->getInterest();
295 pendingInterestTable_.erase(entry);
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800296 onData(interest, data);
297 }
298 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700299}
300
Jeff Thompson0050abe2013-09-17 12:50:25 -0700301void
302Node::shutdown()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700303{
304 transport_->close();
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800305 ioService_->stop();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700306}
307
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800308Node::PendingInterestTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700309Node::getEntryIndexForExpressedInterest(const Name& name)
Jeff Thompson557b81e2013-08-21 15:13:51 -0700310{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800311 for (PendingInterestTable::iterator i = pendingInterestTable_.begin ();
312 i != pendingInterestTable_.end(); ++i)
313 {
314 if ((*i)->getInterest()->matchesName(name))
315 {
316 return i;
317 }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700318 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800319
320 return pendingInterestTable_.end();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700321}
Jeff Thompson86507bc2013-08-23 20:51:38 -0700322
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800323Node::RegisteredPrefixTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700324Node::getEntryForRegisteredPrefix(const Name& name)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700325{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800326 RegisteredPrefixTable::iterator longestPrefix = registeredPrefixTable_.end();
327
328 for (RegisteredPrefixTable::iterator i = registeredPrefixTable_.begin();
329 i != registeredPrefixTable_.end();
330 ++i)
331 {
332 if (longestPrefix == registeredPrefixTable_.end() ||
333 (*i)->getPrefix()->size() > (*longestPrefix)->getPrefix()->size())
334 {
335 longestPrefix = i;
336 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700337 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800338 return longestPrefix;
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700339}
340
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800341Node::PendingInterest::PendingInterest(uint64_t pendingInterestId,
342 const ptr_lib::shared_ptr<const Interest>& interest,
343 const OnData& onData, const OnTimeout& onTimeout)
344: pendingInterestId_(pendingInterestId),
345 interest_(interest),
346 onData_(onData), onTimeout_(onTimeout)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700347{
348 // Set up timeoutTime_.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800349 if (interest_->getInterestLifetime() >= 0)
350 timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + interest_->getInterestLifetime();
Jeff Thompson86507bc2013-08-23 20:51:38 -0700351 else
352 // No timeout.
Alexander Afanasyevb24a68a2013-12-28 16:53:21 -0800353 /**
354 * @todo Set more meaningful default timeout. This timeout MUST exist.
355 */
356 timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + 4000;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700357}
358
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800359void
360Node::PendingInterest::callTimeout()
Jeff Thompson86507bc2013-08-23 20:51:38 -0700361{
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800362 if (onTimeout_) {
363 // Ignore all exceptions.
364 try {
365 onTimeout_(interest_);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700366 }
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800367 catch (...) { }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700368 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700369}
Jeff Thompson557b81e2013-08-21 15:13:51 -0700370
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700371}