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" |
| 9 | |
| 10 | namespace nfd { |
| 11 | |
| 12 | NFD_LOG_INIT("FaceTable"); |
| 13 | |
| 14 | FaceTable::FaceTable(Forwarder& forwarder) |
| 15 | : m_forwarder(forwarder) |
| 16 | , m_lastFaceId(0) |
| 17 | { |
| 18 | } |
| 19 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 20 | FaceTable::~FaceTable() |
| 21 | { |
| 22 | |
| 23 | } |
| 24 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 25 | void |
| 26 | FaceTable::add(shared_ptr<Face> face) |
| 27 | { |
Alexander Afanasyev | bc521a5 | 2014-03-26 23:31:55 -0700 | [diff] [blame^] | 28 | if (face->getId() != INVALID_FACEID && |
| 29 | m_faces.count(face->getId()) > 0) |
| 30 | { |
| 31 | NFD_LOG_DEBUG("Trying to add existing face id=" << face->getId() << " to the face table"); |
| 32 | return; |
| 33 | } |
| 34 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 35 | FaceId faceId = ++m_lastFaceId; |
| 36 | face->setId(faceId); |
| 37 | m_faces[faceId] = face; |
| 38 | NFD_LOG_INFO("addFace id=" << faceId); |
| 39 | |
| 40 | face->onReceiveInterest += bind(&Forwarder::onInterest, |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 41 | &m_forwarder, boost::ref(*face), _1); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 42 | face->onReceiveData += bind(&Forwarder::onData, |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 43 | &m_forwarder, boost::ref(*face), _1); |
| 44 | face->onFail += bind(&FaceTable::remove, |
| 45 | this, face); |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 46 | |
| 47 | this->onAdd(face); |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
| 51 | FaceTable::remove(shared_ptr<Face> face) |
| 52 | { |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 53 | this->onRemove(face); |
| 54 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 55 | FaceId faceId = face->getId(); |
| 56 | m_faces.erase(faceId); |
| 57 | face->setId(INVALID_FACEID); |
| 58 | NFD_LOG_INFO("removeFace id=" << faceId); |
| 59 | |
| 60 | // XXX This clears all subscriptions, because EventEmitter |
| 61 | // does not support only removing Forwarder's subscription |
| 62 | face->onReceiveInterest.clear(); |
| 63 | face->onReceiveData .clear(); |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 64 | // don't clear onFail because other functions may need to execute |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 65 | |
| 66 | m_forwarder.getFib().removeNextHopFromAllEntries(face); |
| 67 | } |
| 68 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 69 | |
| 70 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 71 | } // namespace nfd |