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-2022, 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 <ndn-cxx/util/concepts.hpp>
32 
33 namespace nfd {
34 
36 
38 
39 Face*
40 FaceTable::get(FaceId id) const noexcept
41 {
42  auto i = m_faces.find(id);
43  return i == m_faces.end() ? nullptr : i->second.get();
44 }
45 
46 size_t
47 FaceTable::size() const noexcept
48 {
49  return m_faces.size();
50 }
51 
52 void
53 FaceTable::add(shared_ptr<Face> face)
54 {
55  if (face->getId() != face::INVALID_FACEID && m_faces.count(face->getId()) > 0) {
56  NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table");
57  return;
58  }
59 
60  FaceId faceId = ++m_lastFaceId;
61  BOOST_ASSERT(faceId > face::FACEID_RESERVED_MAX);
62  this->addImpl(std::move(face), faceId);
63 }
64 
65 void
66 FaceTable::addReserved(shared_ptr<Face> face, FaceId faceId)
67 {
68  BOOST_ASSERT(face->getId() == face::INVALID_FACEID);
69  BOOST_ASSERT(faceId <= face::FACEID_RESERVED_MAX);
70  this->addImpl(std::move(face), faceId);
71 }
72 
73 void
74 FaceTable::addImpl(shared_ptr<Face> facePtr, FaceId faceId)
75 {
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;
80 
81  NFD_LOG_INFO("Added face id=" << faceId <<
82  " remote=" << face.getRemoteUri() <<
83  " local=" << face.getLocalUri());
84 
85  connectFaceClosedSignal(face, [this, faceId] { remove(faceId); });
86 
87  this->afterAdd(face);
88 }
89 
90 void
91 FaceTable::remove(FaceId faceId)
92 {
93  auto i = m_faces.find(faceId);
94  BOOST_ASSERT(i != m_faces.end());
95  shared_ptr<Face> face = i->second;
96 
97  this->beforeRemove(*face);
98 
99  m_faces.erase(i);
100  face->setId(face::INVALID_FACEID);
101 
102  NFD_LOG_INFO("Removed face id=" << faceId <<
103  " remote=" << face->getRemoteUri() <<
104  " local=" << face->getLocalUri());
105 
106  // defer Face deallocation, so that Transport isn't deallocated during afterStateChange signal
107  getGlobalIoService().post([face] {});
108 }
109 
111 FaceTable::getForwardRange() const
112 {
113  return m_faces | boost::adaptors::map_values | boost::adaptors::indirected;
114 }
115 
118 {
119  return this->getForwardRange().begin();
120 }
121 
124 {
125  return this->getForwardRange().end();
126 }
127 
128 } // namespace nfd
Container of all faces.
Definition: face-table.hpp:40
const_iterator end() const
Definition: face-table.cpp:123
size_t size() const noexcept
Return the total number of faces.
Definition: face-table.cpp:47
signal::Signal< FaceTable, Face > beforeRemove
Fires immediately before a face is removed.
Definition: face-table.hpp:87
signal::Signal< FaceTable, Face > afterAdd
Fires immediately after a face is added.
Definition: face-table.hpp:81
boost::indirected_range< const boost::select_second_const_range< FaceMap > > ForwardRange
Definition: face-table.hpp:69
Face * get(FaceId id) const noexcept
Get face by FaceId.
Definition: face-table.cpp:40
const_iterator begin() const
Definition: face-table.cpp:117
void add(shared_ptr< Face > face)
Add a face.
Definition: face-table.cpp:53
void addReserved(shared_ptr< Face > face, FaceId faceId)
Add a special face with a reserved FaceId.
Definition: face-table.cpp:66
boost::range_iterator< ForwardRange >::type const_iterator
Definition: face-table.hpp:70
Generalization of a network interface.
Definition: face.hpp:56
#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.
Definition: face-common.hpp:50
constexpr FaceId FACEID_RESERVED_MAX
Upper bound of reserved FaceIds.
Definition: face-common.hpp:58
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
Definition: channel.cpp:46
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:47
Definition: common.hpp:77
NDN_CXX_ASSERT_FORWARD_ITERATOR(FaceTable::const_iterator)
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:36