blob: a95c562260a8f3ab4478db73caf3b29bcd96ff19 [file] [log] [blame]
Junxiao Shia4f2be82014-03-02 22:56:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7b984c62014-07-17 22:18:34 -07003 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shi7b984c62014-07-17 22:18:34 -070024 */
Junxiao Shia4f2be82014-03-02 22:56:41 -070025
26#include "face-table.hpp"
27#include "forwarder.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060028#include "core/logger.hpp"
Junxiao Shia4f2be82014-03-02 22:56:41 -070029
30namespace nfd {
31
32NFD_LOG_INIT("FaceTable");
33
34FaceTable::FaceTable(Forwarder& forwarder)
35 : m_forwarder(forwarder)
Junxiao Shi7b984c62014-07-17 22:18:34 -070036 , m_lastFaceId(FACEID_RESERVED_MAX)
Junxiao Shia4f2be82014-03-02 22:56:41 -070037{
38}
39
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070040FaceTable::~FaceTable()
41{
42
43}
44
Junxiao Shia4f2be82014-03-02 22:56:41 -070045void
46FaceTable::add(shared_ptr<Face> face)
47{
Junxiao Shi7b984c62014-07-17 22:18:34 -070048 if (face->getId() != INVALID_FACEID && m_faces.count(face->getId()) > 0) {
49 NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table");
50 return;
51 }
Alexander Afanasyevbc521a52014-03-26 23:31:55 -070052
Junxiao Shia4f2be82014-03-02 22:56:41 -070053 FaceId faceId = ++m_lastFaceId;
Junxiao Shi7b984c62014-07-17 22:18:34 -070054 BOOST_ASSERT(faceId > FACEID_RESERVED_MAX);
55 this->addImpl(face, faceId);
56}
57
58void
59FaceTable::addReserved(shared_ptr<Face> face, FaceId faceId)
60{
61 BOOST_ASSERT(face->getId() == INVALID_FACEID);
62 BOOST_ASSERT(m_faces.count(face->getId()) == 0);
63 BOOST_ASSERT(faceId <= FACEID_RESERVED_MAX);
64 this->addImpl(face, faceId);
65}
66
67void
68FaceTable::addImpl(shared_ptr<Face> face, FaceId faceId)
69{
Junxiao Shia4f2be82014-03-02 22:56:41 -070070 face->setId(faceId);
71 m_faces[faceId] = face;
Junxiao Shi7b984c62014-07-17 22:18:34 -070072 NFD_LOG_INFO("Added face id=" << faceId << " remote=" << face->getRemoteUri()
73 << " local=" << face->getLocalUri());
Junxiao Shia4f2be82014-03-02 22:56:41 -070074
75 face->onReceiveInterest += bind(&Forwarder::onInterest,
Alexander Afanasyevf6980282014-05-13 18:28:40 -070076 &m_forwarder, ref(*face), _1);
Junxiao Shia4f2be82014-03-02 22:56:41 -070077 face->onReceiveData += bind(&Forwarder::onData,
Alexander Afanasyevf6980282014-05-13 18:28:40 -070078 &m_forwarder, ref(*face), _1);
Junxiao Shic542b2b2014-03-16 21:45:52 -070079 face->onFail += bind(&FaceTable::remove,
80 this, face);
Junxiao Shibd392bf2014-03-17 15:54:11 -070081
82 this->onAdd(face);
Junxiao Shia4f2be82014-03-02 22:56:41 -070083}
84
85void
86FaceTable::remove(shared_ptr<Face> face)
87{
Junxiao Shibd392bf2014-03-17 15:54:11 -070088 this->onRemove(face);
89
Junxiao Shia4f2be82014-03-02 22:56:41 -070090 FaceId faceId = face->getId();
91 m_faces.erase(faceId);
92 face->setId(INVALID_FACEID);
Junxiao Shi6e694322014-04-03 10:27:13 -070093 NFD_LOG_INFO("Removed face id=" << faceId << " remote=" << face->getRemoteUri() <<
94 " local=" << face->getLocalUri());
Junxiao Shia4f2be82014-03-02 22:56:41 -070095
96 // XXX This clears all subscriptions, because EventEmitter
97 // does not support only removing Forwarder's subscription
98 face->onReceiveInterest.clear();
99 face->onReceiveData .clear();
Alexander Afanasyev583760b2014-06-30 19:27:37 -0700100 face->onSendInterest .clear();
101 face->onSendData .clear();
Junxiao Shic542b2b2014-03-16 21:45:52 -0700102 // don't clear onFail because other functions may need to execute
Junxiao Shia4f2be82014-03-02 22:56:41 -0700103
104 m_forwarder.getFib().removeNextHopFromAllEntries(face);
105}
106
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700107
108
Junxiao Shia4f2be82014-03-02 22:56:41 -0700109} // namespace nfd