ndn::Face Class Reference

Provide a communication channel with local or remote NDN forwarder. More...

#include <ndn-cxx/face.hpp>

+ Inheritance diagram for ndn::Face:
+ Collaboration diagram for ndn::Face:

Classes

class  Error
 
class  OversizedPacketError
 Exception thrown when attempting to send a packet over size limit. More...
 

Public Member Functions

 Face (boost::asio::io_context &ioCtx)
 Create Face using default transport and given io_context. More...
 
 Face (const std::string &host, const std::string &port="6363")
 Create Face using TcpTransport. More...
 
 Face (shared_ptr< Transport > transport, boost::asio::io_context &ioCtx)
 Create Face using the given Transport and io_context. More...
 
 Face (shared_ptr< Transport > transport, boost::asio::io_context &ioCtx, KeyChain &keyChain)
 Create Face using the given Transport, io_context, and KeyChain. More...
 
 Face (shared_ptr< Transport > transport, KeyChain &keyChain)
 Create Face using the given Transport and KeyChain. More...
 
 Face (shared_ptr< Transport > transport=nullptr)
 Create Face using the given Transport (or default transport if omitted). More...
 
virtual ~Face ()
 
PendingInterestHandle expressInterest (const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
 Express an Interest. More...
 
boost::asio::io_context & getIoContext () const noexcept
 Returns a reference to the io_context used by this face. More...
 
boost::asio::io_context & getIoService () const noexcept
 
size_t getNPendingInterests () const
 Get number of pending Interests. More...
 
void processEvents (time::milliseconds timeout=0_ms, bool keepRunning=false)
 Run the event loop to process any pending work and execute completion handlers. More...
 
void put (const Data &data)
 Publish a Data packet. More...
 
void put (const lp::Nack &nack)
 Send a Network Nack. More...
 
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. More...
 
void removeAllPendingInterests ()
 Cancel all previously expressed Interests. More...
 
InterestFilterHandle setInterestFilter (const InterestFilter &filter, const InterestCallback &onInterest)
 Set an InterestFilter to dispatch matching incoming Interests to onInterest callback. More...
 
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 filtered prefix with the connected NDN forwarder. More...
 
RegisteredPrefixHandle setInterestFilter (const InterestFilter &filter, const InterestCallback &onInterest, const RegisterPrefixSuccessCallback &onSuccess, 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 filtered prefix with the connected NDN forwarder. More...
 
void shutdown ()
 Shutdown face operations. More...
 

Protected Member Functions

virtual void doProcessEvents (time::milliseconds timeout, bool keepRunning)
 
TransportgetTransport () const
 Returns the underlying transport. More...
 

Detailed Description

Provide a communication channel with local or remote NDN forwarder.

Definition at line 90 of file face.hpp.

Constructor & Destructor Documentation

◆ Face() [1/6]

ndn::Face::Face ( shared_ptr< Transport transport = nullptr)
explicit

Create Face using the given Transport (or default transport if omitted).

Parameters
transportthe transport for lower layer communication. If nullptr, a default transport will be used. The default transport is determined from a FaceUri in NDN_CLIENT_TRANSPORT environ, a FaceUri in configuration file 'transport' key, or UnixTransport.
Exceptions
ConfigFile::Errortransport is nullptr, and the configuration file cannot be parsed or specifies an unsupported protocol
Note
shared_ptr is passed by value because ownership is shared with this class

Definition at line 47 of file face.cpp.

◆ Face() [2/6]

ndn::Face::Face ( boost::asio::io_context &  ioCtx)
explicit

Create Face using default transport and given io_context.

Usage examples:

Face face1;
Face face2(face1.getIoContext());
// Now the following ensures that events on both faces are processed
face1.processEvents();
// or face1.getIoContext().run();
Face(shared_ptr< Transport > transport=nullptr)
Create Face using the given Transport (or default transport if omitted).
Definition: face.cpp:47

or

boost::asio::io_context ioCtx;
Face face1(ioCtx);
Face face2(ioCtx);
ioCtx.run();
Parameters
ioCtxA reference to the io_context object that should control all I/O operations.
Exceptions
ConfigFile::Errorthe configuration file cannot be parsed or specifies an unsupported protocol

Definition at line 55 of file face.cpp.

◆ Face() [3/6]

ndn::Face::Face ( const std::string &  host,
const std::string &  port = "6363" 
)
explicit

Create Face using TcpTransport.

Parameters
hostIP address or hostname of the NDN forwarder
portport number or service name of the NDN forwarder (default: "6363")

Definition at line 62 of file face.cpp.

◆ Face() [4/6]

ndn::Face::Face ( shared_ptr< Transport transport,
KeyChain keyChain 
)

Create Face using the given Transport and KeyChain.

Parameters
transportthe transport for lower layer communication. If nullptr, a default transport will be used.
keyChainthe KeyChain to sign commands
See also
Face(shared_ptr<Transport>)
Exceptions
ConfigFile::Errortransport is nullptr, and the configuration file cannot be parsed or specifies an unsupported protocol
Note
shared_ptr is passed by value because ownership is shared with this class

Definition at line 70 of file face.cpp.

◆ Face() [5/6]

ndn::Face::Face ( shared_ptr< Transport transport,
boost::asio::io_context &  ioCtx 
)

Create Face using the given Transport and io_context.

Parameters
transportthe transport for lower layer communication. If nullptr, a default transport will be used.
ioCtxthe io_context that controls all I/O operations
See also
Face(boost::asio::io_context&)
Face(shared_ptr<Transport>)
Exceptions
ConfigFile::Errortransport is nullptr, and the configuration file cannot be parsed or specifies an unsupported protocol
Note
shared_ptr is passed by value because ownership is shared with this class

Definition at line 77 of file face.cpp.

◆ Face() [6/6]

ndn::Face::Face ( shared_ptr< Transport transport,
boost::asio::io_context &  ioCtx,
KeyChain keyChain 
)

Create Face using the given Transport, io_context, and KeyChain.

Parameters
transportthe transport for lower layer communication. If nullptr, a default transport will be used.
ioCtxthe io_context that controls all I/O operations
keyChainthe KeyChain to sign commands
See also
Face(boost::asio::io_context&)
Face(shared_ptr<Transport>, KeyChain&)
Exceptions
ConfigFile::Errortransport is nullptr, and the configuration file cannot be parsed or specifies an unsupported protocol
Note
shared_ptr is passed by value because ownership is shared with this class

Definition at line 84 of file face.cpp.

◆ ~Face()

ndn::Face::~Face ( )
virtualdefault

Member Function Documentation

◆ doProcessEvents()

void ndn::Face::doProcessEvents ( time::milliseconds  timeout,
bool  keepRunning 
)
protectedvirtual

Definition at line 256 of file face.cpp.

◆ expressInterest()

PendingInterestHandle ndn::Face::expressInterest ( const Interest interest,
const DataCallback afterSatisfied,
const NackCallback afterNacked,
const TimeoutCallback afterTimeout 
)

Express an Interest.

Parameters
interestthe Interest; a copy will be made, so that the caller is not required to maintain the argument unchanged
afterSatisfiedfunction to be invoked if Data is returned
afterNackedfunction to be invoked if Network NACK is returned
afterTimeoutfunction to be invoked if neither Data nor Network NACK is returned within InterestLifetime
Returns
A handle for canceling the pending Interest.
Exceptions
OversizedPacketErrorEncoded Interest size exceeds MAX_NDN_PACKET_SIZE.

Definition at line 151 of file face.cpp.

◆ getIoContext()

boost::asio::io_context& ndn::Face::getIoContext ( ) const
inlinenoexcept

Returns a reference to the io_context used by this face.

Definition at line 422 of file face.hpp.

◆ getIoService()

boost::asio::io_context& ndn::Face::getIoService ( ) const
inlinenoexcept
Deprecated:
Use getIoContext()

Definition at line 432 of file face.hpp.

◆ getNPendingInterests()

size_t ndn::Face::getNPendingInterests ( ) const

Get number of pending Interests.

Definition at line 180 of file face.cpp.

◆ getTransport()

Transport& ndn::Face::getTransport ( ) const
inlineprotected

Returns the underlying transport.

Definition at line 442 of file face.hpp.

◆ processEvents()

void ndn::Face::processEvents ( time::milliseconds  timeout = 0_ms,
bool  keepRunning = false 
)
inline

Run the event loop to process any pending work and execute completion handlers.

This call will block forever (with the default timeout of 0) to process I/O on the face. To exit cleanly on a producer, clear any Interest filters and wait for processEvents() to return. To exit after an error, one can call shutdown(). In consumer applications, processEvents() will return when all expressed Interests have been satisfied, Nacked, or timed out. To terminate earlier, a consumer application should cancel all previously expressed and still-pending Interests.

If timeout is a positive value, then processEvents() will return after the specified duration has elapsed, unless the event loop is stopped earlier with shutdown() or runs out of work to do.

If a negative timeout is specified, then processEvents() will not block and will process only handlers that are ready to run.

processEvents() can be called repeatedly, if desired.

Parameters
timeoutMaximum amount of time to block the event loop (see above).
keepRunningKeep thread in a blocked state (in event processing), even when there are no outstanding events (e.g., no Interest/Data is expected). Ignored if timeout is negative. If timeout is 0 and keepRunning is true, the only way to stop processEvents() is to call shutdown().
Note
This may throw an exception for reading data or in the callback for processing the data. If you call this from an main event loop, you may want to catch and log/disregard all exceptions.
Exceptions
OversizedPacketErrorEncoded packet size exceeds MAX_NDN_PACKET_SIZE.

Definition at line 397 of file face.hpp.

◆ put() [1/2]

void ndn::Face::put ( const Data data)

Publish a Data packet.

Parameters
dataThe Data packet; a copy will be made, so that the caller is not required to maintain the argument unchanged.

This method can be called to satisfy incoming Interests, or to add Data packet into the cache of the local NDN forwarder if forwarder is configured to accept unsolicited Data.

Exceptions
OversizedPacketErrorEncoded Data size exceeds MAX_NDN_PACKET_SIZE.

Definition at line 186 of file face.cpp.

◆ put() [2/2]

void ndn::Face::put ( const lp::Nack nack)

Send a Network Nack.

Parameters
nackThe Nack packet; a copy will be made, so that the caller is not required to maintain the argument unchanged.
Exceptions
OversizedPacketErrorEncoded Nack size exceeds MAX_NDN_PACKET_SIZE.

Definition at line 196 of file face.cpp.

◆ registerPrefix()

RegisteredPrefixHandle ndn::Face::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.

This method only modifies forwarder's RIB and does not associate any onInterest callbacks. Use setInterestFilter method to dispatch incoming Interests to the right callbacks.

Parameters
prefixA prefix to register with the connected NDN forwarder
onSuccessA callback to be called when prefixRegister command succeeds
onFailureA callback to be called when prefixRegister command fails
signingInfoSigning parameters. When omitted, a default parameters used in the signature will be used.
flagsPrefix registration flags
Returns
A handle for unregistering the prefix.
See also
nfd::RouteFlags

Definition at line 242 of file face.cpp.

◆ removeAllPendingInterests()

void ndn::Face::removeAllPendingInterests ( )

Cancel all previously expressed Interests.

Definition at line 170 of file face.cpp.

◆ setInterestFilter() [1/3]

InterestFilterHandle ndn::Face::setInterestFilter ( const InterestFilter filter,
const InterestCallback onInterest 
)

Set an InterestFilter to dispatch matching incoming Interests to onInterest callback.

Parameters
filterInterest filter
onInterestA callback to be called when a matching interest is received

This method modifies library's FIB only, and does not register the prefix with the forwarder. It will always succeed. To register a prefix with the forwarder, use registerPrefix() or one of the other two setInterestFilter() overloads.

Returns
A handle for unsetting the Interest filter.

Definition at line 228 of file face.cpp.

◆ setInterestFilter() [2/3]

RegisteredPrefixHandle ndn::Face::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 filtered prefix with the connected NDN forwarder.

This version of setInterestFilter combines setInterestFilter and registerPrefix operations and is intended to be used when only one filter for the same prefix needed to be set. When multiple names sharing the same prefix should be dispatched to different callbacks, use one registerPrefix call, followed (in onSuccess callback) by a series of setInterestFilter calls.

Parameters
filterInterest filter (prefix part will be registered with the forwarder)
onInterestA callback to be called when a matching interest is received
onFailureA callback to be called when prefixRegister command fails
signingInfoSigning parameters. When omitted, a default parameters used in the signature will be used.
flagsPrefix registration flags
Returns
A handle for unregistering the prefix and unsetting the Interest filter.

Definition at line 206 of file face.cpp.

◆ setInterestFilter() [3/3]

RegisteredPrefixHandle ndn::Face::setInterestFilter ( const InterestFilter filter,
const InterestCallback onInterest,
const RegisterPrefixSuccessCallback onSuccess,
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 filtered prefix with the connected NDN forwarder.

This version of setInterestFilter combines setInterestFilter and registerPrefix operations and is intended to be used when only one filter for the same prefix needed to be set. When multiple names sharing the same prefix should be dispatched to different callbacks, use one registerPrefix call, followed (in onSuccess callback) by a series of setInterestFilter calls.

Parameters
filterInterest filter (prefix part will be registered with the forwarder)
onInterestA callback to be called when a matching interest is received
onSuccessA callback to be called when prefixRegister command succeeds
onFailureA callback to be called when prefixRegister command fails
signingInfoSigning parameters. When omitted, a default parameters used in the signature will be used.
flagsPrefix registration flags
Returns
A handle for unregistering the prefix and unsetting the Interest filter.

Definition at line 214 of file face.cpp.

◆ shutdown()

void ndn::Face::shutdown ( )

Shutdown face operations.

This method cancels all pending operations and closes the connection to the NDN forwarder.

Note that this method does not stop the io_context if it is shared between multiple Faces or with other I/O objects (e.g., Scheduler).

Warning
Calling this method may cause outgoing packets to be lost. Producers that shut down immediately after sending a Data packet should instead clear all Interest filters to shut down cleanly.
See also
processEvents()

Definition at line 288 of file face.cpp.