34 #define IO_CAPTURE_WEAK_IMPL(OP) \
36 weak_ptr<Impl> implWeak(m_impl); \
37 m_ioService.OP([=] { \
38 auto impl = implWeak.lock(); \
39 if (impl != nullptr) {
40 #define IO_CAPTURE_WEAK_IMPL_END \
48 : m_internalIoService(new
boost::asio::io_service())
49 , m_ioService(*m_internalIoService)
51 , m_impl(make_shared<
Impl>(*this))
53 construct(transport, *m_internalKeyChain);
57 : m_ioService(ioService)
59 , m_impl(make_shared<
Impl>(*this))
61 construct(
nullptr, *m_internalKeyChain);
64 Face::Face(
const std::string& host,
const std::string& port)
65 : m_internalIoService(new
boost::asio::io_service())
66 , m_ioService(*m_internalIoService)
68 , m_impl(make_shared<
Impl>(*this))
70 construct(make_shared<TcpTransport>(host, port), *m_internalKeyChain);
74 : m_internalIoService(new
boost::asio::io_service())
75 , m_ioService(*m_internalIoService)
76 , m_impl(make_shared<
Impl>(*this))
78 construct(transport, keyChain);
81 Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService)
82 : m_ioService(ioService)
84 , m_impl(make_shared<
Impl>(*this))
86 construct(transport, *m_internalKeyChain);
89 Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService,
KeyChain& keyChain)
90 : m_ioService(ioService)
91 , m_impl(make_shared<
Impl>(*this))
93 construct(transport, keyChain);
97 Face::makeDefaultTransport()
102 std::string transportUri;
104 const char* transportEnviron = getenv(
"NDN_CLIENT_TRANSPORT");
105 if (transportEnviron !=
nullptr) {
106 transportUri = transportEnviron;
110 transportUri = config.getParsedConfiguration().get<std::string>(
"transport",
"");
113 if (transportUri.empty()) {
118 std::string protocol;
120 util::FaceUri uri(transportUri);
121 protocol = uri.getScheme();
123 if (protocol ==
"unix") {
126 else if (protocol ==
"tcp" || protocol ==
"tcp4" || protocol ==
"tcp6") {
130 BOOST_THROW_EXCEPTION(ConfigFile::Error(
"Unsupported transport protocol \"" + protocol +
"\""));
133 catch (
const Transport::Error& error) {
134 BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
136 catch (
const util::FaceUri::Error& error) {
137 BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
142 Face::construct(shared_ptr<Transport> transport,
KeyChain& keyChain)
144 if (transport ==
nullptr) {
145 transport = makeDefaultTransport();
147 BOOST_ASSERT(transport !=
nullptr);
148 m_transport = transport;
150 m_nfdController.reset(
new nfd::Controller(*
this, keyChain));
153 impl->ensureConnected(
false);
159 shared_ptr<Transport>
165 const PendingInterestId*
171 shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest);
175 BOOST_THROW_EXCEPTION(
Error(
"Interest size exceeds maximum limit"));
180 impl->asyncExpressInterest(interestToExpress, afterSatisfied, afterNacked, afterTimeout);
183 return reinterpret_cast<const PendingInterestId*
>(interestToExpress.get());
186 const PendingInterestId*
194 if (onData !=
nullptr) {
195 onData(interest, const_cast<Data&>(data));
199 if (onTimeout !=
nullptr) {
207 const PendingInterestId*
219 impl->asyncRemovePendingInterest(pendingInterestId);
227 impl->asyncRemoveAllPendingInterests();
234 return m_impl->m_pendingInterestTable.size();
243 bool hasLpFields =
false;
246 if (cachePolicyTag !=
nullptr) {
252 if (congestionMarkTag !=
nullptr) {
263 BOOST_THROW_EXCEPTION(
Error(
"Data size exceeds maximum limit"));
266 impl->asyncSend(wire);
279 if (congestionMarkTag !=
nullptr) {
286 BOOST_THROW_EXCEPTION(
Error(
"Nack size exceeds maximum limit"));
289 impl->asyncSend(wire);
293 const RegisteredPrefixId*
300 return setInterestFilter(interestFilter, onInterest,
nullptr, onFailure, signingInfo, flags);
303 const RegisteredPrefixId*
311 auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
316 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
317 onSuccess, onFailure, flags, options);
320 const InterestFilterId*
324 auto filter = make_shared<InterestFilterRecord>(interestFilter, onInterest);
327 impl->asyncSetInterestFilter(filter);
330 return reinterpret_cast<const InterestFilterId*
>(filter.get());
333 #ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
335 const RegisteredPrefixId*
347 return setInterestFilter(interestFilter, onInterest, onSuccess, onFailure, signingInfo, flags);
350 const RegisteredPrefixId*
361 return setInterestFilter(interestFilter, onInterest, onFailure, signingInfo, flags);
364 const RegisteredPrefixId*
369 const Name& identity,
374 onSuccess, onFailure,
378 const RegisteredPrefixId*
382 const Name& identity,
386 return setInterestFilter(interestFilter, onInterest, onFailure, signingInfo, flags);
389 #endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
391 const RegisteredPrefixId*
401 return m_impl->registerPrefix(prefix,
nullptr, onSuccess, onFailure, flags, options);
404 #ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
405 const RegisteredPrefixId*
416 return registerPrefix(prefix, onSuccess, onFailure, signingInfo, flags);
419 const RegisteredPrefixId*
423 const Name& identity,
427 return registerPrefix(prefix, onSuccess, onFailure, signingInfo, flags);
429 #endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
435 impl->asyncUnregisterPrefix(registeredPrefixId,
nullptr,
nullptr);
443 impl->asyncUnsetInterestFilter(interestFilterId);
453 impl->asyncUnregisterPrefix(registeredPrefixId, onSuccess, onFailure);
460 if (m_ioService.stopped()) {
465 if (timeout < time::milliseconds::zero()) {
471 if (timeout > time::milliseconds::zero()) {
472 boost::asio::io_service& ioService = m_ioService;
473 unique_ptr<boost::asio::io_service::work>& work = m_impl->m_ioServiceWork;
474 m_impl->m_processEventsTimeoutEvent = m_impl->m_scheduler.scheduleEvent(timeout,
475 [&ioService, &work] {
483 m_impl->m_ioServiceWork.reset(
new boost::asio::io_service::work(m_ioService));
489 m_impl->m_ioServiceWork.reset();
490 m_impl->m_pendingInterestTable.clear();
491 m_impl->m_registeredPrefixTable.clear();
500 this->asyncShutdown();
505 Face::asyncShutdown()
507 m_impl->m_pendingInterestTable.clear();
508 m_impl->m_registeredPrefixTable.clear();
510 if (m_transport->isConnected())
511 m_transport->close();
513 m_impl->m_ioServiceWork.reset();
519 template<
typename NetPkt>
533 Face::onReceiveElement(
const Block& blockFromDaemon)
535 lp::Packet lpPacket(blockFromDaemon);
538 Buffer::const_iterator begin, end;
540 Block netPacket(&*begin, std::distance(begin, end));
541 switch (netPacket.type()) {
543 auto interest = make_shared<Interest>(netPacket);
545 auto nack = make_shared<lp::Nack>(std::move(*interest));
548 m_impl->nackPendingInterests(*nack);
552 m_impl->processInterestFilters(*interest);
557 auto data = make_shared<Data>(netPacket);
559 m_impl->satisfyPendingInterests(*data);
size_t wireEncode(EncodingImpl< TAG > &encoder) const
append packet to encoder
Copyright (c) 2013-2016 Regents of the University of California.
const NackHeader & getHeader() const
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command fails.
Copyright (c) 2013-2016 Regents of the University of California.
implementation detail of Face
Buffer::const_iterator end() const
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...
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
declares the set of Interests a producer can serve, which starts with a name prefix, plus an optional regular expression
const Interest & getInterest() const
Class representing a wire element of NDN-TLV packet format.
represents an Interest packet
function< void(const Interest &)> OnTimeout
Callback invoked when expressed Interest times out.
static void extractLpLocalFields(NetPkt &netPacket, const lp::Packet &lpPacket)
extract local fields from NDNLPv2 packet and tag onto a network layer packet
function< void(const InterestFilter &, const Interest &)> OnInterest
Callback invoked when incoming Interest matches the specified InterestFilter.
virtual void doProcessEvents(const time::milliseconds &timeout, bool keepThread)
Signing parameters passed to KeyChain.
SigningInfo signingByCertificate(const Name &certName)
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
const Name & getName() const
Get name of the Data packet.
provides a tag type for simple types
#define IO_CAPTURE_WEAK_IMPL(OP)
Copyright (c) 2013-2017 Regents of the University of California.
void removeAllPendingInterests()
Cancel all previously expressed Interests.
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Fast encoding or block size estimation.
detail::FieldDecl< field_location_tags::Fragment, std::pair< Buffer::const_iterator, Buffer::const_iterator >, tlv::Fragment > FragmentField
The value of the wire encoded field is the data between the provided iterators.
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.
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
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.
Name abstraction to represent an absolute name.
void unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId.
detail::FieldDecl< field_location_tags::Header, NackHeader, tlv::Nack > NackField
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
SigningInfo signingByIdentity(const Name &identityName)
function< void(const Interest &, Data &)> OnData
Callback invoked when expressed Interest gets satisfied with Data packet.
size_t getNPendingInterests() const
Get number of pending Interests.
bool empty() const
Check if name is emtpy.
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command succeeds.
shared_ptr< T > getTag() const
get a tag item
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.
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.
Buffer::const_iterator begin() const
void put(const Data &data)
Publish data packet.
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size