blob: fe509c486961baad87ff42d02dfce2593251adca [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -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 Thompsonaa4e6db2013-07-15 17:25:23 -07005 * See COPYING for copyright and distribution information.
6 */
7
Alexander Afanasyev0222fba2014-02-09 23:16:02 -08008#include "common.hpp"
9
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080010#include "face.hpp"
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070011
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080012#include "security/signature-sha256-with-rsa.hpp"
13
14#include "util/time.hpp"
15#include "util/random.hpp"
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080016#include <cstdlib>
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080017
18#include "management/ndnd-controller.hpp"
19#include "management/nfd-controller.hpp"
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080020#include "management/nrd-controller.hpp"
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070021
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070022namespace ndn {
Alexander Afanasyevb790d952014-01-24 12:07:53 -080023
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080024Face::Face()
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070025{
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080026 construct(shared_ptr<Transport>(new UnixTransport()),
Alexander Afanasyev505646e2014-02-24 20:13:37 -080027 make_shared<boost::asio::io_service>());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080028}
29
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080030Face::Face(const shared_ptr<boost::asio::io_service>& ioService)
31{
32 construct(shared_ptr<Transport>(new UnixTransport()),
Alexander Afanasyev505646e2014-02-24 20:13:37 -080033 ioService);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080034}
35
36Face::Face(const std::string& host, const std::string& port/* = "6363"*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080037{
38 construct(shared_ptr<Transport>(new TcpTransport(host, port)),
Alexander Afanasyev505646e2014-02-24 20:13:37 -080039 make_shared<boost::asio::io_service>());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080040}
41
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080042Face::Face(const shared_ptr<Transport>& transport)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080043{
44 construct(transport,
Alexander Afanasyev505646e2014-02-24 20:13:37 -080045 make_shared<boost::asio::io_service>());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080046}
47
48Face::Face(const shared_ptr<Transport>& transport,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080049 const shared_ptr<boost::asio::io_service>& ioService)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080050{
Alexander Afanasyev505646e2014-02-24 20:13:37 -080051 construct(transport, ioService);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080052}
53
Alexander Afanasyev505646e2014-02-24 20:13:37 -080054void
55Face::setController(const shared_ptr<Controller>& controller)
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080056{
Alexander Afanasyev505646e2014-02-24 20:13:37 -080057 m_fwController = controller;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080058}
59
60void
61Face::construct(const shared_ptr<Transport>& transport,
Alexander Afanasyev505646e2014-02-24 20:13:37 -080062 const shared_ptr<boost::asio::io_service>& ioService)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080063{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -080064 m_pitTimeoutCheckTimerActive = false;
65 m_transport = transport;
66 m_ioService = ioService;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080067
Alexander Afanasyevf39c5372014-02-17 19:42:56 -080068 m_pitTimeoutCheckTimer = make_shared<boost::asio::deadline_timer>(boost::ref(*m_ioService));
69 m_processEventsTimeoutTimer = make_shared<boost::asio::deadline_timer>(boost::ref(*m_ioService));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080070
Alexander Afanasyev505646e2014-02-24 20:13:37 -080071 if (std::getenv("NFD") != 0)
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080072 {
Alexander Afanasyev505646e2014-02-24 20:13:37 -080073 if (std::getenv("NRD") != 0)
74 m_fwController = make_shared<nrd::Controller>(boost::ref(*this));
75 else
76 m_fwController = make_shared<nfd::Controller>(boost::ref(*this));
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080077 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070078 else
Alexander Afanasyev505646e2014-02-24 20:13:37 -080079 m_fwController = make_shared<ndnd::Controller>(boost::ref(*this));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080080}
81
82
83const PendingInterestId*
84Face::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
85{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -080086 if (!m_transport->isConnected())
87 m_transport->connect(*m_ioService,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080088 bind(&Face::onReceiveElement, this, _1));
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000089 else if (!m_transport->isExpectingData())
90 m_transport->resume();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080091
92 shared_ptr<const Interest> interestToExpress(new Interest(interest));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080093
Alexander Afanasyevf39c5372014-02-17 19:42:56 -080094 m_ioService->post(bind(&Face::asyncExpressInterest, this, interestToExpress, onData, onTimeout));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080095
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080096 return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
97}
98
99const PendingInterestId*
100Face::expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800101 const Interest& tmpl,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800102 const OnData& onData, const OnTimeout& onTimeout/* = OnTimeout()*/)
103{
104 return expressInterest(Interest(name,
105 tmpl.getMinSuffixComponents(),
106 tmpl.getMaxSuffixComponents(),
107 tmpl.getExclude(),
108 tmpl.getChildSelector(),
109 tmpl.getMustBeFresh(),
110 tmpl.getScope(),
111 tmpl.getInterestLifetime()),
112 onData, onTimeout);
113}
114
115void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800116Face::asyncExpressInterest(const shared_ptr<const Interest>& interest,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800117 const OnData& onData, const OnTimeout& onTimeout)
118{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800119 m_pendingInterestTable.push_back(shared_ptr<PendingInterest>(new PendingInterest
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800120 (interest, onData, onTimeout)));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800121
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800122 if (!interest->getLocalControlHeader().empty(false, true))
123 {
124 // encode only NextHopFaceId towards the forwarder
125 m_transport->send(interest->getLocalControlHeader().wireEncode(*interest, false, true),
126 interest->wireEncode());
127 }
128 else
129 {
130 m_transport->send(interest->wireEncode());
131 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800132
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800133 if (!m_pitTimeoutCheckTimerActive) {
134 m_pitTimeoutCheckTimerActive = true;
135 m_pitTimeoutCheckTimer->expires_from_now(boost::posix_time::milliseconds(100));
136 m_pitTimeoutCheckTimer->async_wait(bind(&Face::checkPitExpire, this));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800137 }
138}
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800139
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800140void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800141Face::put(const Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800142{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800143 if (!m_transport->isConnected())
144 m_transport->connect(*m_ioService,
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800145 bind(&Face::onReceiveElement, this, _1));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800146
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800147 if (!data.getLocalControlHeader().empty(false, true))
148 {
Alexander Afanasyev83018352014-02-18 19:52:15 -0800149 m_transport->send(data.getLocalControlHeader().wireEncode(data, false, true),
150 data.wireEncode());
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800151 }
152 else
153 {
Alexander Afanasyev83018352014-02-18 19:52:15 -0800154 m_transport->send(data.wireEncode());
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800155 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800156}
157
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800158void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800159Face::removePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800160{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800161 m_ioService->post(bind(&Face::asyncRemovePendingInterest, this, pendingInterestId));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800162}
163
164
165void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800166Face::asyncRemovePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800167{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800168 m_pendingInterestTable.remove_if(MatchPendingInterestId(pendingInterestId));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800169}
170
171const RegisteredPrefixId*
172Face::setInterestFilter(const Name& prefix,
173 const OnInterest& onInterest,
174 const OnSetInterestFilterFailed& onSetInterestFilterFailed)
175{
176 shared_ptr<RegisteredPrefix> prefixToRegister(new RegisteredPrefix(prefix, onInterest));
177
178 m_fwController->selfRegisterPrefix(prefixToRegister->getPrefix(),
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800179 bind(&RegisteredPrefixTable::push_back, &m_registeredPrefixTable, prefixToRegister),
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800180 bind(onSetInterestFilterFailed, prefixToRegister->getPrefix(), _1));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800181
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800182 return reinterpret_cast<const RegisteredPrefixId*>(prefixToRegister.get());
183}
184
185void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800186Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800187{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800188 m_ioService->post(bind(&Face::asyncUnsetInterestFilter, this, registeredPrefixId));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800189}
190
191void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800192Face::asyncUnsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800193{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800194 RegisteredPrefixTable::iterator i = std::find_if(m_registeredPrefixTable.begin(), m_registeredPrefixTable.end(),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800195 MatchRegisteredPrefixId(registeredPrefixId));
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800196 if (i != m_registeredPrefixTable.end())
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800197 {
198 m_fwController->selfDeregisterPrefix((*i)->getPrefix(),
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000199 bind(&Face::finalizeUnsetInterestFilter, this, i),
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800200 Controller::FailCallback());
201 }
202
203 // there cannot be two registered prefixes with the same id. if there are, then something is broken
204}
205
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800206void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000207Face::finalizeUnsetInterestFilter(RegisteredPrefixTable::iterator item)
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800208{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800209 m_registeredPrefixTable.erase(item);
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800210
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800211 if (!m_pitTimeoutCheckTimerActive && m_registeredPrefixTable.empty())
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800212 {
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000213 m_transport->pause();
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800214 if (!m_ioServiceWork) {
215 m_processEventsTimeoutTimer->cancel();
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800216 }
217 }
218}
219
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800220void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800221Face::processEvents(Milliseconds timeout/* = 0 */, bool keepThread/* = false*/)
222{
223 try
224 {
225 if (timeout < 0)
226 {
227 // do not block if timeout is negative, but process pending events
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800228 m_ioService->poll();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800229 return;
230 }
231
232 if (timeout > 0)
233 {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800234 m_processEventsTimeoutTimer->expires_from_now(boost::posix_time::milliseconds(timeout));
235 m_processEventsTimeoutTimer->async_wait(&fireProcessEventsTimeout);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800236 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800237
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800238 if (keepThread) {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800239 // work will ensure that m_ioService is running until work object exists
240 m_ioServiceWork = make_shared<boost::asio::io_service::work>(boost::ref(*m_ioService));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800241 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800242
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800243 m_ioService->run();
244 m_ioService->reset(); // so it is possible to run processEvents again (if necessary)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800245 }
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800246 catch(Face::ProcessEventsTimeout&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800247 {
248 // break
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800249 m_ioService->reset();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800250 }
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800251 catch(const std::exception&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800252 {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800253 m_ioService->reset();
254 m_pendingInterestTable.clear();
255 m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800256 throw;
257 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700258}
259
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800260void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700261Face::shutdown()
Jeff Thompson517ffa82013-08-05 16:04:34 -0700262{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800263 m_pendingInterestTable.clear();
264 m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800265
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800266 m_transport->close();
267 m_pitTimeoutCheckTimer->cancel();
268 m_processEventsTimeoutTimer->cancel();
269 m_pitTimeoutCheckTimerActive = false;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700270}
271
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800272void
273Face::fireProcessEventsTimeout(const boost::system::error_code& error)
274{
275 if (!error) // can fire for some other reason, e.g., cancelled
276 throw Face::ProcessEventsTimeout();
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700277}
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800278
279void
280Face::checkPitExpire()
281{
282 // Check for PIT entry timeouts. Go backwards through the list so we can erase entries.
283 MillisecondsSince1970 nowMilliseconds = getNowMilliseconds();
284
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800285 PendingInterestTable::iterator i = m_pendingInterestTable.begin();
286 while (i != m_pendingInterestTable.end())
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800287 {
288 if ((*i)->isTimedOut(nowMilliseconds))
289 {
290 // Save the PendingInterest and remove it from the PIT. Then call the callback.
291 shared_ptr<PendingInterest> pendingInterest = *i;
292
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800293 i = m_pendingInterestTable.erase(i);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800294
295 pendingInterest->callTimeout();
296 }
297 else
298 ++i;
299 }
300
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800301 if (!m_pendingInterestTable.empty()) {
302 m_pitTimeoutCheckTimerActive = true;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800303
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800304 m_pitTimeoutCheckTimer->expires_from_now(boost::posix_time::milliseconds(100));
305 m_pitTimeoutCheckTimer->async_wait(bind(&Face::checkPitExpire, this));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800306 }
307 else {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800308 m_pitTimeoutCheckTimerActive = false;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800309
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800310 if (m_registeredPrefixTable.empty()) {
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000311 m_transport->pause();
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800312 if (!m_ioServiceWork) {
313 m_processEventsTimeoutTimer->cancel();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800314 }
315 }
316 }
317}
318
319
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800320void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800321Face::onReceiveElement(const Block& blockFromDaemon)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800322{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800323 const Block& block = nfd::LocalControlHeader::getPayload(blockFromDaemon);
324
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800325 if (block.type() == Tlv::Interest)
326 {
327 shared_ptr<Interest> interest(new Interest());
328 interest->wireDecode(block);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800329 if (&block != &blockFromDaemon)
330 interest->getLocalControlHeader().wireDecode(blockFromDaemon);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800331
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800332 processInterestFilters(*interest);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800333 }
334 else if (block.type() == Tlv::Data)
335 {
336 shared_ptr<Data> data(new Data());
337 data->wireDecode(block);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800338 if (&block != &blockFromDaemon)
339 data->getLocalControlHeader().wireDecode(blockFromDaemon);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800340
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800341 satisfyPendingInterests(*data);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800342
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800343 if (m_pendingInterestTable.empty()) {
344 m_pitTimeoutCheckTimer->cancel(); // this will cause checkPitExpire invocation
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800345 }
346 }
Yingdi Yuf9fa52f2014-02-06 12:27:32 -0800347 // ignore any other type
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800348}
349
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800350void
351Face::satisfyPendingInterests(Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800352{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800353 for (PendingInterestTable::iterator i = m_pendingInterestTable.begin ();
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800354 i != m_pendingInterestTable.end();
355 )
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800356 {
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800357 if ((*i)->getInterest()->matchesName(data.getName()))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800358 {
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800359 // Copy pointers to the needed objects and remove the PIT entry before the calling the callback.
360 OnData onData = (*i)->getOnData();
361 shared_ptr<const Interest> interest = (*i)->getInterest();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800362
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800363 PendingInterestTable::iterator next = i;
364 ++next;
365 m_pendingInterestTable.erase(i);
366 i = next;
367
368 if (static_cast<bool>(onData)) {
369 onData(*interest, data);
370 }
371 }
372 else
373 ++i;
374 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800375}
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800376
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800377void
378Face::processInterestFilters(Interest& interest)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800379{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800380 for (RegisteredPrefixTable::iterator i = m_registeredPrefixTable.begin();
381 i != m_registeredPrefixTable.end();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800382 ++i)
383 {
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800384 if ((*i)->getPrefix().isPrefixOf(interest.getName()))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800385 {
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800386 (*i)->getOnInterest()((*i)->getPrefix(), interest);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800387 }
388 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800389}
390
391} // namespace ndn