akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | c6a8522 | 2017-01-03 16:54:34 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 19 | **/ |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 20 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 21 | #ifndef NLSR_NEXTHOP_LIST_HPP |
| 22 | #define NLSR_NEXTHOP_LIST_HPP |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 23 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 24 | #include "nexthop.hpp" |
| 25 | #include "adjacent.hpp" |
| 26 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 27 | #include <set> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 28 | #include <iostream> |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 29 | #include <boost/cstdint.hpp> |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 30 | #include <ndn-cxx/face.hpp> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | namespace nlsr { |
| 33 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 34 | struct NextHopComparator { |
| 35 | bool |
| 36 | operator() (const NextHop& nh1, const NextHop& nh2) const { |
| 37 | if (nh1.getRouteCostAsAdjustedInteger() < nh2.getRouteCostAsAdjustedInteger()) { |
| 38 | return true; |
| 39 | } |
| 40 | else if (nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) { |
| 41 | return nh1.getConnectingFaceUri() < nh2.getConnectingFaceUri(); |
| 42 | } |
| 43 | else { |
| 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | }; |
| 48 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 49 | class NexthopList |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 50 | { |
| 51 | public: |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 52 | NexthopList() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 53 | { |
| 54 | } |
| 55 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 56 | ~NexthopList() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 57 | { |
| 58 | } |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 59 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 60 | /*! \brief Adds a next hop to the list. |
| 61 | \param nh The next hop. |
| 62 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 63 | Adds a next hop to this object. If the next hop is new it is |
| 64 | added. If the next hop already exists in the list then that next |
| 65 | hop's route cost is updated. |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 66 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 67 | void |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 68 | addNextHop(const NextHop& nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 69 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 70 | /*! \brief Remove a next hop from the Next Hop list |
| 71 | \param nh The NextHop we want to remove. |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 72 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 73 | The next hop gets removed only if both next hop face and route cost are same. |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 74 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 75 | void |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 76 | removeNextHop(const NextHop& nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 77 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 78 | size_t |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame^] | 79 | size() const |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 80 | { |
| 81 | return m_nexthopList.size(); |
| 82 | } |
| 83 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 84 | void |
| 85 | reset() |
| 86 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 87 | m_nexthopList.clear(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 90 | std::set<NextHop, NextHopComparator>& |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 91 | getNextHops() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 92 | { |
| 93 | return m_nexthopList; |
| 94 | } |
| 95 | |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 96 | typedef std::set<NextHop, NextHopComparator>::iterator iterator; |
| 97 | typedef std::set<NextHop, NextHopComparator>::const_iterator const_iterator; |
Vince Lehman | 1884108 | 2014-08-19 17:15:24 -0500 | [diff] [blame] | 98 | |
| 99 | iterator |
| 100 | begin() |
| 101 | { |
| 102 | return m_nexthopList.begin(); |
| 103 | } |
| 104 | |
| 105 | iterator |
| 106 | end() |
| 107 | { |
| 108 | return m_nexthopList.end(); |
| 109 | } |
| 110 | |
Vince Lehman | 942eb7b | 2014-10-02 10:09:27 -0500 | [diff] [blame] | 111 | const_iterator |
| 112 | cbegin() const |
| 113 | { |
| 114 | return m_nexthopList.begin(); |
| 115 | } |
| 116 | |
| 117 | const_iterator |
| 118 | cend() const |
| 119 | { |
| 120 | return m_nexthopList.end(); |
| 121 | } |
| 122 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 123 | void |
| 124 | writeLog(); |
| 125 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 126 | private: |
Vince Lehman | ef21d8e | 2015-04-01 15:59:39 -0500 | [diff] [blame] | 127 | std::set<NextHop, NextHopComparator> m_nexthopList; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 128 | }; |
| 129 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 130 | bool |
| 131 | operator==(NexthopList& lhs, NexthopList& rhs); |
| 132 | |
| 133 | bool |
| 134 | operator==(const NexthopList& lhs, const NexthopList& rhs); |
| 135 | |
Nick Gordon | c0c6bcf | 2017-08-15 18:11:21 -0500 | [diff] [blame] | 136 | bool |
| 137 | operator!=(const NexthopList& lhs, const NexthopList& rhs); |
| 138 | |
Nick Gordon | b50e51b | 2016-07-22 16:05:57 -0500 | [diff] [blame] | 139 | std::ostream& |
| 140 | operator<<(std::ostream& os, const NexthopList& nhl); |
| 141 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 142 | } // namespace nlsr |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 143 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 144 | #endif // NLSR_NEXTHOP_LIST_HPP |