24 #include "ndn-cxx/impl/face-impl.hpp"
36 #define IO_CAPTURE_WEAK_IMPL(OP) \
38 weak_ptr<Impl> implWeak(m_impl); \
39 m_ioService.OP([=] { \
40 auto impl = implWeak.lock(); \
41 if (impl != nullptr) {
42 #define IO_CAPTURE_WEAK_IMPL_END \
50 :
Error((pktType ==
'I' ?
"Interest " : pktType ==
'D' ?
"Data " :
"Nack ") +
51 name.toUri() +
" encodes into " +
to_string(wireSize) +
" octets, "
60 : m_internalIoService(make_unique<
boost::asio::io_service>())
61 , m_ioService(*m_internalIoService)
62 , m_internalKeyChain(make_unique<KeyChain>())
64 construct(std::move(transport), *m_internalKeyChain);
68 : m_ioService(ioService)
69 , m_internalKeyChain(make_unique<KeyChain>())
71 construct(
nullptr, *m_internalKeyChain);
74 Face::Face(
const std::string& host,
const std::string& port)
75 : m_internalIoService(make_unique<
boost::asio::io_service>())
76 , m_ioService(*m_internalIoService)
77 , m_internalKeyChain(make_unique<KeyChain>())
79 construct(make_shared<TcpTransport>(host, port), *m_internalKeyChain);
82 Face::Face(shared_ptr<Transport> transport, KeyChain& keyChain)
83 : m_internalIoService(make_unique<
boost::asio::io_service>())
84 , m_ioService(*m_internalIoService)
86 construct(std::move(transport), keyChain);
89 Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService)
90 : m_ioService(ioService)
91 , m_internalKeyChain(make_unique<KeyChain>())
93 construct(std::move(transport), *m_internalKeyChain);
96 Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain)
97 : m_ioService(ioService)
99 construct(std::move(transport), keyChain);
102 shared_ptr<Transport>
103 Face::makeDefaultTransport()
105 std::string transportUri;
107 const char* transportEnviron = getenv(
"NDN_CLIENT_TRANSPORT");
108 if (transportEnviron !=
nullptr) {
109 transportUri = transportEnviron;
113 transportUri = config.getParsedConfiguration().get<std::string>(
"transport",
"");
116 if (transportUri.empty()) {
121 std::string protocol;
123 FaceUri uri(transportUri);
124 protocol = uri.getScheme();
126 if (protocol ==
"unix") {
129 else if (protocol ==
"tcp" || protocol ==
"tcp4" || protocol ==
"tcp6") {
133 NDN_THROW(ConfigFile::Error(
"Unsupported transport protocol \"" + protocol +
"\""));
136 catch (
const Transport::Error&) {
139 catch (
const FaceUri::Error&) {
145 Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
147 BOOST_ASSERT(m_impl ==
nullptr);
148 m_impl = make_shared<Impl>(*
this, keyChain);
150 if (transport ==
nullptr) {
151 transport = makeDefaultTransport();
152 BOOST_ASSERT(transport !=
nullptr);
154 m_transport = std::move(transport);
157 impl->ensureConnected(
false);
163 PendingInterestHandle
169 auto id = m_impl->m_pendingInterestTable.allocateId();
171 auto interest2 = make_shared<Interest>(interest);
172 interest2->getNonce();
175 impl->expressInterest(
id, interest2, afterSatisfied, afterNacked, afterTimeout);
185 impl->removeAllPendingInterests();
192 return m_impl->m_pendingInterestTable.size();
216 return setInterestFilter(filter, onInterest,
nullptr, onFailure, signingInfo, flags);
228 auto id = m_impl->registerPrefix(filter.
getPrefix(), onSuccess, onFailure, flags, options,
236 auto id = m_impl->m_interestFilterTable.allocateId();
239 impl->setInterestFilter(
id, filter, onInterest);
255 auto id = m_impl->registerPrefix(prefix, onSuccess, onFailure, flags, options, nullopt,
nullptr);
262 if (m_ioService.stopped()) {
266 auto onThrow = make_scope_fail([
this] { m_impl->shutdown(); });
268 if (timeout < 0_ms) {
274 if (timeout > 0_ms) {
275 m_impl->m_processEventsTimeoutEvent = m_impl->m_scheduler.schedule(timeout,
276 [&io = m_ioService, &work = m_impl->m_ioServiceWork] {
284 m_impl->m_ioServiceWork = make_unique<boost::asio::io_service::work>(m_ioService);
296 m_transport->close();
303 template<
typename NetPkt>
305 extractLpLocalFields(NetPkt& netPacket,
const lp::Packet& lpPacket)
307 addTagFromField<lp::IncomingFaceIdTag, lp::IncomingFaceIdField>(netPacket, lpPacket);
308 addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(netPacket, lpPacket);
312 Face::onReceiveElement(
const Block& blockFromDaemon)
314 lp::Packet lpPacket(blockFromDaemon);
318 Block netPacket({frag.first, frag.second});
319 switch (netPacket.type()) {
321 auto interest = make_shared<Interest>(netPacket);
323 auto nack = make_shared<lp::Nack>(std::move(*interest));
325 extractLpLocalFields(*nack, lpPacket);
326 NDN_LOG_DEBUG(
">N " << nack->getInterest() <<
'~' << nack->getHeader().getReason());
327 m_impl->nackPendingInterests(*nack);
330 extractLpLocalFields(*interest, lpPacket);
332 m_impl->processIncomingInterest(std::move(interest));
337 auto data = make_shared<Data>(netPacket);
338 extractLpLocalFields(*data, lpPacket);
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);
Represents a Data packet.
OversizedPacketError(char pktType, const Name &name, size_t wireSize)
Constructor.
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
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.
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...
void removeAllPendingInterests()
Cancel all previously expressed Interests.
Face(shared_ptr< Transport > transport=nullptr)
Create Face using given transport (or default transport if omitted)
PendingInterestHandle expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express an Interest.
void shutdown()
Shutdown face operations.
void put(Data data)
Publish a Data packet.
size_t getNPendingInterests() const
Get number of pending Interests.
Handle for a registered Interest filter.
InterestFilterHandle() noexcept=default
Declares the set of Interests a producer can serve.
const Name & getPrefix() const
Represents an Interest packet.
Represents an absolute name.
Handle for a pending Interest.
PendingInterestHandle() noexcept=default
Handle for a registered prefix.
void unregister(const UnregisterPrefixSuccessCallback &onSuccess=nullptr, const UnregisterPrefixFailureCallback &onFailure=nullptr)
Unregister the prefix.
RegisteredPrefixHandle() noexcept=default
static shared_ptr< TcpTransport > create(const std::string &uri)
Create transport with parameters defined in URI.
static shared_ptr< UnixTransport > create(const std::string &uri)
Create transport with parameters defined in URI.
Represents a Network Nack.
Contains options for ControlCommand execution.
CommandOptions & setSigningInfo(const security::SigningInfo &signingInfo)
Sets the signing parameters.
Signing parameters passed to KeyChain.
#define NDN_THROW_NESTED(e)
#define IO_CAPTURE_WEAK_IMPL_END
#define IO_CAPTURE_WEAK_IMPL(OP)
#define NDN_LOG_DEBUG(expression)
Log at DEBUG level.
std::string to_string(const errinfo_stacktrace &x)
FieldDecl< field_location_tags::Fragment, std::pair< Buffer::const_iterator, Buffer::const_iterator >, tlv::Fragment > FragmentField
Declare the Fragment field.
FieldDecl< field_location_tags::Header, NackHeader, tlv::Nack > NackField
boost::chrono::milliseconds milliseconds
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregistering a prefix fails.
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when an incoming Interest matches the specified InterestFilter.
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when a Nack is received in response to an expressed Interest.
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregistering a prefix succeeds.
function< void(const Interest &)> TimeoutCallback
Callback invoked when an expressed Interest times out.
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.
const size_t MAX_NDN_PACKET_SIZE
Practical size limit of a network-layer packet.
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when an expressed Interest is satisfied by a Data packet.