blob: 9a2fe4ce6c9570d865555df78c61c35120d9cbcd [file] [log] [blame]
Junxiao Shia4f2be82014-03-02 22:56:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento981db802018-04-10 18:05:04 -04002/*
Davide Pesavento8f0b8b62023-08-29 21:04:08 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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
26#include "face-table.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/global.hpp"
28#include "common/logger.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070029#include "face/channel.hpp"
Junxiao Shia4f2be82014-03-02 22:56:41 -070030
Davide Pesavento5d642632023-10-03 00:36:08 -040031#include <boost/asio/defer.hpp>
32
Junxiao Shia4f2be82014-03-02 22:56:41 -070033namespace nfd {
34
Davide Pesaventoa3148082018-04-12 18:21:54 -040035NFD_LOG_INIT(FaceTable);
Junxiao Shia4f2be82014-03-02 22:56:41 -070036
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000037Face*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040038FaceTable::get(FaceId id) const noexcept
Junxiao Shiafbd74d2014-11-29 21:18:17 -070039{
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000040 auto i = m_faces.find(id);
Davide Pesavento6fd1e7e2017-02-17 18:45:27 +000041 return i == m_faces.end() ? nullptr : i->second.get();
Junxiao Shiafbd74d2014-11-29 21:18:17 -070042}
43
44size_t
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040045FaceTable::size() const noexcept
Junxiao Shiafbd74d2014-11-29 21:18:17 -070046{
47 return m_faces.size();
48}
49
Junxiao Shia4f2be82014-03-02 22:56:41 -070050void
51FaceTable::add(shared_ptr<Face> face)
52{
Junxiao Shicde37ad2015-12-24 01:02:05 -070053 if (face->getId() != face::INVALID_FACEID && m_faces.count(face->getId()) > 0) {
Junxiao Shi7b984c62014-07-17 22:18:34 -070054 NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table");
55 return;
56 }
Alexander Afanasyevbc521a52014-03-26 23:31:55 -070057
Junxiao Shia4f2be82014-03-02 22:56:41 -070058 FaceId faceId = ++m_lastFaceId;
Junxiao Shicde37ad2015-12-24 01:02:05 -070059 BOOST_ASSERT(faceId > face::FACEID_RESERVED_MAX);
Davide Pesavento6fd1e7e2017-02-17 18:45:27 +000060 this->addImpl(std::move(face), faceId);
Junxiao Shi7b984c62014-07-17 22:18:34 -070061}
62
63void
64FaceTable::addReserved(shared_ptr<Face> face, FaceId faceId)
65{
Junxiao Shicde37ad2015-12-24 01:02:05 -070066 BOOST_ASSERT(face->getId() == face::INVALID_FACEID);
Junxiao Shicde37ad2015-12-24 01:02:05 -070067 BOOST_ASSERT(faceId <= face::FACEID_RESERVED_MAX);
Davide Pesavento6fd1e7e2017-02-17 18:45:27 +000068 this->addImpl(std::move(face), faceId);
Junxiao Shi7b984c62014-07-17 22:18:34 -070069}
70
71void
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040072FaceTable::addImpl(shared_ptr<Face> facePtr, FaceId faceId)
Junxiao Shi7b984c62014-07-17 22:18:34 -070073{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040074 facePtr->setId(faceId);
75 auto [it, isNew] = m_faces.try_emplace(faceId, std::move(facePtr));
76 BOOST_VERIFY(isNew);
77 auto& face = *it->second;
Davide Pesavento6fd1e7e2017-02-17 18:45:27 +000078
79 NFD_LOG_INFO("Added face id=" << faceId <<
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040080 " remote=" << face.getRemoteUri() <<
81 " local=" << face.getLocalUri());
Junxiao Shia4f2be82014-03-02 22:56:41 -070082
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040083 connectFaceClosedSignal(face, [this, faceId] { remove(faceId); });
Junxiao Shibd392bf2014-03-17 15:54:11 -070084
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040085 this->afterAdd(face);
Junxiao Shia4f2be82014-03-02 22:56:41 -070086}
87
88void
Junxiao Shiae04d342016-07-19 13:20:22 +000089FaceTable::remove(FaceId faceId)
Junxiao Shia4f2be82014-03-02 22:56:41 -070090{
Junxiao Shiae04d342016-07-19 13:20:22 +000091 auto i = m_faces.find(faceId);
92 BOOST_ASSERT(i != m_faces.end());
93 shared_ptr<Face> face = i->second;
Junxiao Shibd392bf2014-03-17 15:54:11 -070094
Junxiao Shiae04d342016-07-19 13:20:22 +000095 this->beforeRemove(*face);
96
Junxiao Shiae04d342016-07-19 13:20:22 +000097 m_faces.erase(i);
Junxiao Shicde37ad2015-12-24 01:02:05 -070098 face->setId(face::INVALID_FACEID);
Davide Pesavento66ff0982015-01-29 22:39:00 +010099
100 NFD_LOG_INFO("Removed face id=" << faceId <<
101 " remote=" << face->getRemoteUri() <<
Junxiao Shicde37ad2015-12-24 01:02:05 -0700102 " local=" << face->getLocalUri());
Junxiao Shia4f2be82014-03-02 22:56:41 -0700103
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700104 // defer Face deallocation, so that Transport isn't deallocated during afterStateChange signal
Davide Pesavento5d642632023-10-03 00:36:08 -0400105 boost::asio::defer(getGlobalIoService(), [face] {});
Junxiao Shia4f2be82014-03-02 22:56:41 -0700106}
107
Junxiao Shiafbd74d2014-11-29 21:18:17 -0700108FaceTable::ForwardRange
109FaceTable::getForwardRange() const
110{
Junxiao Shib84e6742016-07-19 13:16:22 +0000111 return m_faces | boost::adaptors::map_values | boost::adaptors::indirected;
Junxiao Shiafbd74d2014-11-29 21:18:17 -0700112}
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700113
Junxiao Shiafbd74d2014-11-29 21:18:17 -0700114FaceTable::const_iterator
115FaceTable::begin() const
116{
117 return this->getForwardRange().begin();
118}
119
120FaceTable::const_iterator
121FaceTable::end() const
122{
123 return this->getForwardRange().end();
124}
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700125
Junxiao Shia4f2be82014-03-02 22:56:41 -0700126} // namespace nfd