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_ABS_FIB_H |
| 22 | #define LFID_ABS_FIB_H |
| 23 | |
| 24 | #include <set> |
| 25 | #include <unordered_map> |
| 26 | |
| 27 | #include "ns3/abort.h" |
| 28 | #include "ns3/ndnSIM/helper/lfid/fib-nexthop.hpp" |
| 29 | #include "ns3/ptr.h" |
| 30 | |
| 31 | namespace ns3 { |
| 32 | namespace ndn { |
| 33 | |
| 34 | class GlobalRouter; |
| 35 | |
| 36 | /** |
| 37 | * An abstract, lightweight representation of the FIB. |
| 38 | */ |
| 39 | class AbstractFib { |
| 40 | public: |
| 41 | using AllNodeFib = std::unordered_map<int, AbstractFib>; |
| 42 | |
| 43 | /** |
| 44 | * @param own The GlobalRouter object representing the current router |
| 45 | * @param numNodes The total number of nodes in the network |
| 46 | */ |
| 47 | AbstractFib(const Ptr<GlobalRouter> own, int numNodes); |
| 48 | |
| 49 | public: |
| 50 | // Getters: |
| 51 | /** |
| 52 | * @return Return set of nexthops per destination |
| 53 | */ |
| 54 | const std::set<FibNextHop>& |
| 55 | getNexthops(int dstId) const; |
| 56 | |
| 57 | /** |
| 58 | * @return Return set of upward nexthops per destination |
| 59 | */ |
| 60 | const std::set<FibNextHop>& |
| 61 | getUpwardNexthops(int dstId) const; |
| 62 | |
| 63 | /** |
| 64 | * Make sure that FIB is consistent (each destination has at least one downward nexthop) |
| 65 | */ |
| 66 | void |
| 67 | checkFib() const; |
| 68 | |
| 69 | /** |
| 70 | * @return Return number nexthops per destination |
| 71 | * @pre Also assure that the destination is not equal to the current nodeId. |
| 72 | */ |
| 73 | int |
| 74 | numEnabledNhPerDst(int dstId) const |
| 75 | { |
| 76 | NS_ABORT_UNLESS(dstId != nodeId); |
| 77 | return static_cast<int>(getNexthops(dstId).size()); |
| 78 | } |
| 79 | |
| 80 | int |
| 81 | getNodeId() const |
| 82 | { |
| 83 | return nodeId; |
| 84 | } |
| 85 | |
| 86 | Ptr<GlobalRouter> |
| 87 | getGR() const; |
| 88 | |
| 89 | std::string |
| 90 | getName() const |
| 91 | { |
| 92 | return nodeName; |
| 93 | } |
| 94 | |
| 95 | int |
| 96 | getDegree() const |
| 97 | { |
| 98 | return nodeDegree; |
| 99 | } |
| 100 | |
| 101 | int |
| 102 | getNumDsts() const |
| 103 | { |
| 104 | return static_cast<int>(perDstFib.size()); |
| 105 | } |
| 106 | |
| 107 | bool |
| 108 | contains(int dstId) const |
| 109 | { |
| 110 | return perDstFib.count(dstId) > 0; |
| 111 | } |
| 112 | |
| 113 | // Functions for range-based loop: |
| 114 | auto |
| 115 | begin() const |
| 116 | { |
| 117 | return perDstFib.cbegin(); |
| 118 | } |
| 119 | |
| 120 | auto |
| 121 | end() const |
| 122 | { |
| 123 | return perDstFib.cend(); |
| 124 | } |
| 125 | |
| 126 | // Setters: |
| 127 | void |
| 128 | insert(int dstId, const FibNextHop& nh); |
| 129 | |
| 130 | size_t |
| 131 | erase(int dstId, int nhId); |
| 132 | |
| 133 | private: |
| 134 | void |
| 135 | checkInputs(); |
| 136 | |
| 137 | void |
| 138 | createEmptyFib(); |
| 139 | |
| 140 | private: |
| 141 | const int nodeId; // Own node id |
| 142 | const std::string nodeName; // Own node name |
| 143 | const int numberOfNodes; |
| 144 | const int nodeDegree; |
| 145 | const Ptr<GlobalRouter> ownRouter; |
| 146 | |
| 147 | int upwardCounter; |
| 148 | int totalNhCounter; |
| 149 | |
| 150 | // DstId -> set<FibNextHop> |
| 151 | std::unordered_map<int, std::set<FibNextHop>> perDstFib; |
| 152 | std::unordered_map<int, std::set<FibNextHop>> upwardPerDstFib; |
| 153 | |
| 154 | friend std::ostream& |
| 155 | operator<<(std::ostream&, const AbstractFib& fib); |
| 156 | }; |
| 157 | |
| 158 | std::ostream& |
| 159 | operator<<(std::ostream& os, const AbstractFib& fib); |
| 160 | |
| 161 | } // namespace ndn |
| 162 | } // namespace ns-3 |
| 163 | |
| 164 | #endif // LFID_ABS_FIB_H |