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; |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame^] | 39 | NFD_LOG_INFO("Added face id=" << faceId << " uri=" << face->getUri()); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 40 | |
| 41 | face->onReceiveInterest += bind(&Forwarder::onInterest, |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 42 | &m_forwarder, boost::ref(*face), _1); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 43 | face->onReceiveData += bind(&Forwarder::onData, |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 44 | &m_forwarder, boost::ref(*face), _1); |
| 45 | face->onFail += bind(&FaceTable::remove, |
| 46 | this, face); |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 47 | |
| 48 | this->onAdd(face); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void |
| 52 | FaceTable::remove(shared_ptr<Face> face) |
| 53 | { |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 54 | this->onRemove(face); |
| 55 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 56 | FaceId faceId = face->getId(); |
| 57 | m_faces.erase(faceId); |
| 58 | face->setId(INVALID_FACEID); |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame^] | 59 | NFD_LOG_INFO("Removed face id=" << faceId << " uri=" << face->getUri()); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 60 | |
| 61 | // XXX This clears all subscriptions, because EventEmitter |
| 62 | // does not support only removing Forwarder's subscription |
| 63 | face->onReceiveInterest.clear(); |
| 64 | face->onReceiveData .clear(); |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 65 | // don't clear onFail because other functions may need to execute |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 66 | |
| 67 | m_forwarder.getFib().removeNextHopFromAllEntries(face); |
| 68 | } |
| 69 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 70 | |
| 71 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 72 | } // namespace nfd |