diff --git a/SConstruct b/SConstruct index 00a799f90..111358020 100644 --- a/SConstruct +++ b/SConstruct @@ -374,6 +374,7 @@ routing.add_sources([ 'routing-environment.cc', 'static-router.cc', 'static-route-manager.cc', + 'candidate-queue.cc', ]) routing.add_headers ([ ]) @@ -381,6 +382,7 @@ routing.add_inst_headers([ 'routing-environment.h', 'static-router.h', 'static-route-manager.h', + 'candidate-queue.h', ]) # utils @@ -429,7 +431,6 @@ ns3.add(sample_packet_printer) sample_packet_printer.add_deps (['common', 'internet-node']) sample_packet_printer.add_source('main-packet-printer.cc') - sample_callback = build.Ns3Module('sample-callback', 'samples') sample_callback.set_executable() ns3.add(sample_callback) @@ -502,6 +503,13 @@ ns3.add(sample_component_manager) sample_component_manager.add_deps(['core']) sample_component_manager.add_source('main-component-manager.cc') +sample_candidate_queue = build.Ns3Module('sample-candidate-queue', 'samples') +sample_candidate_queue.set_executable() +ns3.add(sample_candidate_queue) +sample_candidate_queue.add_deps(['core']) +sample_candidate_queue.add_deps(['routing']) +sample_candidate_queue.add_source('main-candidate-queue.cc') + # examples example_simple_p2p = build.Ns3Module('simple-p2p', 'examples') example_simple_p2p.set_executable() diff --git a/samples/main-candidate-queue.cc b/samples/main-candidate-queue.cc new file mode 100644 index 000000000..7708b8c0f --- /dev/null +++ b/samples/main-candidate-queue.cc @@ -0,0 +1,91 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "ns3/debug.h" +#include "ns3/assert.h" +#include "ns3/candidate-queue.h" + +using namespace ns3; + + int +main (int argc, char *argv[]) +{ + NS_DEBUG_UNCOND("Candidate Queue Test"); + +#if 0 + DebugComponentEnable("CandidateQueue"); +#endif + + CandidateQueue candidate; + NS_ASSERT(candidate.Size () == 0); + + for (int i = 0; i < 100; ++i) + { + SPFVertex *v = new SPFVertex; + v->m_distanceFromRoot = rand () % 100; + candidate.Push (v); + } + + uint32_t lastDistance = 0; + bool ok = true; + + for (int i = 0; i < 100; ++i) + { + SPFVertex *v = candidate.Top (); + candidate.Pop (); + if (v->m_distanceFromRoot < lastDistance) + { + ok = false; + NS_ASSERT(0); + } + lastDistance = v->m_distanceFromRoot; + } + + for (int i = 0; i < 100; ++i) + { + SPFVertex *v = new SPFVertex; + v->m_distanceFromRoot = rand () % 100; + candidate.Push (v); + } + + for (int i = 0; i < 100; ++i) + { + SPFVertex *v = candidate.Fetch (i); + if (v) { + v->m_distanceFromRoot = rand () % 100; + } + } + + candidate.Reorder (); + + lastDistance = 0; + + for (int i = 0; i < 100; ++i) + { + SPFVertex *v = candidate.Top (); + candidate.Pop (); + if (v->m_distanceFromRoot < lastDistance) + { + ok = false; + NS_ASSERT(0); + } + lastDistance = v->m_distanceFromRoot; + } + + NS_ASSERT(candidate.Size () == 0); + + return 0; +} diff --git a/src/routing/candidate-queue.cc b/src/routing/candidate-queue.cc new file mode 100644 index 000000000..a4b29802c --- /dev/null +++ b/src/routing/candidate-queue.cc @@ -0,0 +1,151 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "ns3/debug.h" +#include "ns3/assert.h" +#include "candidate-queue.h" + +NS_DEBUG_COMPONENT_DEFINE ("CandidateQueue"); + +namespace ns3 { + +CandidateQueue::CandidateQueue() + : m_candidates () +{ + NS_DEBUG("CandidateQueue::CandidateQueue ()"); +} + +CandidateQueue::~CandidateQueue() +{ + NS_DEBUG("CandidateQueue::~CandidateQueue ()"); + Clear (); +} + + void +CandidateQueue::Clear (void) +{ + NS_DEBUG("CandidateQueue::Clear ()"); + + while (!m_candidates.empty ()) + { + Pop (); + } +} + + + void +CandidateQueue::Push (SPFVertex *vNew) +{ + NS_DEBUG("CandidateQueue::Push (" << vNew << ")"); + + CandidateList_t::iterator i = m_candidates.begin (); + + for (; i != m_candidates.end (); i++) + { + SPFVertex *v = *i; + if (vNew->m_distanceFromRoot < v->m_distanceFromRoot) + { + break; + } + } + m_candidates.insert(i, vNew); +} + + void +CandidateQueue::Pop (void) +{ + NS_DEBUG("CandidateQueue::Pop ()"); + + if (m_candidates.empty ()) + { + return; + } + + SPFVertex *v = m_candidates.front (); + m_candidates.pop_front (); + delete v; + v = 0; +} + + SPFVertex * +CandidateQueue::Top (void) +{ + NS_DEBUG("CandidateQueue::Top ()"); + + if (m_candidates.empty ()) + { + return 0; + } + + return m_candidates.front (); +} + + bool +CandidateQueue::Empty (void) +{ + NS_DEBUG("CandidateQueue::Empty ()"); + + return m_candidates.empty (); +} + + uint32_t +CandidateQueue::Size (void) +{ + NS_DEBUG("CandidateQueue::Size ()"); + + return m_candidates.size (); +} + + + SPFVertex * +CandidateQueue::Fetch (uint32_t m_distanceFromRoot) +{ + NS_DEBUG("CandidateQueue::Fetch ()"); + + CandidateList_t::iterator i = m_candidates.begin (); + + for (; i != m_candidates.end (); i++) + { + SPFVertex *v = *i; + if (v->m_distanceFromRoot == m_distanceFromRoot) + { + return v; + } + } + + return 0; +} + + void +CandidateQueue::Reorder (void) +{ + NS_DEBUG("CandidateQueue::Reorder ()"); + + std::list temp; + + while (!m_candidates.empty ()) { + SPFVertex *v = m_candidates.front (); + m_candidates.pop_front (); + temp.push_back(v); + } + + while (!temp.empty ()) { + Push (temp.front ()); + temp.pop_front (); + } +} + +} // namespace ns3 diff --git a/src/routing/candidate-queue.h b/src/routing/candidate-queue.h new file mode 100644 index 000000000..e04bd4651 --- /dev/null +++ b/src/routing/candidate-queue.h @@ -0,0 +1,50 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef CANDIDATE_QUEUE_H +#define CANDIDATE_QUEUE_H + +#include +#include +#include "static-route-manager.h" + +namespace ns3 { + +class CandidateQueue +{ +public: + CandidateQueue (); + virtual ~CandidateQueue (); + + void Clear (void); + void Push (SPFVertex *v); + void Pop (void); + SPFVertex* Top (void); + bool Empty (void); + uint32_t Size (void); + SPFVertex* Fetch (uint32_t key); + void Reorder (void); + +protected: + typedef std::list CandidateList_t; + CandidateList_t m_candidates; + +private: +}; + +} // namespace ns3 + +#endif /* CANDIDATE_QUEUE_H */ diff --git a/src/routing/static-router.cc b/src/routing/static-router.cc index b1a470feb..528a1500f 100644 --- a/src/routing/static-router.cc +++ b/src/routing/static-router.cc @@ -170,7 +170,7 @@ StaticRouter::ClearLSAs () } Ipv4Address - StaticRouter::GetRouterId (void) +StaticRouter::GetRouterId (void) { return m_routerId; } diff --git a/src/routing/static-router.h b/src/routing/static-router.h index d65e4e165..bda6cc04a 100644 --- a/src/routing/static-router.h +++ b/src/routing/static-router.h @@ -13,6 +13,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #ifndef STATIC_ROUTER_H #define STATIC_ROUTER_H @@ -39,19 +40,18 @@ namespace ns3 { class StaticRouterLinkRecord { public: - // - // For Type 1 link (PointToPoint), set m_linkId to Router ID of - // neighboring router. - // - // For Type 3 link (Stub), set m_linkId to neighbor's IP address - // +// +// For Type 1 link (PointToPoint), set m_linkId to Router ID of +// neighboring router. +// +// For Type 3 link (Stub), set m_linkId to neighbor's IP address +// Ipv4Address m_linkId; - - // - // For Type 1 link (PointToPoint), set m_linkData to local IP address - // - // For Type 3 link (Stub), set m_linkData to mask 0xffffffff - // +// +// For Type 1 link (PointToPoint), set m_linkData to local IP address +// +// For Type 3 link (Stub), set m_linkData to mask 0xffffffff +// Ipv4Address m_linkData; // for links to RouterLSA, /** * Enumeration of the possible types of Static Router Link Records. These