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 {
61  typedef CandidateQueue::CandidateList_t List_t;
62  typedef List_t::const_iterator CIter_t;
64 
65  os << "*** CandidateQueue Begin (<id, distance, LSA-type>) ***" << std::endl;
66  for (CIter_t iter = list.begin(); iter != list.end(); iter++)
67  {
68  os << "<" << (*iter)->GetVertexId() << ", " << (*iter)->GetDistanceFromRoot() << ", "
69  << (*iter)->GetVertexType() << ">" << std::endl;
70  }
71  os << "*** CandidateQueue End ***";
72  return os;
73 }
74 
76  : m_candidates()
77 {
78  NS_LOG_FUNCTION(this);
79 }
80 
82 {
83  NS_LOG_FUNCTION(this);
84  Clear();
85 }
86 
87 void
89 {
90  NS_LOG_FUNCTION(this);
91  while (!m_candidates.empty())
92  {
93  SPFVertex* p = Pop();
94  delete p;
95  p = nullptr;
96  }
97 }
98 
99 void
101 {
102  NS_LOG_FUNCTION(this << vNew);
103 
104  CandidateList_t::iterator i = std::upper_bound(m_candidates.begin(),
105  m_candidates.end(),
106  vNew,
108  m_candidates.insert(i, vNew);
109 }
110 
111 SPFVertex*
113 {
114  NS_LOG_FUNCTION(this);
115  if (m_candidates.empty())
116  {
117  return nullptr;
118  }
119 
120  SPFVertex* v = m_candidates.front();
121  m_candidates.pop_front();
122  return v;
123 }
124 
125 SPFVertex*
127 {
128  NS_LOG_FUNCTION(this);
129  if (m_candidates.empty())
130  {
131  return nullptr;
132  }
133 
134  return m_candidates.front();
135 }
136 
137 bool
139 {
140  NS_LOG_FUNCTION(this);
141  return m_candidates.empty();
142 }
143 
144 uint32_t
146 {
147  NS_LOG_FUNCTION(this);
148  return m_candidates.size();
149 }
150 
151 SPFVertex*
153 {
154  NS_LOG_FUNCTION(this);
155  CandidateList_t::const_iterator i = m_candidates.begin();
156 
157  for (; i != m_candidates.end(); i++)
158  {
159  SPFVertex* v = *i;
160  if (v->GetVertexId() == addr)
161  {
162  return v;
163  }
164  }
165 
166  return nullptr;
167 }
168 
169 void
171 {
172  NS_LOG_FUNCTION(this);
173 
175  NS_LOG_LOGIC("After reordering the CandidateQueue");
176  NS_LOG_LOGIC(*this);
177 }
178 
179 /*
180  * In this implementation, SPFVertex follows the ordering where
181  * a vertex is ranked first if its GetDistanceFromRoot () is smaller;
182  * In case of a tie, NetworkLSA is always ranked before RouterLSA.
183  *
184  * This ordering is necessary for implementing ECMP
185  */
186 bool
188 {
189  NS_LOG_FUNCTION(&v1 << &v2);
190 
191  bool result = false;
192  if (v1->GetDistanceFromRoot() < v2->GetDistanceFromRoot())
193  {
194  result = true;
195  }
196  else if (v1->GetDistanceFromRoot() == v2->GetDistanceFromRoot())
197  {
200  {
201  result = true;
202  }
203  }
204  return result;
205 }
206 
207 } // 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:43
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:129
#define list