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 | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 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" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 28 | #include "core/logger.hpp" |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 29 | |
| 30 | namespace nfd { |
| 31 | |
| 32 | NFD_LOG_INIT("FaceTable"); |
| 33 | |
| 34 | FaceTable::FaceTable(Forwarder& forwarder) |
| 35 | : m_forwarder(forwarder) |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 36 | , m_lastFaceId(FACEID_RESERVED_MAX) |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 40 | FaceTable::~FaceTable() |
| 41 | { |
| 42 | |
| 43 | } |
| 44 | |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame^] | 45 | shared_ptr<Face> |
| 46 | FaceTable::get(FaceId id) const |
| 47 | { |
| 48 | std::map<FaceId, shared_ptr<Face> >::const_iterator i = m_faces.find(id); |
| 49 | return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second); |
| 50 | } |
| 51 | |
| 52 | size_t |
| 53 | FaceTable::size() const |
| 54 | { |
| 55 | return m_faces.size(); |
| 56 | } |
| 57 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 58 | void |
| 59 | FaceTable::add(shared_ptr<Face> face) |
| 60 | { |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 61 | if (face->getId() != INVALID_FACEID && m_faces.count(face->getId()) > 0) { |
| 62 | NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table"); |
| 63 | return; |
| 64 | } |
Alexander Afanasyev | bc521a5 | 2014-03-26 23:31:55 -0700 | [diff] [blame] | 65 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 66 | FaceId faceId = ++m_lastFaceId; |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 67 | BOOST_ASSERT(faceId > FACEID_RESERVED_MAX); |
| 68 | this->addImpl(face, faceId); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | FaceTable::addReserved(shared_ptr<Face> face, FaceId faceId) |
| 73 | { |
| 74 | BOOST_ASSERT(face->getId() == INVALID_FACEID); |
| 75 | BOOST_ASSERT(m_faces.count(face->getId()) == 0); |
| 76 | BOOST_ASSERT(faceId <= FACEID_RESERVED_MAX); |
| 77 | this->addImpl(face, faceId); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | FaceTable::addImpl(shared_ptr<Face> face, FaceId faceId) |
| 82 | { |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 83 | face->setId(faceId); |
| 84 | m_faces[faceId] = face; |
Junxiao Shi | 7b984c6 | 2014-07-17 22:18:34 -0700 | [diff] [blame] | 85 | NFD_LOG_INFO("Added face id=" << faceId << " remote=" << face->getRemoteUri() |
| 86 | << " local=" << face->getLocalUri()); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 87 | |
| 88 | face->onReceiveInterest += bind(&Forwarder::onInterest, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 89 | &m_forwarder, ref(*face), _1); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 90 | face->onReceiveData += bind(&Forwarder::onData, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 91 | &m_forwarder, ref(*face), _1); |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 92 | face->onFail += bind(&FaceTable::remove, |
| 93 | this, face); |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 94 | |
| 95 | this->onAdd(face); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
| 99 | FaceTable::remove(shared_ptr<Face> face) |
| 100 | { |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 101 | this->onRemove(face); |
| 102 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 103 | FaceId faceId = face->getId(); |
| 104 | m_faces.erase(faceId); |
| 105 | face->setId(INVALID_FACEID); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 106 | NFD_LOG_INFO("Removed face id=" << faceId << " remote=" << face->getRemoteUri() << |
| 107 | " local=" << face->getLocalUri()); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 108 | |
| 109 | // XXX This clears all subscriptions, because EventEmitter |
| 110 | // does not support only removing Forwarder's subscription |
| 111 | face->onReceiveInterest.clear(); |
| 112 | face->onReceiveData .clear(); |
Alexander Afanasyev | 583760b | 2014-06-30 19:27:37 -0700 | [diff] [blame] | 113 | face->onSendInterest .clear(); |
| 114 | face->onSendData .clear(); |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 115 | // don't clear onFail because other functions may need to execute |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 116 | |
| 117 | m_forwarder.getFib().removeNextHopFromAllEntries(face); |
| 118 | } |
| 119 | |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame^] | 120 | FaceTable::ForwardRange |
| 121 | FaceTable::getForwardRange() const |
| 122 | { |
| 123 | return m_faces | boost::adaptors::map_values; |
| 124 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 125 | |
Junxiao Shi | afbd74d | 2014-11-29 21:18:17 -0700 | [diff] [blame^] | 126 | FaceTable::const_iterator |
| 127 | FaceTable::begin() const |
| 128 | { |
| 129 | return this->getForwardRange().begin(); |
| 130 | } |
| 131 | |
| 132 | FaceTable::const_iterator |
| 133 | FaceTable::end() const |
| 134 | { |
| 135 | return this->getForwardRange().end(); |
| 136 | } |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 137 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 138 | } // namespace nfd |