blob: 7df0eef8d315a9d700ea82ee54c5fb39faa6d2a2 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
alvy297f4162015-03-03 17:15:33 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
alvy297f4162015-03-03 17:15:33 -060021
Nick Gordonf14ec352017-07-24 16:09:58 -050022#include "name-prefix-list.hpp"
Nick Gordonf14ec352017-07-24 16:09:58 -050023#include "common.hpp"
24#include "logger.hpp"
25
akmhoque53353462014-04-22 08:43:45 -050026#include <iostream>
27#include <algorithm>
28
akmhoque53353462014-04-22 08:43:45 -050029namespace nlsr {
30
akmhoque674b0b12014-05-20 14:33:28 -050031INIT_LOGGER("NamePrefixList");
32
akmhoquec8a10f72014-04-25 18:42:55 -050033NamePrefixList::NamePrefixList()
akmhoque53353462014-04-22 08:43:45 -050034{
35}
36
akmhoquec8a10f72014-04-25 18:42:55 -050037NamePrefixList::~NamePrefixList()
akmhoque53353462014-04-22 08:43:45 -050038{
39}
40
Nick Gordonf14ec352017-07-24 16:09:58 -050041std::vector<NamePrefixList::NamePair>::iterator
42NamePrefixList::get(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050043{
Nick Gordonf14ec352017-07-24 16:09:58 -050044 return std::find_if(m_names.begin(), m_names.end(),
45 [&] (const NamePrefixList::NamePair& pair) {
46 return name == std::get<NamePrefixList::NamePairIndex::NAME>(pair);
47 });
48}
49
50std::vector<std::string>::iterator
51NamePrefixList::getSource(const std::string& source, std::vector<NamePair>::iterator& entry)
52{
53 return std::find_if(std::get<NamePairIndex::SOURCES>(*entry).begin(),
54 std::get<NamePairIndex::SOURCES>(*entry).end(),
55 [&] (const std::string& containerSource) {
56 return source == containerSource;
57 });
akmhoque53353462014-04-22 08:43:45 -050058}
59
alvy297f4162015-03-03 17:15:33 -060060bool
Nick Gordonf14ec352017-07-24 16:09:58 -050061NamePrefixList::insert(const ndn::Name& name, const std::string& source)
akmhoque53353462014-04-22 08:43:45 -050062{
Nick Gordonf14ec352017-07-24 16:09:58 -050063 auto pairItr = get(name);
64 if (pairItr == m_names.end()) {
65 std::vector<std::string> sources{source};
66 m_names.push_back(std::tie(name, sources));
alvy297f4162015-03-03 17:15:33 -060067 return true;
akmhoque53353462014-04-22 08:43:45 -050068 }
Nick Gordonf14ec352017-07-24 16:09:58 -050069 else {
70 std::vector<std::string>& sources = std::get<NamePrefixList::NamePairIndex::SOURCES>(*pairItr);
71 auto sourceItr = getSource(source, pairItr);
72 if (sourceItr == sources.end()) {
73 sources.push_back(source);
74 return true;
75 }
76 }
77 return false;
78}
alvy297f4162015-03-03 17:15:33 -060079
Nick Gordonf14ec352017-07-24 16:09:58 -050080bool
81NamePrefixList::remove(const ndn::Name& name, const std::string& source)
82{
83 auto pairItr = get(name);
84 if (pairItr != m_names.end()) {
85 std::vector<std::string>& sources = std::get<NamePrefixList::NamePairIndex::SOURCES>(*pairItr);
86 auto sourceItr = getSource(source, pairItr);
87 if (sourceItr != sources.end()) {
88 sources.erase(sourceItr);
89 if (sources.size() == 0) {
90 m_names.erase(pairItr);
91 }
92 return true;
93 }
94 }
alvy297f4162015-03-03 17:15:33 -060095 return false;
akmhoque53353462014-04-22 08:43:45 -050096}
97
Nick Gordon56d1fae2017-05-26 16:39:25 -050098bool
99NamePrefixList::operator==(const NamePrefixList& other) const
100{
Nick Gordonf14ec352017-07-24 16:09:58 -0500101 return m_names == other.m_names;
Nick Gordon56d1fae2017-05-26 16:39:25 -0500102}
103
akmhoque53353462014-04-22 08:43:45 -0500104void
akmhoquec8a10f72014-04-25 18:42:55 -0500105NamePrefixList::sort()
akmhoque53353462014-04-22 08:43:45 -0500106{
Nick Gordonf14ec352017-07-24 16:09:58 -0500107 std::sort(m_names.begin(), m_names.end());
108}
109
110std::list<ndn::Name>
111NamePrefixList::getNames() const
112{
113 std::list<ndn::Name> names;
114 for (const auto& namePair : m_names) {
115 names.push_back(std::get<NamePrefixList::NamePairIndex::NAME>(namePair));
116 }
117 return names;
118}
119
120uint32_t
121NamePrefixList::countSources(const ndn::Name& name) const
122{
123 return getSources(name).size();
124}
125
126const std::vector<std::string>
127NamePrefixList::getSources(const ndn::Name& name) const
128{
129 auto it = std::find_if(m_names.begin(), m_names.end(),
130 [&] (const NamePrefixList::NamePair& pair) {
131 return name == std::get<NamePrefixList::NamePairIndex::NAME>(pair);
132 });
133 if (it != m_names.end()) {
134 return std::get<NamePrefixList::NamePairIndex::SOURCES>(*it);
135 }
136 else {
137 return std::vector<std::string>{};
138 }
akmhoque53353462014-04-22 08:43:45 -0500139}
140
Nick Gordon56d1fae2017-05-26 16:39:25 -0500141std::ostream&
142operator<<(std::ostream& os, const NamePrefixList& list) {
143 os << "Name prefix list: {\n";
Nick Gordonf14ec352017-07-24 16:09:58 -0500144 for (const auto& name : list.getNames()) {
145 os << name << "\n"
146 << "Sources:\n";
147 for (const auto& source : list.getSources(name)) {
148 os << " " << source << "\n";
149 }
Nick Gordon56d1fae2017-05-26 16:39:25 -0500150 }
151 os << "}" << std::endl;
152 return os;
153}
154
Nick Gordonfad8e252016-08-11 14:21:38 -0500155} // namespace nlsr