NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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_forwarder(forwarder)
38  , m_lastFaceId(face::FACEID_RESERVED_MAX)
39 {
40 }
41 
43 {
44 
45 }
46 
47 shared_ptr<Face>
49 {
50  std::map<FaceId, shared_ptr<Face> >::const_iterator i = m_faces.find(id);
51  return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second);
52 }
53 
54 size_t
56 {
57  return m_faces.size();
58 }
59 
60 void
61 FaceTable::add(shared_ptr<Face> face)
62 {
63  if (face->getId() != face::INVALID_FACEID && m_faces.count(face->getId()) > 0) {
64  NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table");
65  return;
66  }
67 
68  FaceId faceId = ++m_lastFaceId;
69  BOOST_ASSERT(faceId > face::FACEID_RESERVED_MAX);
70  this->addImpl(face, faceId);
71 }
72 
73 void
74 FaceTable::addReserved(shared_ptr<Face> face, FaceId faceId)
75 {
76  BOOST_ASSERT(face->getId() == face::INVALID_FACEID);
77  BOOST_ASSERT(m_faces.count(face->getId()) == 0);
78  BOOST_ASSERT(faceId <= face::FACEID_RESERVED_MAX);
79  this->addImpl(face, faceId);
80 }
81 
82 void
83 FaceTable::addImpl(shared_ptr<Face> face, FaceId faceId)
84 {
85  face->setId(faceId);
86  m_faces[faceId] = face;
87  NFD_LOG_INFO("Added face id=" << faceId << " remote=" << face->getRemoteUri()
88  << " local=" << face->getLocalUri());
89 
90  face->afterReceiveInterest.connect(bind(&Forwarder::startProcessInterest, &m_forwarder, ref(*face), _1));
91  face->afterReceiveData.connect(bind(&Forwarder::startProcessData, &m_forwarder, ref(*face), _1));
92  face->afterReceiveNack.connect(bind(&Forwarder::startProcessNack, &m_forwarder, ref(*face), _1));
93 
94  std::weak_ptr<Face> weakFace = face;
95  connectFaceClosedSignal(*face, [this, weakFace] {
96  shared_ptr<Face> face = weakFace.lock();
97  if (face != nullptr) {
98  this->remove(face);
99  }
100  });
101 
102  this->afterAdd(face);
103 }
104 
105 void
106 FaceTable::remove(shared_ptr<Face> face)
107 {
108  this->beforeRemove(face);
109 
110  FaceId faceId = face->getId();
111  m_faces.erase(faceId);
112  face->setId(face::INVALID_FACEID);
113 
114  NFD_LOG_INFO("Removed face id=" << faceId <<
115  " remote=" << face->getRemoteUri() <<
116  " local=" << face->getLocalUri());
117 
118  m_forwarder.getFib().removeNextHopFromAllEntries(face);
119 
120  // defer Face deallocation, so that Transport isn't deallocated during afterStateChange signal
121  getGlobalIoService().post([face] {});
122 }
123 
125 FaceTable::getForwardRange() const
126 {
127  return m_faces | boost::adaptors::map_values;
128 }
129 
132 {
133  return this->getForwardRange().begin();
134 }
135 
138 {
139  return this->getForwardRange().end();
140 }
141 
142 } // namespace nfd
signal::Signal< FaceTable, shared_ptr< Face > > afterAdd
fires after a Face is added
Definition: face-table.hpp:78
size_t size() const
Definition: face-table.cpp:55
boost::range_iterator< ForwardRange >::type const_iterator
ForwardIterator for shared_ptr<Face>
Definition: face-table.hpp:67
void add(shared_ptr< Face > face)
Definition: face-table.cpp:61
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
Definition: channel.cpp:41
void startProcessInterest(Face &face, const Interest &interest)
start incoming Interest processing
Definition: forwarder.cpp:61
main class of NFD
Definition: forwarder.hpp:55
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
const_iterator begin() const
Definition: face-table.cpp:131
signal::Signal< FaceTable, shared_ptr< Face > > beforeRemove
fires before a Face is removed
Definition: face-table.hpp:84
#define NFD_LOG_WARN(expression)
Definition: logger.hpp:58
void removeNextHopFromAllEntries(shared_ptr< Face > face)
removes the NextHop record for face in all entrites
Definition: fib.cpp:157
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
const_iterator end() const
Definition: face-table.cpp:137
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
void startProcessData(Face &face, const Data &data)
start incoming Data processing
Definition: forwarder.cpp:80
const FaceId FACEID_RESERVED_MAX
upper bound of reserved FaceIds
Definition: face.hpp:50
Fib & getFib()
Definition: forwarder.hpp:301
boost::select_second_const_range< FaceMap > ForwardRange
Definition: face-table.hpp:63
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
FaceTable(Forwarder &forwarder)
Definition: face-table.cpp:36
void addReserved(shared_ptr< Face > face, FaceId faceId)
add a special Face with a reserved FaceId
Definition: face-table.cpp:74
uint64_t FaceId
identifies a face
Definition: face.hpp:39
shared_ptr< Face > get(FaceId id) const
Definition: face-table.cpp:48
const FaceId INVALID_FACEID
indicates an invalid FaceId
Definition: face.hpp:42
void startProcessNack(Face &face, const lp::Nack &nack)
start incoming Nack processing
Definition: forwarder.cpp:89