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-2018, 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 "core/global-io.hpp"
28 #include "core/logger.hpp"
29 #include "face/channel.hpp"
30 
31 #include <ndn-cxx/util/concepts.hpp>
32 
33 namespace nfd {
34 
36 
37 NFD_LOG_INIT("FaceTable");
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, bind(&FaceTable::remove, this, 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
signal::Signal< FaceTable, Face & > afterAdd
fires after a face is added
Definition: face-table.hpp:84
size_t size() const
Definition: face-table.cpp:52
boost::indirected_range< const boost::select_second_const_range< FaceMap > > ForwardRange
Definition: face-table.hpp:69
generalization of a network interface
Definition: face.hpp:67
void add(shared_ptr< Face > face)
add a face
Definition: face-table.cpp:58
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
signal::Signal< FaceTable, Face & > beforeRemove
fires before a face is removed
Definition: face-table.hpp:90
const_iterator begin() const
Definition: face-table.cpp:121
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
Definition: channel.cpp:40
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:45
boost::range_iterator< ForwardRange >::type const_iterator
ForwardIterator for Face&.
Definition: face-table.hpp:73
const_iterator end() const
Definition: face-table.cpp:127
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
void post(const std::function< void()> &callback)
Definition: global-io.cpp:34
const FaceId FACEID_RESERVED_MAX
upper bound of reserved FaceIds
Definition: face.hpp:50
#define NFD_LOG_WARN(expression)
Definition: logger.hpp:58
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.hpp:39
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
const FaceId INVALID_FACEID
indicates an invalid FaceId
Definition: face.hpp:42