blob: 22f4963826477ee9616d6e9ff90345e2b21c4347 [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
Yingdi Yu61ec2722014-01-20 14:22:32 -080011#include <ndn-cpp-dev/forwarding-entry.hpp>
12#include <ndn-cpp-dev/face-instance.hpp>
13#include <ndn-cpp-dev/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
Yingdi Yu61ec2722014-01-20 14:22:32 -080017#include <ndn-cpp-dev/security/signature-sha256-with-rsa.hpp>
18#include <ndn-cpp-dev/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
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080028Node::Node(const ptr_lib::shared_ptr<Transport>& transport)
Alexander Afanasyevbf082112014-01-09 14:27:55 -080029 : pitTimeoutCheckTimerActive_(false)
30 , transport_(transport)
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080031 , ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0)
32{
33 ioService_ = ptr_lib::make_shared<boost::asio::io_service>();
34 pitTimeoutCheckTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
35 processEventsTimeoutTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080036}
37
38Node::Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<boost::asio::io_service> &ioService)
39 : ioService_(ioService)
Alexander Afanasyevbf082112014-01-09 14:27:55 -080040 , pitTimeoutCheckTimerActive_(false)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080041 , transport_(transport)
42 , ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0)
Jeff Thompson557b81e2013-08-21 15:13:51 -070043{
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080044 pitTimeoutCheckTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
45 processEventsTimeoutTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
Jeff Thompson557b81e2013-08-21 15:13:51 -070046}
47
Alexander Afanasyevb790d952014-01-24 12:07:53 -080048const PendingInterestId*
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080049Node::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070050{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080051 if (!transport_->isConnected())
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080052 transport_->connect(*ioService_,
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080053 ptr_lib::bind(&Node::onReceiveElement, this, _1));
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080054
Alexander Afanasyevb790d952014-01-24 12:07:53 -080055 ptr_lib::shared_ptr<const Interest> interestToExpress(new Interest(interest));
56
57 ioService_->post(func_lib::bind(&Node::asyncExpressInterest, this, interestToExpress, onData, onTimeout));
58
59 return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
60}
61
62void
63Node::asyncExpressInterest(const ptr_lib::shared_ptr<const Interest> &interest, const OnData& onData, const OnTimeout& onTimeout)
64{
65 pendingInterestTable_.push_back(ptr_lib::shared_ptr<PendingInterest>(new PendingInterest
66 (interest, onData, onTimeout)));
67
68 transport_->send(interest->wireEncode());
Alexander Afanasyevbf082112014-01-09 14:27:55 -080069
70 if (!pitTimeoutCheckTimerActive_) {
71 pitTimeoutCheckTimerActive_ = true;
72 pitTimeoutCheckTimer_->expires_from_now(boost::posix_time::milliseconds(100));
73 pitTimeoutCheckTimer_->async_wait(func_lib::bind(&Node::checkPitExpire, this));
74 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070075}
Alexander Afanasyevb790d952014-01-24 12:07:53 -080076
Jeff Thompson11095142013-10-01 16:20:28 -070077void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080078Node::put(const Data &data)
79{
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080080 if (!transport_->isConnected())
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -080081 transport_->connect(*ioService_,
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080082 ptr_lib::bind(&Node::onReceiveElement, this, _1));
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080083
84 transport_->send(data.wireEncode());
85}
86
87
88void
Alexander Afanasyevb790d952014-01-24 12:07:53 -080089Node::removePendingInterest(const PendingInterestId *pendingInterestId)
Jeff Thompson11095142013-10-01 16:20:28 -070090{
Alexander Afanasyevb790d952014-01-24 12:07:53 -080091 ioService_->post(func_lib::bind(&Node::asyncRemovePendingInterest, this, pendingInterestId));
Jeff Thompson11095142013-10-01 16:20:28 -070092}
93
Alexander Afanasyevb790d952014-01-24 12:07:53 -080094
95void
96Node::asyncRemovePendingInterest(const PendingInterestId *pendingInterestId)
97{
98 std::remove_if(pendingInterestTable_.begin(), pendingInterestTable_.end(),
99 MatchPendingInterestId(pendingInterestId));
100}
101
102const RegisteredPrefixId*
Jeff Thompson590ec232013-09-18 15:55:56 -0700103Node::registerPrefix
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800104 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700105{
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800106 ptr_lib::shared_ptr<RegisteredPrefix> prefixToRegister(new RegisteredPrefix(prefix, onInterest));
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800107
Jeff Thompson86507bc2013-08-23 20:51:38 -0700108 if (ndndId_.size() == 0) {
109 // First fetch the ndndId of the connected hub.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800110 NdndIdFetcher fetcher(ndndId_,
111 func_lib::bind(&Node::registerPrefixHelper, this,
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800112 prefixToRegister, onRegisterFailed, flags),
113 func_lib::bind(onRegisterFailed, prefixToRegister->getPrefix().shared_from_this()));
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800114
115 // @todo: Check if this crash
Jeff Thompsonce115762013-12-18 14:59:56 -0800116 // 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 -0800117 expressInterest(ndndIdFetcherInterest_, fetcher, fetcher);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700118 }
119 else
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800120 registerPrefixHelper(prefixToRegister, onRegisterFailed, flags);
Jeff Thompson11095142013-10-01 16:20:28 -0700121
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800122 return reinterpret_cast<const RegisteredPrefixId*>(prefixToRegister.get());
Jeff Thompson11095142013-10-01 16:20:28 -0700123}
124
125void
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800126Node::removeRegisteredPrefix(const RegisteredPrefixId *registeredPrefixId)
Jeff Thompson11095142013-10-01 16:20:28 -0700127{
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800128 RegisteredPrefixTable::iterator i = std::find_if(registeredPrefixTable_.begin(), registeredPrefixTable_.end(),
129 MatchRegisteredPrefixId(registeredPrefixId));
130 if (i != registeredPrefixTable_.end())
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -0800131 {
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800132 ForwardingEntry forwardingEntry("unreg", (*i)->getPrefix(), faceId_);
133 Data data;
134 data.setContent(forwardingEntry.wireEncode());
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -0800135
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800136 SignatureSha256WithRsa signature;
137 signature.setValue(Block(Tlv::SignatureValue, ptr_lib::make_shared<Buffer>()));
138 data.setSignature(signature);
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -0800139
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800140 // Create an interest where the name has the encoded Data packet.
141 Name interestName;
142 interestName.append("ndnx");
143 interestName.append(ndndId_);
144 interestName.append("unreg");
145 interestName.append(data.wireEncode());
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -0800146
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800147 Interest interest(interestName);
148 interest.setScope(1);
149 interest.setInterestLifetime(1000);
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -0800150
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800151 expressInterest(interest, OnData(), OnTimeout());
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -0800152
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800153 registeredPrefixTable_.erase(i);
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -0800154 }
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800155
156 // there cannot be two registered prefixes with the same id. if there are, then something is broken
Jeff Thompson86507bc2013-08-23 20:51:38 -0700157}
158
Jeff Thompson0050abe2013-09-17 12:50:25 -0700159void
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800160Node::registerPrefixHelper(const ptr_lib::shared_ptr<RegisteredPrefix> &prefixToRegister,
Alexander Afanasyev79100492014-01-03 15:35:38 -0800161 const OnRegisterFailed& onRegisterFailed,
162 const ForwardingFlags& flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700163{
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700164 // Create a ForwardingEntry.
Alexander Afanasyevfbdfa092013-12-28 20:44:49 -0800165
166 // AlexA: ndnd ignores any freshness that is larger than 3600 sec and sets 300 sec instead
167 // to register "forever" (=2000000000 sec), freshnessPeriod must be omitted
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800168 ForwardingEntry forwardingEntry("selfreg", prefixToRegister->getPrefix(), -1, flags, -1);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800169 Block content = forwardingEntry.wireEncode();
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700170
171 // Set the ForwardingEntry as the content of a Data packet and sign.
172 Data data;
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700173 data.setContent(content);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700174
Alexander Afanasyev79100492014-01-03 15:35:38 -0800175 // Create an empty signature, since nobody going to verify it for now
176 // @todo In the future, we may require real signatures to do the registration
177 SignatureSha256WithRsa signature;
178 signature.setValue(Block(Tlv::SignatureValue, ptr_lib::make_shared<Buffer>()));
179 data.setSignature(signature);
180
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700181 // Create an interest where the name has the encoded Data packet.
182 Name interestName;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800183 interestName.append("ndnx");
Jeff Thompson3a715632013-10-31 11:36:35 -0700184 interestName.append(ndndId_);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800185 interestName.append("selfreg");
Alexander Afanasyev79100492014-01-03 15:35:38 -0800186 interestName.append(data.wireEncode());
Alexander Afanasyev18371872014-01-05 23:00:26 -0800187
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700188 Interest interest(interestName);
189 interest.setScope(1);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800190 interest.setInterestLifetime(1000);
191
192 expressInterest(interest,
193 func_lib::bind(&Node::registerPrefixFinal, this,
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800194 prefixToRegister, onRegisterFailed, _1, _2),
195 func_lib::bind(onRegisterFailed, prefixToRegister->getPrefix().shared_from_this()));
Alexander Afanasyev18371872014-01-05 23:00:26 -0800196}
197
198void
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800199Node::registerPrefixFinal(const ptr_lib::shared_ptr<RegisteredPrefix> &prefixToRegister,
Alexander Afanasyev18371872014-01-05 23:00:26 -0800200 const OnRegisterFailed& onRegisterFailed,
201 const ptr_lib::shared_ptr<const Interest>&, const ptr_lib::shared_ptr<Data>&data)
202{
203 Block content = data->getContent();
204 content.parse();
205
206 if (content.getAll().empty())
207 {
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800208 onRegisterFailed(prefixToRegister->getPrefix().shared_from_this());
Alexander Afanasyev18371872014-01-05 23:00:26 -0800209 return;
210 }
211
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800212 Block::element_iterator val = content.getAll().begin();
213
214 switch(val->type())
Alexander Afanasyev18371872014-01-05 23:00:26 -0800215 {
216 case Tlv::FaceManagement::ForwardingEntry:
217 {
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800218 ForwardingEntry entry;
219 entry.wireDecode(*val);
220
221 // Save the onInterest callback and send the registration interest.
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800222 registeredPrefixTable_.push_back(prefixToRegister);
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800223
224 /// @todo Notify user about successful registration
225
Alexander Afanasyev18371872014-01-05 23:00:26 -0800226 // succeeded
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800227 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800228 }
229 case Tlv::FaceManagement::StatusResponse:
230 {
231 // failed :(
232 StatusResponse resp;
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800233 resp.wireDecode(*val);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800234
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -0800235 // std::cerr << "StatusReponse: " << resp << std::endl;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800236
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800237 onRegisterFailed(prefixToRegister->getPrefix().shared_from_this());
Alexander Afanasyev18371872014-01-05 23:00:26 -0800238 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800239 }
240 default:
241 {
242 // failed :(
243
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800244 onRegisterFailed(prefixToRegister->getPrefix().shared_from_this());
Alexander Afanasyev18371872014-01-05 23:00:26 -0800245 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800246 }
247 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700248}
249
Jeff Thompson0050abe2013-09-17 12:50:25 -0700250void
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800251Node::processEvents(Milliseconds timeout/* = 0 */, bool keepThread/* = false*/)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700252{
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800253 try
254 {
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800255 if (timeout < 0)
256 {
257 // do not block if timeout is negative, but process pending events
258 ioService_->poll();
259 return;
260 }
261
262 if (timeout > 0)
263 {
264 processEventsTimeoutTimer_->expires_from_now(boost::posix_time::milliseconds(timeout));
Jeff Thompson6e9fc182014-01-23 13:04:08 -0800265 processEventsTimeoutTimer_->async_wait(&fireProcessEventsTimeout);
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800266 }
267
268 if (keepThread) {
269 // work will ensure that ioService_ is running until work object exists
270 ioServiceWork_ = ptr_lib::make_shared<boost::asio::io_service::work>(boost::ref(*ioService_));
271 }
272
Alexander Afanasyeve1b7a5d2013-12-29 16:23:52 -0800273 ioService_->run();
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800274 ioService_->reset(); // so it is possible to run processEvents again (if necessary)
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800275 }
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800276 catch(Node::ProcessEventsTimeout &)
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800277 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800278 // break
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800279 ioService_->reset();
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800280 }
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800281 catch(const std::exception &)
282 {
283 ioService_->reset();
284 pendingInterestTable_.clear();
285 registeredPrefixTable_.clear();
286 throw;
287 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800288}
289
290void
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800291Node::fireProcessEventsTimeout(const boost::system::error_code& error)
292{
293 if (!error) // can fire for some other reason, e.g., cancelled
294 throw Node::ProcessEventsTimeout();
295}
296
297void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800298Node::checkPitExpire()
299{
Jeff Thompson48917f02013-08-21 17:12:45 -0700300 // Check for PIT entry timeouts. Go backwards through the list so we can erase entries.
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700301 MillisecondsSince1970 nowMilliseconds = ndn_getNowMilliseconds();
Jeff Thompson11095142013-10-01 16:20:28 -0700302 for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) {
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800303 if (pendingInterestTable_[i]->isTimedOut(nowMilliseconds)) {
304 // Save the PendingInterest and remove it from the PIT. Then call the callback.
Jeff Thompsonce115762013-12-18 14:59:56 -0800305 ptr_lib::shared_ptr<PendingInterest> pendingInterest = pendingInterestTable_[i];
Jeff Thompson11095142013-10-01 16:20:28 -0700306 pendingInterestTable_.erase(pendingInterestTable_.begin() + i);
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800307 pendingInterest->callTimeout();
Jeff Thompson48917f02013-08-21 17:12:45 -0700308
309 // Refresh now since the timeout callback might have delayed.
Jeff Thompson9ae4d782013-10-17 10:25:54 -0700310 nowMilliseconds = ndn_getNowMilliseconds();
Jeff Thompson48917f02013-08-21 17:12:45 -0700311 }
312 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800313
Alexander Afanasyevbf082112014-01-09 14:27:55 -0800314 if (!pendingInterestTable_.empty()) {
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -0800315 pitTimeoutCheckTimerActive_ = true;
316
Alexander Afanasyevbf082112014-01-09 14:27:55 -0800317 pitTimeoutCheckTimer_->expires_from_now(boost::posix_time::milliseconds(100));
318 pitTimeoutCheckTimer_->async_wait(func_lib::bind(&Node::checkPitExpire, this));
319 }
320 else {
321 pitTimeoutCheckTimerActive_ = false;
322
323 if (registeredPrefixTable_.empty()) {
324 transport_->close();
Alexander Afanasyevf75a0aa2014-01-09 14:29:22 -0800325 if (!ioServiceWork_) {
326 processEventsTimeoutTimer_->cancel();
327 }
Alexander Afanasyevbf082112014-01-09 14:27:55 -0800328 }
329 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700330}
331
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800332
Jeff Thompson0050abe2013-09-17 12:50:25 -0700333void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800334Node::onReceiveElement(const Block &block)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700335{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800336 if (block.type() == Tlv::Interest)
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800337 {
338 ptr_lib::shared_ptr<Interest> interest(new Interest());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800339 interest->wireDecode(block);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700340
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800341 RegisteredPrefixTable::iterator entry = getEntryForRegisteredPrefix(interest->getName());
342 if (entry != registeredPrefixTable_.end()) {
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800343 (*entry)->getOnInterest()((*entry)->getPrefix().shared_from_this(), interest);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800344 }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700345 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800346 else if (block.type() == Tlv::Data)
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800347 {
348 ptr_lib::shared_ptr<Data> data(new Data());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800349 data->wireDecode(block);
350
351 PendingInterestTable::iterator entry = getEntryIndexForExpressedInterest(data->getName());
352 if (entry != pendingInterestTable_.end()) {
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800353 // Copy pointers to the needed objects and remove the PIT entry before the calling the callback.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800354 const OnData onData = (*entry)->getOnData();
355 const ptr_lib::shared_ptr<const Interest> interest = (*entry)->getInterest();
356 pendingInterestTable_.erase(entry);
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -0800357
358 if (onData) {
359 onData(interest, data);
360 }
361
362 if (pendingInterestTable_.empty()) {
363 pitTimeoutCheckTimer_->cancel(); // this will cause checkPitExpire invocation
364 }
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800365 }
366 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700367}
368
Jeff Thompson0050abe2013-09-17 12:50:25 -0700369void
370Node::shutdown()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700371{
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800372 pendingInterestTable_.clear();
373 registeredPrefixTable_.clear();
374
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700375 transport_->close();
Alexander Afanasyevbf082112014-01-09 14:27:55 -0800376 pitTimeoutCheckTimer_->cancel();
377 processEventsTimeoutTimer_->cancel();
378 pitTimeoutCheckTimerActive_ = false;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700379}
380
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800381Node::PendingInterestTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700382Node::getEntryIndexForExpressedInterest(const Name& name)
Jeff Thompson557b81e2013-08-21 15:13:51 -0700383{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800384 for (PendingInterestTable::iterator i = pendingInterestTable_.begin ();
385 i != pendingInterestTable_.end(); ++i)
386 {
387 if ((*i)->getInterest()->matchesName(name))
388 {
389 return i;
390 }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700391 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800392
393 return pendingInterestTable_.end();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700394}
Jeff Thompson86507bc2013-08-23 20:51:38 -0700395
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800396Node::RegisteredPrefixTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700397Node::getEntryForRegisteredPrefix(const Name& name)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700398{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800399 RegisteredPrefixTable::iterator longestPrefix = registeredPrefixTable_.end();
400
401 for (RegisteredPrefixTable::iterator i = registeredPrefixTable_.begin();
402 i != registeredPrefixTable_.end();
403 ++i)
404 {
405 if (longestPrefix == registeredPrefixTable_.end() ||
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800406 (*i)->getPrefix().size() > (*longestPrefix)->getPrefix().size())
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800407 {
408 longestPrefix = i;
409 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700410 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800411 return longestPrefix;
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700412}
413
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800414Node::PendingInterest::PendingInterest(const ptr_lib::shared_ptr<const Interest>& interest,
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800415 const OnData& onData, const OnTimeout& onTimeout)
Alexander Afanasyevb790d952014-01-24 12:07:53 -0800416: interest_(interest),
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800417 onData_(onData), onTimeout_(onTimeout)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700418{
419 // Set up timeoutTime_.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800420 if (interest_->getInterestLifetime() >= 0)
421 timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + interest_->getInterestLifetime();
Jeff Thompson86507bc2013-08-23 20:51:38 -0700422 else
423 // No timeout.
Alexander Afanasyevb24a68a2013-12-28 16:53:21 -0800424 /**
425 * @todo Set more meaningful default timeout. This timeout MUST exist.
426 */
427 timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + 4000;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700428}
429
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800430void
431Node::PendingInterest::callTimeout()
Jeff Thompson86507bc2013-08-23 20:51:38 -0700432{
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800433 if (onTimeout_) {
Alexander Afanasyevbc343ef2014-01-09 22:36:20 -0800434 onTimeout_(interest_);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700435 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700436}
Jeff Thompson557b81e2013-08-21 15:13:51 -0700437
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700438}