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();
187 impl->asyncExpressInterest(interest2, afterSatisfied, afterNacked, afterTimeout);
190 return reinterpret_cast<const PendingInterestId*
>(interest2.get());
197 impl->asyncRemovePendingInterest(pendingInterestId);
205 impl->asyncRemoveAllPendingInterests();
212 return m_impl->m_pendingInterestTable.size();
219 impl->asyncPutData(data);
227 impl->asyncPutNack(nack);
231 const RegisteredPrefixId*
238 return setInterestFilter(interestFilter, onInterest,
nullptr, onFailure, signingInfo, flags);
241 const RegisteredPrefixId*
249 auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
254 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
255 onSuccess, onFailure, flags, options);
258 const InterestFilterId*
262 auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
265 impl->asyncSetInterestFilter(filter);
268 return reinterpret_cast<const InterestFilterId*
>(filter.get());
271 const RegisteredPrefixId*
281 return m_impl->registerPrefix(prefix,
nullptr, onSuccess, onFailure, flags, options);
288 impl->asyncUnregisterPrefix(registeredPrefixId,
nullptr,
nullptr);
296 impl->asyncUnsetInterestFilter(interestFilterId);
306 impl->asyncUnregisterPrefix(registeredPrefixId, onSuccess, onFailure);
313 if (m_ioService.stopped()) {
318 if (timeout < time::milliseconds::zero()) {
324 if (timeout > time::milliseconds::zero()) {
325 boost::asio::io_service& ioService = m_ioService;
326 unique_ptr<boost::asio::io_service::work>& work = m_impl->m_ioServiceWork;
327 m_impl->m_processEventsTimeoutEvent = m_impl->m_scheduler.scheduleEvent(timeout,
328 [&ioService, &work] {
336 m_impl->m_ioServiceWork.reset(
new 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);
Copyright (c) 2013-2017 Regents of the University of California.
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command fails.
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
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
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