blob: a8a8053e8efb0c5b0f7772e523f9b2af346bb8e5 [file] [log] [blame]
Junxiao Shia4f2be82014-03-02 22:56:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, 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"
Junxiao Shi40cb61c2015-09-23 18:36:45 -070028#include "core/global-io.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060029#include "core/logger.hpp"
Junxiao Shia4f2be82014-03-02 22:56:41 -070030
31namespace nfd {
32
33NFD_LOG_INIT("FaceTable");
34
35FaceTable::FaceTable(Forwarder& forwarder)
36 : m_forwarder(forwarder)
Junxiao Shi7b984c62014-07-17 22:18:34 -070037 , m_lastFaceId(FACEID_RESERVED_MAX)
Junxiao Shia4f2be82014-03-02 22:56:41 -070038{
39}
40
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070041FaceTable::~FaceTable()
42{
43
44}
45
Junxiao Shiafbd74d2014-11-29 21:18:17 -070046shared_ptr<Face>
47FaceTable::get(FaceId id) const
48{
49 std::map<FaceId, shared_ptr<Face> >::const_iterator i = m_faces.find(id);
50 return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second);
51}
52
53size_t
54FaceTable::size() const
55{
56 return m_faces.size();
57}
58
Junxiao Shia4f2be82014-03-02 22:56:41 -070059void
60FaceTable::add(shared_ptr<Face> face)
61{
Junxiao Shi7b984c62014-07-17 22:18:34 -070062 if (face->getId() != INVALID_FACEID && m_faces.count(face->getId()) > 0) {
63 NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table");
64 return;
65 }
Alexander Afanasyevbc521a52014-03-26 23:31:55 -070066
Junxiao Shia4f2be82014-03-02 22:56:41 -070067 FaceId faceId = ++m_lastFaceId;
Junxiao Shi7b984c62014-07-17 22:18:34 -070068 BOOST_ASSERT(faceId > FACEID_RESERVED_MAX);
69 this->addImpl(face, faceId);
70}
71
72void
73FaceTable::addReserved(shared_ptr<Face> face, FaceId faceId)
74{
75 BOOST_ASSERT(face->getId() == INVALID_FACEID);
76 BOOST_ASSERT(m_faces.count(face->getId()) == 0);
77 BOOST_ASSERT(faceId <= FACEID_RESERVED_MAX);
78 this->addImpl(face, faceId);
79}
80
81void
82FaceTable::addImpl(shared_ptr<Face> face, FaceId faceId)
83{
Junxiao Shia4f2be82014-03-02 22:56:41 -070084 face->setId(faceId);
85 m_faces[faceId] = face;
Junxiao Shi7b984c62014-07-17 22:18:34 -070086 NFD_LOG_INFO("Added face id=" << faceId << " remote=" << face->getRemoteUri()
87 << " local=" << face->getLocalUri());
Junxiao Shia4f2be82014-03-02 22:56:41 -070088
Junxiao Shi0355e9f2015-09-02 07:24:53 -070089 face->onReceiveInterest.connect(bind(&Forwarder::startProcessInterest,
90 &m_forwarder, ref(*face), _1));
91 face->onReceiveData.connect(bind(&Forwarder::startProcessData,
92 &m_forwarder, ref(*face), _1));
Junxiao Shi5e5e4452015-09-24 16:56:52 -070093 face->onReceiveNack.connect(bind(&Forwarder::startProcessNack,
94 &m_forwarder, ref(*face), _1));
Davide Pesavento66ff0982015-01-29 22:39:00 +010095 face->onFail.connectSingleShot(bind(&FaceTable::remove, this, face, _1));
Junxiao Shibd392bf2014-03-17 15:54:11 -070096
97 this->onAdd(face);
Junxiao Shia4f2be82014-03-02 22:56:41 -070098}
99
100void
Davide Pesavento66ff0982015-01-29 22:39:00 +0100101FaceTable::remove(shared_ptr<Face> face, const std::string& reason)
Junxiao Shia4f2be82014-03-02 22:56:41 -0700102{
Junxiao Shibd392bf2014-03-17 15:54:11 -0700103 this->onRemove(face);
104
Junxiao Shia4f2be82014-03-02 22:56:41 -0700105 FaceId faceId = face->getId();
106 m_faces.erase(faceId);
107 face->setId(INVALID_FACEID);
Davide Pesavento66ff0982015-01-29 22:39:00 +0100108
109 NFD_LOG_INFO("Removed face id=" << faceId <<
110 " remote=" << face->getRemoteUri() <<
111 " local=" << face->getLocalUri() <<
112 " (" << reason << ")");
Junxiao Shia4f2be82014-03-02 22:56:41 -0700113
114 m_forwarder.getFib().removeNextHopFromAllEntries(face);
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700115
116 // defer Face deallocation, so that Transport isn't deallocated during afterStateChange signal
117 getGlobalIoService().post([face] {});
Junxiao Shia4f2be82014-03-02 22:56:41 -0700118}
119
Junxiao Shiafbd74d2014-11-29 21:18:17 -0700120FaceTable::ForwardRange
121FaceTable::getForwardRange() const
122{
123 return m_faces | boost::adaptors::map_values;
124}
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700125
Junxiao Shiafbd74d2014-11-29 21:18:17 -0700126FaceTable::const_iterator
127FaceTable::begin() const
128{
129 return this->getForwardRange().begin();
130}
131
132FaceTable::const_iterator
133FaceTable::end() const
134{
135 return this->getForwardRange().end();
136}
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700137
Junxiao Shia4f2be82014-03-02 22:56:41 -0700138} // namespace nfd