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 "core/asserts.hpp"
28 #include "core/global-io.hpp"
29 #include "core/logger.hpp"
30 #include "face/channel.hpp"
31 
32 namespace nfd {
33 
35 
36 NFD_LOG_INIT("FaceTable");
37 
39  : m_lastFaceId(face::FACEID_RESERVED_MAX)
40 {
41 }
42 
43 Face*
45 {
46  auto i = m_faces.find(id);
47  return i == m_faces.end() ? nullptr : i->second.get();
48 }
49 
50 size_t
52 {
53  return m_faces.size();
54 }
55 
56 void
57 FaceTable::add(shared_ptr<Face> face)
58 {
59  if (face->getId() != face::INVALID_FACEID && m_faces.count(face->getId()) > 0) {
60  NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table");
61  return;
62  }
63 
64  FaceId faceId = ++m_lastFaceId;
65  BOOST_ASSERT(faceId > face::FACEID_RESERVED_MAX);
66  this->addImpl(std::move(face), faceId);
67 }
68 
69 void
70 FaceTable::addReserved(shared_ptr<Face> face, FaceId faceId)
71 {
72  BOOST_ASSERT(face->getId() == face::INVALID_FACEID);
73  BOOST_ASSERT(faceId <= face::FACEID_RESERVED_MAX);
74  this->addImpl(std::move(face), faceId);
75 }
76 
77 void
78 FaceTable::addImpl(shared_ptr<Face> face, FaceId faceId)
79 {
80  face->setId(faceId);
81  auto ret = m_faces.emplace(faceId, face);
82  BOOST_VERIFY(ret.second);
83 
84  NFD_LOG_INFO("Added face id=" << faceId <<
85  " remote=" << face->getRemoteUri() <<
86  " local=" << face->getLocalUri());
87 
88  connectFaceClosedSignal(*face, bind(&FaceTable::remove, this, faceId));
89 
90  this->afterAdd(*face);
91 }
92 
93 void
94 FaceTable::remove(FaceId faceId)
95 {
96  auto i = m_faces.find(faceId);
97  BOOST_ASSERT(i != m_faces.end());
98  shared_ptr<Face> face = i->second;
99 
100  this->beforeRemove(*face);
101 
102  m_faces.erase(i);
103  face->setId(face::INVALID_FACEID);
104 
105  NFD_LOG_INFO("Removed face id=" << faceId <<
106  " remote=" << face->getRemoteUri() <<
107  " local=" << face->getLocalUri());
108 
109  // defer Face deallocation, so that Transport isn't deallocated during afterStateChange signal
110  getGlobalIoService().post([face] {});
111 }
112 
114 FaceTable::getForwardRange() const
115 {
116  return m_faces | boost::adaptors::map_values | boost::adaptors::indirected;
117 }
118 
121 {
122  return this->getForwardRange().begin();
123 }
124 
127 {
128  return this->getForwardRange().end();
129 }
130 
131 } // namespace nfd
size_t size() const
Definition: face-table.cpp:51
Face * get(FaceId id) const
get face by FaceId
Definition: face-table.cpp:44
void addReserved(shared_ptr< Face > face, FaceId faceId)
add a special face with a reserved FaceId
Definition: face-table.cpp:70
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
Definition: channel.cpp:40
boost::range_iterator< ForwardRange >::type const_iterator
ForwardIterator for Face&.
Definition: face-table.hpp:73
const_iterator begin() const
Definition: face-table.cpp:120
boost::indirected_range< const boost::select_second_const_range< FaceMap >> ForwardRange
Definition: face-table.hpp:69
#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:57
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
signal::Signal< FaceTable, Face & > beforeRemove
fires before a face is removed
Definition: face-table.hpp:90
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:84
#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:126
boost::asio::io_service & getGlobalIoService()
Definition: global-io.cpp:41