A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
candidate-queue.cc
Go to the documentation of this file.
1/*
2 * Copyright 2007 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#include "candidate-queue.h"
8
10
11#include "ns3/assert.h"
12#include "ns3/log.h"
13
14#include <algorithm>
15#include <iostream>
16
17namespace ns3
18{
19
20NS_LOG_COMPONENT_DEFINE("CandidateQueue");
21
22/**
23 * @brief Stream insertion operator.
24 *
25 * @param os the reference to the output stream
26 * @param t the SPFVertex type
27 * @returns the reference to the output stream
28 */
29template <typename T>
30std::ostream&
31operator<<(std::ostream& os, const typename SPFVertex<T>::VertexType& t)
32{
33 switch (t)
34 {
36 os << "router";
37 break;
39 os << "network";
40 break;
41 default:
42 os << "unknown";
43 break;
44 };
45 return os;
46}
47
48template <typename T>
49std::ostream&
50operator<<(std::ostream& os, const CandidateQueue<T>& q)
51{
52 const typename CandidateQueue<T>::CandidateList_t& list = q.m_candidates;
53
54 os << "*** CandidateQueue Begin (<id, distance, LSA-type>) ***" << std::endl;
55 for (auto iter = list.begin(); iter != list.end(); iter++)
56 {
57 os << "<" << (*iter)->GetVertexId() << ", " << (*iter)->GetDistanceFromRoot() << ", "
58 << (*iter)->GetVertexType() << ">" << std::endl;
59 }
60 os << "*** CandidateQueue End ***";
61 return os;
62}
63
64template <typename T>
70
71template <typename T>
77
78template <typename T>
79void
81{
82 NS_LOG_FUNCTION(this);
83 while (!m_candidates.empty())
84 {
85 SPFVertex<T>* p = Pop();
86 delete p;
87 p = nullptr;
88 }
89}
90
91template <typename T>
92void
94{
95 NS_LOG_FUNCTION(this << vNew);
96
97 auto i = std::upper_bound(m_candidates.begin(),
98 m_candidates.end(),
99 vNew,
101 m_candidates.insert(i, vNew);
102}
103
104template <typename T>
107{
108 NS_LOG_FUNCTION(this);
109 if (m_candidates.empty())
110 {
111 return nullptr;
112 }
113
114 SPFVertex<T>* v = m_candidates.front();
115 m_candidates.pop_front();
116 return v;
117}
118
119template <typename T>
122{
123 NS_LOG_FUNCTION(this);
124 if (m_candidates.empty())
125 {
126 return nullptr;
127 }
128
129 return m_candidates.front();
130}
131
132template <typename T>
133bool
135{
136 NS_LOG_FUNCTION(this);
137 return m_candidates.empty();
138}
139
140template <typename T>
143{
144 NS_LOG_FUNCTION(this);
145 return m_candidates.size();
146}
147
148template <typename T>
151{
152 NS_LOG_FUNCTION(this);
153 auto i = m_candidates.begin();
154
155 for (; i != m_candidates.end(); i++)
156 {
157 SPFVertex<T>* v = *i;
158 if (v->GetVertexId() == addr)
159 {
160 return v;
161 }
162 }
163
164 return nullptr;
165}
166
167template <typename T>
168void
170{
171 NS_LOG_FUNCTION(this);
172
174 NS_LOG_LOGIC("After reordering the CandidateQueue");
175 NS_LOG_LOGIC(*this);
176}
177
178/*
179 * In this implementation, SPFVertex follows the ordering where
180 * a vertex is ranked first if its GetDistanceFromRoot () is smaller;
181 * In case of a tie, NetworkLSA is always ranked before RouterLSA.
182 *
183 * This ordering is necessary for implementing ECMP
184 */
185template <typename T>
186bool
188{
189 NS_LOG_FUNCTION(&v1 << &v2);
190
191 bool result = false;
193 {
194 result = true;
195 }
196 else if (v1->GetDistanceFromRoot() == v2->GetDistanceFromRoot())
197 {
200 {
201 result = true;
202 }
203 }
204 return result;
205}
206template class CandidateQueue<Ipv4Manager>;
207template class CandidateQueue<Ipv6Manager>;
208
209} // namespace ns3
uint32_t q
uint32_t v
return result
A Candidate Queue used in routing calculations.
void Push(SPFVertex< T > *vNew)
Push a Shortest Path First Vertex pointer onto the queue according to the priority scheme.
CandidateList_t m_candidates
SPFVertex candidates.
SPFVertex< T > * Top() const
Return the Shortest Path First Vertex pointer at the top of the queue.
SPFVertex< T > * Find(const IpAddress 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.
virtual ~CandidateQueue()
Destroy an SPF Candidate Queue and release any resources held by the contents.
SPFVertex< T > * Pop()
Pop the Shortest Path First Vertex pointer at the top of the queue.
typename std::conditional_t< IsIpv4, Ipv4Address, Ipv6Address > IpAddress
Alias for Ipv4Address and Ipv6Address classes.
uint32_t Size() const
Return the number of Shortest Path First Vertex pointers presently stored in the Candidate Queue.
void Clear()
Empty the Candidate Queue and release all of the resources associated with the Shortest Path First Ve...
void Reorder()
Reorders the Candidate Queue according to the priority scheme.
std::list< SPFVertex< T > * > CandidateList_t
container of SPFVertex pointers
static bool CompareSPFVertex(const SPFVertex< T > *v1, const SPFVertex< T > *v2)
return true if v1 < v2
Vertex used in shortest path first (SPF) computations.
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.
uint32_t GetDistanceFromRoot() const
Get the distance from the root vertex to "this" SPFVertex object.
VertexType GetVertexType() const
Get the Vertex Type field of a SPFVertex object.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:274
#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:148
#define list