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/asserts.hpp"
29 #include "core/global-io.hpp"
30 #include "core/logger.hpp"
31 #include "face/channel.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  if (i == m_faces.end()) {
49  return nullptr;
50  }
51  return i->second.get();
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  connectFaceClosedSignal(*face, bind(&FaceTable::remove, this, faceId));
91 
92  this->afterAdd(*face);
93 }
94 
95 void
96 FaceTable::remove(FaceId faceId)
97 {
98  auto i = m_faces.find(faceId);
99  BOOST_ASSERT(i != m_faces.end());
100  shared_ptr<Face> face = i->second;
101 
102  this->beforeRemove(*face);
103 
104  m_faces.erase(i);
105  face->setId(face::INVALID_FACEID);
106 
107  NFD_LOG_INFO("Removed face id=" << faceId <<
108  " remote=" << face->getRemoteUri() <<
109  " local=" << face->getLocalUri());
110 
111  // defer Face deallocation, so that Transport isn't deallocated during afterStateChange signal
112  getGlobalIoService().post([face] {});
113 }
114 
116 FaceTable::getForwardRange() const
117 {
118  return m_faces | boost::adaptors::map_values | boost::adaptors::indirected;
119 }
120 
123 {
124  return this->getForwardRange().begin();
125 }
126 
129 {
130  return this->getForwardRange().end();
131 }
132 
133 } // namespace nfd
boost::range_iterator< ForwardRange >::type const_iterator
ForwardIterator for Face&.
Definition: face-table.hpp:74
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
Definition: channel.cpp:41
size_t size() const
Definition: face-table.cpp:55
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:45
void addReserved(shared_ptr< Face > face, FaceId faceId)
add a special face with a reserved FaceId
Definition: face-table.cpp:74
boost::indirected_range< const boost::select_second_const_range< FaceMap > > ForwardRange
Definition: face-table.hpp:70
const_iterator begin() const
Definition: face-table.cpp:122
#define NFD_LOG_WARN(expression)
Definition: logger.hpp:163
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:162
void add(shared_ptr< Face > face)
add a face
Definition: face-table.cpp:61
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
signal::Signal< FaceTable, Face & > beforeRemove
fires before a face is removed
Definition: face-table.hpp:91
const FaceId FACEID_RESERVED_MAX
upper bound of reserved FaceIds
Definition: face.hpp:50
signal::Signal< FaceTable, Face & > afterAdd
fires after a face is added
Definition: face-table.hpp:85
#define NFD_LOG_INIT(name)
Definition: logger.hpp:122
NFD_ASSERT_FORWARD_ITERATOR(FaceTable::const_iterator)
uint64_t FaceId
identifies a face
Definition: face.hpp:39
const FaceId INVALID_FACEID
indicates an invalid FaceId
Definition: face.hpp:42
const_iterator end() const
Definition: face-table.cpp:128
boost::asio::io_service & getGlobalIoService()
Definition: global-io.cpp:41