ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
face.hpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2025 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_CXX_FACE_HPP
23#define NDN_CXX_FACE_HPP
24
25#include "ndn-cxx/data.hpp"
26#include "ndn-cxx/interest.hpp"
31#include "ndn-cxx/lp/nack.hpp"
34
35namespace ndn {
36
37class Transport;
38
39class PendingInterestHandle;
40class RegisteredPrefixHandle;
41class InterestFilterHandle;
42
43namespace detail {
44using RecordId = uint64_t;
45} // namespace detail
46
50using DataCallback = std::function<void(const Interest&, const Data&)>;
51
55using NackCallback = std::function<void(const Interest&, const lp::Nack&)>;
56
60using TimeoutCallback = std::function<void(const Interest&)>;
61
65using InterestCallback = std::function<void(const InterestFilter&, const Interest&)>;
66
70using RegisterPrefixSuccessCallback = std::function<void(const Name&)>;
71
75using RegisterPrefixFailureCallback = std::function<void(const Name&, const std::string&)>;
76
80using UnregisterPrefixSuccessCallback = std::function<void()>;
81
85using UnregisterPrefixFailureCallback = std::function<void(const std::string&)>;
86
90class Face : noncopyable
91{
92public:
93 class Error : public std::runtime_error
94 {
95 public:
96 using std::runtime_error::runtime_error;
97 };
98
103 {
104 public:
111 OversizedPacketError(char pktType, const Name& name, size_t wireSize);
112
113 public:
114 const char pktType;
115 const Name name;
116 const size_t wireSize;
117 };
118
119public: // constructors
131 explicit
132 Face(shared_ptr<Transport> transport = nullptr);
133
160 explicit
161 Face(boost::asio::io_context& ioCtx);
162
169 explicit
170 Face(const std::string& host, const std::string& port = "6363");
171
184 Face(shared_ptr<Transport> transport, KeyChain& keyChain);
185
199 Face(shared_ptr<Transport> transport, boost::asio::io_context& ioCtx);
200
215 Face(shared_ptr<Transport> transport, boost::asio::io_context& ioCtx, KeyChain& keyChain);
216
217 virtual
219
220public: // consumer
233 expressInterest(const Interest& interest,
234 const DataCallback& afterSatisfied,
235 const NackCallback& afterNacked,
236 const TimeoutCallback& afterTimeout);
237
241 void
243
247 size_t
248 getNPendingInterests() const;
249
250public: // producer
271 setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
272 const RegisterPrefixFailureCallback& onFailure,
273 const security::SigningInfo& signingInfo = security::SigningInfo(),
274 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
275
297 setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
298 const RegisterPrefixSuccessCallback& onSuccess,
299 const RegisterPrefixFailureCallback& onFailure,
300 const security::SigningInfo& signingInfo = security::SigningInfo(),
301 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
302
316 setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest);
317
336 registerPrefix(const Name& prefix,
337 const RegisterPrefixSuccessCallback& onSuccess,
338 const RegisterPrefixFailureCallback& onFailure,
339 const security::SigningInfo& signingInfo = security::SigningInfo(),
340 uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
341
352 void
353 put(const Data& data);
354
361 void
362 put(const lp::Nack& nack);
363
364public: // event loop routines
396 void
397 processEvents(time::milliseconds timeout = 0_ms, bool keepRunning = false)
398 {
399 this->doProcessEvents(timeout, keepRunning);
400 }
401
415 void
416 shutdown();
417
421 boost::asio::io_context&
422 getIoContext() const noexcept
423 {
424 return m_ioCtx;
425 }
426
431 Transport&
433 {
434 return *m_transport;
435 }
436
437protected:
438 virtual void
439 doProcessEvents(time::milliseconds timeout, bool keepRunning);
440
441private:
445 void
446 construct(shared_ptr<Transport> transport, KeyChain& keyChain);
447
448 void
449 onReceiveElement(const Block& blockFromDaemon);
450
451private:
453 unique_ptr<boost::asio::io_context> m_internalIoCtx;
455 boost::asio::io_context& m_ioCtx;
456
457 shared_ptr<Transport> m_transport;
458
466 unique_ptr<KeyChain> m_internalKeyChain;
467
468 class Impl;
469 shared_ptr<Impl> m_impl;
470
474};
475
484{
485public:
486 PendingInterestHandle() noexcept = default;
487
488private:
489 PendingInterestHandle(weak_ptr<Face::Impl> impl, detail::RecordId id);
490
491 friend Face;
492};
493
506using ScopedPendingInterestHandle = detail::ScopedCancelHandle<PendingInterestHandle>;
507
511{
512public:
513 RegisteredPrefixHandle() noexcept = default;
514
517 void
518 unregister(const UnregisterPrefixSuccessCallback& onSuccess = nullptr,
519 const UnregisterPrefixFailureCallback& onFailure = nullptr);
520
521private:
522 RegisteredPrefixHandle(weak_ptr<Face::Impl> impl, detail::RecordId id);
523
524 static void
525 unregister(const weak_ptr<Face::Impl>& impl, detail::RecordId id,
526 const UnregisterPrefixSuccessCallback& onSuccess,
527 const UnregisterPrefixFailureCallback& onFailure);
528
529private:
530 weak_ptr<Face::Impl> m_weakImpl;
531 detail::RecordId m_id = 0;
532
533 friend Face;
534};
535
549using ScopedRegisteredPrefixHandle = detail::ScopedCancelHandle<RegisteredPrefixHandle>;
550
558class InterestFilterHandle : public detail::CancelHandle
559{
560public:
561 InterestFilterHandle() noexcept = default;
562
563private:
564 InterestFilterHandle(weak_ptr<Face::Impl> impl, detail::RecordId id);
565
566 friend Face;
567};
568
581using ScopedInterestFilterHandle = detail::ScopedCancelHandle<InterestFilterHandle>;
582
583} // namespace ndn
584
585#endif // NDN_CXX_FACE_HPP
Represents a TLV element of the NDN packet format.
Definition block.hpp:45
Represents a Data packet.
Definition data.hpp:39
Exception thrown when attempting to send a packet over size limit.
Definition face.hpp:103
Provide a communication channel with local or remote NDN forwarder.
Definition face.hpp:91
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.
Definition face.cpp:242
virtual ~Face()
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...
Definition face.cpp:206
virtual void doProcessEvents(time::milliseconds timeout, bool keepRunning)
Definition face.cpp:256
void removeAllPendingInterests()
Cancel all previously expressed Interests.
Definition face.cpp:170
void put(const Data &data)
Publish a Data packet.
Definition face.cpp:186
PendingInterestHandle expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express an Interest.
Definition face.cpp:151
void shutdown()
Shutdown face operations.
Definition face.cpp:288
boost::asio::io_context & getIoContext() const noexcept
Returns a reference to the io_context used by this face.
Definition face.hpp:422
size_t getNPendingInterests() const
Get number of pending Interests.
Definition face.cpp:180
Transport & getTransport() const
Returns the underlying transport.
Definition face.hpp:432
void processEvents(time::milliseconds timeout=0_ms, bool keepRunning=false)
Run the event loop to process any pending work and execute completion handlers.
Definition face.hpp:397
Handle for a registered Interest filter.
Definition face.hpp:559
InterestFilterHandle() noexcept=default
Declares the set of Interests a producer can serve.
Represents an Interest packet.
Definition interest.hpp:50
Represents an absolute name.
Definition name.hpp:45
Handle for a pending Interest.
Definition face.hpp:484
PendingInterestHandle() noexcept=default
Handle for a registered prefix.
Definition face.hpp:511
RegisteredPrefixHandle() noexcept=default
Provides a "TLV-oriented" delivery service.
Definition transport.hpp:37
Handle to cancel an operation.
Represents a Network Nack.
Definition nack.hpp:39
The main interface for signing key management.
Definition key-chain.hpp:87
Signing parameters passed to KeyChain.
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition common.hpp:48
@ ROUTE_FLAG_CHILD_INHERIT
uint64_t RecordId
Definition face.hpp:44
::boost::chrono::milliseconds milliseconds
Definition time.hpp:52
Definition data.cpp:25
std::function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Definition face.hpp:70
std::function< void(const Interest &)> TimeoutCallback
Callback invoked when an expressed Interest times out.
Definition face.hpp:60
std::function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregistering a prefix fails.
Definition face.hpp:85
std::function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when a Nack is received in response to an expressed Interest.
Definition face.hpp:55
std::function< void(const Interest &, const Data &)> DataCallback
Callback invoked when an expressed Interest is satisfied by a Data packet.
Definition face.hpp:50
std::function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregistering a prefix succeeds.
Definition face.hpp:80
std::function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback invoked when registerPrefix or setInterestFilter command fails.
Definition face.hpp:75
std::function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when an incoming Interest matches the specified InterestFilter.
Definition face.hpp:65