Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 4 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FW_FACE_TABLE_HPP |
| 27 | #define NFD_DAEMON_FW_FACE_TABLE_HPP |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 28 | |
| 29 | #include "face/face.hpp" |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 30 | |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 31 | #include <boost/range/adaptor/indirected.hpp> |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame] | 32 | #include <boost/range/adaptor/map.hpp> |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 33 | |
Junxiao Shi | 1e06417 | 2014-12-14 19:37:46 -0700 | [diff] [blame] | 34 | namespace nfd { |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 36 | /** |
| 37 | * \brief Container of all faces. |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 38 | */ |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 39 | class FaceTable : noncopyable |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 40 | { |
| 41 | public: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 42 | /** \brief Add a face. |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 43 | * |
| 44 | * FaceTable obtains shared ownership of the face. |
| 45 | * The channel or protocol factory that creates the face may retain ownership. |
| 46 | */ |
Junxiao Shi | dcffdaa | 2016-07-26 02:23:56 +0000 | [diff] [blame] | 47 | void |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 48 | add(shared_ptr<Face> face); |
| 49 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 50 | /** \brief Add a special face with a reserved FaceId. |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 51 | */ |
Junxiao Shi | dcffdaa | 2016-07-26 02:23:56 +0000 | [diff] [blame] | 52 | void |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 53 | addReserved(shared_ptr<Face> face, FaceId faceId); |
| 54 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 55 | /** \brief Get face by FaceId. |
| 56 | * \return A pointer to the face if found, nullptr otherwise; |
| 57 | * `face->shared_from_this()` can be used if a `shared_ptr` is desired. |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 58 | */ |
Junxiao Shi | dcffdaa | 2016-07-26 02:23:56 +0000 | [diff] [blame] | 59 | Face* |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 60 | get(FaceId id) const noexcept; |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 61 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 62 | /** \brief Return the total number of faces. |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 63 | */ |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 64 | size_t |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 65 | size() const noexcept; |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 66 | |
| 67 | public: // enumeration |
Davide Pesavento | 6fd1e7e | 2017-02-17 18:45:27 +0000 | [diff] [blame] | 68 | using FaceMap = std::map<FaceId, shared_ptr<Face>>; |
| 69 | using ForwardRange = boost::indirected_range<const boost::select_second_const_range<FaceMap>>; |
Davide Pesavento | 6fd1e7e | 2017-02-17 18:45:27 +0000 | [diff] [blame] | 70 | using const_iterator = boost::range_iterator<ForwardRange>::type; |
Alexander Afanasyev | 7b7dfdd | 2014-03-21 13:57:54 -0700 | [diff] [blame] | 71 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 72 | const_iterator |
| 73 | begin() const; |
| 74 | |
| 75 | const_iterator |
| 76 | end() const; |
| 77 | |
Junxiao Shi | 1e06417 | 2014-12-14 19:37:46 -0700 | [diff] [blame] | 78 | public: // signals |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 79 | /** \brief Fires immediately after a face is added. |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 80 | */ |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 81 | signal::Signal<FaceTable, Face> afterAdd; |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 82 | |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 83 | /** \brief Fires immediately before a face is removed. |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 84 | * |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 85 | * When this signal is emitted, the face is still in FaceTable and has a valid FaceId. |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 86 | */ |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 87 | signal::Signal<FaceTable, Face> beforeRemove; |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 88 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 89 | private: |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 90 | void |
| 91 | addImpl(shared_ptr<Face> face, FaceId faceId); |
| 92 | |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 93 | void |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 94 | remove(FaceId faceId); |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 95 | |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame] | 96 | ForwardRange |
| 97 | getForwardRange() const; |
| 98 | |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 99 | private: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 100 | FaceId m_lastFaceId = face::FACEID_RESERVED_MAX; |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 101 | FaceMap m_faces; |
| 102 | }; |
| 103 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 104 | } // namespace nfd |
| 105 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 106 | #endif // NFD_DAEMON_FW_FACE_TABLE_HPP |