23 #include "detail/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, " 61 , m_ioService(*m_internalIoService)
63 , m_impl(make_shared<Impl>(*this))
65 construct(std::move(transport), *m_internalKeyChain);
69 : m_ioService(ioService)
71 , m_impl(make_shared<Impl>(*this))
73 construct(
nullptr, *m_internalKeyChain);
76 Face::Face(
const std::string& host,
const std::string& port)
78 , m_ioService(*m_internalIoService)
80 , m_impl(make_shared<Impl>(*this))
82 construct(make_shared<TcpTransport>(host, port), *m_internalKeyChain);
85 Face::Face(shared_ptr<Transport> transport, KeyChain& keyChain)
87 , m_ioService(*m_internalIoService)
88 , m_impl(make_shared<Impl>(*this))
90 construct(std::move(transport), keyChain);
93 Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService)
94 : m_ioService(ioService)
96 , m_impl(make_shared<Impl>(*this))
98 construct(std::move(transport), *m_internalKeyChain);
101 Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain)
102 : m_ioService(ioService)
103 , m_impl(make_shared<Impl>(*this))
105 construct(std::move(transport), keyChain);
108 shared_ptr<Transport>
109 Face::makeDefaultTransport()
114 std::string transportUri;
116 const char* transportEnviron = getenv(
"NDN_CLIENT_TRANSPORT");
117 if (transportEnviron !=
nullptr) {
118 transportUri = transportEnviron;
125 if (transportUri.empty()) {
130 std::string protocol;
135 if (protocol ==
"unix") {
138 else if (protocol ==
"tcp" || protocol ==
"tcp4" || protocol ==
"tcp6") {
142 BOOST_THROW_EXCEPTION(
ConfigFile::Error(
"Unsupported transport protocol \"" + protocol +
"\""));
154 Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
156 if (transport ==
nullptr) {
157 transport = makeDefaultTransport();
159 BOOST_ASSERT(transport !=
nullptr);
160 m_transport = std::move(transport);
162 m_nfdController = make_unique<nfd::Controller>(*
this, keyChain);
165 impl->ensureConnected(
false);
171 shared_ptr<Transport>
177 const PendingInterestId*
183 shared_ptr<Interest> interest2 = make_shared<Interest>(interest);
184 interest2->getNonce();
188 impl->asyncExpressInterest(interest2, afterSatisfied, afterNacked, afterTimeout);
191 return reinterpret_cast<const PendingInterestId*
>(interest2.get());
198 impl->asyncRemovePendingInterest(pendingInterestId);
206 impl->asyncRemoveAllPendingInterests();
213 return m_impl->m_pendingInterestTable.size();
221 impl->asyncPutData(data);
230 impl->asyncPutNack(nack);
234 const RegisteredPrefixId*
241 return setInterestFilter(interestFilter, onInterest,
nullptr, onFailure, signingInfo, flags);
244 const RegisteredPrefixId*
252 auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
257 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
258 onSuccess, onFailure, flags, options);
261 const InterestFilterId*
265 auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
268 impl->asyncSetInterestFilter(filter);
271 return reinterpret_cast<const InterestFilterId*
>(filter.get());
274 const RegisteredPrefixId*
284 return m_impl->registerPrefix(prefix,
nullptr, onSuccess, onFailure, flags, options);
291 impl->asyncUnregisterPrefix(registeredPrefixId,
nullptr,
nullptr);
299 impl->asyncUnsetInterestFilter(interestFilterId);
309 impl->asyncUnregisterPrefix(registeredPrefixId, onSuccess, onFailure);
316 if (m_ioService.stopped()) {
321 if (timeout < time::milliseconds::zero()) {
327 if (timeout > time::milliseconds::zero()) {
328 boost::asio::io_service& ioService = m_ioService;
329 unique_ptr<boost::asio::io_service::work>& work = m_impl->m_ioServiceWork;
330 m_impl->m_processEventsTimeoutEvent = m_impl->m_scheduler.scheduleEvent(timeout,
331 [&ioService, &work] {
339 m_impl->m_ioServiceWork.reset(
new boost::asio::io_service::work(m_ioService));
345 m_impl->m_ioServiceWork.reset();
346 m_impl->m_pendingInterestTable.clear();
347 m_impl->m_registeredPrefixTable.clear();
356 this->asyncShutdown();
361 Face::asyncShutdown()
363 m_impl->m_pendingInterestTable.clear();
364 m_impl->m_registeredPrefixTable.clear();
366 if (m_transport->isConnected())
367 m_transport->close();
369 m_impl->m_ioServiceWork.reset();
375 template<
typename NetPkt>
379 addTagFromField<lp::IncomingFaceIdTag, lp::IncomingFaceIdField>(netPacket, lpPacket);
380 addTagFromField<lp::CongestionMarkTag, lp::CongestionMarkField>(netPacket, lpPacket);
384 Face::onReceiveElement(
const Block& blockFromDaemon)
389 Buffer::const_iterator begin, end;
391 Block netPacket(&*begin, std::distance(begin, end));
392 switch (netPacket.type()) {
394 auto interest = make_shared<Interest>(netPacket);
396 auto nack = make_shared<lp::Nack>(std::move(*interest));
399 NDN_LOG_DEBUG(
">N " << nack->getInterest() <<
'~' << nack->getHeader().getReason());
400 m_impl->nackPendingInterests(*nack);
405 m_impl->processIncomingInterest(std::move(interest));
410 auto data = make_shared<Data>(netPacket);
413 m_impl->satisfyPendingInterests(*data);
Copyright (c) 2013-2017 Regents of the University of California.
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
const NackHeader & getHeader() const
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command fails.
Copyright (c) 2013-2017 Regents of the University of California.
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...
declares the set of Interests a producer can serve, which starts with a name prefix, plus an optional regular expression
unique_ptr< T > make_unique(Args &&...args)
const Parsed & getParsedConfiguration() const
const Interest & getInterest() 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
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.
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
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when incoming Interest matches the specified InterestFilter.
#define IO_CAPTURE_WEAK_IMPL_END
const Name & getName() const
Get name.
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)
const PendingInterestId * expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
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.
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.
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size