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") {
141 BOOST_THROW_EXCEPTION(
ConfigFile::Error(
"Unsupported transport protocol \"" + protocol +
"\""));
153 Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
155 if (transport ==
nullptr) {
156 transport = makeDefaultTransport();
158 BOOST_ASSERT(transport !=
nullptr);
159 m_transport = std::move(transport);
161 m_nfdController = make_unique<nfd::Controller>(*
this, keyChain);
164 impl->ensureConnected(
false);
170 shared_ptr<Transport>
182 auto interest2 = make_shared<Interest>(interest);
183 interest2->getNonce();
186 impl->asyncExpressInterest(interest2, afterSatisfied, afterNacked, afterTimeout);
196 impl->asyncRemovePendingInterest(pendingInterestId);
204 impl->asyncRemoveAllPendingInterests();
211 return m_impl->m_pendingInterestTable.size();
218 impl->asyncPutData(data);
226 impl->asyncPutNack(nack);
237 return setInterestFilter(interestFilter, onInterest,
nullptr, onFailure, signingInfo, flags);
248 auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
253 auto id = m_impl->registerPrefix(interestFilter.getPrefix(), filter,
254 onSuccess, onFailure, flags, options);
262 auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
265 impl->asyncSetInterestFilter(filter);
268 auto id =
reinterpret_cast<const InterestFilterId*
>(filter.get());
282 auto id = m_impl->registerPrefix(prefix,
nullptr, onSuccess, onFailure, flags, options);
290 impl->asyncUnregisterPrefix(registeredPrefixId,
nullptr,
nullptr);
298 impl->asyncUnsetInterestFilter(interestFilterId);
308 impl->asyncUnregisterPrefix(registeredPrefixId, onSuccess, onFailure);
315 if (m_ioService.stopped()) {
320 if (timeout < time::milliseconds::zero()) {
326 if (timeout > time::milliseconds::zero()) {
327 m_impl->m_processEventsTimeoutEvent = m_impl->m_scheduler.scheduleEvent(timeout,
328 [&io = m_ioService, &work = m_impl->m_ioServiceWork] {
336 m_impl->m_ioServiceWork = make_unique<boost::asio::io_service::work>(m_ioService);
342 m_impl->m_ioServiceWork.reset();
343 m_impl->m_pendingInterestTable.clear();
344 m_impl->m_registeredPrefixTable.clear();
353 this->asyncShutdown();
358 Face::asyncShutdown()
360 m_impl->m_pendingInterestTable.clear();
361 m_impl->m_registeredPrefixTable.clear();
363 if (m_transport->isConnected())
364 m_transport->close();
366 m_impl->m_ioServiceWork.reset();
372 template<
typename NetPkt>
376 addTagFromField<lp::IncomingFaceIdTag, lp::IncomingFaceIdField>(netPacket, lpPacket);
377 addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(netPacket, lpPacket);
381 Face::onReceiveElement(
const Block& blockFromDaemon)
386 Buffer::const_iterator begin, end;
388 Block netPacket(&*begin, std::distance(begin, end));
389 switch (netPacket.type()) {
391 auto interest = make_shared<Interest>(netPacket);
393 auto nack = make_shared<lp::Nack>(std::move(*interest));
396 NDN_LOG_DEBUG(
">N " << nack->getInterest() <<
'~' << nack->getHeader().getReason());
397 m_impl->nackPendingInterests(*nack);
402 m_impl->processIncomingInterest(std::move(interest));
407 auto data = make_shared<Data>(netPacket);
410 m_impl->satisfyPendingInterests(*data);
435 if (m_id ==
nullptr) {
436 if (onFailure !=
nullptr) {
437 onFailure(
"RegisteredPrefixHandle is empty");
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.
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
#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
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
void unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId.
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 &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...
Face(shared_ptr< Transport > transport=nullptr)
Create Face using given transport (or default transport if omitted)
A handle of registered prefix.
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.
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size