blob: 7397c60a76f5006a3853c8f819be41c38fae9eec [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 {
Junxiao Shi40631842014-03-01 13:52:37 -070012
13const shared_ptr<fib::Entry> Fib::m_emptyEntry = make_shared<fib::Entry>(Name());
14
HangZhangad4afd12014-03-01 11:03:08 +080015Fib::Fib(NameTree& nameTree)
16 : m_nameTree(nameTree)
17 , m_nItems(0)
Junxiao Shic1e12362014-01-24 20:03:26 -070018{
Junxiao Shic1e12362014-01-24 20:03:26 -070019}
20
21Fib::~Fib()
22{
23}
24
Junxiao Shi40631842014-03-01 13:52:37 -070025static inline bool
26predicate_NameTreeEntry_hasFibEntry(const name_tree::Entry& entry)
27{
28 return static_cast<bool>(entry.getFibEntry());
29}
30
Junxiao Shic1e12362014-01-24 20:03:26 -070031std::pair<shared_ptr<fib::Entry>, bool>
32Fib::insert(const Name& prefix)
33{
HangZhangad4afd12014-03-01 11:03:08 +080034 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix);
35 shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry();
36 if (static_cast<bool>(entry))
37 return std::make_pair(entry, false);
38 entry = make_shared<fib::Entry>(prefix);
39 nameTreeEntry->setFibEntry(entry);
Junxiao Shiefceadc2014-03-09 18:52:57 -070040 ++m_nItems;
Junxiao Shic1e12362014-01-24 20:03:26 -070041 return std::make_pair(entry, true);
42}
43
Junxiao Shic1e12362014-01-24 20:03:26 -070044shared_ptr<fib::Entry>
45Fib::findLongestPrefixMatch(const Name& prefix) const
46{
Junxiao Shi40631842014-03-01 13:52:37 -070047 shared_ptr<name_tree::Entry> nameTreeEntry =
48 m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasFibEntry);
49 if (static_cast<bool>(nameTreeEntry)) {
50 return nameTreeEntry->getFibEntry();
HangZhangad4afd12014-03-01 11:03:08 +080051 }
Junxiao Shi40631842014-03-01 13:52:37 -070052 return m_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -070053}
54
Steve DiBenedettod5f87932014-02-05 15:11:39 -070055shared_ptr<fib::Entry>
Junxiao Shidbe71732014-02-21 22:23:28 -070056Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const
57{
58 return this->findLongestPrefixMatch(pitEntry.getName());
59}
60
61shared_ptr<fib::Entry>
62Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
63{
64 return this->findLongestPrefixMatch(measurementsEntry.getName());
65}
66
67shared_ptr<fib::Entry>
Steve DiBenedettod5f87932014-02-05 15:11:39 -070068Fib::findExactMatch(const Name& prefix) const
69{
HangZhangad4afd12014-03-01 11:03:08 +080070 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
71 if (static_cast<bool>(nameTreeEntry))
72 return nameTreeEntry->getFibEntry();
Steve DiBenedettod5f87932014-02-05 15:11:39 -070073 return shared_ptr<fib::Entry>();
74}
75
76void
HangZhangad4afd12014-03-01 11:03:08 +080077Fib::erase(const Name& prefix)
Steve DiBenedettod5f87932014-02-05 15:11:39 -070078{
HangZhangad4afd12014-03-01 11:03:08 +080079 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
80 if (static_cast<bool>(nameTreeEntry))
81 {
Junxiao Shiefceadc2014-03-09 18:52:57 -070082 nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>());
83 --m_nItems;
84 }
85}
86
87void
88Fib::erase(const fib::Entry& entry)
89{
90 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(entry);
91 if (static_cast<bool>(nameTreeEntry))
92 {
93 nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>());
94 --m_nItems;
HangZhangad4afd12014-03-01 11:03:08 +080095 }
Junxiao Shic1e12362014-01-24 20:03:26 -070096}
97
98void
99Fib::removeNextHopFromAllEntries(shared_ptr<Face> face)
100{
Junxiao Shiefceadc2014-03-09 18:52:57 -0700101 for (NameTree::const_iterator it = m_nameTree.fullEnumerate(
102 &predicate_NameTreeEntry_hasFibEntry); it != m_nameTree.end(); ++it) {
Haowei Yuane1079fc2014-03-08 14:41:25 -0600103 shared_ptr<fib::Entry> entry = it->getFibEntry();
Junxiao Shi40631842014-03-01 13:52:37 -0700104 entry->removeNextHop(face);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700105 if (!entry->hasNextHops()) {
106 this->erase(*entry);
107 }
HangZhangad4afd12014-03-01 11:03:08 +0800108 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700109}
110
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800111} // namespace nfd