NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019, 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 
32 
33 namespace nfd {
34 
36 
38 
40  : m_lastFaceId(face::FACEID_RESERVED_MAX)
41 {
42 }
43 
44 Face*
46 {
47  auto i = m_faces.find(id);
48  return i == m_faces.end() ? nullptr : i->second.get();
49 }
50 
51 size_t
53 {
54  return m_faces.size();
55 }
56 
57 void
58 FaceTable::add(shared_ptr<Face> face)
59 {
60  if (face->getId() != face::INVALID_FACEID && m_faces.count(face->getId()) > 0) {
61  NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table");
62  return;
63  }
64 
65  FaceId faceId = ++m_lastFaceId;
66  BOOST_ASSERT(faceId > face::FACEID_RESERVED_MAX);
67  this->addImpl(std::move(face), faceId);
68 }
69 
70 void
71 FaceTable::addReserved(shared_ptr<Face> face, FaceId faceId)
72 {
73  BOOST_ASSERT(face->getId() == face::INVALID_FACEID);
74  BOOST_ASSERT(faceId <= face::FACEID_RESERVED_MAX);
75  this->addImpl(std::move(face), faceId);
76 }
77 
78 void
79 FaceTable::addImpl(shared_ptr<Face> face, FaceId faceId)
80 {
81  face->setId(faceId);
82  auto ret = m_faces.emplace(faceId, face);
83  BOOST_VERIFY(ret.second);
84 
85  NFD_LOG_INFO("Added face id=" << faceId <<
86  " remote=" << face->getRemoteUri() <<
87  " local=" << face->getLocalUri());
88 
89  connectFaceClosedSignal(*face, [=] { remove(faceId); });
90 
91  this->afterAdd(*face);
92 }
93 
94 void
95 FaceTable::remove(FaceId faceId)
96 {
97  auto i = m_faces.find(faceId);
98  BOOST_ASSERT(i != m_faces.end());
99  shared_ptr<Face> face = i->second;
100 
101  this->beforeRemove(*face);
102 
103  m_faces.erase(i);
104  face->setId(face::INVALID_FACEID);
105 
106  NFD_LOG_INFO("Removed face id=" << faceId <<
107  " remote=" << face->getRemoteUri() <<
108  " local=" << face->getLocalUri());
109 
110  // defer Face deallocation, so that Transport isn't deallocated during afterStateChange signal
111  getGlobalIoService().post([face] {});
112 }
113 
115 FaceTable::getForwardRange() const
116 {
117  return m_faces | boost::adaptors::map_values | boost::adaptors::indirected;
118 }
119 
122 {
123  return this->getForwardRange().begin();
124 }
125 
128 {
129  return this->getForwardRange().end();
130 }
131 
132 } // namespace nfd
size_t size() const
Definition: face-table.cpp:52
boost::indirected_range< const boost::select_second_const_range< FaceMap > > ForwardRange
Definition: face-table.hpp:70
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
void add(shared_ptr< Face > face)
add a face
Definition: face-table.cpp:58
detail::SimulatorIo & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Definition: global.cpp:49
const_iterator begin() const
Definition: face-table.cpp:121
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:45
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
Definition: channel.cpp:47
signal::Signal< FaceTable, Face > beforeRemove
Fires immediately before a face is removed.
Definition: face-table.hpp:91
#define NFD_LOG_INFO
Definition: logger.hpp:39
ndn Face
Definition: face-impl.hpp:42
boost::range_iterator< ForwardRange >::type const_iterator
ForwardIterator for Face&.
Definition: face-table.hpp:74
container of all faces
Definition: face-table.hpp:38
const_iterator end() const
Definition: face-table.cpp:127
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
void post(const std::function< void()> &callback)
Definition: global.cpp:35
const FaceId FACEID_RESERVED_MAX
upper bound of reserved FaceIds
Definition: face-common.hpp:55
#define NFD_LOG_WARN
Definition: logger.hpp:40
void addReserved(shared_ptr< Face > face, FaceId faceId)
add a special face with a reserved FaceId
Definition: face-table.cpp:71
NDN_CXX_ASSERT_FORWARD_ITERATOR(FaceTable::const_iterator)
uint64_t FaceId
Identifies a face.
Definition: face-common.hpp:44
const FaceId INVALID_FACEID
indicates an invalid FaceId
Definition: face-common.hpp:47
signal::Signal< FaceTable, Face > afterAdd
Fires immediately after a face is added.
Definition: face-table.hpp:85