35 #define IO_CAPTURE_WEAK_IMPL(OP) \
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 \
49 : m_internalIoService(new
boost::asio::io_service())
50 , m_ioService(*m_internalIoService)
51 , m_internalKeyChain(new KeyChain())
52 , m_impl(make_shared<
Impl>(*this))
54 construct(transport, *m_internalKeyChain);
58 : m_ioService(ioService)
59 , m_internalKeyChain(new KeyChain())
60 , m_impl(make_shared<
Impl>(*this))
62 construct(
nullptr, *m_internalKeyChain);
65 Face::Face(
const std::string& host,
const std::string& port)
66 : m_internalIoService(new
boost::asio::io_service())
67 , m_ioService(*m_internalIoService)
68 , m_internalKeyChain(new KeyChain())
69 , m_impl(make_shared<
Impl>(*this))
71 construct(make_shared<TcpTransport>(host, port), *m_internalKeyChain);
74 Face::Face(shared_ptr<Transport> transport, KeyChain& keyChain)
75 : m_internalIoService(new
boost::asio::io_service())
76 , m_ioService(*m_internalIoService)
77 , m_impl(make_shared<
Impl>(*this))
79 construct(transport, keyChain);
82 Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService)
83 : m_ioService(ioService)
84 , m_internalKeyChain(new KeyChain())
85 , m_impl(make_shared<
Impl>(*this))
87 construct(transport, *m_internalKeyChain);
90 Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain)
91 : m_ioService(ioService)
92 , m_impl(make_shared<
Impl>(*this))
94 construct(transport, keyChain);
98 Face::makeDefaultTransport()
103 std::string transportUri;
105 const char* transportEnviron = getenv(
"NDN_CLIENT_TRANSPORT");
106 if (transportEnviron !=
nullptr) {
107 transportUri = transportEnviron;
111 transportUri = config.getParsedConfiguration().get<std::string>(
"transport",
"");
114 if (transportUri.empty()) {
119 std::string protocol;
121 util::FaceUri uri(transportUri);
122 protocol = uri.getScheme();
124 if (protocol ==
"unix") {
127 else if (protocol ==
"tcp" || protocol ==
"tcp4" || protocol ==
"tcp6") {
131 BOOST_THROW_EXCEPTION(ConfigFile::Error(
"Unsupported transport protocol \"" + protocol +
"\""));
134 catch (
const Transport::Error& error) {
135 BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
137 catch (
const util::FaceUri::Error& error) {
138 BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
143 Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
145 if (transport ==
nullptr) {
146 transport = makeDefaultTransport();
148 BOOST_ASSERT(transport !=
nullptr);
149 m_transport = transport;
151 m_nfdController.reset(
new nfd::Controller(*
this, keyChain));
154 impl->ensureConnected(
false);
160 shared_ptr<Transport>
166 const PendingInterestId*
172 shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest);
176 BOOST_THROW_EXCEPTION(
Error(
"Interest size exceeds maximum limit"));
181 impl->asyncExpressInterest(interestToExpress, afterSatisfied, afterNacked, afterTimeout);
184 return reinterpret_cast<const PendingInterestId*
>(interestToExpress.get());
187 const PendingInterestId*
195 if (onData !=
nullptr) {
196 onData(interest, const_cast<Data&>(data));
200 if (onTimeout !=
nullptr) {
208 const PendingInterestId*
220 impl->asyncRemovePendingInterest(pendingInterestId);
228 impl->asyncRemoveAllPendingInterests();
235 return m_impl->m_pendingInterestTable.size();
244 if (cachePolicyTag !=
nullptr) {
252 BOOST_THROW_EXCEPTION(
Error(
"Data size exceeds maximum limit"));
255 impl->asyncSend(wire);
270 BOOST_THROW_EXCEPTION(
Error(
"Nack size exceeds maximum limit"));
273 impl->asyncSend(wire);
277 const RegisteredPrefixId*
284 return setInterestFilter(interestFilter, onInterest,
nullptr, onFailure, signingInfo, flags);
287 const RegisteredPrefixId*
295 auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
300 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
301 onSuccess, onFailure, flags, options);
304 const InterestFilterId*
308 auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
311 impl->asyncSetInterestFilter(filter);
314 return reinterpret_cast<const InterestFilterId*
>(filter.get());
317 #ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
319 const RegisteredPrefixId*
331 return setInterestFilter(interestFilter, onInterest, onSuccess, onFailure, signingInfo, flags);
334 const RegisteredPrefixId*
345 return setInterestFilter(interestFilter, onInterest, onFailure, signingInfo, flags);
348 const RegisteredPrefixId*
353 const Name& identity,
358 onSuccess, onFailure,
362 const RegisteredPrefixId*
366 const Name& identity,
370 return setInterestFilter(interestFilter, onInterest, onFailure, signingInfo, flags);
373 #endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
375 const RegisteredPrefixId*
385 return m_impl->registerPrefix(prefix,
nullptr, onSuccess, onFailure, flags, options);
388 #ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
389 const RegisteredPrefixId*
400 return registerPrefix(prefix, onSuccess, onFailure, signingInfo, flags);
403 const RegisteredPrefixId*
407 const Name& identity,
411 return registerPrefix(prefix, onSuccess, onFailure, signingInfo, flags);
413 #endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
419 impl->asyncUnregisterPrefix(registeredPrefixId,
nullptr,
nullptr);
427 impl->asyncUnsetInterestFilter(interestFilterId);
437 impl->asyncUnregisterPrefix(registeredPrefixId, onSuccess, onFailure);
444 if (m_ioService.stopped()) {
449 if (timeout < time::milliseconds::zero()) {
455 if (timeout > time::milliseconds::zero()) {
456 boost::asio::io_service& ioService = m_ioService;
457 unique_ptr<boost::asio::io_service::work>& work = m_impl->m_ioServiceWork;
458 m_impl->m_processEventsTimeoutEvent = m_impl->m_scheduler.scheduleEvent(timeout,
459 [&ioService, &work] {
467 m_impl->m_ioServiceWork.reset(
new boost::asio::io_service::work(m_ioService));
473 m_impl->m_ioServiceWork.reset();
474 m_impl->m_pendingInterestTable.clear();
475 m_impl->m_registeredPrefixTable.clear();
484 this->asyncShutdown();
489 Face::asyncShutdown()
491 m_impl->m_pendingInterestTable.clear();
492 m_impl->m_registeredPrefixTable.clear();
494 if (m_transport->isConnected())
495 m_transport->close();
497 m_impl->m_ioServiceWork.reset();
503 template<
typename NETPKT>
513 Face::onReceiveElement(
const Block& blockFromDaemon)
515 lp::Packet lpPacket(blockFromDaemon);
518 Buffer::const_iterator begin, end;
520 Block netPacket(&*begin, std::distance(begin, end));
521 switch (netPacket.type()) {
523 auto interest = make_shared<Interest>(netPacket);
525 auto nack = make_shared<lp::Nack>(std::move(*interest));
528 m_impl->nackPendingInterests(*nack);
532 m_impl->processInterestFilters(*interest);
537 auto data = make_shared<Data>(netPacket);
539 m_impl->satisfyPendingInterests(*data);
size_t wireEncode(EncodingImpl< TAG > &encoder) const
append packet to encoder
Copyright (c) 2013-2016 Regents of the University of California.
const NackHeader & getHeader() const
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command fails.
Copyright (c) 2013-2016 Regents of the University of California.
implementation detail of Face
Buffer::const_iterator end() const
const RegisteredPrefixId * setInterestFilter(const InterestFilter &interestFilter, 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...
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
declares the set of Interests a producer can serve, which starts with a name prefix, plus an optional regular expression
const Interest & getInterest() const
Class representing a wire element of NDN-TLV packet format.
represents an Interest packet
function< void(const Interest &)> OnTimeout
Callback invoked when expressed Interest times out.
function< void(const InterestFilter &, const Interest &)> OnInterest
Callback invoked when incoming Interest matches the specified InterestFilter.
virtual void doProcessEvents(const time::milliseconds &timeout, bool keepThread)
Signing parameters passed to KeyChain.
SigningInfo signingByCertificate(const Name &certName)
void unregisterPrefix(const RegisteredPrefixId *registeredPrefixId, const UnregisterPrefixSuccessCallback &onSuccess, const UnregisterPrefixFailureCallback &onFailure)
Unregister prefix from RIB.
static shared_ptr< UnixTransport > create(const std::string &uri)
Create transport with parameters defined in URI.
represents a Network Nack
const Name & getName() const
Get name of the Data packet.
provides a tag type for simple types
#define IO_CAPTURE_WEAK_IMPL(OP)
Copyright (c) 2013-2016 Regents of the University of California.
void removeAllPendingInterests()
Cancel all previously expressed Interests.
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Fast encoding or block size estimation.
detail::FieldDecl< field_location_tags::Fragment, std::pair< Buffer::const_iterator, Buffer::const_iterator >, tlv::Fragment > FragmentField
The value of the wire encoded field is the data between the provided iterators.
FIELD::ValueType get(size_t index=0) const
contains options for ControlCommand execution
shared_ptr< Transport > getTransport()
static shared_ptr< TcpTransport > create(const std::string &uri)
Create transport with parameters defined in URI.
void shutdown()
Shutdown face operations.
SigningInfo signingByIdentity(const Name &identity)
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback invoked when registerPrefix or setInterestFilter command fails.
function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Name abstraction to represent an absolute name.
void unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId.
detail::FieldDecl< field_location_tags::Header, NackHeader, tlv::Nack > NackField
CommandOptions & setSigningInfo(const security::SigningInfo &signingInfo)
sets signing parameters
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when incoming Interest matches the specified InterestFilter.
#define IO_CAPTURE_WEAK_IMPL_END
function< void(const Interest &, Data &)> OnData
Callback invoked when expressed Interest gets satisfied with Data packet.
size_t getNPendingInterests() const
Get number of pending Interests.
bool empty() const
Check if name is emtpy.
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command succeeds.
shared_ptr< T > getTag() const
get a tag item
const PendingInterestId * expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
static void extractLpLocalFields(NETPKT &netPacket, const lp::Packet &lpPacket)
extract local fields from NDNLPv2 packet and tag onto a network layer packet
function< void(const Interest &)> TimeoutCallback
Callback invoked when expressed Interest times out.
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when Nack is sent in response to expressed Interest.
const RegisteredPrefixId * 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.
Face(shared_ptr< Transport > transport=nullptr)
Create Face using given transport (or default transport if omitted)
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when expressed Interest gets satisfied with a Data packet.
void removePendingInterest(const PendingInterestId *pendingInterestId)
Cancel previously expressed Interest.
Buffer::const_iterator begin() const
void put(const Data &data)
Publish data packet.
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size