blob: 07409c6136602b6c7fcf668d8bb79d0134e68a91 [file] [log] [blame]
Junxiao Shia4f2be82014-03-02 22:56:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento6fd1e7e2017-02-17 18:45:27 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * 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"
Junxiao Shi3aba7542016-12-24 02:42:52 +000027#include "core/asserts.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 Shicde37ad2015-12-24 01:02:05 -070030#include "face/channel.hpp"
Junxiao Shia4f2be82014-03-02 22:56:41 -070031
32namespace nfd {
33
Junxiao Shi3aba7542016-12-24 02:42:52 +000034NFD_ASSERT_FORWARD_ITERATOR(FaceTable::const_iterator);
35
Junxiao Shia4f2be82014-03-02 22:56:41 -070036NFD_LOG_INIT("FaceTable");
37
Junxiao Shidcffdaa2016-07-26 02:23:56 +000038FaceTable::FaceTable()
39 : m_lastFaceId(face::FACEID_RESERVED_MAX)
Junxiao Shia4f2be82014-03-02 22:56:41 -070040{
41}
42
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000043Face*
Junxiao Shiafbd74d2014-11-29 21:18:17 -070044FaceTable::get(FaceId id) const
45{
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000046 auto i = m_faces.find(id);
Davide Pesavento6fd1e7e2017-02-17 18:45:27 +000047 return i == m_faces.end() ? nullptr : i->second.get();
Junxiao Shiafbd74d2014-11-29 21:18:17 -070048}
49
50size_t
51FaceTable::size() const
52{
53 return m_faces.size();
54}
55
Junxiao Shia4f2be82014-03-02 22:56:41 -070056void
57FaceTable::add(shared_ptr<Face> face)
58{
Junxiao Shicde37ad2015-12-24 01:02:05 -070059 if (face->getId() != face::INVALID_FACEID && m_faces.count(face->getId()) > 0) {
Junxiao Shi7b984c62014-07-17 22:18:34 -070060 NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table");
61 return;
62 }
Alexander Afanasyevbc521a52014-03-26 23:31:55 -070063
Junxiao Shia4f2be82014-03-02 22:56:41 -070064 FaceId faceId = ++m_lastFaceId;
Junxiao Shicde37ad2015-12-24 01:02:05 -070065 BOOST_ASSERT(faceId > face::FACEID_RESERVED_MAX);
Davide Pesavento6fd1e7e2017-02-17 18:45:27 +000066 this->addImpl(std::move(face), faceId);
Junxiao Shi7b984c62014-07-17 22:18:34 -070067}
68
69void
70FaceTable::addReserved(shared_ptr<Face> face, FaceId faceId)
71{
Junxiao Shicde37ad2015-12-24 01:02:05 -070072 BOOST_ASSERT(face->getId() == face::INVALID_FACEID);
Junxiao Shicde37ad2015-12-24 01:02:05 -070073 BOOST_ASSERT(faceId <= face::FACEID_RESERVED_MAX);
Davide Pesavento6fd1e7e2017-02-17 18:45:27 +000074 this->addImpl(std::move(face), faceId);
Junxiao Shi7b984c62014-07-17 22:18:34 -070075}
76
77void
78FaceTable::addImpl(shared_ptr<Face> face, FaceId faceId)
79{
Junxiao Shia4f2be82014-03-02 22:56:41 -070080 face->setId(faceId);
Davide Pesavento6fd1e7e2017-02-17 18:45:27 +000081 auto ret = m_faces.emplace(faceId, face);
Junxiao Shida89fa52017-03-22 21:34:56 +000082 BOOST_VERIFY(ret.second);
Davide Pesavento6fd1e7e2017-02-17 18:45:27 +000083
84 NFD_LOG_INFO("Added face id=" << faceId <<
85 " remote=" << face->getRemoteUri() <<
86 " local=" << face->getLocalUri());
Junxiao Shia4f2be82014-03-02 22:56:41 -070087
Junxiao Shiae04d342016-07-19 13:20:22 +000088 connectFaceClosedSignal(*face, bind(&FaceTable::remove, this, faceId));
Junxiao Shibd392bf2014-03-17 15:54:11 -070089
Junxiao Shiae04d342016-07-19 13:20:22 +000090 this->afterAdd(*face);
Junxiao Shia4f2be82014-03-02 22:56:41 -070091}
92
93void
Junxiao Shiae04d342016-07-19 13:20:22 +000094FaceTable::remove(FaceId faceId)
Junxiao Shia4f2be82014-03-02 22:56:41 -070095{
Junxiao Shiae04d342016-07-19 13:20:22 +000096 auto i = m_faces.find(faceId);
97 BOOST_ASSERT(i != m_faces.end());
98 shared_ptr<Face> face = i->second;
Junxiao Shibd392bf2014-03-17 15:54:11 -070099
Junxiao Shiae04d342016-07-19 13:20:22 +0000100 this->beforeRemove(*face);
101
Junxiao Shiae04d342016-07-19 13:20:22 +0000102 m_faces.erase(i);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700103 face->setId(face::INVALID_FACEID);
Davide Pesavento66ff0982015-01-29 22:39:00 +0100104
105 NFD_LOG_INFO("Removed face id=" << faceId <<
106 " remote=" << face->getRemoteUri() <<
Junxiao Shicde37ad2015-12-24 01:02:05 -0700107 " local=" << face->getLocalUri());
Junxiao Shia4f2be82014-03-02 22:56:41 -0700108
Junxiao Shi40cb61c2015-09-23 18:36:45 -0700109 // defer Face deallocation, so that Transport isn't deallocated during afterStateChange signal
110 getGlobalIoService().post([face] {});
Junxiao Shia4f2be82014-03-02 22:56:41 -0700111}
112
Junxiao Shiafbd74d2014-11-29 21:18:17 -0700113FaceTable::ForwardRange
114FaceTable::getForwardRange() const
115{
Junxiao Shib84e6742016-07-19 13:16:22 +0000116 return m_faces | boost::adaptors::map_values | boost::adaptors::indirected;
Junxiao Shiafbd74d2014-11-29 21:18:17 -0700117}
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700118
Junxiao Shiafbd74d2014-11-29 21:18:17 -0700119FaceTable::const_iterator
120FaceTable::begin() const
121{
122 return this->getForwardRange().begin();
123}
124
125FaceTable::const_iterator
126FaceTable::end() const
127{
128 return this->getForwardRange().end();
129}
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700130
Junxiao Shia4f2be82014-03-02 22:56:41 -0700131} // namespace nfd