A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
candidate-queue.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright 2007 University of Washington
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*/
18
19
#include <algorithm>
20
#include <iostream>
21
#include "ns3/log.h"
22
#include "ns3/assert.h"
23
#include "
candidate-queue.h
"
24
#include "
global-route-manager-impl.h
"
25
26
NS_LOG_COMPONENT_DEFINE
(
"CandidateQueue"
);
27
28
namespace
ns3 {
29
30
std::ostream&
31
operator<<
(std::ostream& os,
const
SPFVertex::VertexType
& t)
32
{
33
switch
(t)
34
{
35
case
SPFVertex::VertexRouter
: os <<
"router"
;
break
;
36
case
SPFVertex::VertexNetwork
: os <<
"network"
;
break
;
37
default
: os <<
"unknown"
;
break
;
38
};
39
return
os;
40
}
41
42
std::ostream&
43
operator<<
(std::ostream& os,
const
CandidateQueue
& q)
44
{
45
typedef
CandidateQueue::CandidateList_t
List_t;
46
typedef
List_t::const_iterator CIter_t;
47
const
CandidateQueue::CandidateList_t
&
list
= q.
m_candidates
;
48
49
os <<
"*** CandidateQueue Begin (<id, distance, LSA-type>) ***"
<< std::endl;
50
for
(CIter_t iter = list.begin (); iter != list.end (); iter++)
51
{
52
os <<
"<"
53
<< (*iter)->GetVertexId () <<
", "
54
<< (*iter)->GetDistanceFromRoot () <<
", "
55
<< (*iter)->GetVertexType () <<
">"
<< std::endl;
56
}
57
os <<
"*** CandidateQueue End ***"
;
58
return
os;
59
}
60
61
CandidateQueue::CandidateQueue
()
62
: m_candidates ()
63
{
64
NS_LOG_FUNCTION_NOARGS
();
65
}
66
67
CandidateQueue::~CandidateQueue
()
68
{
69
NS_LOG_FUNCTION_NOARGS
();
70
Clear
();
71
}
72
73
void
74
CandidateQueue::Clear
(
void
)
75
{
76
NS_LOG_FUNCTION_NOARGS
();
77
while
(!
m_candidates
.empty ())
78
{
79
SPFVertex
*p =
Pop
();
80
delete
p;
81
p = 0;
82
}
83
}
84
85
void
86
CandidateQueue::Push
(
SPFVertex
*vNew)
87
{
88
NS_LOG_FUNCTION
(
this
<< vNew);
89
90
CandidateList_t::iterator i = std::upper_bound (
91
m_candidates
.begin (),
m_candidates
.end (), vNew,
92
&
CandidateQueue::CompareSPFVertex
93
);
94
m_candidates
.insert (i, vNew);
95
}
96
97
SPFVertex
*
98
CandidateQueue::Pop
(
void
)
99
{
100
NS_LOG_FUNCTION_NOARGS
();
101
if
(
m_candidates
.empty ())
102
{
103
return
0;
104
}
105
106
SPFVertex
*v =
m_candidates
.front ();
107
m_candidates
.pop_front ();
108
return
v;
109
}
110
111
SPFVertex
*
112
CandidateQueue::Top
(
void
)
const
113
{
114
NS_LOG_FUNCTION_NOARGS
();
115
if
(
m_candidates
.empty ())
116
{
117
return
0;
118
}
119
120
return
m_candidates
.front ();
121
}
122
123
bool
124
CandidateQueue::Empty
(
void
)
const
125
{
126
NS_LOG_FUNCTION_NOARGS
();
127
return
m_candidates
.empty ();
128
}
129
130
uint32_t
131
CandidateQueue::Size
(
void
)
const
132
{
133
NS_LOG_FUNCTION_NOARGS
();
134
return
m_candidates
.size ();
135
}
136
137
SPFVertex
*
138
CandidateQueue::Find
(
const
Ipv4Address
addr)
const
139
{
140
NS_LOG_FUNCTION_NOARGS
();
141
CandidateList_t::const_iterator i =
m_candidates
.begin ();
142
143
for
(; i !=
m_candidates
.end (); i++)
144
{
145
SPFVertex
*v = *i;
146
if
(v->
GetVertexId
() == addr)
147
{
148
return
v;
149
}
150
}
151
152
return
0;
153
}
154
155
void
156
CandidateQueue::Reorder
(
void
)
157
{
158
NS_LOG_FUNCTION_NOARGS
();
159
160
m_candidates
.sort (&
CandidateQueue::CompareSPFVertex
);
161
NS_LOG_LOGIC
(
"After reordering the CandidateQueue"
);
162
NS_LOG_LOGIC
(*
this
);
163
}
164
165
/*
166
* In this implementation, SPFVertex follows the ordering where
167
* a vertex is ranked first if its GetDistanceFromRoot () is smaller;
168
* In case of a tie, NetworkLSA is always ranked before RouterLSA.
169
*
170
* This ordering is necessary for implementing ECMP
171
*/
172
bool
173
CandidateQueue::CompareSPFVertex
(
const
SPFVertex
* v1,
const
SPFVertex
* v2)
174
{
175
NS_LOG_FUNCTION
(&v1 << &v2);
176
177
bool
result =
false
;
178
if
(v1->
GetDistanceFromRoot
() < v2->
GetDistanceFromRoot
())
179
{
180
result =
true
;
181
}
182
else
if
(v1->
GetDistanceFromRoot
() == v2->
GetDistanceFromRoot
())
183
{
184
if
(v1->
GetVertexType
() ==
SPFVertex::VertexNetwork
185
&& v2->
GetVertexType
() ==
SPFVertex::VertexRouter
)
186
{
187
result =
true
;
188
}
189
}
190
return
result;
191
}
192
193
}
// namespace ns3
src
internet
model
candidate-queue.cc
Generated on Tue Oct 9 2012 16:45:37 for ns-3 by
1.8.1.2