Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "face-table.hpp" |
| 8 | #include "forwarder.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 9 | #include "core/logger.hpp" |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 10 | |
| 11 | namespace nfd { |
| 12 | |
| 13 | NFD_LOG_INIT("FaceTable"); |
| 14 | |
| 15 | FaceTable::FaceTable(Forwarder& forwarder) |
| 16 | : m_forwarder(forwarder) |
| 17 | , m_lastFaceId(0) |
| 18 | { |
| 19 | } |
| 20 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 21 | FaceTable::~FaceTable() |
| 22 | { |
| 23 | |
| 24 | } |
| 25 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 26 | void |
| 27 | FaceTable::add(shared_ptr<Face> face) |
| 28 | { |
Alexander Afanasyev | bc521a5 | 2014-03-26 23:31:55 -0700 | [diff] [blame] | 29 | if (face->getId() != INVALID_FACEID && |
| 30 | m_faces.count(face->getId()) > 0) |
| 31 | { |
| 32 | NFD_LOG_DEBUG("Trying to add existing face id=" << face->getId() << " to the face table"); |
| 33 | return; |
| 34 | } |
| 35 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 36 | FaceId faceId = ++m_lastFaceId; |
| 37 | face->setId(faceId); |
| 38 | m_faces[faceId] = face; |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 39 | NFD_LOG_INFO("Added face id=" << faceId << " remote=" << face->getRemoteUri() << |
| 40 | " local=" << face->getLocalUri()); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 41 | |
| 42 | face->onReceiveInterest += bind(&Forwarder::onInterest, |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 43 | &m_forwarder, boost::ref(*face), _1); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 44 | face->onReceiveData += bind(&Forwarder::onData, |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 45 | &m_forwarder, boost::ref(*face), _1); |
| 46 | face->onFail += bind(&FaceTable::remove, |
| 47 | this, face); |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 48 | |
| 49 | this->onAdd(face); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void |
| 53 | FaceTable::remove(shared_ptr<Face> face) |
| 54 | { |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 55 | this->onRemove(face); |
| 56 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 57 | FaceId faceId = face->getId(); |
| 58 | m_faces.erase(faceId); |
| 59 | face->setId(INVALID_FACEID); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 60 | NFD_LOG_INFO("Removed face id=" << faceId << " remote=" << face->getRemoteUri() << |
| 61 | " local=" << face->getLocalUri()); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 62 | |
| 63 | // XXX This clears all subscriptions, because EventEmitter |
| 64 | // does not support only removing Forwarder's subscription |
| 65 | face->onReceiveInterest.clear(); |
| 66 | face->onReceiveData .clear(); |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 67 | // don't clear onFail because other functions may need to execute |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 68 | |
| 69 | m_forwarder.getFib().removeNextHopFromAllEntries(face); |
| 70 | } |
| 71 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 72 | |
| 73 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 74 | } // namespace nfd |