blob: 2773f07ac44d775ee97e210488ce914e3699adc0 [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"
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060016#include "util/config-file.hpp"
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080017#include <cstdlib>
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080018
19#include "management/ndnd-controller.hpp"
20#include "management/nfd-controller.hpp"
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080021#include "management/nrd-controller.hpp"
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070022
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070023namespace ndn {
Alexander Afanasyevb790d952014-01-24 12:07:53 -080024
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080025Face::Face()
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070026{
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060027 const std::string socketName = UnixTransport::getDefaultSocketName(m_config);
28 construct(shared_ptr<Transport>(new UnixTransport(socketName)),
Alexander Afanasyev505646e2014-02-24 20:13:37 -080029 make_shared<boost::asio::io_service>());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080030}
31
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080032Face::Face(const shared_ptr<boost::asio::io_service>& ioService)
33{
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060034 const std::string socketName = UnixTransport::getDefaultSocketName(m_config);
35 construct(shared_ptr<Transport>(new UnixTransport(socketName)),
Alexander Afanasyev505646e2014-02-24 20:13:37 -080036 ioService);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080037}
38
39Face::Face(const std::string& host, const std::string& port/* = "6363"*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080040{
41 construct(shared_ptr<Transport>(new TcpTransport(host, port)),
Alexander Afanasyev505646e2014-02-24 20:13:37 -080042 make_shared<boost::asio::io_service>());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080043}
44
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080045Face::Face(const shared_ptr<Transport>& transport)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080046{
47 construct(transport,
Alexander Afanasyev505646e2014-02-24 20:13:37 -080048 make_shared<boost::asio::io_service>());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080049}
50
51Face::Face(const shared_ptr<Transport>& transport,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080052 const shared_ptr<boost::asio::io_service>& ioService)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080053{
Alexander Afanasyev505646e2014-02-24 20:13:37 -080054 construct(transport, ioService);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080055}
56
Alexander Afanasyev505646e2014-02-24 20:13:37 -080057void
58Face::setController(const shared_ptr<Controller>& controller)
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080059{
Alexander Afanasyev505646e2014-02-24 20:13:37 -080060 m_fwController = controller;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080061}
62
63void
64Face::construct(const shared_ptr<Transport>& transport,
Alexander Afanasyev505646e2014-02-24 20:13:37 -080065 const shared_ptr<boost::asio::io_service>& ioService)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080066{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -080067 m_pitTimeoutCheckTimerActive = false;
68 m_transport = transport;
69 m_ioService = ioService;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080070
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070071 m_pitTimeoutCheckTimer = make_shared<monotonic_deadline_timer>(boost::ref(*m_ioService));
72 m_processEventsTimeoutTimer = make_shared<monotonic_deadline_timer>(boost::ref(*m_ioService));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080073
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060074 std::string protocol = "nrd-0.1";
75
76 try
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -080077 {
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060078 protocol = m_config.getParsedConfiguration().get<std::string>("protocol");
79 }
80 catch (const boost::property_tree::ptree_bad_path& error)
81 {
82 // protocol not specified
83 }
84 catch (const boost::property_tree::ptree_bad_data& error)
85 {
86 throw ConfigFile::Error(error.what());
87 }
88
89 if (isSupportedNrdProtocol(protocol))
90 {
91 m_fwController = make_shared<nrd::Controller>(boost::ref(*this));
92 }
93 if (isSupportedNfdProtocol(protocol))
94 {
95 m_fwController = make_shared<nfd::Controller>(boost::ref(*this));
96 }
97 else if (isSupportedNdndProtocol(protocol))
98 {
99 m_fwController = make_shared<ndnd::Controller>(boost::ref(*this));
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800100 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700101 else
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600102 {
103 throw Face::Error("Cannot create controller for unsupported protocol \"" + protocol + "\"");
104 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800105}
106
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800107const PendingInterestId*
108Face::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
109{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800110 if (!m_transport->isConnected())
111 m_transport->connect(*m_ioService,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800112 bind(&Face::onReceiveElement, this, _1));
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000113 else if (!m_transport->isExpectingData())
114 m_transport->resume();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800115
116 shared_ptr<const Interest> interestToExpress(new Interest(interest));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800117
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800118 m_ioService->post(bind(&Face::asyncExpressInterest, this, interestToExpress, onData, onTimeout));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800119
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800120 return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
121}
122
123const PendingInterestId*
124Face::expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800125 const Interest& tmpl,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800126 const OnData& onData, const OnTimeout& onTimeout/* = OnTimeout()*/)
127{
128 return expressInterest(Interest(name,
129 tmpl.getMinSuffixComponents(),
130 tmpl.getMaxSuffixComponents(),
131 tmpl.getExclude(),
132 tmpl.getChildSelector(),
133 tmpl.getMustBeFresh(),
134 tmpl.getScope(),
135 tmpl.getInterestLifetime()),
136 onData, onTimeout);
137}
138
139void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800140Face::asyncExpressInterest(const shared_ptr<const Interest>& interest,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800141 const OnData& onData, const OnTimeout& onTimeout)
142{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800143 m_pendingInterestTable.push_back(shared_ptr<PendingInterest>(new PendingInterest
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800144 (interest, onData, onTimeout)));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800145
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800146 if (!interest->getLocalControlHeader().empty(false, true))
147 {
148 // encode only NextHopFaceId towards the forwarder
149 m_transport->send(interest->getLocalControlHeader().wireEncode(*interest, false, true),
150 interest->wireEncode());
151 }
152 else
153 {
154 m_transport->send(interest->wireEncode());
155 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800156
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800157 if (!m_pitTimeoutCheckTimerActive) {
158 m_pitTimeoutCheckTimerActive = true;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700159 m_pitTimeoutCheckTimer->expires_from_now(time::milliseconds(100));
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800160 m_pitTimeoutCheckTimer->async_wait(bind(&Face::checkPitExpire, this));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800161 }
162}
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800163
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800164void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800165Face::put(const Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800166{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800167 if (!m_transport->isConnected())
168 m_transport->connect(*m_ioService,
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800169 bind(&Face::onReceiveElement, this, _1));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800170
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800171 if (!data.getLocalControlHeader().empty(false, true))
172 {
Alexander Afanasyev83018352014-02-18 19:52:15 -0800173 m_transport->send(data.getLocalControlHeader().wireEncode(data, false, true),
174 data.wireEncode());
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800175 }
176 else
177 {
Alexander Afanasyev83018352014-02-18 19:52:15 -0800178 m_transport->send(data.wireEncode());
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800179 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800180}
181
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800182void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800183Face::removePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800184{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800185 m_ioService->post(bind(&Face::asyncRemovePendingInterest, this, pendingInterestId));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800186}
187
188
189void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800190Face::asyncRemovePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800191{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800192 m_pendingInterestTable.remove_if(MatchPendingInterestId(pendingInterestId));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800193}
194
195const RegisteredPrefixId*
196Face::setInterestFilter(const Name& prefix,
197 const OnInterest& onInterest,
198 const OnSetInterestFilterFailed& onSetInterestFilterFailed)
199{
200 shared_ptr<RegisteredPrefix> prefixToRegister(new RegisteredPrefix(prefix, onInterest));
201
202 m_fwController->selfRegisterPrefix(prefixToRegister->getPrefix(),
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800203 bind(&RegisteredPrefixTable::push_back, &m_registeredPrefixTable, prefixToRegister),
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800204 bind(onSetInterestFilterFailed, prefixToRegister->getPrefix(), _1));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800205
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800206 return reinterpret_cast<const RegisteredPrefixId*>(prefixToRegister.get());
207}
208
209void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800210Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800211{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800212 m_ioService->post(bind(&Face::asyncUnsetInterestFilter, this, registeredPrefixId));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800213}
214
215void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800216Face::asyncUnsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800217{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800218 RegisteredPrefixTable::iterator i = std::find_if(m_registeredPrefixTable.begin(), m_registeredPrefixTable.end(),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800219 MatchRegisteredPrefixId(registeredPrefixId));
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800220 if (i != m_registeredPrefixTable.end())
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800221 {
222 m_fwController->selfDeregisterPrefix((*i)->getPrefix(),
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000223 bind(&Face::finalizeUnsetInterestFilter, this, i),
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800224 Controller::FailCallback());
225 }
226
227 // there cannot be two registered prefixes with the same id. if there are, then something is broken
228}
229
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800230void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000231Face::finalizeUnsetInterestFilter(RegisteredPrefixTable::iterator item)
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800232{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800233 m_registeredPrefixTable.erase(item);
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800234
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800235 if (!m_pitTimeoutCheckTimerActive && m_registeredPrefixTable.empty())
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800236 {
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000237 m_transport->pause();
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800238 if (!m_ioServiceWork) {
239 m_processEventsTimeoutTimer->cancel();
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800240 }
241 }
242}
243
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800244void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700245Face::processEvents(const time::milliseconds& timeout/* = time::milliseconds::zero()*/,
246 bool keepThread/* = false*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800247{
248 try
249 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700250 if (timeout < time::milliseconds::zero())
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800251 {
252 // do not block if timeout is negative, but process pending events
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800253 m_ioService->poll();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800254 return;
255 }
256
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700257 if (timeout > time::milliseconds::zero())
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800258 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700259 m_processEventsTimeoutTimer->expires_from_now(time::milliseconds(timeout));
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800260 m_processEventsTimeoutTimer->async_wait(&fireProcessEventsTimeout);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800261 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800262
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800263 if (keepThread) {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800264 // work will ensure that m_ioService is running until work object exists
265 m_ioServiceWork = make_shared<boost::asio::io_service::work>(boost::ref(*m_ioService));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800266 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800267
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800268 m_ioService->run();
269 m_ioService->reset(); // so it is possible to run processEvents again (if necessary)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800270 }
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800271 catch(Face::ProcessEventsTimeout&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800272 {
273 // break
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800274 m_ioService->reset();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800275 }
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800276 catch(const std::exception&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800277 {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800278 m_ioService->reset();
279 m_pendingInterestTable.clear();
280 m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800281 throw;
282 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700283}
284
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800285void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700286Face::shutdown()
Jeff Thompson517ffa82013-08-05 16:04:34 -0700287{
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700288 m_ioService->post(bind(&Face::asyncShutdown, this));
289}
290
291void
292Face::asyncShutdown()
293{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800294 m_pendingInterestTable.clear();
295 m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800296
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800297 m_transport->close();
298 m_pitTimeoutCheckTimer->cancel();
299 m_processEventsTimeoutTimer->cancel();
300 m_pitTimeoutCheckTimerActive = false;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700301}
302
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800303void
304Face::fireProcessEventsTimeout(const boost::system::error_code& error)
305{
306 if (!error) // can fire for some other reason, e.g., cancelled
307 throw Face::ProcessEventsTimeout();
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700308}
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800309
310void
311Face::checkPitExpire()
312{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700313 // Check for PIT entry timeouts.
314 time::steady_clock::TimePoint now = time::steady_clock::now();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800315
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800316 PendingInterestTable::iterator i = m_pendingInterestTable.begin();
317 while (i != m_pendingInterestTable.end())
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800318 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700319 if ((*i)->isTimedOut(now))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800320 {
321 // Save the PendingInterest and remove it from the PIT. Then call the callback.
322 shared_ptr<PendingInterest> pendingInterest = *i;
323
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800324 i = m_pendingInterestTable.erase(i);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800325
326 pendingInterest->callTimeout();
327 }
328 else
329 ++i;
330 }
331
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800332 if (!m_pendingInterestTable.empty()) {
333 m_pitTimeoutCheckTimerActive = true;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800334
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700335 m_pitTimeoutCheckTimer->expires_from_now(time::milliseconds(100));
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800336 m_pitTimeoutCheckTimer->async_wait(bind(&Face::checkPitExpire, this));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800337 }
338 else {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800339 m_pitTimeoutCheckTimerActive = false;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800340
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800341 if (m_registeredPrefixTable.empty()) {
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000342 m_transport->pause();
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800343 if (!m_ioServiceWork) {
344 m_processEventsTimeoutTimer->cancel();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800345 }
346 }
347 }
348}
349
350
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800351void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800352Face::onReceiveElement(const Block& blockFromDaemon)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800353{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800354 const Block& block = nfd::LocalControlHeader::getPayload(blockFromDaemon);
355
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800356 if (block.type() == Tlv::Interest)
357 {
358 shared_ptr<Interest> interest(new Interest());
359 interest->wireDecode(block);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800360 if (&block != &blockFromDaemon)
361 interest->getLocalControlHeader().wireDecode(blockFromDaemon);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800362
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800363 processInterestFilters(*interest);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800364 }
365 else if (block.type() == Tlv::Data)
366 {
367 shared_ptr<Data> data(new Data());
368 data->wireDecode(block);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800369 if (&block != &blockFromDaemon)
370 data->getLocalControlHeader().wireDecode(blockFromDaemon);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800371
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800372 satisfyPendingInterests(*data);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800373
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800374 if (m_pendingInterestTable.empty()) {
375 m_pitTimeoutCheckTimer->cancel(); // this will cause checkPitExpire invocation
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800376 }
377 }
Yingdi Yuf9fa52f2014-02-06 12:27:32 -0800378 // ignore any other type
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800379}
380
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800381void
382Face::satisfyPendingInterests(Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800383{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800384 for (PendingInterestTable::iterator i = m_pendingInterestTable.begin ();
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800385 i != m_pendingInterestTable.end();
386 )
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800387 {
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800388 if ((*i)->getInterest()->matchesName(data.getName()))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800389 {
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800390 // Copy pointers to the needed objects and remove the PIT entry before the calling the callback.
391 OnData onData = (*i)->getOnData();
392 shared_ptr<const Interest> interest = (*i)->getInterest();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800393
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800394 PendingInterestTable::iterator next = i;
395 ++next;
396 m_pendingInterestTable.erase(i);
397 i = next;
398
399 if (static_cast<bool>(onData)) {
400 onData(*interest, data);
401 }
402 }
403 else
404 ++i;
405 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800406}
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800407
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800408void
409Face::processInterestFilters(Interest& interest)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800410{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800411 for (RegisteredPrefixTable::iterator i = m_registeredPrefixTable.begin();
412 i != m_registeredPrefixTable.end();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800413 ++i)
414 {
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800415 if ((*i)->getPrefix().isPrefixOf(interest.getName()))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800416 {
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800417 (*i)->getOnInterest()((*i)->getPrefix(), interest);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800418 }
419 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800420}
421
422} // namespace ndn