Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 24 | |
| 25 | #include "face-table.hpp" |
| 26 | #include "forwarder.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | |
| 31 | NFD_LOG_INIT("FaceTable"); |
| 32 | |
| 33 | FaceTable::FaceTable(Forwarder& forwarder) |
| 34 | : m_forwarder(forwarder) |
| 35 | , m_lastFaceId(0) |
| 36 | { |
| 37 | } |
| 38 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 39 | FaceTable::~FaceTable() |
| 40 | { |
| 41 | |
| 42 | } |
| 43 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 44 | void |
| 45 | FaceTable::add(shared_ptr<Face> face) |
| 46 | { |
Alexander Afanasyev | bc521a5 | 2014-03-26 23:31:55 -0700 | [diff] [blame] | 47 | if (face->getId() != INVALID_FACEID && |
| 48 | m_faces.count(face->getId()) > 0) |
| 49 | { |
| 50 | NFD_LOG_DEBUG("Trying to add existing face id=" << face->getId() << " to the face table"); |
| 51 | return; |
| 52 | } |
| 53 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 54 | FaceId faceId = ++m_lastFaceId; |
| 55 | face->setId(faceId); |
| 56 | m_faces[faceId] = face; |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 57 | NFD_LOG_INFO("Added face id=" << faceId << " remote=" << face->getRemoteUri() << |
| 58 | " local=" << face->getLocalUri()); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 59 | |
| 60 | face->onReceiveInterest += bind(&Forwarder::onInterest, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 61 | &m_forwarder, ref(*face), _1); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 62 | face->onReceiveData += bind(&Forwarder::onData, |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 63 | &m_forwarder, ref(*face), _1); |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 64 | face->onFail += bind(&FaceTable::remove, |
| 65 | this, face); |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 66 | |
| 67 | this->onAdd(face); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void |
| 71 | FaceTable::remove(shared_ptr<Face> face) |
| 72 | { |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 73 | this->onRemove(face); |
| 74 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 75 | FaceId faceId = face->getId(); |
| 76 | m_faces.erase(faceId); |
| 77 | face->setId(INVALID_FACEID); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 78 | NFD_LOG_INFO("Removed face id=" << faceId << " remote=" << face->getRemoteUri() << |
| 79 | " local=" << face->getLocalUri()); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 80 | |
| 81 | // XXX This clears all subscriptions, because EventEmitter |
| 82 | // does not support only removing Forwarder's subscription |
| 83 | face->onReceiveInterest.clear(); |
| 84 | face->onReceiveData .clear(); |
Alexander Afanasyev | 583760b | 2014-06-30 19:27:37 -0700 | [diff] [blame] | 85 | face->onSendInterest .clear(); |
| 86 | face->onSendData .clear(); |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 87 | // don't clear onFail because other functions may need to execute |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 88 | |
| 89 | m_forwarder.getFib().removeNextHopFromAllEntries(face); |
| 90 | } |
| 91 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 92 | |
| 93 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 94 | } // namespace nfd |