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>())
63 construct(std::move(transport), *m_internalKeyChain);
67 : m_ioService(ioService)
68 , m_internalKeyChain(make_unique<
KeyChain>())
70 construct(
nullptr, *m_internalKeyChain);
73 Face::Face(
const std::string& host,
const std::string& port)
74 : m_internalIoService(make_unique<
boost::asio::io_service>())
75 , m_ioService(*m_internalIoService)
76 , m_internalKeyChain(make_unique<
KeyChain>())
78 construct(make_shared<TcpTransport>(host, port), *m_internalKeyChain);
82 : m_internalIoService(make_unique<
boost::asio::io_service>())
83 , m_ioService(*m_internalIoService)
85 construct(std::move(transport), keyChain);
88 Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService)
89 : m_ioService(ioService)
90 , m_internalKeyChain(make_unique<
KeyChain>())
92 construct(std::move(transport), *m_internalKeyChain);
95 Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService,
KeyChain& keyChain)
96 : m_ioService(ioService)
98 construct(std::move(transport), keyChain);
101 shared_ptr<Transport>
102 Face::makeDefaultTransport()
104 std::string transportUri;
106 const char* transportEnviron = getenv(
"NDN_CLIENT_TRANSPORT");
107 if (transportEnviron !=
nullptr) {
108 transportUri = transportEnviron;
115 if (transportUri.empty()) {
120 std::string protocol;
125 if (protocol ==
"unix") {
128 else if (protocol ==
"tcp" || protocol ==
"tcp4" || protocol ==
"tcp6") {
144 Face::construct(shared_ptr<Transport> transport,
KeyChain& keyChain)
146 BOOST_ASSERT(m_impl ==
nullptr);
147 m_impl = make_shared<Impl>(*
this, keyChain);
149 if (transport ==
nullptr) {
150 transport = makeDefaultTransport();
151 BOOST_ASSERT(transport !=
nullptr);
153 m_transport = std::move(transport);
156 impl->ensureConnected(
false);
168 auto id = m_impl->m_pendingInterestTable.allocateId();
170 auto interest2 = make_shared<Interest>(interest);
171 interest2->getNonce();
174 impl->expressInterest(
id, interest2, afterSatisfied, afterNacked, afterTimeout);
184 impl->removeAllPendingInterests();
191 return m_impl->m_pendingInterestTable.size();
215 return setInterestFilter(filter, onInterest,
nullptr, onFailure, signingInfo, flags);
227 auto id = m_impl->registerPrefix(filter.
getPrefix(), onSuccess, onFailure, flags, options,
235 auto id = m_impl->m_interestFilterTable.allocateId();
238 impl->setInterestFilter(
id, filter, onInterest);
254 auto id = m_impl->registerPrefix(prefix, onSuccess, onFailure, flags, options, nullopt,
nullptr);
261 if (m_ioService.stopped()) {
265 auto onThrow = make_scope_fail([
this] { m_impl->shutdown(); });
267 if (timeout < 0_ms) {
273 if (timeout > 0_ms) {
274 m_impl->m_processEventsTimeoutEvent = m_impl->m_scheduler.schedule(timeout,
275 [&io = m_ioService, &work = m_impl->m_ioServiceWork] {
283 m_impl->m_ioServiceWork = make_unique<boost::asio::io_service::work>(m_ioService);
294 if (m_transport->isConnected())
295 m_transport->close();
302 template<
typename NetPkt>
306 addTagFromField<lp::IncomingFaceIdTag, lp::IncomingFaceIdField>(netPacket, lpPacket);
307 addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(netPacket, lpPacket);
311 Face::onReceiveElement(
const Block& blockFromDaemon)
316 Buffer::const_iterator begin, end;
318 Block netPacket(&*begin, std::distance(begin, end));
319 switch (netPacket.type()) {
321 auto interest = make_shared<Interest>(netPacket);
323 auto nack = make_shared<lp::Nack>(std::move(*interest));
326 NDN_LOG_DEBUG(
">N " << nack->getInterest() <<
'~' << nack->getHeader().getReason());
327 m_impl->nackPendingInterests(*nack);
332 m_impl->processIncomingInterest(std::move(interest));
337 auto data = make_shared<Data>(netPacket);
340 m_impl->satisfyPendingInterests(*data);
347 : CancelHandle([w = std::move(weakImpl),
id] {
348 auto impl = w.lock();
349 if (impl !=
nullptr) {
350 impl->asyncRemovePendingInterest(
id);
357 : CancelHandle([=] { unregister(weakImpl,
id,
nullptr,
nullptr); })
358 , m_weakImpl(std::move(weakImpl))
371 onFailure(
"RegisteredPrefixHandle is empty");
376 unregister(m_weakImpl, m_id, onSuccess, onFailure);
385 auto impl = weakImpl.lock();
386 if (impl !=
nullptr) {
387 impl->asyncUnregisterPrefix(
id, onSuccess, onFailure);
389 else if (onFailure) {
390 onFailure(
"Face already closed");
395 : CancelHandle([w = std::move(weakImpl),
id] {
396 auto impl = w.lock();
397 if (impl !=
nullptr) {
398 impl->asyncUnsetInterestFilter(
id);
#define NDN_THROW_NESTED(e)
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
The interface of signing key management.
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregistering a prefix fails.
std::string to_string(const T &val)
void unregister(const UnregisterPrefixSuccessCallback &onSuccess=nullptr, const UnregisterPrefixFailureCallback &onFailure=nullptr)
Unregister the prefix.
const Name & getPrefix() const
declares the set of Interests a producer can serve, which starts with a name prefix, plus an optional regular expression
Represents a TLV element of the 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.
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
Handle for a pending Interest.
static shared_ptr< TcpTransport > create(const std::string &uri)
Create transport with parameters defined in URI.
size_t getNPendingInterests() const
Get number of pending Interests.
void shutdown()
Shutdown face operations.
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 an incoming Interest matches the specified InterestFilter.
#define IO_CAPTURE_WEAK_IMPL_END
const std::string & getScheme() const
get scheme (protocol)
RegisteredPrefixHandle() noexcept
InterestFilterHandle() noexcept=default
System configuration file for NDN platform.
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregistering a prefix succeeds.
void put(Data data)
Publish data packet.
OversizedPacketError(char pktType, const Name &name, size_t wireSize)
Constructor.
Handle for a registered Interest filter.
PendingInterestHandle() noexcept=default
function< void(const Interest &)> TimeoutCallback
Callback invoked when an expressed Interest times out.
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when a Nack is received in response to an 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...
const Parsed & getParsedConfiguration() const
Face(shared_ptr< Transport > transport=nullptr)
Create Face using given transport (or default transport if omitted)
Handle for a registered prefix.
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when an expressed Interest is satisfied by a Data packet.
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size