blob: 25ce3ab7b49448aa2451ebc897f5f5c6adcc9a1e [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));
89
90 shared_ptr<const Interest> interestToExpress(new Interest(interest));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080091
Alexander Afanasyevf39c5372014-02-17 19:42:56 -080092 m_ioService->post(bind(&Face::asyncExpressInterest, this, interestToExpress, onData, onTimeout));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080093
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080094 return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
95}
96
97const PendingInterestId*
98Face::expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080099 const Interest& tmpl,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800100 const OnData& onData, const OnTimeout& onTimeout/* = OnTimeout()*/)
101{
102 return expressInterest(Interest(name,
103 tmpl.getMinSuffixComponents(),
104 tmpl.getMaxSuffixComponents(),
105 tmpl.getExclude(),
106 tmpl.getChildSelector(),
107 tmpl.getMustBeFresh(),
108 tmpl.getScope(),
109 tmpl.getInterestLifetime()),
110 onData, onTimeout);
111}
112
113void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800114Face::asyncExpressInterest(const shared_ptr<const Interest>& interest,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800115 const OnData& onData, const OnTimeout& onTimeout)
116{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800117 m_pendingInterestTable.push_back(shared_ptr<PendingInterest>(new PendingInterest
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800118 (interest, onData, onTimeout)));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800119
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800120 if (!interest->getLocalControlHeader().empty(false, true))
121 {
122 // encode only NextHopFaceId towards the forwarder
123 m_transport->send(interest->getLocalControlHeader().wireEncode(*interest, false, true),
124 interest->wireEncode());
125 }
126 else
127 {
128 m_transport->send(interest->wireEncode());
129 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800130
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800131 if (!m_pitTimeoutCheckTimerActive) {
132 m_pitTimeoutCheckTimerActive = true;
133 m_pitTimeoutCheckTimer->expires_from_now(boost::posix_time::milliseconds(100));
134 m_pitTimeoutCheckTimer->async_wait(bind(&Face::checkPitExpire, this));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800135 }
136}
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800137
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800138void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800139Face::put(const Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800140{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800141 if (!m_transport->isConnected())
142 m_transport->connect(*m_ioService,
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800143 bind(&Face::onReceiveElement, this, _1));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800144
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800145 if (!data.getLocalControlHeader().empty(false, true))
146 {
Alexander Afanasyev83018352014-02-18 19:52:15 -0800147 m_transport->send(data.getLocalControlHeader().wireEncode(data, false, true),
148 data.wireEncode());
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800149 }
150 else
151 {
Alexander Afanasyev83018352014-02-18 19:52:15 -0800152 m_transport->send(data.wireEncode());
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800153 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800154}
155
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800156void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800157Face::removePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800158{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800159 m_ioService->post(bind(&Face::asyncRemovePendingInterest, this, pendingInterestId));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800160}
161
162
163void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800164Face::asyncRemovePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800165{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800166 m_pendingInterestTable.remove_if(MatchPendingInterestId(pendingInterestId));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800167}
168
169const RegisteredPrefixId*
170Face::setInterestFilter(const Name& prefix,
171 const OnInterest& onInterest,
172 const OnSetInterestFilterFailed& onSetInterestFilterFailed)
173{
174 shared_ptr<RegisteredPrefix> prefixToRegister(new RegisteredPrefix(prefix, onInterest));
175
176 m_fwController->selfRegisterPrefix(prefixToRegister->getPrefix(),
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800177 bind(&RegisteredPrefixTable::push_back, &m_registeredPrefixTable, prefixToRegister),
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800178 bind(onSetInterestFilterFailed, prefixToRegister->getPrefix(), _1));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800179
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800180 return reinterpret_cast<const RegisteredPrefixId*>(prefixToRegister.get());
181}
182
183void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800184Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800185{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800186 m_ioService->post(bind(&Face::asyncUnsetInterestFilter, this, registeredPrefixId));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800187}
188
189void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800190Face::asyncUnsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800191{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800192 RegisteredPrefixTable::iterator i = std::find_if(m_registeredPrefixTable.begin(), m_registeredPrefixTable.end(),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800193 MatchRegisteredPrefixId(registeredPrefixId));
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800194 if (i != m_registeredPrefixTable.end())
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800195 {
196 m_fwController->selfDeregisterPrefix((*i)->getPrefix(),
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800197 bind(&Face::finalizeUnsertInterestFilter, this, i),
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800198 Controller::FailCallback());
199 }
200
201 // there cannot be two registered prefixes with the same id. if there are, then something is broken
202}
203
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800204void
205Face::finalizeUnsertInterestFilter(RegisteredPrefixTable::iterator item)
206{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800207 m_registeredPrefixTable.erase(item);
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800208
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800209 if (!m_pitTimeoutCheckTimerActive && m_registeredPrefixTable.empty())
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800210 {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800211 m_transport->close();
212 if (!m_ioServiceWork) {
213 m_processEventsTimeoutTimer->cancel();
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800214 }
215 }
216}
217
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800218void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800219Face::processEvents(Milliseconds timeout/* = 0 */, bool keepThread/* = false*/)
220{
221 try
222 {
223 if (timeout < 0)
224 {
225 // do not block if timeout is negative, but process pending events
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800226 m_ioService->poll();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800227 return;
228 }
229
230 if (timeout > 0)
231 {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800232 m_processEventsTimeoutTimer->expires_from_now(boost::posix_time::milliseconds(timeout));
233 m_processEventsTimeoutTimer->async_wait(&fireProcessEventsTimeout);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800234 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800235
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800236 if (keepThread) {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800237 // work will ensure that m_ioService is running until work object exists
238 m_ioServiceWork = make_shared<boost::asio::io_service::work>(boost::ref(*m_ioService));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800239 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800240
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800241 m_ioService->run();
242 m_ioService->reset(); // so it is possible to run processEvents again (if necessary)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800243 }
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800244 catch(Face::ProcessEventsTimeout&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800245 {
246 // break
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800247 m_ioService->reset();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800248 }
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800249 catch(const std::exception&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800250 {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800251 m_ioService->reset();
252 m_pendingInterestTable.clear();
253 m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800254 throw;
255 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700256}
257
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800258void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700259Face::shutdown()
Jeff Thompson517ffa82013-08-05 16:04:34 -0700260{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800261 m_pendingInterestTable.clear();
262 m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800263
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800264 m_transport->close();
265 m_pitTimeoutCheckTimer->cancel();
266 m_processEventsTimeoutTimer->cancel();
267 m_pitTimeoutCheckTimerActive = false;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700268}
269
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800270void
271Face::fireProcessEventsTimeout(const boost::system::error_code& error)
272{
273 if (!error) // can fire for some other reason, e.g., cancelled
274 throw Face::ProcessEventsTimeout();
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700275}
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800276
277void
278Face::checkPitExpire()
279{
280 // Check for PIT entry timeouts. Go backwards through the list so we can erase entries.
281 MillisecondsSince1970 nowMilliseconds = getNowMilliseconds();
282
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800283 PendingInterestTable::iterator i = m_pendingInterestTable.begin();
284 while (i != m_pendingInterestTable.end())
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800285 {
286 if ((*i)->isTimedOut(nowMilliseconds))
287 {
288 // Save the PendingInterest and remove it from the PIT. Then call the callback.
289 shared_ptr<PendingInterest> pendingInterest = *i;
290
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800291 i = m_pendingInterestTable.erase(i);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800292
293 pendingInterest->callTimeout();
294 }
295 else
296 ++i;
297 }
298
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800299 if (!m_pendingInterestTable.empty()) {
300 m_pitTimeoutCheckTimerActive = true;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800301
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800302 m_pitTimeoutCheckTimer->expires_from_now(boost::posix_time::milliseconds(100));
303 m_pitTimeoutCheckTimer->async_wait(bind(&Face::checkPitExpire, this));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800304 }
305 else {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800306 m_pitTimeoutCheckTimerActive = false;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800307
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800308 if (m_registeredPrefixTable.empty()) {
309 m_transport->close();
310 if (!m_ioServiceWork) {
311 m_processEventsTimeoutTimer->cancel();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800312 }
313 }
314 }
315}
316
317
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800318void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800319Face::onReceiveElement(const Block& blockFromDaemon)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800320{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800321 const Block& block = nfd::LocalControlHeader::getPayload(blockFromDaemon);
322
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800323 if (block.type() == Tlv::Interest)
324 {
325 shared_ptr<Interest> interest(new Interest());
326 interest->wireDecode(block);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800327 if (&block != &blockFromDaemon)
328 interest->getLocalControlHeader().wireDecode(blockFromDaemon);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800329
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800330 RegisteredPrefixTable::iterator entry = getEntryForRegisteredPrefix(interest->getName());
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800331 if (entry != m_registeredPrefixTable.end()) {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800332 (*entry)->getOnInterest()((*entry)->getPrefix(), *interest);
333 }
334 }
335 else if (block.type() == Tlv::Data)
336 {
337 shared_ptr<Data> data(new Data());
338 data->wireDecode(block);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800339 if (&block != &blockFromDaemon)
340 data->getLocalControlHeader().wireDecode(blockFromDaemon);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800341
342 PendingInterestTable::iterator entry = getEntryIndexForExpressedInterest(data->getName());
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800343 if (entry != m_pendingInterestTable.end()) {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800344 // Copy pointers to the needed objects and remove the PIT entry before the calling the callback.
345 const OnData onData = (*entry)->getOnData();
346 const shared_ptr<const Interest> interest = (*entry)->getInterest();
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800347 m_pendingInterestTable.erase(entry);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800348
349 if (onData) {
350 onData(*interest, *data);
351 }
352
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800353 if (m_pendingInterestTable.empty()) {
354 m_pitTimeoutCheckTimer->cancel(); // this will cause checkPitExpire invocation
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800355 }
356 }
357 }
Yingdi Yuf9fa52f2014-02-06 12:27:32 -0800358 // ignore any other type
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800359}
360
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800361Face::PendingInterestTable::iterator
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800362Face::getEntryIndexForExpressedInterest(const Name& name)
363{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800364 for (PendingInterestTable::iterator i = m_pendingInterestTable.begin ();
365 i != m_pendingInterestTable.end(); ++i)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800366 {
367 if ((*i)->getInterest()->matchesName(name))
368 {
369 return i;
370 }
371 }
372
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800373 return m_pendingInterestTable.end();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800374}
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800375
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800376Face::RegisteredPrefixTable::iterator
377Face::getEntryForRegisteredPrefix(const Name& name)
378{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800379 RegisteredPrefixTable::iterator longestPrefix = m_registeredPrefixTable.end();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800380
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800381 for (RegisteredPrefixTable::iterator i = m_registeredPrefixTable.begin();
382 i != m_registeredPrefixTable.end();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800383 ++i)
384 {
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800385 if ((*i)->getPrefix().isPrefixOf(name))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800386 {
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800387
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800388 if (longestPrefix == m_registeredPrefixTable.end() ||
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800389 (*i)->getPrefix().size() > (*longestPrefix)->getPrefix().size())
390 {
391 longestPrefix = i;
392 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800393 }
394 }
395 return longestPrefix;
396}
397
398} // namespace ndn