blob: 4f1387b97d76d92563d23eb0b5671d0d031c88fc [file] [log] [blame]
Obaid793401d2014-02-27 19:13:49 -06001/* -*- 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 "rib.hpp"
8
9namespace ndn {
10namespace nrd {
11
12Rib::Rib()
13{
14}
15
Obaid3f48fe52014-02-27 21:45:23 -060016
Obaid793401d2014-02-27 19:13:49 -060017Rib::~Rib()
18{
19}
20
21static inline bool
Obaid03f4dc92014-04-13 10:57:04 -050022compareNameFaceProtocol(const PrefixRegOptions& opt1, const PrefixRegOptions& opt2)
Obaid793401d2014-02-27 19:13:49 -060023{
Obaid03f4dc92014-04-13 10:57:04 -050024 return (opt1.getName() == opt2.getName() &&
25 opt1.getFaceId() == opt2.getFaceId() &&
26 opt1.getProtocol() == opt2.getProtocol());
Obaid793401d2014-02-27 19:13:49 -060027}
28
Obaid3f48fe52014-02-27 21:45:23 -060029
Obaid793401d2014-02-27 19:13:49 -060030Rib::const_iterator
31Rib::find(const PrefixRegOptions& options) const
32{
33 RibTable::const_iterator it = std::find_if(m_rib.begin(), m_rib.end(),
Obaid03f4dc92014-04-13 10:57:04 -050034 bind(&compareNameFaceProtocol, _1, options));
Obaid793401d2014-02-27 19:13:49 -060035 if (it == m_rib.end())
36 {
37 return end();
38 }
39 else
40 return it;
41}
42
Obaid3f48fe52014-02-27 21:45:23 -060043
Obaid793401d2014-02-27 19:13:49 -060044void
45Rib::insert(const PrefixRegOptions& options)
46{
Obaid03f4dc92014-04-13 10:57:04 -050047 RibTable::iterator it = std::find_if(m_rib.begin(), m_rib.end(),
48 bind(&compareNameFaceProtocol, _1, options));
Obaid793401d2014-02-27 19:13:49 -060049 if (it == m_rib.end())
50 {
51 m_rib.push_front(options);
52 }
Obaid03f4dc92014-04-13 10:57:04 -050053 else
54 {
55 //entry exist, update other fields
56 it->setFlags(options.getFlags());
57 it->setCost(options.getCost());
58 it->setExpirationPeriod(options.getExpirationPeriod());
59 it->setProtocol(options.getProtocol());
60 }
Obaid793401d2014-02-27 19:13:49 -060061}
62
Obaid3f48fe52014-02-27 21:45:23 -060063
Obaid793401d2014-02-27 19:13:49 -060064void
65Rib::erase(const PrefixRegOptions& options)
66{
67 RibTable::iterator it = std::find_if(m_rib.begin(), m_rib.end(),
Obaid03f4dc92014-04-13 10:57:04 -050068 bind(&compareNameFaceProtocol, _1, options));
Obaid793401d2014-02-27 19:13:49 -060069 if (it != m_rib.end())
70 {
71 m_rib.erase(it);
72 }
73}
74
Obaid68711872014-04-08 03:16:40 -050075void
76Rib::erase(uint64_t faceId)
77{
78 //Keep it simple for now, with Trie this will be changed.
79 RibTable::iterator it = m_rib.begin();
80 while (it != m_rib.end())
81 {
82 if (it->getFaceId() == faceId)
83 it = m_rib.erase(it);
84 else
85 ++it;
86 }
87}
88
Obaid793401d2014-02-27 19:13:49 -060089} // namespace nrd
90} // namespace ndn