A Discrete-Event Network Simulator
API
 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"
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;
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 
62  : m_candidates ()
63 {
65 }
66 
68 {
70  Clear ();
71 }
72 
73 void
75 {
77  while (!m_candidates.empty ())
78  {
79  SPFVertex *p = Pop ();
80  delete p;
81  p = 0;
82  }
83 }
84 
85 void
87 {
88  NS_LOG_FUNCTION (this << vNew);
89 
90  CandidateList_t::iterator i = std::upper_bound (
91  m_candidates.begin (), m_candidates.end (), vNew,
93  );
94  m_candidates.insert (i, vNew);
95 }
96 
97 SPFVertex *
99 {
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 *
113 {
115  if (m_candidates.empty ())
116  {
117  return 0;
118  }
119 
120  return m_candidates.front ();
121 }
122 
123 bool
125 {
127  return m_candidates.empty ();
128 }
129 
130 uint32_t
132 {
134  return m_candidates.size ();
135 }
136 
137 SPFVertex *
139 {
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
157 {
159 
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
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  {
186  {
187  result = true;
188  }
189  }
190  return result;
191 }
192 
193 } // namespace ns3