face.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * 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.
20  */
21 
22 #include "ndn-cxx/face.hpp"
23 #include "ndn-cxx/encoding/tlv.hpp"
24 #include "ndn-cxx/impl/face-impl.hpp"
25 #include "ndn-cxx/net/face-uri.hpp"
27 #include "ndn-cxx/util/random.hpp"
28 #include "ndn-cxx/util/time.hpp"
29 
30 // NDN_LOG_INIT(ndn.Face) is declared in face-impl.hpp
31 
32 // A callback scheduled through io.post and io.dispatch may be invoked after the face is destructed.
33 // To prevent this situation, use these macros to capture Face::m_impl as weak_ptr and skip callback
34 // execution if the face has been destructed.
35 #define IO_CAPTURE_WEAK_IMPL(OP) \
36  { \
37  weak_ptr<Impl> implWeak(m_impl); \
38  m_ioService.OP([=] { \
39  auto impl = implWeak.lock(); \
40  if (impl != nullptr) {
41 #define IO_CAPTURE_WEAK_IMPL_END \
42  } \
43  }); \
44  }
45 
46 namespace ndn {
47 
48 Face::OversizedPacketError::OversizedPacketError(char pktType, const Name& name, size_t wireSize)
49  : Error((pktType == 'I' ? "Interest " : pktType == 'D' ? "Data " : "Nack ") +
50  name.toUri() + " encodes into " + to_string(wireSize) + " octets, "
51  "exceeding the implementation limit of " + to_string(MAX_NDN_PACKET_SIZE) + " octets")
52  , pktType(pktType)
53  , name(name)
54  , wireSize(wireSize)
55 {
56 }
57 
58 Face::Face(shared_ptr<Transport> transport)
59  : m_internalIoService(make_unique<boost::asio::io_service>())
60  , m_ioService(*m_internalIoService)
61  , m_internalKeyChain(make_unique<KeyChain>())
62  , m_impl(make_shared<Impl>(*this))
63 {
64  construct(std::move(transport), *m_internalKeyChain);
65 }
66 
67 Face::Face(boost::asio::io_service& ioService)
68  : m_ioService(ioService)
69  , m_internalKeyChain(make_unique<KeyChain>())
70  , m_impl(make_shared<Impl>(*this))
71 {
72  construct(nullptr, *m_internalKeyChain);
73 }
74 
75 Face::Face(const std::string& host, const std::string& port)
76  : m_internalIoService(make_unique<boost::asio::io_service>())
77  , m_ioService(*m_internalIoService)
78  , m_internalKeyChain(make_unique<KeyChain>())
79  , m_impl(make_shared<Impl>(*this))
80 {
81  construct(make_shared<TcpTransport>(host, port), *m_internalKeyChain);
82 }
83 
84 Face::Face(shared_ptr<Transport> transport, KeyChain& keyChain)
85  : m_internalIoService(make_unique<boost::asio::io_service>())
86  , m_ioService(*m_internalIoService)
87  , m_impl(make_shared<Impl>(*this))
88 {
89  construct(std::move(transport), keyChain);
90 }
91 
92 Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService)
93  : m_ioService(ioService)
94  , m_internalKeyChain(make_unique<KeyChain>())
95  , m_impl(make_shared<Impl>(*this))
96 {
97  construct(std::move(transport), *m_internalKeyChain);
98 }
99 
100 Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain)
101  : m_ioService(ioService)
102  , m_impl(make_shared<Impl>(*this))
103 {
104  construct(std::move(transport), keyChain);
105 }
106 
107 shared_ptr<Transport>
108 Face::makeDefaultTransport()
109 {
110  // transport=unix:///var/run/nfd.sock
111  // transport=tcp://localhost:6363
112 
113  std::string transportUri;
114 
115  const char* transportEnviron = getenv("NDN_CLIENT_TRANSPORT");
116  if (transportEnviron != nullptr) {
117  transportUri = transportEnviron;
118  }
119  else {
120  ConfigFile config;
121  transportUri = config.getParsedConfiguration().get<std::string>("transport", "");
122  }
123 
124  if (transportUri.empty()) {
125  // transport not specified, use default Unix transport.
126  return UnixTransport::create("");
127  }
128 
129  std::string protocol;
130  try {
131  FaceUri uri(transportUri);
132  protocol = uri.getScheme();
133 
134  if (protocol == "unix") {
135  return UnixTransport::create(transportUri);
136  }
137  else if (protocol == "tcp" || protocol == "tcp4" || protocol == "tcp6") {
138  return TcpTransport::create(transportUri);
139  }
140  else {
141  NDN_THROW(ConfigFile::Error("Unsupported transport protocol \"" + protocol + "\""));
142  }
143  }
144  catch (const Transport::Error&) {
145  NDN_THROW_NESTED(ConfigFile::Error("Failed to create transport"));
146  }
147  catch (const FaceUri::Error&) {
148  NDN_THROW_NESTED(ConfigFile::Error("Failed to create transport"));
149  }
150 }
151 
152 void
153 Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
154 {
155  if (transport == nullptr) {
156  transport = makeDefaultTransport();
157  }
158  BOOST_ASSERT(transport != nullptr);
159  m_transport = std::move(transport);
160 
161  m_nfdController = make_unique<nfd::Controller>(*this, keyChain);
162 
163  IO_CAPTURE_WEAK_IMPL(post) {
164  impl->ensureConnected(false);
166 }
167 
168 Face::~Face() = default;
169 
170 shared_ptr<Transport>
172 {
173  return m_transport;
174 }
175 
178  const DataCallback& afterSatisfied,
179  const NackCallback& afterNacked,
180  const TimeoutCallback& afterTimeout)
181 {
182  auto id = m_impl->m_pendingInterestTable.allocateId();
183 
184  auto interest2 = make_shared<Interest>(interest);
185  interest2->getNonce();
186 
187  IO_CAPTURE_WEAK_IMPL(post) {
188  impl->asyncExpressInterest(id, interest2, afterSatisfied, afterNacked, afterTimeout);
190 
191  return PendingInterestHandle(*this, reinterpret_cast<const PendingInterestId*>(id));
192 }
193 
194 void
195 Face::cancelPendingInterest(const PendingInterestId* pendingInterestId)
196 {
197  IO_CAPTURE_WEAK_IMPL(post) {
198  impl->asyncRemovePendingInterest(reinterpret_cast<RecordId>(pendingInterestId));
200 }
201 
202 void
204 {
205  IO_CAPTURE_WEAK_IMPL(post) {
206  impl->asyncRemoveAllPendingInterests();
208 }
209 
210 size_t
212 {
213  return m_impl->m_pendingInterestTable.size();
214 }
215 
216 void
218 {
219  IO_CAPTURE_WEAK_IMPL(post) {
220  impl->asyncPutData(data);
222 }
223 
224 void
226 {
227  IO_CAPTURE_WEAK_IMPL(post) {
228  impl->asyncPutNack(nack);
230 }
231 
233 Face::setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
234  const RegisterPrefixFailureCallback& onFailure,
235  const security::SigningInfo& signingInfo, uint64_t flags)
236 {
237  return setInterestFilter(filter, onInterest, nullptr, onFailure, signingInfo, flags);
238 }
239 
241 Face::setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
242  const RegisterPrefixSuccessCallback& onSuccess,
243  const RegisterPrefixFailureCallback& onFailure,
244  const security::SigningInfo& signingInfo, uint64_t flags)
245 {
246  nfd::CommandOptions options;
247  options.setSigningInfo(signingInfo);
248 
249  auto id = m_impl->registerPrefix(filter.getPrefix(), onSuccess, onFailure, flags, options,
250  filter, onInterest);
251  return RegisteredPrefixHandle(*this, reinterpret_cast<const RegisteredPrefixId*>(id));
252 }
253 
255 Face::setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest)
256 {
257  auto id = m_impl->m_interestFilterTable.allocateId();
258 
259  IO_CAPTURE_WEAK_IMPL(post) {
260  impl->asyncSetInterestFilter(id, filter, onInterest);
262 
263  return InterestFilterHandle(*this, reinterpret_cast<const InterestFilterId*>(id));
264 }
265 
266 void
267 Face::clearInterestFilter(const InterestFilterId* interestFilterId)
268 {
269  IO_CAPTURE_WEAK_IMPL(post) {
270  impl->asyncUnsetInterestFilter(reinterpret_cast<RecordId>(interestFilterId));
272 }
273 
276  const RegisterPrefixSuccessCallback& onSuccess,
277  const RegisterPrefixFailureCallback& onFailure,
278  const security::SigningInfo& signingInfo,
279  uint64_t flags)
280 {
281  nfd::CommandOptions options;
282  options.setSigningInfo(signingInfo);
283 
284  auto id = m_impl->registerPrefix(prefix, onSuccess, onFailure, flags, options, nullopt, nullptr);
285  return RegisteredPrefixHandle(*this, reinterpret_cast<const RegisteredPrefixId*>(id));
286 }
287 
288 void
289 Face::unregisterPrefixImpl(const RegisteredPrefixId* registeredPrefixId,
290  const UnregisterPrefixSuccessCallback& onSuccess,
291  const UnregisterPrefixFailureCallback& onFailure)
292 {
293  IO_CAPTURE_WEAK_IMPL(post) {
294  impl->asyncUnregisterPrefix(reinterpret_cast<RecordId>(registeredPrefixId),
295  onSuccess, onFailure);
297 }
298 
299 void
300 Face::doProcessEvents(time::milliseconds timeout, bool keepThread)
301 {
302  if (m_ioService.stopped()) {
303  m_ioService.reset(); // ensure that run()/poll() will do some work
304  }
305 
306  try {
307  if (timeout < time::milliseconds::zero()) {
308  // do not block if timeout is negative, but process pending events
309  m_ioService.poll();
310  return;
311  }
312 
313  if (timeout > time::milliseconds::zero()) {
314  m_impl->m_processEventsTimeoutEvent = m_impl->m_scheduler.schedule(timeout,
315  [&io = m_ioService, &work = m_impl->m_ioServiceWork] {
316  io.stop();
317  work.reset();
318  });
319  }
320 
321  if (keepThread) {
322  // work will ensure that m_ioService is running until work object exists
323  m_impl->m_ioServiceWork = make_unique<boost::asio::io_service::work>(m_ioService);
324  }
325 
326  m_ioService.run();
327  }
328  catch (...) {
329  m_impl->m_ioServiceWork.reset();
330  m_impl->m_pendingInterestTable.clear();
331  m_impl->m_registeredPrefixTable.clear();
332  throw;
333  }
334 }
335 
336 void
338 {
339  IO_CAPTURE_WEAK_IMPL(post) {
340  impl->m_pendingInterestTable.clear();
341  impl->m_registeredPrefixTable.clear();
342 
343  if (m_transport->isConnected())
344  m_transport->close();
345 
346  impl->m_ioServiceWork.reset();
348 }
349 
353 template<typename NetPkt>
354 static void
355 extractLpLocalFields(NetPkt& netPacket, const lp::Packet& lpPacket)
356 {
357  addTagFromField<lp::IncomingFaceIdTag, lp::IncomingFaceIdField>(netPacket, lpPacket);
358  addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(netPacket, lpPacket);
359 }
360 
361 void
362 Face::onReceiveElement(const Block& blockFromDaemon)
363 {
364  lp::Packet lpPacket(blockFromDaemon); // bare Interest/Data is a valid lp::Packet,
365  // no need to distinguish
366 
367  Buffer::const_iterator begin, end;
368  std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
369  Block netPacket(&*begin, std::distance(begin, end));
370  switch (netPacket.type()) {
371  case tlv::Interest: {
372  auto interest = make_shared<Interest>(netPacket);
373  if (lpPacket.has<lp::NackField>()) {
374  auto nack = make_shared<lp::Nack>(std::move(*interest));
375  nack->setHeader(lpPacket.get<lp::NackField>());
376  extractLpLocalFields(*nack, lpPacket);
377  NDN_LOG_DEBUG(">N " << nack->getInterest() << '~' << nack->getHeader().getReason());
378  m_impl->nackPendingInterests(*nack);
379  }
380  else {
381  extractLpLocalFields(*interest, lpPacket);
382  NDN_LOG_DEBUG(">I " << *interest);
383  m_impl->processIncomingInterest(std::move(interest));
384  }
385  break;
386  }
387  case tlv::Data: {
388  auto data = make_shared<Data>(netPacket);
389  extractLpLocalFields(*data, lpPacket);
390  NDN_LOG_DEBUG(">D " << data->getName());
391  m_impl->satisfyPendingInterests(*data);
392  break;
393  }
394  }
395 }
396 
397 PendingInterestHandle::PendingInterestHandle(Face& face, const PendingInterestId* id)
398  : CancelHandle([&face, id] { face.cancelPendingInterest(id); })
399  , m_id(id)
400 {
401 }
402 
403 RegisteredPrefixHandle::RegisteredPrefixHandle(Face& face, const RegisteredPrefixId* id)
404  : CancelHandle([&face, id] { face.unregisterPrefixImpl(id, nullptr, nullptr); })
405  , m_face(&face)
406  , m_id(id)
407 {
408  // The lambda passed to CancelHandle constructor cannot call this->unregister,
409  // because base class destructor cannot access the member fields of this subclass.
410 }
411 
412 void
414  const UnregisterPrefixFailureCallback& onFailure)
415 {
416  if (m_id == nullptr) {
417  if (onFailure != nullptr) {
418  onFailure("RegisteredPrefixHandle is empty");
419  }
420  return;
421  }
422 
423  m_face->unregisterPrefixImpl(m_id, onSuccess, onFailure);
424  m_face = nullptr;
425  m_id = nullptr;
426 }
427 
428 InterestFilterHandle::InterestFilterHandle(Face& face, const InterestFilterId* id)
429  : CancelHandle([&face, id] { face.clearInterestFilter(id); })
430  , m_id(id)
431 {
432 }
433 
434 } // namespace ndn
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:71
Definition: data.cpp:26
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
Definition: face.cpp:300
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command fails.
Definition: face.hpp:88
virtual ~Face()
void unregister(const UnregisterPrefixSuccessCallback &onSuccess=nullptr, const UnregisterPrefixFailureCallback &onFailure=nullptr)
Unregister the prefix.
Definition: face.cpp:413
declares the set of Interests a producer can serve, which starts with a name prefix, plus an optional regular expression
const Parsed & getParsedConfiguration() const
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:44
#define NDN_LOG_DEBUG(expression)
Log at DEBUG level.
Definition: logger.hpp:262
bool has() const
Definition: packet.hpp:74
static void extractLpLocalFields(NetPkt &netPacket, const lp::Packet &lpPacket)
extract local fields from NDNLPv2 packet and tag onto a network layer packet
Definition: face.cpp:355
Signing parameters passed to KeyChain.
static shared_ptr< UnixTransport > create(const std::string &uri)
Create transport with parameters defined in URI.
represents a Network Nack
Definition: nack.hpp:38
#define NDN_THROW(e)
Definition: exception.hpp:61
Declare a field.
Definition: field-decl.hpp:179
#define IO_CAPTURE_WEAK_IMPL(OP)
Definition: face.cpp:35
void removeAllPendingInterests()
Cancel all previously expressed Interests.
Definition: face.cpp:203
const Name & getPrefix() const
FIELD::ValueType get(size_t index=0) const
Definition: packet.hpp:96
RegisteredPrefixHandle registerPrefix(const Name &prefix, const RegisterPrefixSuccessCallback &onSuccess, const RegisterPrefixFailureCallback &onFailure, const security::SigningInfo &signingInfo=security::SigningInfo(), uint64_t flags=nfd::ROUTE_FLAG_CHILD_INHERIT)
Register prefix with the connected NDN forwarder.
Definition: face.cpp:275
contains options for ControlCommand execution
A handle of pending Interest.
Definition: face.hpp:548
shared_ptr< Transport > getTransport()
Definition: face.cpp:171
static shared_ptr< TcpTransport > create(const std::string &uri)
Create transport with parameters defined in URI.
void shutdown()
Shutdown face operations.
Definition: face.cpp:337
CancelHandle() noexcept=default
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:93
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback invoked when registerPrefix or setInterestFilter command fails.
Definition: face.hpp:78
function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Definition: face.hpp:73
Represents an absolute name.
Definition: name.hpp:43
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
CommandOptions & setSigningInfo(const security::SigningInfo &signingInfo)
sets signing parameters
PendingInterestHandle expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
Definition: face.cpp:177
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when incoming Interest matches the specified InterestFilter.
Definition: face.hpp:68
#define IO_CAPTURE_WEAK_IMPL_END
Definition: face.cpp:41
RegisteredPrefixHandle() noexcept
Definition: face.hpp:587
InterestFilterHandle() noexcept=default
System configuration file for NDN platform.
Definition: config-file.hpp:48
const std::string & getScheme() const
get scheme (protocol)
Definition: face-uri.hpp:109
size_t getNPendingInterests() const
Get number of pending Interests.
Definition: face.cpp:211
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command succeeds.
Definition: face.hpp:83
void put(Data data)
Publish data packet.
Definition: face.cpp:217
OversizedPacketError(char pktType, const Name &name, size_t wireSize)
Constructor.
Definition: face.cpp:48
std::string to_string(const V &v)
Definition: backports.hpp:67
A handle of registered Interest filter.
Definition: face.hpp:642
PendingInterestHandle() noexcept=default
function< void(const Interest &)> TimeoutCallback
Callback invoked when expressed Interest times out.
Definition: face.hpp:63
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when Nack is sent in response to expressed Interest.
Definition: face.hpp:58
Represents a Data packet.
Definition: data.hpp:35
RegisteredPrefixHandle setInterestFilter(const InterestFilter &filter, const InterestCallback &onInterest, const RegisterPrefixFailureCallback &onFailure, const security::SigningInfo &signingInfo=security::SigningInfo(), uint64_t flags=nfd::ROUTE_FLAG_CHILD_INHERIT)
Set InterestFilter to dispatch incoming matching interest to onInterest callback and register the fil...
Definition: face.cpp:233
Face(shared_ptr< Transport > transport=nullptr)
Create Face using given transport (or default transport if omitted)
Definition: face.cpp:58
const nullopt_t nullopt((nullopt_t::init()))
A handle of registered prefix.
Definition: face.hpp:584
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when expressed Interest gets satisfied with a Data packet.
Definition: face.hpp:53
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size
Definition: tlv.hpp:41