Klaus Schneider | 3878430 | 2019-08-31 18:26:36 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2019 Klaus Schneider, The University of Arizona |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Klaus Schneider <klaus@cs.arizona.edu> |
| 19 | */ |
| 20 | |
| 21 | #ifndef LFID_FIB_NH_H |
| 22 | #define LFID_FIB_NH_H |
| 23 | |
| 24 | #include <iosfwd> |
| 25 | #include <unordered_set> |
| 26 | |
| 27 | #include "ns3/abort.h" |
| 28 | |
| 29 | namespace ns3 { |
| 30 | namespace ndn { |
| 31 | |
| 32 | enum class NextHopType { DOWNWARD, |
| 33 | UPWARD, |
| 34 | DISABLED }; |
| 35 | |
| 36 | class FibNextHop { |
| 37 | public: |
| 38 | static constexpr int MAX_COST = 1 * 1000 * 1000; |
| 39 | |
| 40 | /** |
| 41 | * @param nhId The nexthop id. |
| 42 | * @param cost The link cost of a nexthop. has to be larger than 0! |
| 43 | * @param costDelta The cost difference (relative to the shortest path nexthop) |
| 44 | * @param type The nexthop type @sa NextHopType |
| 45 | */ |
| 46 | FibNextHop(int cost, int nhId, int costDelta = -1, NextHopType type = NextHopType::DISABLED); |
| 47 | |
| 48 | // Getters |
| 49 | int |
| 50 | getNexthopId() const |
| 51 | { |
| 52 | return m_nhId; |
| 53 | } |
| 54 | |
| 55 | int |
| 56 | getCost() const |
| 57 | { |
| 58 | return m_cost; |
| 59 | } |
| 60 | |
| 61 | int |
| 62 | getCostDelta() const |
| 63 | { |
| 64 | return m_costDelta; |
| 65 | } |
| 66 | |
| 67 | NextHopType |
| 68 | getType() const |
| 69 | { |
| 70 | return m_type; |
| 71 | } |
| 72 | |
| 73 | // Setters: |
| 74 | void |
| 75 | setType(const NextHopType& newType) |
| 76 | { |
| 77 | NS_ABORT_UNLESS(newType != NextHopType::DISABLED); |
| 78 | this->m_type = newType; |
| 79 | } |
| 80 | |
| 81 | // Only used in old fillFib: |
| 82 | void |
| 83 | setCost(int newCost, int newCostDelta) |
| 84 | { |
| 85 | NS_ABORT_UNLESS(newCost > 0); |
| 86 | NS_ABORT_UNLESS(newCostDelta >= 0); |
| 87 | this->m_cost = newCost; |
| 88 | this->m_costDelta = newCostDelta; |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | // Order of FibNexthop: |
| 93 | friend bool |
| 94 | operator<(const FibNextHop& own, const FibNextHop& other) |
| 95 | { |
| 96 | NS_ABORT_UNLESS(own.m_nhId != -1); |
| 97 | |
| 98 | return std::tie(own.m_costDelta, own.m_cost, own.m_nhId) |
| 99 | < std::tie(other.m_costDelta, other.m_cost, other.m_nhId); |
| 100 | } |
| 101 | |
| 102 | friend bool |
| 103 | operator==(const FibNextHop& own, const FibNextHop& other) |
| 104 | { |
| 105 | if (other.m_nhId == own.m_nhId) { |
| 106 | // Check that there are no FibNextHop with identical id, but different cost. |
| 107 | NS_ABORT_UNLESS(other.m_cost == own.m_cost); |
| 108 | NS_ABORT_UNLESS(other.m_costDelta == own.m_costDelta); |
| 109 | return true; |
| 110 | } |
| 111 | else { |
| 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | friend bool |
| 117 | operator!=(const FibNextHop& own, const FibNextHop& other) |
| 118 | { |
| 119 | return !(own == other); |
| 120 | } |
| 121 | |
| 122 | friend std::ostream& |
| 123 | operator<<(std::ostream&, const FibNextHop& fib); |
| 124 | |
| 125 | private: |
| 126 | int m_cost; |
| 127 | int m_nhId; |
| 128 | NextHopType m_type; |
| 129 | int m_costDelta; |
| 130 | }; |
| 131 | |
| 132 | std::ostream& |
| 133 | operator<<(std::ostream& os, const NextHopType& type); |
| 134 | std::ostream& |
| 135 | operator<<(std::ostream& os, const FibNextHop& a); |
| 136 | |
| 137 | } // namespace ndn |
| 138 | } // namespace ns-3 |
| 139 | |
| 140 | namespace std { |
| 141 | template <> |
| 142 | struct hash<ns3::ndn::FibNextHop>; |
| 143 | } |
| 144 | |
| 145 | #endif // LFID_FIB_NH_H |