Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 4 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 25 | |
| 26 | #include "face-table.hpp" |
| 27 | #include "forwarder.hpp" |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 28 | #include "core/global-io.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 29 | #include "core/logger.hpp" |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 30 | #include "face/channel.hpp" |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | |
| 34 | NFD_LOG_INIT("FaceTable"); |
| 35 | |
| 36 | FaceTable::FaceTable(Forwarder& forwarder) |
| 37 | : m_forwarder(forwarder) |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 38 | , m_lastFaceId(face::FACEID_RESERVED_MAX) |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 42 | FaceTable::~FaceTable() |
| 43 | { |
| 44 | |
| 45 | } |
| 46 | |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 47 | Face* |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame] | 48 | FaceTable::get(FaceId id) const |
| 49 | { |
Junxiao Shi | 5b43f9a | 2016-07-19 13:15:56 +0000 | [diff] [blame] | 50 | auto i = m_faces.find(id); |
| 51 | if (i == m_faces.end()) { |
| 52 | return nullptr; |
| 53 | } |
| 54 | return i->second.get(); |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | size_t |
| 58 | FaceTable::size() const |
| 59 | { |
| 60 | return m_faces.size(); |
| 61 | } |
| 62 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 63 | void |
| 64 | FaceTable::add(shared_ptr<Face> face) |
| 65 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 66 | if (face->getId() != face::INVALID_FACEID && m_faces.count(face->getId()) > 0) { |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 67 | NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table"); |
| 68 | return; |
| 69 | } |
Alexander Afanasyev | bc521a5 | 2014-03-26 23:31:55 -0700 | [diff] [blame] | 70 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 71 | FaceId faceId = ++m_lastFaceId; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 72 | BOOST_ASSERT(faceId > face::FACEID_RESERVED_MAX); |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 73 | this->addImpl(face, faceId); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | FaceTable::addReserved(shared_ptr<Face> face, FaceId faceId) |
| 78 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 79 | BOOST_ASSERT(face->getId() == face::INVALID_FACEID); |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 80 | BOOST_ASSERT(m_faces.count(face->getId()) == 0); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 81 | BOOST_ASSERT(faceId <= face::FACEID_RESERVED_MAX); |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 82 | this->addImpl(face, faceId); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | FaceTable::addImpl(shared_ptr<Face> face, FaceId faceId) |
| 87 | { |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 88 | face->setId(faceId); |
| 89 | m_faces[faceId] = face; |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 90 | NFD_LOG_INFO("Added face id=" << faceId << " remote=" << face->getRemoteUri() |
| 91 | << " local=" << face->getLocalUri()); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 92 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 93 | face->afterReceiveInterest.connect(bind(&Forwarder::startProcessInterest, &m_forwarder, ref(*face), _1)); |
| 94 | face->afterReceiveData.connect(bind(&Forwarder::startProcessData, &m_forwarder, ref(*face), _1)); |
| 95 | face->afterReceiveNack.connect(bind(&Forwarder::startProcessNack, &m_forwarder, ref(*face), _1)); |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame^] | 96 | connectFaceClosedSignal(*face, bind(&FaceTable::remove, this, faceId)); |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 97 | |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame^] | 98 | this->afterAdd(*face); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame^] | 102 | FaceTable::remove(FaceId faceId) |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 103 | { |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame^] | 104 | auto i = m_faces.find(faceId); |
| 105 | BOOST_ASSERT(i != m_faces.end()); |
| 106 | shared_ptr<Face> face = i->second; |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 107 | |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame^] | 108 | this->beforeRemove(*face); |
| 109 | |
| 110 | m_forwarder.getFib().removeNextHopFromAllEntries(*face); |
| 111 | |
| 112 | m_faces.erase(i); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 113 | face->setId(face::INVALID_FACEID); |
Davide Pesavento | 66ff098 | 2015-01-29 22:39:00 +0100 | [diff] [blame] | 114 | |
| 115 | NFD_LOG_INFO("Removed face id=" << faceId << |
| 116 | " remote=" << face->getRemoteUri() << |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 117 | " local=" << face->getLocalUri()); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 118 | |
Junxiao Shi | 40cb61c | 2015-09-23 18:36:45 -0700 | [diff] [blame] | 119 | // defer Face deallocation, so that Transport isn't deallocated during afterStateChange signal |
| 120 | getGlobalIoService().post([face] {}); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame] | 123 | FaceTable::ForwardRange |
| 124 | FaceTable::getForwardRange() const |
| 125 | { |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 126 | return m_faces | boost::adaptors::map_values | boost::adaptors::indirected; |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame] | 127 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 128 | |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame] | 129 | FaceTable::const_iterator |
| 130 | FaceTable::begin() const |
| 131 | { |
| 132 | return this->getForwardRange().begin(); |
| 133 | } |
| 134 | |
| 135 | FaceTable::const_iterator |
| 136 | FaceTable::end() const |
| 137 | { |
| 138 | return this->getForwardRange().end(); |
| 139 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 140 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 141 | } // namespace nfd |