blob: 9c7a61125de989933ef76d8199f1bdae6de2d6df [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "fib.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -07008#include "pit-entry.hpp"
9#include "measurements-entry.hpp"
Junxiao Shic1e12362014-01-24 20:03:26 -070010
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080011namespace nfd {
HangZhangad4afd12014-03-01 11:03:08 +080012Fib::Fib(NameTree& nameTree)
13 : m_nameTree(nameTree)
14 , m_nItems(0)
Junxiao Shic1e12362014-01-24 20:03:26 -070015{
HangZhangad4afd12014-03-01 11:03:08 +080016 m_rootEntry = (this->insert(Name())).first;
Junxiao Shic1e12362014-01-24 20:03:26 -070017}
18
19Fib::~Fib()
20{
21}
22
Junxiao Shic1e12362014-01-24 20:03:26 -070023std::pair<shared_ptr<fib::Entry>, bool>
24Fib::insert(const Name& prefix)
25{
HangZhangad4afd12014-03-01 11:03:08 +080026 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix);
27 shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry();
28 if (static_cast<bool>(entry))
29 return std::make_pair(entry, false);
30 entry = make_shared<fib::Entry>(prefix);
31 nameTreeEntry->setFibEntry(entry);
32 m_nItems++;
Junxiao Shic1e12362014-01-24 20:03:26 -070033 return std::make_pair(entry, true);
34}
35
Junxiao Shic1e12362014-01-24 20:03:26 -070036shared_ptr<fib::Entry>
37Fib::findLongestPrefixMatch(const Name& prefix) const
38{
HangZhangad4afd12014-03-01 11:03:08 +080039 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findLongestPrefixMatch(prefix);
40 while (static_cast<bool>(nameTreeEntry))
41 {
42 if (static_cast<bool>(nameTreeEntry->getFibEntry()))
43 return nameTreeEntry->getFibEntry();
44 else
45 nameTreeEntry = nameTreeEntry->getParent();
46 }
47 return m_rootEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -070048}
49
Steve DiBenedettod5f87932014-02-05 15:11:39 -070050shared_ptr<fib::Entry>
Junxiao Shidbe71732014-02-21 22:23:28 -070051Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const
52{
53 return this->findLongestPrefixMatch(pitEntry.getName());
54}
55
56shared_ptr<fib::Entry>
57Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
58{
59 return this->findLongestPrefixMatch(measurementsEntry.getName());
60}
61
62shared_ptr<fib::Entry>
Steve DiBenedettod5f87932014-02-05 15:11:39 -070063Fib::findExactMatch(const Name& prefix) const
64{
HangZhangad4afd12014-03-01 11:03:08 +080065 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
66 if (static_cast<bool>(nameTreeEntry))
67 return nameTreeEntry->getFibEntry();
Steve DiBenedettod5f87932014-02-05 15:11:39 -070068 return shared_ptr<fib::Entry>();
69}
70
71void
HangZhangad4afd12014-03-01 11:03:08 +080072Fib::erase(const Name& prefix)
Steve DiBenedettod5f87932014-02-05 15:11:39 -070073{
HangZhangad4afd12014-03-01 11:03:08 +080074 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
75 if (static_cast<bool>(nameTreeEntry))
76 {
77 nameTreeEntry->eraseFibEntry(nameTreeEntry->getFibEntry());
78 m_nItems--;
79 }
Junxiao Shic1e12362014-01-24 20:03:26 -070080}
81
82void
83Fib::removeNextHopFromAllEntries(shared_ptr<Face> face)
84{
HangZhangad4afd12014-03-01 11:03:08 +080085 shared_ptr<fib::Entry> entry;
86 shared_ptr<std::vector<shared_ptr<name_tree::Entry > > > res = m_nameTree.fullEnumerate();
87 for (int i = 0; i < res->size(); i++)
88 {
89 entry = (*res)[i]->getFibEntry();
90 if (static_cast<bool>(entry))
91 entry->removeNextHop(face);
92 }
Junxiao Shic1e12362014-01-24 20:03:26 -070093}
94
95
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080096} // namespace nfd