NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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; -*- */
26 #include "face-table.hpp"
27 #include "forwarder.hpp"
28 #include "core/global-io.hpp"
29 #include "core/logger.hpp"
30 #include "face/channel.hpp"
31 
32 namespace nfd {
33 
34 NFD_LOG_INIT("FaceTable");
35 
37  : m_lastFaceId(face::FACEID_RESERVED_MAX)
38 {
39 }
40 
41 Face*
43 {
44  auto i = m_faces.find(id);
45  if (i == m_faces.end()) {
46  return nullptr;
47  }
48  return 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(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(m_faces.count(face->getId()) == 0);
75  BOOST_ASSERT(faceId <= face::FACEID_RESERVED_MAX);
76  this->addImpl(face, faceId);
77 }
78 
79 void
80 FaceTable::addImpl(shared_ptr<Face> face, FaceId faceId)
81 {
82  face->setId(faceId);
83  m_faces[faceId] = face;
84  NFD_LOG_INFO("Added face id=" << faceId << " remote=" << face->getRemoteUri()
85  << " local=" << face->getLocalUri());
86 
87  connectFaceClosedSignal(*face, bind(&FaceTable::remove, this, faceId));
88 
89  this->afterAdd(*face);
90 }
91 
92 void
93 FaceTable::remove(FaceId faceId)
94 {
95  auto i = m_faces.find(faceId);
96  BOOST_ASSERT(i != m_faces.end());
97  shared_ptr<Face> face = i->second;
98 
99  this->beforeRemove(*face);
100 
101  m_faces.erase(i);
102  face->setId(face::INVALID_FACEID);
103 
104  NFD_LOG_INFO("Removed face id=" << faceId <<
105  " remote=" << face->getRemoteUri() <<
106  " local=" << face->getLocalUri());
107 
108  // defer Face deallocation, so that Transport isn't deallocated during afterStateChange signal
109  getGlobalIoService().post([face] {});
110 }
111 
113 FaceTable::getForwardRange() const
114 {
115  return m_faces | boost::adaptors::map_values | boost::adaptors::indirected;
116 }
117 
120 {
121  return this->getForwardRange().begin();
122 }
123 
126 {
127  return this->getForwardRange().end();
128 }
129 
130 } // namespace nfd
signal::Signal< FaceTable, Face & > afterAdd
fires after a face is added
Definition: face-table.hpp:85
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
boost::range_iterator< ForwardRange >::type const_iterator
ForwardIterator for Face&.
Definition: face-table.hpp:74
generalization of a network interface
Definition: face.hpp:67
void add(shared_ptr< Face > face)
add a face
Definition: face-table.cpp:58
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
Definition: channel.cpp:41
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
signal::Signal< FaceTable, Face & > beforeRemove
fires before a face is removed
Definition: face-table.hpp:91
const_iterator begin() const
Definition: face-table.cpp:119
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:42
const_iterator end() const
Definition: face-table.cpp:125
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
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