blob: c77d3e0e445b9e9659c8bca7ba5618fe2e270c13 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07002/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070020 */
21
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080022#include "face.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070023#include "detail/face-impl.hpp"
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070024
Junxiao Shi468abc32014-11-04 09:12:47 -070025#include "encoding/tlv.hpp"
Junxiao Shic6acc7a2015-06-23 10:03:56 -070026#include "security/signing-helpers.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080027#include "util/time.hpp"
28#include "util/random.hpp"
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070029#include "util/face-uri.hpp"
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070030
Junxiao Shiae0b4182016-08-08 22:53:17 +000031// A callback scheduled through io.post and io.dispatch may be invoked after the face
32// is destructed. To prevent this situation, these macros captures Face::m_impl as weak_ptr,
33// and skips callback execution if the face has been destructed.
34#define IO_CAPTURE_WEAK_IMPL(OP) \
35 { \
36 weak_ptr<Impl> implWeak(m_impl); \
37 m_ioService.OP([=] { \
38 auto impl = implWeak.lock(); \
39 if (impl != nullptr) {
40#define IO_CAPTURE_WEAK_IMPL_END \
41 } \
42 }); \
43 }
44
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070045namespace ndn {
Alexander Afanasyevb790d952014-01-24 12:07:53 -080046
Junxiao Shi103d8ed2016-08-07 20:34:10 +000047Face::Face(shared_ptr<Transport> transport)
Junxiao Shi2cced062014-11-02 21:27:38 -070048 : m_internalIoService(new boost::asio::io_service())
49 , m_ioService(*m_internalIoService)
50 , m_internalKeyChain(new KeyChain())
Junxiao Shiae0b4182016-08-08 22:53:17 +000051 , m_impl(make_shared<Impl>(*this))
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070052{
Junxiao Shi103d8ed2016-08-07 20:34:10 +000053 construct(transport, *m_internalKeyChain);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080054}
55
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070056Face::Face(boost::asio::io_service& ioService)
Junxiao Shi2cced062014-11-02 21:27:38 -070057 : m_ioService(ioService)
58 , m_internalKeyChain(new KeyChain())
Junxiao Shiae0b4182016-08-08 22:53:17 +000059 , m_impl(make_shared<Impl>(*this))
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070060{
Alexander Afanasyevbb64c172015-12-29 20:32:45 -080061 construct(nullptr, *m_internalKeyChain);
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070062}
63
Junxiao Shi103d8ed2016-08-07 20:34:10 +000064Face::Face(const std::string& host, const std::string& port)
Junxiao Shi2cced062014-11-02 21:27:38 -070065 : m_internalIoService(new boost::asio::io_service())
66 , m_ioService(*m_internalIoService)
67 , m_internalKeyChain(new KeyChain())
Junxiao Shiae0b4182016-08-08 22:53:17 +000068 , m_impl(make_shared<Impl>(*this))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080069{
Joao Pereira68c0d882015-05-19 14:27:55 -040070 construct(make_shared<TcpTransport>(host, port), *m_internalKeyChain);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080071}
72
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070073Face::Face(shared_ptr<Transport> transport, KeyChain& keyChain)
74 : m_internalIoService(new boost::asio::io_service())
75 , m_ioService(*m_internalIoService)
Junxiao Shiae0b4182016-08-08 22:53:17 +000076 , m_impl(make_shared<Impl>(*this))
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070077{
78 construct(transport, keyChain);
79}
80
81Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService)
Junxiao Shi2cced062014-11-02 21:27:38 -070082 : m_ioService(ioService)
83 , m_internalKeyChain(new KeyChain())
Junxiao Shiae0b4182016-08-08 22:53:17 +000084 , m_impl(make_shared<Impl>(*this))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080085{
Joao Pereira68c0d882015-05-19 14:27:55 -040086 construct(transport, *m_internalKeyChain);
Junxiao Shiedd834e2014-10-28 20:28:58 -070087}
88
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070089Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain)
Junxiao Shi2cced062014-11-02 21:27:38 -070090 : m_ioService(ioService)
Junxiao Shiae0b4182016-08-08 22:53:17 +000091 , m_impl(make_shared<Impl>(*this))
Junxiao Shiedd834e2014-10-28 20:28:58 -070092{
Joao Pereira68c0d882015-05-19 14:27:55 -040093 construct(transport, keyChain);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080094}
95
Alexander Afanasyevbb64c172015-12-29 20:32:45 -080096shared_ptr<Transport>
97Face::makeDefaultTransport()
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070098{
99 // transport=unix:///var/run/nfd.sock
100 // transport=tcp://localhost:6363
101
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700102 std::string transportUri;
103
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000104 const char* transportEnviron = getenv("NDN_CLIENT_TRANSPORT");
105 if (transportEnviron != nullptr) {
106 transportUri = transportEnviron;
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700107 }
108 else {
109 ConfigFile config;
110 transportUri = config.getParsedConfiguration().get<std::string>("transport", "");
111 }
112
113 if (transportUri.empty()) {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800114 // transport not specified, use default Unix transport.
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700115 return UnixTransport::create("");
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800116 }
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700117
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800118 std::string protocol;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800119 try {
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700120 util::FaceUri uri(transportUri);
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800121 protocol = uri.getScheme();
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700122
123 if (protocol == "unix") {
124 return UnixTransport::create(transportUri);
125 }
126 else if (protocol == "tcp" || protocol == "tcp4" || protocol == "tcp6") {
127 return TcpTransport::create(transportUri);
128 }
129 else {
130 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unsupported transport protocol \"" + protocol + "\""));
131 }
132 }
133 catch (const Transport::Error& error) {
134 BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800135 }
136 catch (const util::FaceUri::Error& error) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700137 BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800138 }
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700139}
140
141void
Joao Pereira68c0d882015-05-19 14:27:55 -0400142Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800143{
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800144 if (transport == nullptr) {
145 transport = makeDefaultTransport();
146 }
147 BOOST_ASSERT(transport != nullptr);
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800148 m_transport = transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800149
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800150 m_nfdController.reset(new nfd::Controller(*this, keyChain));
151
Junxiao Shiae0b4182016-08-08 22:53:17 +0000152 IO_CAPTURE_WEAK_IMPL(post) {
153 impl->ensureConnected(false);
154 } IO_CAPTURE_WEAK_IMPL_END
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800155}
156
Joao Pereira68c0d882015-05-19 14:27:55 -0400157Face::~Face() = default;
Junxiao Shiedd834e2014-10-28 20:28:58 -0700158
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800159shared_ptr<Transport>
160Face::getTransport()
161{
162 return m_transport;
163}
164
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800165const PendingInterestId*
Eric Newberry83872fd2015-08-06 17:01:24 -0700166Face::expressInterest(const Interest& interest,
167 const DataCallback& afterSatisfied,
168 const NackCallback& afterNacked,
169 const TimeoutCallback& afterTimeout)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800170{
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700171 shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800172
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700173 // Use `interestToExpress` to avoid wire format creation for the original Interest
Eric Newberry83872fd2015-08-06 17:01:24 -0700174 if (interestToExpress->wireEncode().size() > MAX_NDN_PACKET_SIZE) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700175 BOOST_THROW_EXCEPTION(Error("Interest size exceeds maximum limit"));
Eric Newberry83872fd2015-08-06 17:01:24 -0700176 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700177
Alexander Afanasyev4e50b972014-03-25 10:57:50 -0700178 // If the same ioService thread, dispatch directly calls the method
Junxiao Shiae0b4182016-08-08 22:53:17 +0000179 IO_CAPTURE_WEAK_IMPL(dispatch) {
180 impl->asyncExpressInterest(interestToExpress, afterSatisfied, afterNacked, afterTimeout);
181 } IO_CAPTURE_WEAK_IMPL_END
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800182
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800183 return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
184}
185
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800186void
Junxiao Shiae0b4182016-08-08 22:53:17 +0000187Face::removePendingInterest(const PendingInterestId* pendingInterestId)
188{
189 IO_CAPTURE_WEAK_IMPL(post) {
190 impl->asyncRemovePendingInterest(pendingInterestId);
191 } IO_CAPTURE_WEAK_IMPL_END
192}
193
194void
195Face::removeAllPendingInterests()
196{
197 IO_CAPTURE_WEAK_IMPL(post) {
198 impl->asyncRemoveAllPendingInterests();
199 } IO_CAPTURE_WEAK_IMPL_END
200}
201
202size_t
203Face::getNPendingInterests() const
204{
205 return m_impl->m_pendingInterestTable.size();
206}
207
208void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800209Face::put(const Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800210{
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000211 Block wire = data.wireEncode();
212
Eric Newberry4d261b62016-11-10 13:40:09 -0700213 lp::Packet packet;
214 bool hasLpFields = false;
215
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000216 shared_ptr<lp::CachePolicyTag> cachePolicyTag = data.getTag<lp::CachePolicyTag>();
217 if (cachePolicyTag != nullptr) {
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000218 packet.add<lp::CachePolicyField>(*cachePolicyTag);
Eric Newberry4d261b62016-11-10 13:40:09 -0700219 hasLpFields = true;
220 }
221
222 shared_ptr<lp::CongestionMarkTag> congestionMarkTag = data.getTag<lp::CongestionMarkTag>();
223 if (congestionMarkTag != nullptr) {
224 packet.add<lp::CongestionMarkField>(*congestionMarkTag);
225 hasLpFields = true;
226 }
227
228 if (hasLpFields) {
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000229 packet.add<lp::FragmentField>(std::make_pair(wire.begin(), wire.end()));
230 wire = packet.wireEncode();
231 }
232
233 if (wire.size() > MAX_NDN_PACKET_SIZE)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700234 BOOST_THROW_EXCEPTION(Error("Data size exceeds maximum limit"));
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700235
Junxiao Shiae0b4182016-08-08 22:53:17 +0000236 IO_CAPTURE_WEAK_IMPL(dispatch) {
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000237 impl->asyncSend(wire);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000238 } IO_CAPTURE_WEAK_IMPL_END
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800239}
240
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800241void
Eric Newberry83872fd2015-08-06 17:01:24 -0700242Face::put(const lp::Nack& nack)
243{
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000244 lp::Packet packet;
245 packet.add<lp::NackField>(nack.getHeader());
246 const Block& interestWire = nack.getInterest().wireEncode();
247 packet.add<lp::FragmentField>(std::make_pair(interestWire.begin(), interestWire.end()));
248
Eric Newberry4d261b62016-11-10 13:40:09 -0700249 shared_ptr<lp::CongestionMarkTag> congestionMarkTag = nack.getTag<lp::CongestionMarkTag>();
250 if (congestionMarkTag != nullptr) {
251 packet.add<lp::CongestionMarkField>(*congestionMarkTag);
252 }
253
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000254 Block wire = packet.wireEncode();
255
256 if (wire.size() > MAX_NDN_PACKET_SIZE)
257 BOOST_THROW_EXCEPTION(Error("Nack size exceeds maximum limit"));
Eric Newberry83872fd2015-08-06 17:01:24 -0700258
Junxiao Shiae0b4182016-08-08 22:53:17 +0000259 IO_CAPTURE_WEAK_IMPL(dispatch) {
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000260 impl->asyncSend(wire);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000261 } IO_CAPTURE_WEAK_IMPL_END
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700262}
263
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800264const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000265Face::setInterestFilter(const InterestFilter& interestFilter,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000266 const InterestCallback& onInterest,
267 const RegisterPrefixFailureCallback& onFailure,
268 const security::SigningInfo& signingInfo,
269 uint64_t flags)
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700270{
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000271 return setInterestFilter(interestFilter, onInterest, nullptr, onFailure, signingInfo, flags);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700272}
273
274const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000275Face::setInterestFilter(const InterestFilter& interestFilter,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000276 const InterestCallback& onInterest,
277 const RegisterPrefixSuccessCallback& onSuccess,
278 const RegisterPrefixFailureCallback& onFailure,
279 const security::SigningInfo& signingInfo,
280 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700281{
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000282 auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700283
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000284 nfd::CommandOptions options;
285 options.setSigningInfo(signingInfo);
Junxiao Shi388ec252014-11-02 15:19:57 -0700286
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000287 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
288 onSuccess, onFailure, flags, options);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700289}
290
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700291const InterestFilterId*
292Face::setInterestFilter(const InterestFilter& interestFilter,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000293 const InterestCallback& onInterest)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700294{
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000295 auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700296
Junxiao Shiae0b4182016-08-08 22:53:17 +0000297 IO_CAPTURE_WEAK_IMPL(post) {
298 impl->asyncSetInterestFilter(filter);
299 } IO_CAPTURE_WEAK_IMPL_END
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700300
301 return reinterpret_cast<const InterestFilterId*>(filter.get());
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700302}
303
Joao Pereira0b3cac52015-07-02 14:49:49 -0400304const RegisteredPrefixId*
305Face::registerPrefix(const Name& prefix,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000306 const RegisterPrefixSuccessCallback& onSuccess,
307 const RegisterPrefixFailureCallback& onFailure,
308 const security::SigningInfo& signingInfo,
309 uint64_t flags)
Joao Pereira0b3cac52015-07-02 14:49:49 -0400310{
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000311 nfd::CommandOptions options;
312 options.setSigningInfo(signingInfo);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400313
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000314 return m_impl->registerPrefix(prefix, nullptr, onSuccess, onFailure, flags, options);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400315}
316
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700317void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800318Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800319{
Junxiao Shiae0b4182016-08-08 22:53:17 +0000320 IO_CAPTURE_WEAK_IMPL(post) {
321 impl->asyncUnregisterPrefix(registeredPrefixId, nullptr, nullptr);
322 } IO_CAPTURE_WEAK_IMPL_END
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800323}
324
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700325void
326Face::unsetInterestFilter(const InterestFilterId* interestFilterId)
327{
Junxiao Shiae0b4182016-08-08 22:53:17 +0000328 IO_CAPTURE_WEAK_IMPL(post) {
329 impl->asyncUnsetInterestFilter(interestFilterId);
330 } IO_CAPTURE_WEAK_IMPL_END
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700331}
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700332
333void
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700334Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
335 const UnregisterPrefixSuccessCallback& onSuccess,
336 const UnregisterPrefixFailureCallback& onFailure)
337{
Junxiao Shiae0b4182016-08-08 22:53:17 +0000338 IO_CAPTURE_WEAK_IMPL(post) {
339 impl->asyncUnregisterPrefix(registeredPrefixId, onSuccess, onFailure);
340 } IO_CAPTURE_WEAK_IMPL_END
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700341}
342
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800343void
Junxiao Shic828dfc2016-09-15 13:26:22 +0000344Face::doProcessEvents(const time::milliseconds& timeout, bool keepThread)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800345{
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500346 if (m_ioService.stopped()) {
347 m_ioService.reset(); // ensure that run()/poll() will do some work
348 }
349
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700350 try {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800351 if (timeout < time::milliseconds::zero()) {
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000352 // do not block if timeout is negative, but process pending events
353 m_ioService.poll();
354 return;
355 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800356
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800357 if (timeout > time::milliseconds::zero()) {
358 boost::asio::io_service& ioService = m_ioService;
359 unique_ptr<boost::asio::io_service::work>& work = m_impl->m_ioServiceWork;
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000360 m_impl->m_processEventsTimeoutEvent = m_impl->m_scheduler.scheduleEvent(timeout,
361 [&ioService, &work] {
362 ioService.stop();
363 work.reset();
364 });
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800365 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700366
367 if (keepThread) {
368 // work will ensure that m_ioService is running until work object exists
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800369 m_impl->m_ioServiceWork.reset(new boost::asio::io_service::work(m_ioService));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800370 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700371
Junxiao Shi2cced062014-11-02 21:27:38 -0700372 m_ioService.run();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700373 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700374 catch (...) {
375 m_impl->m_ioServiceWork.reset();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700376 m_impl->m_pendingInterestTable.clear();
377 m_impl->m_registeredPrefixTable.clear();
378 throw;
379 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700380}
381
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800382void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700383Face::shutdown()
Jeff Thompson517ffa82013-08-05 16:04:34 -0700384{
Junxiao Shiae0b4182016-08-08 22:53:17 +0000385 IO_CAPTURE_WEAK_IMPL(post) {
386 this->asyncShutdown();
387 } IO_CAPTURE_WEAK_IMPL_END
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700388}
389
390void
391Face::asyncShutdown()
392{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700393 m_impl->m_pendingInterestTable.clear();
394 m_impl->m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800395
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700396 if (m_transport->isConnected())
397 m_transport->close();
398
Alexander Afanasyev1f5486e2014-07-10 17:45:49 -0700399 m_impl->m_ioServiceWork.reset();
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700400}
401
Eric Newberry83872fd2015-08-06 17:01:24 -0700402/**
403 * @brief extract local fields from NDNLPv2 packet and tag onto a network layer packet
404 */
Eric Newberry4d261b62016-11-10 13:40:09 -0700405template<typename NetPkt>
Eric Newberry83872fd2015-08-06 17:01:24 -0700406static void
Eric Newberry4d261b62016-11-10 13:40:09 -0700407extractLpLocalFields(NetPkt& netPacket, const lp::Packet& lpPacket)
Eric Newberry83872fd2015-08-06 17:01:24 -0700408{
409 if (lpPacket.has<lp::IncomingFaceIdField>()) {
Junxiao Shi4b469982015-12-03 18:20:19 +0000410 netPacket.setTag(make_shared<lp::IncomingFaceIdTag>(lpPacket.get<lp::IncomingFaceIdField>()));
Eric Newberry83872fd2015-08-06 17:01:24 -0700411 }
Eric Newberry4d261b62016-11-10 13:40:09 -0700412
413 if (lpPacket.has<lp::CongestionMarkField>()) {
414 netPacket.setTag(make_shared<lp::CongestionMarkTag>(lpPacket.get<lp::CongestionMarkField>()));
415 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700416}
417
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800418void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800419Face::onReceiveElement(const Block& blockFromDaemon)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800420{
Eric Newberry83872fd2015-08-06 17:01:24 -0700421 lp::Packet lpPacket(blockFromDaemon); // bare Interest/Data is a valid lp::Packet,
422 // no need to distinguish
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800423
Eric Newberry83872fd2015-08-06 17:01:24 -0700424 Buffer::const_iterator begin, end;
425 std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
426 Block netPacket(&*begin, std::distance(begin, end));
427 switch (netPacket.type()) {
428 case tlv::Interest: {
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000429 auto interest = make_shared<Interest>(netPacket);
Eric Newberry83872fd2015-08-06 17:01:24 -0700430 if (lpPacket.has<lp::NackField>()) {
431 auto nack = make_shared<lp::Nack>(std::move(*interest));
432 nack->setHeader(lpPacket.get<lp::NackField>());
433 extractLpLocalFields(*nack, lpPacket);
434 m_impl->nackPendingInterests(*nack);
435 }
436 else {
437 extractLpLocalFields(*interest, lpPacket);
438 m_impl->processInterestFilters(*interest);
439 }
440 break;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800441 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700442 case tlv::Data: {
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000443 auto data = make_shared<Data>(netPacket);
Eric Newberry83872fd2015-08-06 17:01:24 -0700444 extractLpLocalFields(*data, lpPacket);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700445 m_impl->satisfyPendingInterests(*data);
Eric Newberry83872fd2015-08-06 17:01:24 -0700446 break;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800447 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700448 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800449}
450
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800451} // namespace ndn