blob: eea7641ef252283e023236908cc305333bf3726e [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);
40 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 {
82 nameTreeEntry->eraseFibEntry(nameTreeEntry->getFibEntry());
83 m_nItems--;
84 }
Junxiao Shic1e12362014-01-24 20:03:26 -070085}
86
87void
88Fib::removeNextHopFromAllEntries(shared_ptr<Face> face)
89{
Haowei Yuane1079fc2014-03-08 14:41:25 -060090 for (NameTree::const_iterator it =
91 m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry); it != m_nameTree.end(); it++)
92 {
93 shared_ptr<fib::Entry> entry = it->getFibEntry();
Junxiao Shi40631842014-03-01 13:52:37 -070094 entry->removeNextHop(face);
HangZhangad4afd12014-03-01 11:03:08 +080095 }
Junxiao Shic1e12362014-01-24 20:03:26 -070096}
97
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080098} // namespace nfd