blob: 4cda948500577f05ecb2af903764450614af4dd5 [file] [log] [blame]
Junxiao Shia4f2be82014-03-02 22:56:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shia6de4292016-07-12 02:08:10 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shi7b984c62014-07-17 22:18:34 -070024 */
Junxiao Shia4f2be82014-03-02 22:56:41 -070025
26#include "face-table.hpp"
27#include "forwarder.hpp"
Junxiao Shi40cb61c2015-09-23 18:36:45 -070028#include "core/global-io.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060029#include "core/logger.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070030#include "face/channel.hpp"
Junxiao Shia4f2be82014-03-02 22:56:41 -070031
32namespace nfd {
33
34NFD_LOG_INIT("FaceTable");
35
36FaceTable::FaceTable(Forwarder& forwarder)
37 : m_forwarder(forwarder)
Junxiao Shicde37ad2015-12-24 01:02:05 -070038 , m_lastFaceId(face::FACEID_RESERVED_MAX)
Junxiao Shia4f2be82014-03-02 22:56:41 -070039{
40}
41
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070042FaceTable::~FaceTable()
43{
44
45}
46
Junxiao Shiafbd74d2014-11-29 21:18:17 -070047shared_ptr<Face>
48FaceTable::get(FaceId id) const
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
54size_t
55FaceTable::size() const
56{
57 return m_faces.size();
58}
59
Junxiao Shia4f2be82014-03-02 22:56:41 -070060void
61FaceTable::add(shared_ptr<Face> face)
62{
Junxiao Shicde37ad2015-12-24 01:02:05 -070063 if (face->getId() != face::INVALID_FACEID && m_faces.count(face->getId()) > 0) {
Junxiao Shi7b984c62014-07-17 22:18:34 -070064 NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table");
65 return;
66 }
Alexander Afanasyevbc521a52014-03-26 23:31:55 -070067
Junxiao Shia4f2be82014-03-02 22:56:41 -070068 FaceId faceId = ++m_lastFaceId;
Junxiao Shicde37ad2015-12-24 01:02:05 -070069 BOOST_ASSERT(faceId > face::FACEID_RESERVED_MAX);
Junxiao Shi7b984c62014-07-17 22:18:34 -070070 this->addImpl(face, faceId);
71}
72
73void
74FaceTable::addReserved(shared_ptr<Face> face, FaceId faceId)
75{
Junxiao Shicde37ad2015-12-24 01:02:05 -070076 BOOST_ASSERT(face->getId() == face::INVALID_FACEID);
Junxiao Shi7b984c62014-07-17 22:18:34 -070077 BOOST_ASSERT(m_faces.count(face->getId()) == 0);
Junxiao Shicde37ad2015-12-24 01:02:05 -070078 BOOST_ASSERT(faceId <= face::FACEID_RESERVED_MAX);
Junxiao Shi7b984c62014-07-17 22:18:34 -070079 this->addImpl(face, faceId);
80}
81
82void
83FaceTable::addImpl(shared_ptr<Face> face, FaceId faceId)
84{
Junxiao Shia4f2be82014-03-02 22:56:41 -070085 face->setId(faceId);
86 m_faces[faceId] = face;
Junxiao Shi7b984c62014-07-17 22:18:34 -070087 NFD_LOG_INFO("Added face id=" << faceId << " remote=" << face->getRemoteUri()
88 << " local=" << face->getLocalUri());
Junxiao Shia4f2be82014-03-02 22:56:41 -070089
Junxiao Shicde37ad2015-12-24 01:02:05 -070090 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 connectFaceClosedSignal(*face, bind(&FaceTable::remove, this, face));
Junxiao Shibd392bf2014-03-17 15:54:11 -070094
Junxiao Shicde37ad2015-12-24 01:02:05 -070095 this->afterAdd(face);
Junxiao Shia4f2be82014-03-02 22:56:41 -070096}
97
98void
Junxiao Shicde37ad2015-12-24 01:02:05 -070099FaceTable::remove(shared_ptr<Face> face)
Junxiao Shia4f2be82014-03-02 22:56:41 -0700100{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700101 this->beforeRemove(face);
Junxiao Shibd392bf2014-03-17 15:54:11 -0700102
Junxiao Shia4f2be82014-03-02 22:56:41 -0700103 FaceId faceId = face->getId();
104 m_faces.erase(faceId);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700105 face->setId(face::INVALID_FACEID);
Davide Pesavento66ff0982015-01-29 22:39:00 +0100106
107 NFD_LOG_INFO("Removed face id=" << faceId <<
108 " remote=" << face->getRemoteUri() <<
Junxiao Shicde37ad2015-12-24 01:02:05 -0700109 " local=" << face->getLocalUri());
Junxiao Shia4f2be82014-03-02 22:56:41 -0700110
Junxiao Shia6de4292016-07-12 02:08:10 +0000111 m_forwarder.getFib().removeNextHopFromAllEntries(*face);
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700112
113 // defer Face deallocation, so that Transport isn't deallocated during afterStateChange signal
114 getGlobalIoService().post([face] {});
Junxiao Shia4f2be82014-03-02 22:56:41 -0700115}
116
Junxiao Shiafbd74d2014-11-29 21:18:17 -0700117FaceTable::ForwardRange
118FaceTable::getForwardRange() const
119{
120 return m_faces | boost::adaptors::map_values;
121}
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700122
Junxiao Shiafbd74d2014-11-29 21:18:17 -0700123FaceTable::const_iterator
124FaceTable::begin() const
125{
126 return this->getForwardRange().begin();
127}
128
129FaceTable::const_iterator
130FaceTable::end() const
131{
132 return this->getForwardRange().end();
133}
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700134
Junxiao Shia4f2be82014-03-02 22:56:41 -0700135} // namespace nfd