NFD: Named Data Networking Forwarding Daemon 24.07-28-gdcc0e6e0
Loading...
Searching...
No Matches
face-table.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2023, Regents of the University of California,
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.
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/>.
24 */
25
26#include "face-table.hpp"
27#include "common/global.hpp"
28#include "common/logger.hpp"
29#include "face/channel.hpp"
30
31#include <boost/asio/defer.hpp>
32
33namespace nfd {
34
35NFD_LOG_INIT(FaceTable);
36
37Face*
38FaceTable::get(FaceId id) const noexcept
39{
40 auto i = m_faces.find(id);
41 return i == m_faces.end() ? nullptr : i->second.get();
42}
43
44size_t
45FaceTable::size() const noexcept
46{
47 return m_faces.size();
48}
49
50void
51FaceTable::add(shared_ptr<Face> face)
52{
53 if (face->getId() != face::INVALID_FACEID && m_faces.count(face->getId()) > 0) {
54 NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table");
55 return;
56 }
57
58 FaceId faceId = ++m_lastFaceId;
59 BOOST_ASSERT(faceId > face::FACEID_RESERVED_MAX);
60 this->addImpl(std::move(face), faceId);
61}
62
63void
64FaceTable::addReserved(shared_ptr<Face> face, FaceId faceId)
65{
66 BOOST_ASSERT(face->getId() == face::INVALID_FACEID);
67 BOOST_ASSERT(faceId <= face::FACEID_RESERVED_MAX);
68 this->addImpl(std::move(face), faceId);
69}
70
71void
72FaceTable::addImpl(shared_ptr<Face> facePtr, FaceId faceId)
73{
74 facePtr->setId(faceId);
75 auto [it, isNew] = m_faces.try_emplace(faceId, std::move(facePtr));
76 BOOST_VERIFY(isNew);
77 auto& face = *it->second;
78
79 NFD_LOG_INFO("Added face id=" << faceId <<
80 " remote=" << face.getRemoteUri() <<
81 " local=" << face.getLocalUri());
82
83 connectFaceClosedSignal(face, [this, faceId] { remove(faceId); });
84
85 this->afterAdd(face);
86}
87
88void
89FaceTable::remove(FaceId faceId)
90{
91 auto i = m_faces.find(faceId);
92 BOOST_ASSERT(i != m_faces.end());
93 shared_ptr<Face> face = i->second;
94
95 this->beforeRemove(*face);
96
97 m_faces.erase(i);
98 face->setId(face::INVALID_FACEID);
99
100 NFD_LOG_INFO("Removed face id=" << faceId <<
101 " remote=" << face->getRemoteUri() <<
102 " local=" << face->getLocalUri());
103
104 // defer Face deallocation, so that Transport isn't deallocated during afterStateChange signal
105 boost::asio::defer(getGlobalIoService(), [face] {});
106}
107
109FaceTable::getForwardRange() const
110{
111 return m_faces | boost::adaptors::map_values | boost::adaptors::indirected;
112}
113
116{
117 return this->getForwardRange().begin();
118}
119
122{
123 return this->getForwardRange().end();
124}
125
126} // namespace nfd
const_iterator end() const
size_t size() const noexcept
Return the total number of faces.
signal::Signal< FaceTable, Face > beforeRemove
Fires immediately before a face is removed.
signal::Signal< FaceTable, Face > afterAdd
Fires immediately after a face is added.
boost::indirected_range< const boost::select_second_const_range< FaceMap > > ForwardRange
Face * get(FaceId id) const noexcept
Get face by FaceId.
const_iterator begin() const
void add(shared_ptr< Face > face)
Add a face.
void addReserved(shared_ptr< Face > face, FaceId faceId)
Add a special face with a reserved FaceId.
boost::range_iterator< ForwardRange >::type const_iterator
#define NFD_LOG_INFO
Definition logger.hpp:39
#define NFD_LOG_INIT(name)
Definition logger.hpp:31
#define NFD_LOG_WARN
Definition logger.hpp:40
constexpr FaceId INVALID_FACEID
Indicates an invalid FaceId.
constexpr FaceId FACEID_RESERVED_MAX
Upper bound of reserved FaceIds.
Definition common.hpp:71
boost::asio::io_context & getGlobalIoService()
Returns the global io_context instance for the calling thread.
Definition global.cpp:36