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 | 981db80 | 2018-04-10 18:05:04 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [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 | |
| 26 | #include "face-table.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/global.hpp" |
| 28 | #include "common/logger.hpp" |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 29 | #include "face/channel.hpp" |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 981db80 | 2018-04-10 18:05:04 -0400 | [diff] [blame] | 31 | #include <ndn-cxx/util/concepts.hpp> |
| 32 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 33 | namespace nfd { |
| 34 | |
Davide Pesavento | 981db80 | 2018-04-10 18:05:04 -0400 | [diff] [blame] | 35 | NDN_CXX_ASSERT_FORWARD_ITERATOR(FaceTable::const_iterator); |
Junxiao Shi | 3aba754 | 2016-12-24 02:42:52 +0000 | [diff] [blame] | 36 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 37 | NFD_LOG_INIT(FaceTable); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 38 | |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 39 | Face* |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 40 | FaceTable::get(FaceId id) const noexcept |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame] | 41 | { |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 42 | auto i = m_faces.find(id); |
Davide Pesavento | 6fd1e7e | 2017-02-17 18:45:27 +0000 | [diff] [blame] | 43 | return i == m_faces.end() ? nullptr : i->second.get(); |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | size_t |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 47 | FaceTable::size() const noexcept |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame] | 48 | { |
| 49 | return m_faces.size(); |
| 50 | } |
| 51 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 52 | void |
| 53 | FaceTable::add(shared_ptr<Face> face) |
| 54 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 55 | if (face->getId() != face::INVALID_FACEID && m_faces.count(face->getId()) > 0) { |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 56 | NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table"); |
| 57 | return; |
| 58 | } |
Alexander Afanasyev | bc521a5 | 2014-03-26 23:31:55 -0700 | [diff] [blame] | 59 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 60 | FaceId faceId = ++m_lastFaceId; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 61 | BOOST_ASSERT(faceId > face::FACEID_RESERVED_MAX); |
Davide Pesavento | 6fd1e7e | 2017-02-17 18:45:27 +0000 | [diff] [blame] | 62 | this->addImpl(std::move(face), faceId); |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void |
| 66 | FaceTable::addReserved(shared_ptr<Face> face, FaceId faceId) |
| 67 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 68 | BOOST_ASSERT(face->getId() == face::INVALID_FACEID); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 69 | BOOST_ASSERT(faceId <= face::FACEID_RESERVED_MAX); |
Davide Pesavento | 6fd1e7e | 2017-02-17 18:45:27 +0000 | [diff] [blame] | 70 | this->addImpl(std::move(face), faceId); |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 74 | FaceTable::addImpl(shared_ptr<Face> facePtr, FaceId faceId) |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 75 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 76 | facePtr->setId(faceId); |
| 77 | auto [it, isNew] = m_faces.try_emplace(faceId, std::move(facePtr)); |
| 78 | BOOST_VERIFY(isNew); |
| 79 | auto& face = *it->second; |
Davide Pesavento | 6fd1e7e | 2017-02-17 18:45:27 +0000 | [diff] [blame] | 80 | |
| 81 | NFD_LOG_INFO("Added face id=" << faceId << |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 82 | " remote=" << face.getRemoteUri() << |
| 83 | " local=" << face.getLocalUri()); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 84 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 85 | connectFaceClosedSignal(face, [this, faceId] { remove(faceId); }); |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 86 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 87 | this->afterAdd(face); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 91 | FaceTable::remove(FaceId faceId) |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 92 | { |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 93 | auto i = m_faces.find(faceId); |
| 94 | BOOST_ASSERT(i != m_faces.end()); |
| 95 | shared_ptr<Face> face = i->second; |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 96 | |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 97 | this->beforeRemove(*face); |
| 98 | |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 99 | m_faces.erase(i); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 100 | face->setId(face::INVALID_FACEID); |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 101 | |
| 102 | NFD_LOG_INFO("Removed face id=" << faceId << |
| 103 | " remote=" << face->getRemoteUri() << |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 104 | " local=" << face->getLocalUri()); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 105 | |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 106 | // defer Face deallocation, so that Transport isn't deallocated during afterStateChange signal |
| 107 | getGlobalIoService().post([face] {}); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame] | 110 | FaceTable::ForwardRange |
| 111 | FaceTable::getForwardRange() const |
| 112 | { |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 113 | return m_faces | boost::adaptors::map_values | boost::adaptors::indirected; |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame] | 114 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 115 | |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame] | 116 | FaceTable::const_iterator |
| 117 | FaceTable::begin() const |
| 118 | { |
| 119 | return this->getForwardRange().begin(); |
| 120 | } |
| 121 | |
| 122 | FaceTable::const_iterator |
| 123 | FaceTable::end() const |
| 124 | { |
| 125 | return this->getForwardRange().end(); |
| 126 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 127 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 128 | } // namespace nfd |