blob: 742367b50c8e59bce27e33192de93d5f910facb3 [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"
23
24#include "common.hpp"
25#include "logger.hpp"
26
akmhoque53353462014-04-22 08:43:45 -050027#include <iostream>
28#include <algorithm>
29
akmhoquec8a10f72014-04-25 18:42:55 -050030#include <ndn-cxx/common.hpp>
31
akmhoque53353462014-04-22 08:43:45 -050032namespace nlsr {
33
akmhoque674b0b12014-05-20 14:33:28 -050034INIT_LOGGER("NamePrefixList");
35
akmhoque53353462014-04-22 08:43:45 -050036using namespace std;
37
akmhoquec8a10f72014-04-25 18:42:55 -050038NamePrefixList::NamePrefixList()
akmhoque53353462014-04-22 08:43:45 -050039{
40}
41
akmhoquec8a10f72014-04-25 18:42:55 -050042NamePrefixList::~NamePrefixList()
akmhoque53353462014-04-22 08:43:45 -050043{
44}
45
Nick Gordonf14ec352017-07-24 16:09:58 -050046std::vector<NamePrefixList::NamePair>::iterator
47NamePrefixList::get(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050048{
Nick Gordonf14ec352017-07-24 16:09:58 -050049 return std::find_if(m_names.begin(), m_names.end(),
50 [&] (const NamePrefixList::NamePair& pair) {
51 return name == std::get<NamePrefixList::NamePairIndex::NAME>(pair);
52 });
53}
54
55std::vector<std::string>::iterator
56NamePrefixList::getSource(const std::string& source, std::vector<NamePair>::iterator& entry)
57{
58 return std::find_if(std::get<NamePairIndex::SOURCES>(*entry).begin(),
59 std::get<NamePairIndex::SOURCES>(*entry).end(),
60 [&] (const std::string& containerSource) {
61 return source == containerSource;
62 });
akmhoque53353462014-04-22 08:43:45 -050063}
64
alvy297f4162015-03-03 17:15:33 -060065bool
Nick Gordonf14ec352017-07-24 16:09:58 -050066NamePrefixList::insert(const ndn::Name& name, const std::string& source)
akmhoque53353462014-04-22 08:43:45 -050067{
Nick Gordonf14ec352017-07-24 16:09:58 -050068 auto pairItr = get(name);
69 if (pairItr == m_names.end()) {
70 std::vector<std::string> sources{source};
71 m_names.push_back(std::tie(name, sources));
alvy297f4162015-03-03 17:15:33 -060072 return true;
akmhoque53353462014-04-22 08:43:45 -050073 }
Nick Gordonf14ec352017-07-24 16:09:58 -050074 else {
75 std::vector<std::string>& sources = std::get<NamePrefixList::NamePairIndex::SOURCES>(*pairItr);
76 auto sourceItr = getSource(source, pairItr);
77 if (sourceItr == sources.end()) {
78 sources.push_back(source);
79 return true;
80 }
81 }
82 return false;
83}
alvy297f4162015-03-03 17:15:33 -060084
Nick Gordonf14ec352017-07-24 16:09:58 -050085bool
86NamePrefixList::remove(const ndn::Name& name, const std::string& source)
87{
88 auto pairItr = get(name);
89 if (pairItr != m_names.end()) {
90 std::vector<std::string>& sources = std::get<NamePrefixList::NamePairIndex::SOURCES>(*pairItr);
91 auto sourceItr = getSource(source, pairItr);
92 if (sourceItr != sources.end()) {
93 sources.erase(sourceItr);
94 if (sources.size() == 0) {
95 m_names.erase(pairItr);
96 }
97 return true;
98 }
99 }
alvy297f4162015-03-03 17:15:33 -0600100 return false;
akmhoque53353462014-04-22 08:43:45 -0500101}
102
Nick Gordon56d1fae2017-05-26 16:39:25 -0500103bool
104NamePrefixList::operator==(const NamePrefixList& other) const
105{
Nick Gordonf14ec352017-07-24 16:09:58 -0500106 return m_names == other.m_names;
Nick Gordon56d1fae2017-05-26 16:39:25 -0500107}
108
akmhoque53353462014-04-22 08:43:45 -0500109void
akmhoquec8a10f72014-04-25 18:42:55 -0500110NamePrefixList::sort()
akmhoque53353462014-04-22 08:43:45 -0500111{
Nick Gordonf14ec352017-07-24 16:09:58 -0500112 std::sort(m_names.begin(), m_names.end());
113}
114
115std::list<ndn::Name>
116NamePrefixList::getNames() const
117{
118 std::list<ndn::Name> names;
119 for (const auto& namePair : m_names) {
120 names.push_back(std::get<NamePrefixList::NamePairIndex::NAME>(namePair));
121 }
122 return names;
123}
124
125uint32_t
126NamePrefixList::countSources(const ndn::Name& name) const
127{
128 return getSources(name).size();
129}
130
131const std::vector<std::string>
132NamePrefixList::getSources(const ndn::Name& name) const
133{
134 auto it = std::find_if(m_names.begin(), m_names.end(),
135 [&] (const NamePrefixList::NamePair& pair) {
136 return name == std::get<NamePrefixList::NamePairIndex::NAME>(pair);
137 });
138 if (it != m_names.end()) {
139 return std::get<NamePrefixList::NamePairIndex::SOURCES>(*it);
140 }
141 else {
142 return std::vector<std::string>{};
143 }
akmhoque53353462014-04-22 08:43:45 -0500144}
145
Nick Gordon56d1fae2017-05-26 16:39:25 -0500146std::ostream&
147operator<<(std::ostream& os, const NamePrefixList& list) {
148 os << "Name prefix list: {\n";
Nick Gordonf14ec352017-07-24 16:09:58 -0500149 for (const auto& name : list.getNames()) {
150 os << name << "\n"
151 << "Sources:\n";
152 for (const auto& source : list.getSources(name)) {
153 os << " " << source << "\n";
154 }
Nick Gordon56d1fae2017-05-26 16:39:25 -0500155 }
156 os << "}" << std::endl;
157 return os;
158}
159
Nick Gordonfad8e252016-08-11 14:21:38 -0500160} // namespace nlsr