Obaid | 793401d | 2014-02-27 19:13:49 -0600 | [diff] [blame] | 1 | /* -*- 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 | |
| 9 | namespace ndn { |
| 10 | namespace nrd { |
| 11 | |
| 12 | Rib::Rib() |
| 13 | { |
| 14 | } |
| 15 | |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 16 | |
Obaid | 793401d | 2014-02-27 19:13:49 -0600 | [diff] [blame] | 17 | Rib::~Rib() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | static inline bool |
Obaid | 03f4dc9 | 2014-04-13 10:57:04 -0500 | [diff] [blame] | 22 | compareNameFaceProtocol(const PrefixRegOptions& opt1, const PrefixRegOptions& opt2) |
Obaid | 793401d | 2014-02-27 19:13:49 -0600 | [diff] [blame] | 23 | { |
Obaid | 03f4dc9 | 2014-04-13 10:57:04 -0500 | [diff] [blame] | 24 | return (opt1.getName() == opt2.getName() && |
| 25 | opt1.getFaceId() == opt2.getFaceId() && |
| 26 | opt1.getProtocol() == opt2.getProtocol()); |
Obaid | 793401d | 2014-02-27 19:13:49 -0600 | [diff] [blame] | 27 | } |
| 28 | |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 29 | |
Obaid | 793401d | 2014-02-27 19:13:49 -0600 | [diff] [blame] | 30 | Rib::const_iterator |
| 31 | Rib::find(const PrefixRegOptions& options) const |
| 32 | { |
| 33 | RibTable::const_iterator it = std::find_if(m_rib.begin(), m_rib.end(), |
Obaid | 03f4dc9 | 2014-04-13 10:57:04 -0500 | [diff] [blame] | 34 | bind(&compareNameFaceProtocol, _1, options)); |
Obaid | 793401d | 2014-02-27 19:13:49 -0600 | [diff] [blame] | 35 | if (it == m_rib.end()) |
| 36 | { |
| 37 | return end(); |
| 38 | } |
| 39 | else |
| 40 | return it; |
| 41 | } |
| 42 | |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 43 | |
Obaid | 793401d | 2014-02-27 19:13:49 -0600 | [diff] [blame] | 44 | void |
| 45 | Rib::insert(const PrefixRegOptions& options) |
| 46 | { |
Obaid | 03f4dc9 | 2014-04-13 10:57:04 -0500 | [diff] [blame] | 47 | RibTable::iterator it = std::find_if(m_rib.begin(), m_rib.end(), |
| 48 | bind(&compareNameFaceProtocol, _1, options)); |
Obaid | 793401d | 2014-02-27 19:13:49 -0600 | [diff] [blame] | 49 | if (it == m_rib.end()) |
| 50 | { |
| 51 | m_rib.push_front(options); |
| 52 | } |
Obaid | 03f4dc9 | 2014-04-13 10:57:04 -0500 | [diff] [blame] | 53 | 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 | } |
Obaid | 793401d | 2014-02-27 19:13:49 -0600 | [diff] [blame] | 61 | } |
| 62 | |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 63 | |
Obaid | 793401d | 2014-02-27 19:13:49 -0600 | [diff] [blame] | 64 | void |
| 65 | Rib::erase(const PrefixRegOptions& options) |
| 66 | { |
| 67 | RibTable::iterator it = std::find_if(m_rib.begin(), m_rib.end(), |
Obaid | 03f4dc9 | 2014-04-13 10:57:04 -0500 | [diff] [blame] | 68 | bind(&compareNameFaceProtocol, _1, options)); |
Obaid | 793401d | 2014-02-27 19:13:49 -0600 | [diff] [blame] | 69 | if (it != m_rib.end()) |
| 70 | { |
| 71 | m_rib.erase(it); |
| 72 | } |
| 73 | } |
| 74 | |
Obaid | 6871187 | 2014-04-08 03:16:40 -0500 | [diff] [blame] | 75 | void |
| 76 | Rib::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 | |
Obaid | 793401d | 2014-02-27 19:13:49 -0600 | [diff] [blame] | 89 | } // namespace nrd |
| 90 | } // namespace ndn |