24 #include "ndn-cxx/impl/face-impl.hpp" 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 :
Error((pktType ==
'I' ?
"Interest " : pktType ==
'D' ?
"Data " :
"Nack ") +
50 name.toUri() +
" encodes into " +
to_string(wireSize) +
" octets, " 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))
64 construct(
std::move(transport), *m_internalKeyChain);
68 : m_ioService(ioService)
69 , m_internalKeyChain(make_unique<KeyChain>())
70 , m_impl(make_shared<Impl>(*this))
72 construct(
nullptr, *m_internalKeyChain);
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))
81 construct(make_shared<TcpTransport>(host, port), *m_internalKeyChain);
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))
89 construct(
std::move(transport), keyChain);
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))
97 construct(
std::move(transport), *m_internalKeyChain);
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))
104 construct(
std::move(transport), keyChain);
107 shared_ptr<Transport>
108 Face::makeDefaultTransport()
113 std::string transportUri;
115 const char* transportEnviron = getenv(
"NDN_CLIENT_TRANSPORT");
116 if (transportEnviron !=
nullptr) {
117 transportUri = transportEnviron;
124 if (transportUri.empty()) {
129 std::string protocol;
134 if (protocol ==
"unix") {
137 else if (protocol ==
"tcp" || protocol ==
"tcp4" || protocol ==
"tcp6") {
153 Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
155 if (transport ==
nullptr) {
156 transport = makeDefaultTransport();
158 BOOST_ASSERT(transport !=
nullptr);
161 m_nfdController = make_unique<nfd::Controller>(*
this, keyChain);
164 impl->ensureConnected(
false);
170 shared_ptr<Transport>
182 auto id = m_impl->m_pendingInterestTable.allocateId();
184 auto interest2 = make_shared<Interest>(interest);
185 interest2->getNonce();
188 impl->asyncExpressInterest(
id, interest2, afterSatisfied, afterNacked, afterTimeout);
195 Face::cancelPendingInterest(
const PendingInterestId* pendingInterestId)
198 impl->asyncRemovePendingInterest(reinterpret_cast<RecordId>(pendingInterestId));
206 impl->asyncRemoveAllPendingInterests();
213 return m_impl->m_pendingInterestTable.size();
220 impl->asyncPutData(data);
228 impl->asyncPutNack(nack);
237 return setInterestFilter(filter, onInterest,
nullptr, onFailure, signingInfo, flags);
249 auto id = m_impl->registerPrefix(filter.
getPrefix(), onSuccess, onFailure, flags, options,
257 auto id = m_impl->m_interestFilterTable.allocateId();
260 impl->asyncSetInterestFilter(
id, filter, onInterest);
267 Face::clearInterestFilter(
const InterestFilterId* interestFilterId)
270 impl->asyncUnsetInterestFilter(reinterpret_cast<RecordId>(interestFilterId));
284 auto id = m_impl->registerPrefix(prefix, onSuccess, onFailure, flags, options,
nullopt,
nullptr);
289 Face::unregisterPrefixImpl(
const RegisteredPrefixId* registeredPrefixId,
294 impl->asyncUnregisterPrefix(reinterpret_cast<RecordId>(registeredPrefixId),
295 onSuccess, onFailure);
302 if (m_ioService.stopped()) {
307 if (timeout < time::milliseconds::zero()) {
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] {
323 m_impl->m_ioServiceWork = make_unique<boost::asio::io_service::work>(m_ioService);
329 m_impl->m_ioServiceWork.reset();
330 m_impl->m_pendingInterestTable.clear();
331 m_impl->m_registeredPrefixTable.clear();
340 impl->m_pendingInterestTable.clear();
341 impl->m_registeredPrefixTable.clear();
343 if (m_transport->isConnected())
344 m_transport->close();
346 impl->m_ioServiceWork.reset();
353 template<
typename NetPkt>
357 addTagFromField<lp::IncomingFaceIdTag, lp::IncomingFaceIdField>(netPacket, lpPacket);
358 addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(netPacket, lpPacket);
362 Face::onReceiveElement(
const Block& blockFromDaemon)
367 Buffer::const_iterator begin, end;
369 Block netPacket(&*begin, std::distance(begin, end));
370 switch (netPacket.type()) {
372 auto interest = make_shared<Interest>(netPacket);
374 auto nack = make_shared<lp::Nack>(
std::move(*interest));
377 NDN_LOG_DEBUG(
">N " << nack->getInterest() <<
'~' << nack->getHeader().getReason());
378 m_impl->nackPendingInterests(*nack);
383 m_impl->processIncomingInterest(
std::move(interest));
388 auto data = make_shared<Data>(netPacket);
391 m_impl->satisfyPendingInterests(*data);
398 : CancelHandle([&face, id] { face.cancelPendingInterest(
id); })
404 :
CancelHandle([&face, id] { face.unregisterPrefixImpl(
id,
nullptr,
nullptr); })
416 if (m_id ==
nullptr) {
417 if (onFailure !=
nullptr) {
418 onFailure(
"RegisteredPrefixHandle is empty");
423 m_face->unregisterPrefixImpl(m_id, onSuccess, onFailure);
429 :
CancelHandle([&face, id] { face.clearInterestFilter(
id); })
#define NDN_THROW_NESTED(e)
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command fails.
void unregister(const UnregisterPrefixSuccessCallback &onSuccess=nullptr, const UnregisterPrefixFailureCallback &onFailure=nullptr)
Unregister the prefix.
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.
Represents an Interest packet.
#define NDN_LOG_DEBUG(expression)
Log at DEBUG level.
static void extractLpLocalFields(NetPkt &netPacket, const lp::Packet &lpPacket)
extract local fields from NDNLPv2 packet and tag onto a network layer packet
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
#define IO_CAPTURE_WEAK_IMPL(OP)
void removeAllPendingInterests()
Cancel all previously expressed Interests.
const Name & getPrefix() const
FIELD::ValueType get(size_t index=0) const
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.
contains options for ControlCommand execution
A handle of pending Interest.
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.
CancelHandle() noexcept=default
Provide a communication channel with local or remote NDN forwarder.
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.
Represents an absolute name.
represents the underlying protocol and address used by a Face
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.
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when incoming Interest matches the specified InterestFilter.
#define IO_CAPTURE_WEAK_IMPL_END
RegisteredPrefixHandle() noexcept
InterestFilterHandle() noexcept=default
System configuration file for NDN platform.
const std::string & getScheme() const
get scheme (protocol)
size_t getNPendingInterests() const
Get number of pending Interests.
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command succeeds.
void put(Data data)
Publish data packet.
OversizedPacketError(char pktType, const Name &name, size_t wireSize)
Constructor.
std::string to_string(const V &v)
A handle of registered Interest filter.
PendingInterestHandle() noexcept=default
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.
Represents a Data packet.
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...
Face(shared_ptr< Transport > transport=nullptr)
Create Face using given transport (or default transport if omitted)
const nullopt_t nullopt((nullopt_t::init()))
A handle of registered prefix.
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when expressed Interest gets satisfied with a Data packet.
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size