blob: e405889445363243c2c5cfc035c42f2f6ccfd940 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050019 **/
Nick Gordond0a7df32017-05-30 16:44:34 -050020
akmhoquefdbddb12014-05-02 18:35:19 -050021#ifndef NLSR_NEXTHOP_LIST_HPP
22#define NLSR_NEXTHOP_LIST_HPP
akmhoque53353462014-04-22 08:43:45 -050023
Nick Gordond0a7df32017-05-30 16:44:34 -050024#include "nexthop.hpp"
25#include "adjacent.hpp"
26
akmhoquefdbddb12014-05-02 18:35:19 -050027#include <ndn-cxx/face.hpp>
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080028#include <set>
akmhoque53353462014-04-22 08:43:45 -050029
akmhoque53353462014-04-22 08:43:45 -050030namespace nlsr {
31
Vince Lehmanef21d8e2015-04-01 15:59:39 -050032struct NextHopComparator {
33 bool
34 operator() (const NextHop& nh1, const NextHop& nh2) const {
35 if (nh1.getRouteCostAsAdjustedInteger() < nh2.getRouteCostAsAdjustedInteger()) {
36 return true;
37 }
38 else if (nh1.getRouteCostAsAdjustedInteger() == nh2.getRouteCostAsAdjustedInteger()) {
39 return nh1.getConnectingFaceUri() < nh2.getConnectingFaceUri();
40 }
41 else {
42 return false;
43 }
44 }
45};
46
akmhoquec8a10f72014-04-25 18:42:55 -050047class NexthopList
akmhoque53353462014-04-22 08:43:45 -050048{
49public:
akmhoquec8a10f72014-04-25 18:42:55 -050050 NexthopList()
akmhoque53353462014-04-22 08:43:45 -050051 {
52 }
53
akmhoquec8a10f72014-04-25 18:42:55 -050054 ~NexthopList()
akmhoque53353462014-04-22 08:43:45 -050055 {
56 }
akmhoquefdbddb12014-05-02 18:35:19 -050057
Nick G97e34942016-07-11 14:46:27 -050058 /*! \brief Adds a next hop to the list.
59 \param nh The next hop.
60
Nick Gordonb50e51b2016-07-22 16:05:57 -050061 Adds a next hop to this object. If the next hop is new it is
62 added. If the next hop already exists in the list then that next
63 hop's route cost is updated.
Nick G97e34942016-07-11 14:46:27 -050064 */
akmhoque53353462014-04-22 08:43:45 -050065 void
Vince Lehmanef21d8e2015-04-01 15:59:39 -050066 addNextHop(const NextHop& nh);
akmhoque53353462014-04-22 08:43:45 -050067
Vince Lehmanef21d8e2015-04-01 15:59:39 -050068 /*! \brief Remove a next hop from the Next Hop list
69 \param nh The NextHop we want to remove.
Nick G97e34942016-07-11 14:46:27 -050070
Vince Lehmanef21d8e2015-04-01 15:59:39 -050071 The next hop gets removed only if both next hop face and route cost are same.
Nick G97e34942016-07-11 14:46:27 -050072 */
akmhoque53353462014-04-22 08:43:45 -050073 void
Vince Lehmanef21d8e2015-04-01 15:59:39 -050074 removeNextHop(const NextHop& nh);
akmhoque53353462014-04-22 08:43:45 -050075
akmhoquefdbddb12014-05-02 18:35:19 -050076 size_t
Nick Gordonff9a6272017-10-12 13:38:29 -050077 size() const
Nick Gordonb50e51b2016-07-22 16:05:57 -050078 {
79 return m_nexthopList.size();
80 }
81
akmhoque53353462014-04-22 08:43:45 -050082 void
83 reset()
84 {
akmhoquefdbddb12014-05-02 18:35:19 -050085 m_nexthopList.clear();
akmhoque53353462014-04-22 08:43:45 -050086 }
87
laqinfan35731852017-08-08 06:17:39 -050088 const std::set<NextHop, NextHopComparator>&
89 getNextHops() const
akmhoque53353462014-04-22 08:43:45 -050090 {
91 return m_nexthopList;
92 }
93
Vince Lehmanef21d8e2015-04-01 15:59:39 -050094 typedef std::set<NextHop, NextHopComparator>::iterator iterator;
95 typedef std::set<NextHop, NextHopComparator>::const_iterator const_iterator;
Vince Lehman18841082014-08-19 17:15:24 -050096
97 iterator
98 begin()
99 {
100 return m_nexthopList.begin();
101 }
102
103 iterator
104 end()
105 {
106 return m_nexthopList.end();
107 }
108
Vince Lehman942eb7b2014-10-02 10:09:27 -0500109 const_iterator
110 cbegin() const
111 {
112 return m_nexthopList.begin();
113 }
114
115 const_iterator
116 cend() const
117 {
118 return m_nexthopList.end();
119 }
120
akmhoque674b0b12014-05-20 14:33:28 -0500121 void
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600122 writeLog() const;
akmhoque674b0b12014-05-20 14:33:28 -0500123
akmhoque53353462014-04-22 08:43:45 -0500124private:
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500125 std::set<NextHop, NextHopComparator> m_nexthopList;
akmhoque53353462014-04-22 08:43:45 -0500126};
127
Nick Gordonb50e51b2016-07-22 16:05:57 -0500128bool
129operator==(NexthopList& lhs, NexthopList& rhs);
130
131bool
132operator==(const NexthopList& lhs, const NexthopList& rhs);
133
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500134bool
135operator!=(const NexthopList& lhs, const NexthopList& rhs);
136
Nick Gordonb50e51b2016-07-22 16:05:57 -0500137std::ostream&
138operator<<(std::ostream& os, const NexthopList& nhl);
139
Nick Gordonfad8e252016-08-11 14:21:38 -0500140} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500141
Nick Gordond0a7df32017-05-30 16:44:34 -0500142#endif // NLSR_NEXTHOP_LIST_HPP