A Discrete-Event Network Simulator
API
candidate-queue.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2007 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17 
18 #include "candidate-queue.h"
19 
21 
22 #include "ns3/assert.h"
23 #include "ns3/log.h"
24 
25 #include <algorithm>
26 #include <iostream>
27 
28 namespace ns3
29 {
30 
31 NS_LOG_COMPONENT_DEFINE("CandidateQueue");
32 
40 std::ostream&
41 operator<<(std::ostream& os, const SPFVertex::VertexType& t)
42 {
43  switch (t)
44  {
46  os << "router";
47  break;
49  os << "network";
50  break;
51  default:
52  os << "unknown";
53  break;
54  };
55  return os;
56 }
57 
58 std::ostream&
59 operator<<(std::ostream& os, const CandidateQueue& q)
60 {
62 
63  os << "*** CandidateQueue Begin (<id, distance, LSA-type>) ***" << std::endl;
64  for (auto iter = list.begin(); iter != list.end(); iter++)
65  {
66  os << "<" << (*iter)->GetVertexId() << ", " << (*iter)->GetDistanceFromRoot() << ", "
67  << (*iter)->GetVertexType() << ">" << std::endl;
68  }
69  os << "*** CandidateQueue End ***";
70  return os;
71 }
72 
74  : m_candidates()
75 {
76  NS_LOG_FUNCTION(this);
77 }
78 
80 {
81  NS_LOG_FUNCTION(this);
82  Clear();
83 }
84 
85 void
87 {
88  NS_LOG_FUNCTION(this);
89  while (!m_candidates.empty())
90  {
91  SPFVertex* p = Pop();
92  delete p;
93  p = nullptr;
94  }
95 }
96 
97 void
99 {
100  NS_LOG_FUNCTION(this << vNew);
101 
102  auto i = std::upper_bound(m_candidates.begin(),
103  m_candidates.end(),
104  vNew,
106  m_candidates.insert(i, vNew);
107 }
108 
109 SPFVertex*
111 {
112  NS_LOG_FUNCTION(this);
113  if (m_candidates.empty())
114  {
115  return nullptr;
116  }
117 
118  SPFVertex* v = m_candidates.front();
119  m_candidates.pop_front();
120  return v;
121 }
122 
123 SPFVertex*
125 {
126  NS_LOG_FUNCTION(this);
127  if (m_candidates.empty())
128  {
129  return nullptr;
130  }
131 
132  return m_candidates.front();
133 }
134 
135 bool
137 {
138  NS_LOG_FUNCTION(this);
139  return m_candidates.empty();
140 }
141 
142 uint32_t
144 {
145  NS_LOG_FUNCTION(this);
146  return m_candidates.size();
147 }
148 
149 SPFVertex*
151 {
152  NS_LOG_FUNCTION(this);
153  auto i = m_candidates.begin();
154 
155  for (; i != m_candidates.end(); i++)
156  {
157  SPFVertex* v = *i;
158  if (v->GetVertexId() == addr)
159  {
160  return v;
161  }
162  }
163 
164  return nullptr;
165 }
166 
167 void
169 {
170  NS_LOG_FUNCTION(this);
171 
173  NS_LOG_LOGIC("After reordering the CandidateQueue");
174  NS_LOG_LOGIC(*this);
175 }
176 
177 /*
178  * In this implementation, SPFVertex follows the ordering where
179  * a vertex is ranked first if its GetDistanceFromRoot () is smaller;
180  * In case of a tie, NetworkLSA is always ranked before RouterLSA.
181  *
182  * This ordering is necessary for implementing ECMP
183  */
184 bool
186 {
187  NS_LOG_FUNCTION(&v1 << &v2);
188 
189  bool result = false;
190  if (v1->GetDistanceFromRoot() < v2->GetDistanceFromRoot())
191  {
192  result = true;
193  }
194  else if (v1->GetDistanceFromRoot() == v2->GetDistanceFromRoot())
195  {
198  {
199  result = true;
200  }
201  }
202  return result;
203 }
204 
205 } // namespace ns3
A Candidate Queue used in routing calculations.
static bool CompareSPFVertex(const SPFVertex *v1, const SPFVertex *v2)
return true if v1 < v2
SPFVertex * Pop()
Pop the Shortest Path First Vertex pointer at the top of the queue.
SPFVertex * Top() const
Return the Shortest Path First Vertex pointer at the top of the queue.
uint32_t Size() const
Return the number of Shortest Path First Vertex pointers presently stored in the Candidate Queue.
void Push(SPFVertex *vNew)
Push a Shortest Path First Vertex pointer onto the queue according to the priority scheme.
void Reorder()
Reorders the Candidate Queue according to the priority scheme.
std::list< SPFVertex * > CandidateList_t
container of SPFVertex pointers
virtual ~CandidateQueue()
Destroy an SPF Candidate Queue and release any resources held by the contents.
CandidateList_t m_candidates
SPFVertex candidates.
void Clear()
Empty the Candidate Queue and release all of the resources associated with the Shortest Path First Ve...
SPFVertex * Find(const Ipv4Address addr) const
Searches the Candidate Queue for a Shortest Path First Vertex pointer that points to a vertex having ...
CandidateQueue()
Create an empty SPF Candidate Queue.
bool Empty() const
Test the Candidate Queue to determine if it is empty.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Vertex used in shortest path first (SPF) computations.
Ipv4Address GetVertexId() const
Get the Vertex ID field of a SPFVertex object.
VertexType
Enumeration of the possible types of SPFVertex objects.
@ VertexNetwork
Vertex representing a network in the topology.
@ VertexRouter
Vertex representing a router in the topology.
VertexType GetVertexType() const
Get the Vertex Type field of a SPFVertex object.
uint32_t GetDistanceFromRoot() const
Get the distance from the root vertex to "this" SPFVertex object.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
#define list