blob: dd61ca8dbff314bfdf0e1a9db862207c92cd58d1 [file] [log] [blame]
Junxiao Shia4f2be82014-03-02 22:56:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa4abfb02019-10-06 16:02:56 -04002/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Junxiao Shicde37ad2015-12-24 01:02:05 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi7b984c62014-07-17 22:18:34 -070024 */
Junxiao Shia4f2be82014-03-02 22:56:41 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FW_FACE_TABLE_HPP
27#define NFD_DAEMON_FW_FACE_TABLE_HPP
Junxiao Shia4f2be82014-03-02 22:56:41 -070028
29#include "face/face.hpp"
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040030
Junxiao Shib84e6742016-07-19 13:16:22 +000031#include <boost/range/adaptor/indirected.hpp>
Junxiao Shiafbd74d2014-11-29 21:18:17 -070032#include <boost/range/adaptor/map.hpp>
Junxiao Shia4f2be82014-03-02 22:56:41 -070033
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050034#include <map>
35
Junxiao Shi1e064172014-12-14 19:37:46 -070036namespace nfd {
Junxiao Shia4f2be82014-03-02 22:56:41 -070037
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040038/**
39 * \brief Container of all faces.
Junxiao Shia4f2be82014-03-02 22:56:41 -070040 */
Junxiao Shi7b984c62014-07-17 22:18:34 -070041class FaceTable : noncopyable
Junxiao Shia4f2be82014-03-02 22:56:41 -070042{
43public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040044 /** \brief Add a face.
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000045 *
46 * FaceTable obtains shared ownership of the face.
47 * The channel or protocol factory that creates the face may retain ownership.
48 */
Junxiao Shidcffdaa2016-07-26 02:23:56 +000049 void
Junxiao Shia4f2be82014-03-02 22:56:41 -070050 add(shared_ptr<Face> face);
51
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040052 /** \brief Add a special face with a reserved FaceId.
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000053 */
Junxiao Shidcffdaa2016-07-26 02:23:56 +000054 void
Junxiao Shi7b984c62014-07-17 22:18:34 -070055 addReserved(shared_ptr<Face> face, FaceId faceId);
56
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040057 /** \brief Get face by FaceId.
58 * \return A pointer to the face if found, nullptr otherwise;
59 * `face->shared_from_this()` can be used if a `shared_ptr` is desired.
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000060 */
Junxiao Shidcffdaa2016-07-26 02:23:56 +000061 Face*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040062 get(FaceId id) const noexcept;
Junxiao Shia4f2be82014-03-02 22:56:41 -070063
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040064 /** \brief Return the total number of faces.
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000065 */
Junxiao Shia4f2be82014-03-02 22:56:41 -070066 size_t
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040067 size() const noexcept;
Junxiao Shia4f2be82014-03-02 22:56:41 -070068
69public: // enumeration
Davide Pesavento6fd1e7e2017-02-17 18:45:27 +000070 using FaceMap = std::map<FaceId, shared_ptr<Face>>;
71 using ForwardRange = boost::indirected_range<const boost::select_second_const_range<FaceMap>>;
Davide Pesavento6fd1e7e2017-02-17 18:45:27 +000072 using const_iterator = boost::range_iterator<ForwardRange>::type;
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -070073
Junxiao Shia4f2be82014-03-02 22:56:41 -070074 const_iterator
75 begin() const;
76
77 const_iterator
78 end() const;
79
Junxiao Shi1e064172014-12-14 19:37:46 -070080public: // signals
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040081 /** \brief Fires immediately after a face is added.
Junxiao Shibd392bf2014-03-17 15:54:11 -070082 */
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040083 signal::Signal<FaceTable, Face> afterAdd;
Junxiao Shibd392bf2014-03-17 15:54:11 -070084
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040085 /** \brief Fires immediately before a face is removed.
Junxiao Shibd392bf2014-03-17 15:54:11 -070086 *
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040087 * When this signal is emitted, the face is still in FaceTable and has a valid FaceId.
Junxiao Shibd392bf2014-03-17 15:54:11 -070088 */
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040089 signal::Signal<FaceTable, Face> beforeRemove;
Junxiao Shibd392bf2014-03-17 15:54:11 -070090
Junxiao Shia4f2be82014-03-02 22:56:41 -070091private:
Junxiao Shi7b984c62014-07-17 22:18:34 -070092 void
93 addImpl(shared_ptr<Face> face, FaceId faceId);
94
Junxiao Shic542b2b2014-03-16 21:45:52 -070095 void
Junxiao Shiae04d342016-07-19 13:20:22 +000096 remove(FaceId faceId);
Junxiao Shic542b2b2014-03-16 21:45:52 -070097
Junxiao Shiafbd74d2014-11-29 21:18:17 -070098 ForwardRange
99 getForwardRange() const;
100
Junxiao Shic542b2b2014-03-16 21:45:52 -0700101private:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400102 FaceId m_lastFaceId = face::FACEID_RESERVED_MAX;
Junxiao Shia4f2be82014-03-02 22:56:41 -0700103 FaceMap m_faces;
104};
105
Junxiao Shia4f2be82014-03-02 22:56:41 -0700106} // namespace nfd
107
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700108#endif // NFD_DAEMON_FW_FACE_TABLE_HPP