blob: 3fa693a80c48ac4075c64422e0badc3bc4fe4703 [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 Afanasyev09c613f2014-01-29 00:23:58 -080015#include "face.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070016#include "detail/face-impl.hpp"
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070017
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070018#include "interest.hpp"
19#include "data.hpp"
20#include "security/identity-certificate.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080021
22#include "util/time.hpp"
23#include "util/random.hpp"
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070024
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070025namespace ndn {
Alexander Afanasyevb790d952014-01-24 12:07:53 -080026
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080027Face::Face()
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070028 : m_nfdController(new nfd::Controller(*this))
29 , m_isDirectNfdFibManagementRequested(false)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070030 , m_impl(make_shared<Impl>(ref(*this)))
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070031{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070032 const std::string socketName = UnixTransport::getDefaultSocketName(m_impl->m_config);
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070033 construct(make_shared<UnixTransport>(socketName),
Alexander Afanasyev505646e2014-02-24 20:13:37 -080034 make_shared<boost::asio::io_service>());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080035}
36
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080037Face::Face(const shared_ptr<boost::asio::io_service>& ioService)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070038 : m_nfdController(new nfd::Controller(*this))
39 , m_isDirectNfdFibManagementRequested(false)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070040 , m_impl(make_shared<Impl>(ref(*this)))
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080041{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070042 const std::string socketName = UnixTransport::getDefaultSocketName(m_impl->m_config);
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070043 construct(make_shared<UnixTransport>(socketName),
Alexander Afanasyev505646e2014-02-24 20:13:37 -080044 ioService);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080045}
46
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070047class NullIoDeleter
48{
49public:
50 void
51 operator()(boost::asio::io_service*)
52 {
53 }
54};
55
56Face::Face(boost::asio::io_service& ioService)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070057 : m_nfdController(make_shared<nfd::Controller>(ref(*this)))
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070058 , m_isDirectNfdFibManagementRequested(false)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070059 , m_impl(make_shared<Impl>(ref(*this)))
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070060{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070061 const std::string socketName = UnixTransport::getDefaultSocketName(m_impl->m_config);
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070062 construct(make_shared<UnixTransport>(socketName),
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070063 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 Afanasyev258ec2b2014-05-14 16:15:37 -070067 : m_nfdController(make_shared<nfd::Controller>(ref(*this)))
68 , m_impl(make_shared<Impl>(ref(*this)))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080069{
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070070 construct(make_shared<TcpTransport>(host, port),
Alexander Afanasyev505646e2014-02-24 20:13:37 -080071 make_shared<boost::asio::io_service>());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080072}
73
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080074Face::Face(const shared_ptr<Transport>& transport)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070075 : m_nfdController(make_shared<nfd::Controller>(ref(*this)))
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070076 , m_isDirectNfdFibManagementRequested(false)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070077 , m_impl(make_shared<Impl>(ref(*this)))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080078{
79 construct(transport,
Alexander Afanasyev505646e2014-02-24 20:13:37 -080080 make_shared<boost::asio::io_service>());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080081}
82
83Face::Face(const shared_ptr<Transport>& transport,
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070084 boost::asio::io_service& ioService)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070085 : m_nfdController(make_shared<nfd::Controller>(ref(*this)))
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070086 , m_isDirectNfdFibManagementRequested(false)
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070087 , m_impl(make_shared<Impl>(ref(*this)))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080088{
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070089 construct(transport,
90 shared_ptr<boost::asio::io_service>(&ioService, NullIoDeleter()));
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080091}
92
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080093void
94Face::construct(const shared_ptr<Transport>& transport,
Alexander Afanasyev505646e2014-02-24 20:13:37 -080095 const shared_ptr<boost::asio::io_service>& ioService)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080096{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070097 m_impl->m_pitTimeoutCheckTimerActive = false;
Alexander Afanasyevf39c5372014-02-17 19:42:56 -080098 m_transport = transport;
99 m_ioService = ioService;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800100
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700101 m_impl->m_pitTimeoutCheckTimer = make_shared<monotonic_deadline_timer>(ref(*m_ioService));
102 m_impl->m_processEventsTimeoutTimer = make_shared<monotonic_deadline_timer>(ref(*m_ioService));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800103
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600104 std::string protocol = "nrd-0.1";
105
106 try
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800107 {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700108 protocol = m_impl->m_config.getParsedConfiguration().get<std::string>("protocol");
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600109 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700110 catch (boost::property_tree::ptree_bad_path& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600111 {
112 // protocol not specified
113 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700114 catch (boost::property_tree::ptree_bad_data& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600115 {
116 throw ConfigFile::Error(error.what());
117 }
118
119 if (isSupportedNrdProtocol(protocol))
120 {
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700121 // do nothing
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600122 }
Steve DiBenedettoacab8802014-03-24 11:15:57 -0600123 else if (isSupportedNfdProtocol(protocol))
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600124 {
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700125 m_isDirectNfdFibManagementRequested = true;
Alexander Afanasyevefe3ab22014-02-19 14:57:50 -0800126 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700127 else
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600128 {
129 throw Face::Error("Cannot create controller for unsupported protocol \"" + protocol + "\"");
130 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800131}
132
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800133const PendingInterestId*
134Face::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
135{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800136 if (!m_transport->isConnected())
137 m_transport->connect(*m_ioService,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800138 bind(&Face::onReceiveElement, this, _1));
139
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700140 shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800141
Alexander Afanasyev4e50b972014-03-25 10:57:50 -0700142 // If the same ioService thread, dispatch directly calls the method
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700143 m_ioService->dispatch(bind(&Impl::asyncExpressInterest, m_impl,
Alexander Afanasyev4e50b972014-03-25 10:57:50 -0700144 interestToExpress, onData, onTimeout));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800145
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800146 return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
147}
148
149const PendingInterestId*
150Face::expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800151 const Interest& tmpl,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800152 const OnData& onData, const OnTimeout& onTimeout/* = OnTimeout()*/)
153{
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700154 return expressInterest(Interest(tmpl)
155 .setName(name)
156 .setNonce(0),
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800157 onData, onTimeout);
158}
159
160void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800161Face::put(const Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800162{
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800163 if (!m_transport->isConnected())
164 m_transport->connect(*m_ioService,
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800165 bind(&Face::onReceiveElement, this, _1));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800166
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800167 if (!data.getLocalControlHeader().empty(false, true))
168 {
Alexander Afanasyev83018352014-02-18 19:52:15 -0800169 m_transport->send(data.getLocalControlHeader().wireEncode(data, false, true),
170 data.wireEncode());
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800171 }
172 else
173 {
Alexander Afanasyev83018352014-02-18 19:52:15 -0800174 m_transport->send(data.wireEncode());
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800175 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800176}
177
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800178void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800179Face::removePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800180{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700181 m_ioService->post(bind(&Impl::asyncRemovePendingInterest, m_impl, pendingInterestId));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800182}
183
184
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700185
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800186const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000187Face::setInterestFilter(const InterestFilter& interestFilter,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800188 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700189 const RegisterPrefixSuccessCallback& onSuccess,
190 const RegisterPrefixFailureCallback& onFailure,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700191 const IdentityCertificate& certificate)
192{
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700193 shared_ptr<InterestFilterRecord> filter =
194 make_shared<InterestFilterRecord>(interestFilter, onInterest);
195
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700196 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
197 onSuccess, onFailure,
198 certificate);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700199}
200
201const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000202Face::setInterestFilter(const InterestFilter& interestFilter,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700203 const OnInterest& onInterest,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700204 const RegisterPrefixFailureCallback& onFailure,
205 const IdentityCertificate& certificate)
206{
207 shared_ptr<InterestFilterRecord> filter =
208 make_shared<InterestFilterRecord>(interestFilter, onInterest);
209
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700210 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
211 RegisterPrefixSuccessCallback(), onFailure,
212 certificate);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700213}
214
215const RegisteredPrefixId*
216Face::setInterestFilter(const InterestFilter& interestFilter,
217 const OnInterest& onInterest,
218 const RegisterPrefixSuccessCallback& onSuccess,
219 const RegisterPrefixFailureCallback& onFailure,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700220 const Name& identity)
221{
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700222 shared_ptr<InterestFilterRecord> filter =
223 make_shared<InterestFilterRecord>(interestFilter, onInterest);
224
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700225 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
226 onSuccess, onFailure,
227 identity);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700228}
229
230const RegisteredPrefixId*
231Face::setInterestFilter(const InterestFilter& interestFilter,
232 const OnInterest& onInterest,
233 const RegisterPrefixFailureCallback& onFailure,
234 const Name& identity)
235{
236 shared_ptr<InterestFilterRecord> filter =
237 make_shared<InterestFilterRecord>(interestFilter, onInterest);
238
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700239 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
240 RegisterPrefixSuccessCallback(), onFailure,
241 identity);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700242}
243
244
245const InterestFilterId*
246Face::setInterestFilter(const InterestFilter& interestFilter,
247 const OnInterest& onInterest)
248{
249 shared_ptr<InterestFilterRecord> filter =
250 make_shared<InterestFilterRecord>(interestFilter, onInterest);
251
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700252 getIoService().post(bind(&Impl::asyncSetInterestFilter, m_impl, filter));
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700253
254 return reinterpret_cast<const InterestFilterId*>(filter.get());
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700255}
256
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700257const RegisteredPrefixId*
258Face::registerPrefix(const Name& prefix,
259 const RegisterPrefixSuccessCallback& onSuccess,
260 const RegisterPrefixFailureCallback& onFailure,
261 const IdentityCertificate& certificate)
262{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700263 return m_impl->registerPrefix(prefix, shared_ptr<InterestFilterRecord>(),
264 onSuccess, onFailure,
265 certificate);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700266}
267
268const RegisteredPrefixId*
269Face::registerPrefix(const Name& prefix,
270 const RegisterPrefixSuccessCallback& onSuccess,
271 const RegisterPrefixFailureCallback& onFailure,
272 const Name& identity)
273{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700274 return m_impl->registerPrefix(prefix, shared_ptr<InterestFilterRecord>(),
275 onSuccess, onFailure,
276 identity);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700277}
278
279
280void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800281Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800282{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700283 m_ioService->post(bind(&Impl::asyncUnregisterPrefix, m_impl, registeredPrefixId,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700284 UnregisterPrefixSuccessCallback(), UnregisterPrefixFailureCallback()));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800285}
286
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700287void
288Face::unsetInterestFilter(const InterestFilterId* interestFilterId)
289{
290 m_ioService->post(bind(&Impl::asyncUnsetInterestFilter, m_impl, interestFilterId));
291}
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700292
293void
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700294Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
295 const UnregisterPrefixSuccessCallback& onSuccess,
296 const UnregisterPrefixFailureCallback& onFailure)
297{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700298 m_ioService->post(bind(&Impl::asyncUnregisterPrefix, m_impl, registeredPrefixId,
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700299 onSuccess, onFailure));
300}
301
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800302void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700303Face::processEvents(const time::milliseconds& timeout/* = time::milliseconds::zero()*/,
304 bool keepThread/* = false*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800305{
306 try
307 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700308 if (timeout < time::milliseconds::zero())
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800309 {
310 // do not block if timeout is negative, but process pending events
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800311 m_ioService->poll();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800312 return;
313 }
314
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700315 if (timeout > time::milliseconds::zero())
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800316 {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700317 m_impl->m_processEventsTimeoutTimer->expires_from_now(time::milliseconds(timeout));
318 m_impl->m_processEventsTimeoutTimer->async_wait(&fireProcessEventsTimeout);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800319 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800320
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800321 if (keepThread) {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800322 // work will ensure that m_ioService is running until work object exists
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700323 m_impl->m_ioServiceWork = make_shared<boost::asio::io_service::work>(ref(*m_ioService));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800324 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800325
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800326 m_ioService->run();
327 m_ioService->reset(); // so it is possible to run processEvents again (if necessary)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800328 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700329 catch (Face::ProcessEventsTimeout&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800330 {
331 // break
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800332 m_ioService->reset();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800333 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700334 catch (std::exception&)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800335 {
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800336 m_ioService->reset();
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700337 m_impl->m_pendingInterestTable.clear();
338 m_impl->m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800339 throw;
340 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700341}
342
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800343void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700344Face::shutdown()
Jeff Thompson517ffa82013-08-05 16:04:34 -0700345{
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700346 m_ioService->post(bind(&Face::asyncShutdown, this));
347}
348
349void
350Face::asyncShutdown()
351{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700352 m_impl->m_pendingInterestTable.clear();
353 m_impl->m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800354
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700355 if (m_transport->isConnected())
356 m_transport->close();
357
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700358 m_impl->m_pitTimeoutCheckTimer->cancel();
359 m_impl->m_processEventsTimeoutTimer->cancel();
360 m_impl->m_pitTimeoutCheckTimerActive = false;
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700361}
362
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800363void
364Face::fireProcessEventsTimeout(const boost::system::error_code& error)
365{
366 if (!error) // can fire for some other reason, e.g., cancelled
367 throw Face::ProcessEventsTimeout();
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700368}
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800369
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800370
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800371void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800372Face::onReceiveElement(const Block& blockFromDaemon)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800373{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800374 const Block& block = nfd::LocalControlHeader::getPayload(blockFromDaemon);
375
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800376 if (block.type() == Tlv::Interest)
377 {
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700378 shared_ptr<Interest> interest = make_shared<Interest>();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800379 interest->wireDecode(block);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800380 if (&block != &blockFromDaemon)
381 interest->getLocalControlHeader().wireDecode(blockFromDaemon);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800382
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700383 m_impl->processInterestFilters(*interest);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800384 }
385 else if (block.type() == Tlv::Data)
386 {
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700387 shared_ptr<Data> data = make_shared<Data>();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800388 data->wireDecode(block);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800389 if (&block != &blockFromDaemon)
390 data->getLocalControlHeader().wireDecode(blockFromDaemon);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800391
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700392 m_impl->satisfyPendingInterests(*data);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800393
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700394 if (m_impl->m_pendingInterestTable.empty()) {
395 m_impl->m_pitTimeoutCheckTimer->cancel(); // this will cause checkPitExpire invocation
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800396 }
397 }
Yingdi Yuf9fa52f2014-02-06 12:27:32 -0800398 // ignore any other type
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800399}
400
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800401
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800402
403} // namespace ndn