blob: b1d34fd081acb2872debd566af4a584139b85439 [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/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070013 */
14
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080015#include "common.hpp"
16
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080017#include "face.hpp"
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070018
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080019#include "security/signature-sha256-with-rsa.hpp"
20
21#include "util/time.hpp"
22#include "util/random.hpp"
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060023#include "util/config-file.hpp"
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080024#include <cstdlib>
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080025
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080026#include "management/nfd-controller.hpp"
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070027
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070028namespace ndn {
Alexander Afanasyevb790d952014-01-24 12:07:53 -080029
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080030Face::Face()
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070031 : m_nfdController(new nfd::Controller(*this))
32 , m_isDirectNfdFibManagementRequested(false)
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070033{
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 make_shared<boost::asio::io_service>());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080037}
38
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080039Face::Face(const shared_ptr<boost::asio::io_service>& ioService)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070040 : m_nfdController(new nfd::Controller(*this))
41 , m_isDirectNfdFibManagementRequested(false)
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080042{
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060043 const std::string socketName = UnixTransport::getDefaultSocketName(m_config);
44 construct(shared_ptr<Transport>(new UnixTransport(socketName)),
Alexander Afanasyev505646e2014-02-24 20:13:37 -080045 ioService);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080046}
47
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070048class NullIoDeleter
49{
50public:
51 void
52 operator()(boost::asio::io_service*)
53 {
54 }
55};
56
57Face::Face(boost::asio::io_service& ioService)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070058 : m_nfdController(new nfd::Controller(*this))
59 , m_isDirectNfdFibManagementRequested(false)
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070060{
61 const std::string socketName = UnixTransport::getDefaultSocketName(m_config);
62 construct(shared_ptr<Transport>(new UnixTransport(socketName)),
63 shared_ptr<boost::asio::io_service>(&ioService, NullIoDeleter()));
64}
65
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080066Face::Face(const std::string& host, const std::string& port/* = "6363"*/)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070067 : m_nfdController(new nfd::Controller(*this))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080068{
69 construct(shared_ptr<Transport>(new TcpTransport(host, port)),
Alexander Afanasyev505646e2014-02-24 20:13:37 -080070 make_shared<boost::asio::io_service>());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080071}
72
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080073Face::Face(const shared_ptr<Transport>& transport)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070074 : m_nfdController(new nfd::Controller(*this))
75 , m_isDirectNfdFibManagementRequested(false)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080076{
77 construct(transport,
Alexander Afanasyev505646e2014-02-24 20:13:37 -080078 make_shared<boost::asio::io_service>());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080079}
80
81Face::Face(const shared_ptr<Transport>& transport,
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070082 boost::asio::io_service& ioService)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070083 : m_nfdController(new nfd::Controller(*this))
84 , m_isDirectNfdFibManagementRequested(false)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080085{
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070086 construct(transport,
87 shared_ptr<boost::asio::io_service>(&ioService, NullIoDeleter()));
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080088}
89
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070090Face::~Face()
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080091{
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070092 delete m_nfdController;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080093}
94
95void
96Face::construct(const shared_ptr<Transport>& transport,
Alexander Afanasyev505646e2014-02-24 20:13:37 -080097 const shared_ptr<boost::asio::io_service>& ioService)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080098{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -080099 m_pitTimeoutCheckTimerActive = false;
100 m_transport = transport;
101 m_ioService = ioService;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800102
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700103 m_pitTimeoutCheckTimer = make_shared<monotonic_deadline_timer>(ref(*m_ioService));
104 m_processEventsTimeoutTimer = make_shared<monotonic_deadline_timer>(ref(*m_ioService));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800105
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600106 std::string protocol = "nrd-0.1";
107
108 try
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800109 {
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600110 protocol = m_config.getParsedConfiguration().get<std::string>("protocol");
111 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700112 catch (boost::property_tree::ptree_bad_path& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600113 {
114 // protocol not specified
115 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700116 catch (boost::property_tree::ptree_bad_data& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600117 {
118 throw ConfigFile::Error(error.what());
119 }
120
121 if (isSupportedNrdProtocol(protocol))
122 {
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700123 // do nothing
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600124 }
Steve DiBenedettoacab8802014-03-24 11:15:57 -0600125 else if (isSupportedNfdProtocol(protocol))
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600126 {
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700127 m_isDirectNfdFibManagementRequested = true;
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800128 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700129 else
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600130 {
131 throw Face::Error("Cannot create controller for unsupported protocol \"" + protocol + "\"");
132 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800133}
134
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800135const PendingInterestId*
136Face::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
137{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800138 if (!m_transport->isConnected())
139 m_transport->connect(*m_ioService,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800140 bind(&Face::onReceiveElement, this, _1));
141
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700142 shared_ptr<Interest> interestToExpress(new Interest(interest));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800143
Alexander Afanasyev4e50b972014-03-25 10:57:50 -0700144 // If the same ioService thread, dispatch directly calls the method
145 m_ioService->dispatch(bind(&Face::asyncExpressInterest, this,
146 interestToExpress, onData, onTimeout));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800147
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800148 return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
149}
150
151const PendingInterestId*
152Face::expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800153 const Interest& tmpl,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800154 const OnData& onData, const OnTimeout& onTimeout/* = OnTimeout()*/)
155{
156 return expressInterest(Interest(name,
157 tmpl.getMinSuffixComponents(),
158 tmpl.getMaxSuffixComponents(),
159 tmpl.getExclude(),
160 tmpl.getChildSelector(),
161 tmpl.getMustBeFresh(),
162 tmpl.getScope(),
163 tmpl.getInterestLifetime()),
164 onData, onTimeout);
165}
166
167void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800168Face::asyncExpressInterest(const shared_ptr<const Interest>& interest,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800169 const OnData& onData, const OnTimeout& onTimeout)
170{
Alexander Afanasyev4e50b972014-03-25 10:57:50 -0700171 if (!m_transport->isExpectingData())
172 m_transport->resume();
173
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800174 m_pendingInterestTable.push_back(shared_ptr<PendingInterest>(new PendingInterest
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800175 (interest, onData, onTimeout)));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800176
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800177 if (!interest->getLocalControlHeader().empty(false, true))
178 {
179 // encode only NextHopFaceId towards the forwarder
180 m_transport->send(interest->getLocalControlHeader().wireEncode(*interest, false, true),
181 interest->wireEncode());
182 }
183 else
184 {
185 m_transport->send(interest->wireEncode());
186 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800187
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800188 if (!m_pitTimeoutCheckTimerActive) {
189 m_pitTimeoutCheckTimerActive = true;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700190 m_pitTimeoutCheckTimer->expires_from_now(time::milliseconds(100));
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800191 m_pitTimeoutCheckTimer->async_wait(bind(&Face::checkPitExpire, this));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800192 }
193}
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800194
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800195void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800196Face::put(const Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800197{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800198 if (!m_transport->isConnected())
199 m_transport->connect(*m_ioService,
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800200 bind(&Face::onReceiveElement, this, _1));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800201
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800202 if (!data.getLocalControlHeader().empty(false, true))
203 {
Alexander Afanasyev83018352014-02-18 19:52:15 -0800204 m_transport->send(data.getLocalControlHeader().wireEncode(data, false, true),
205 data.wireEncode());
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800206 }
207 else
208 {
Alexander Afanasyev83018352014-02-18 19:52:15 -0800209 m_transport->send(data.wireEncode());
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800210 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800211}
212
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800213void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800214Face::removePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800215{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800216 m_ioService->post(bind(&Face::asyncRemovePendingInterest, this, pendingInterestId));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800217}
218
219
220void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800221Face::asyncRemovePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800222{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800223 m_pendingInterestTable.remove_if(MatchPendingInterestId(pendingInterestId));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800224}
225
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700226
227template<class SignatureGenerator>
228const RegisteredPrefixId*
229Face::setInterestFilterImpl(const InterestFilter& interestFilter,
230 const OnInterest& onInterest,
231 const OnSetInterestFilterFailed& onSetInterestFilterFailed,
232 const SignatureGenerator& signatureGenerator)
233{
234 typedef void (nfd::Controller::*Registrator)
235 (const nfd::ControlParameters&,
236 const nfd::Controller::CommandSucceedCallback&,
237 const nfd::Controller::CommandFailCallback&,
238 const SignatureGenerator&,
239 const time::milliseconds&);
240
241 Registrator registrator, unregistrator;
242 if (!m_isDirectNfdFibManagementRequested) {
243 registrator = static_cast<Registrator>(&nfd::Controller::start<nfd::RibRegisterCommand>);
244 unregistrator = static_cast<Registrator>(&nfd::Controller::start<nfd::RibUnregisterCommand>);
245 }
246 else {
247 registrator = static_cast<Registrator>(&nfd::Controller::start<nfd::FibAddNextHopCommand>);
248 unregistrator = static_cast<Registrator>(&nfd::Controller::start<nfd::FibRemoveNextHopCommand>);
249 }
250
251 shared_ptr<InterestFilterRecord> filter =
252 make_shared<InterestFilterRecord>(interestFilter, onInterest);
253
254 nfd::ControlParameters parameters;
255 parameters.setName(interestFilter.getPrefix());
256
257 RegisteredPrefix::Unregistrator bindedUnregistrator =
258 bind(unregistrator, m_nfdController, parameters, _1, _2,
259 signatureGenerator,
260 m_nfdController->getDefaultCommandTimeout());
261
262 shared_ptr<RegisteredPrefix> prefixToRegister =
263 ndn::make_shared<RegisteredPrefix>(interestFilter.getPrefix(), filter, bindedUnregistrator);
264
265 (m_nfdController->*registrator)(parameters,
266 bind(&Face::afterPrefixRegistered, this, prefixToRegister),
267 bind(onSetInterestFilterFailed, prefixToRegister->getPrefix(), _2),
268 signatureGenerator,
269 m_nfdController->getDefaultCommandTimeout());
270
271 return reinterpret_cast<const RegisteredPrefixId*>(prefixToRegister.get());
272}
273
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800274const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000275Face::setInterestFilter(const InterestFilter& interestFilter,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800276 const OnInterest& onInterest,
277 const OnSetInterestFilterFailed& onSetInterestFilterFailed)
278{
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700279 return setInterestFilterImpl(interestFilter, onInterest, onSetInterestFilterFailed,
280 IdentityCertificate());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800281}
282
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700283const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000284Face::setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700285 const OnInterest& onInterest,
286 const OnSetInterestFilterFailed& onSetInterestFilterFailed,
287 const IdentityCertificate& certificate)
288{
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700289 return setInterestFilterImpl(interestFilter, onInterest, onSetInterestFilterFailed,
290 certificate);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700291}
292
293const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000294Face::setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700295 const OnInterest& onInterest,
296 const OnSetInterestFilterFailed& onSetInterestFilterFailed,
297 const Name& identity)
298{
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700299 return setInterestFilterImpl(interestFilter, onInterest, onSetInterestFilterFailed,
300 identity);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700301}
302
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800303void
Alexander Afanasyev90164962014-03-06 08:29:59 +0000304Face::afterPrefixRegistered(const shared_ptr<RegisteredPrefix>& registeredPrefix)
305{
306 m_registeredPrefixTable.push_back(registeredPrefix);
307
308 if (static_cast<bool>(registeredPrefix->getFilter()))
309 {
310 // it was a combined operation
311 m_interestFilterTable.push_back(registeredPrefix->getFilter());
312 }
313}
314
315void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800316Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800317{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800318 m_ioService->post(bind(&Face::asyncUnsetInterestFilter, this, registeredPrefixId));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800319}
320
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700321
322void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800323Face::asyncUnsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800324{
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700325 RegisteredPrefixTable::iterator i = std::find_if(m_registeredPrefixTable.begin(),
326 m_registeredPrefixTable.end(),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800327 MatchRegisteredPrefixId(registeredPrefixId));
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800328 if (i != m_registeredPrefixTable.end())
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800329 {
Alexander Afanasyev90164962014-03-06 08:29:59 +0000330 const shared_ptr<InterestFilterRecord>& filter = (*i)->getFilter();
331 if (static_cast<bool>(filter))
332 {
333 // it was a combined operation
334 m_interestFilterTable.remove(filter);
335 }
336
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700337 (*i)->unregister(bind(&Face::finalizeUnregisterPrefix, this, i),
338 RegisteredPrefix::FailureCallback());
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700339 }
340
341 // there cannot be two registered prefixes with the same id
342}
343
344void
Alexander Afanasyev90164962014-03-06 08:29:59 +0000345Face::finalizeUnregisterPrefix(RegisteredPrefixTable::iterator item)
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800346{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800347 m_registeredPrefixTable.erase(item);
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800348
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800349 if (!m_pitTimeoutCheckTimerActive && m_registeredPrefixTable.empty())
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800350 {
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000351 m_transport->pause();
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800352 if (!m_ioServiceWork) {
353 m_processEventsTimeoutTimer->cancel();
Alexander Afanasyev12dfbad2014-02-11 14:42:46 -0800354 }
355 }
356}
357
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800358void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700359Face::processEvents(const time::milliseconds& timeout/* = time::milliseconds::zero()*/,
360 bool keepThread/* = false*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800361{
362 try
363 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700364 if (timeout < time::milliseconds::zero())
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800365 {
366 // do not block if timeout is negative, but process pending events
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800367 m_ioService->poll();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800368 return;
369 }
370
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700371 if (timeout > time::milliseconds::zero())
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800372 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700373 m_processEventsTimeoutTimer->expires_from_now(time::milliseconds(timeout));
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800374 m_processEventsTimeoutTimer->async_wait(&fireProcessEventsTimeout);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800375 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800376
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800377 if (keepThread) {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800378 // work will ensure that m_ioService is running until work object exists
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700379 m_ioServiceWork = make_shared<boost::asio::io_service::work>(ref(*m_ioService));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800380 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800381
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800382 m_ioService->run();
383 m_ioService->reset(); // so it is possible to run processEvents again (if necessary)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800384 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700385 catch (Face::ProcessEventsTimeout&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800386 {
387 // break
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800388 m_ioService->reset();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800389 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700390 catch (std::exception&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800391 {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800392 m_ioService->reset();
393 m_pendingInterestTable.clear();
394 m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800395 throw;
396 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700397}
398
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800399void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700400Face::shutdown()
Jeff Thompson517ffa82013-08-05 16:04:34 -0700401{
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700402 m_ioService->post(bind(&Face::asyncShutdown, this));
403}
404
405void
406Face::asyncShutdown()
407{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800408 m_pendingInterestTable.clear();
409 m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800410
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800411 m_transport->close();
412 m_pitTimeoutCheckTimer->cancel();
413 m_processEventsTimeoutTimer->cancel();
414 m_pitTimeoutCheckTimerActive = false;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700415}
416
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800417void
418Face::fireProcessEventsTimeout(const boost::system::error_code& error)
419{
420 if (!error) // can fire for some other reason, e.g., cancelled
421 throw Face::ProcessEventsTimeout();
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700422}
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800423
424void
425Face::checkPitExpire()
426{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700427 // Check for PIT entry timeouts.
428 time::steady_clock::TimePoint now = time::steady_clock::now();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800429
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800430 PendingInterestTable::iterator i = m_pendingInterestTable.begin();
431 while (i != m_pendingInterestTable.end())
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800432 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700433 if ((*i)->isTimedOut(now))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800434 {
435 // Save the PendingInterest and remove it from the PIT. Then call the callback.
436 shared_ptr<PendingInterest> pendingInterest = *i;
437
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800438 i = m_pendingInterestTable.erase(i);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800439
440 pendingInterest->callTimeout();
441 }
442 else
443 ++i;
444 }
445
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800446 if (!m_pendingInterestTable.empty()) {
447 m_pitTimeoutCheckTimerActive = true;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800448
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700449 m_pitTimeoutCheckTimer->expires_from_now(time::milliseconds(100));
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800450 m_pitTimeoutCheckTimer->async_wait(bind(&Face::checkPitExpire, this));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800451 }
452 else {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800453 m_pitTimeoutCheckTimerActive = false;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800454
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800455 if (m_registeredPrefixTable.empty()) {
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000456 m_transport->pause();
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800457 if (!m_ioServiceWork) {
458 m_processEventsTimeoutTimer->cancel();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800459 }
460 }
461 }
462}
463
464
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800465void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800466Face::onReceiveElement(const Block& blockFromDaemon)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800467{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800468 const Block& block = nfd::LocalControlHeader::getPayload(blockFromDaemon);
469
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800470 if (block.type() == Tlv::Interest)
471 {
472 shared_ptr<Interest> interest(new Interest());
473 interest->wireDecode(block);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800474 if (&block != &blockFromDaemon)
475 interest->getLocalControlHeader().wireDecode(blockFromDaemon);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800476
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800477 processInterestFilters(*interest);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800478 }
479 else if (block.type() == Tlv::Data)
480 {
481 shared_ptr<Data> data(new Data());
482 data->wireDecode(block);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800483 if (&block != &blockFromDaemon)
484 data->getLocalControlHeader().wireDecode(blockFromDaemon);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800485
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800486 satisfyPendingInterests(*data);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800487
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800488 if (m_pendingInterestTable.empty()) {
489 m_pitTimeoutCheckTimer->cancel(); // this will cause checkPitExpire invocation
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800490 }
491 }
Yingdi Yuf9fa52f2014-02-06 12:27:32 -0800492 // ignore any other type
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800493}
494
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800495void
496Face::satisfyPendingInterests(Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800497{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800498 for (PendingInterestTable::iterator i = m_pendingInterestTable.begin ();
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800499 i != m_pendingInterestTable.end();
500 )
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800501 {
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700502 if ((*i)->getInterest()->matchesData(data))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800503 {
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -0700504 // Copy pointers to the objects and remove the PIT entry before calling the callback.
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800505 OnData onData = (*i)->getOnData();
506 shared_ptr<const Interest> interest = (*i)->getInterest();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800507
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800508 PendingInterestTable::iterator next = i;
509 ++next;
510 m_pendingInterestTable.erase(i);
511 i = next;
512
513 if (static_cast<bool>(onData)) {
514 onData(*interest, data);
515 }
516 }
517 else
518 ++i;
519 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800520}
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800521
Alexander Afanasyev42c81852014-02-25 21:37:26 -0800522void
523Face::processInterestFilters(Interest& interest)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800524{
Alexander Afanasyev90164962014-03-06 08:29:59 +0000525 for (InterestFilterTable::iterator i = m_interestFilterTable.begin();
526 i != m_interestFilterTable.end();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800527 ++i)
528 {
Alexander Afanasyev90164962014-03-06 08:29:59 +0000529 if ((*i)->doesMatch(interest.getName()))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800530 {
Alexander Afanasyev90164962014-03-06 08:29:59 +0000531 (**i)(interest);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800532 }
533 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800534}
535
536} // namespace ndn