Enhancement 1393 - IPv6 Routing Helper RT Print functions
authorTommaso Pecorella <tommaso.pecorella@unifi.it>
Sat, 17 Mar 2012 23:23:56 +0100
changeset 7791 21c3495394b4
parent 7790 47d6d575412c
child 7792 e3d19bee62e8
Enhancement 1393 - IPv6 Routing Helper RT Print functions
CHANGES.html
RELEASE_NOTES
src/internet/helper/ipv6-routing-helper.cc
src/internet/helper/ipv6-routing-helper.h
src/internet/model/ipv4-routing-protocol.h
src/internet/model/ipv6-list-routing.cc
src/internet/model/ipv6-list-routing.h
src/internet/model/ipv6-routing-protocol.h
src/internet/model/ipv6-static-routing.cc
src/internet/model/ipv6-static-routing.h
src/internet/test/ipv6-list-routing-test-suite.cc
--- a/CHANGES.html	Wed Mar 21 18:51:55 2012 +0100
+++ b/CHANGES.html	Sat Mar 17 23:23:56 2012 +0100
@@ -41,7 +41,7 @@
 our best but can guarantee that there will be things that fall through
 the cracks, unfortunately.  If you, as a user, can suggest improvements
 to this file based on your experience, please contribute a patch or drop
-us a note on ns-developers mailing list.  </p>
+us a note on ns-developers mailing list.</p>
 
 <hr>
 <h1>Changes from ns-3.13 to ns-3-dev</h1>
@@ -74,6 +74,10 @@
 needed for TCP.  This lead to a small change in the UDP and ICMPv6 L4
 protocols as well.
 </li>
+<li>
+Ipv6RoutingHelper can now print the IPv6 Routing Tables at specific 
+intervals or time. Exactly like Ipv4RoutingHelper do.
+</li>
 </ul>
 
 <h2>Changes to build system:</h2>
--- a/RELEASE_NOTES	Wed Mar 21 18:51:55 2012 +0100
+++ b/RELEASE_NOTES	Sat Mar 17 23:23:56 2012 +0100
@@ -24,7 +24,10 @@
 - Dual-stacked IPv6 sockets are implemented. An IPv6 socket can accept an IPv4 
   connection, returning the senders address as an IPv4-mapped address 
   (IPV6_V6ONLY socket option is not implemented).
-
+- Ipv6RoutingHelper is now in-line with Ipv4RoutingHelper concerning the RT 
+  print functions. Various minor changes made in Ipv6RoutingProtocol and derived 
+  classes to make this possible.
+   
 Bugs fixed
 ----------
  - bug 1319 - Fix Ipv6RawSocketImpl Icmpv6 filter
--- a/src/internet/helper/ipv6-routing-helper.cc	Wed Mar 21 18:51:55 2012 +0100
+++ b/src/internet/helper/ipv6-routing-helper.cc	Sat Mar 17 23:23:56 2012 +0100
@@ -18,6 +18,10 @@
  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
  */
 
+#include "ns3/node.h"
+#include "ns3/node-list.h"
+#include "ns3/simulator.h"
+#include "ns3/ipv6-routing-protocol.h"
 #include "ipv6-routing-helper.h"
 
 namespace ns3 {
@@ -26,4 +30,55 @@
 {
 }
 
+void
+Ipv6RoutingHelper::PrintRoutingTableAllAt (Time printTime, Ptr<OutputStreamWrapper> stream) const
+{
+  for (uint32_t i = 0; i < NodeList::GetNNodes (); i++)
+    {
+      Ptr<Node> node = NodeList::GetNode (i);
+      Simulator::Schedule (printTime, &Ipv6RoutingHelper::Print, this, node, stream);
+    }
+}
+
+void
+Ipv6RoutingHelper::PrintRoutingTableAllEvery (Time printInterval, Ptr<OutputStreamWrapper> stream) const
+{
+  for (uint32_t i = 0; i < NodeList::GetNNodes (); i++)
+    {
+      Ptr<Node> node = NodeList::GetNode (i);
+      Simulator::Schedule (printInterval, &Ipv6RoutingHelper::PrintEvery, this, printInterval, node, stream);
+    }
+}
+
+void
+Ipv6RoutingHelper::PrintRoutingTableAt (Time printTime, Ptr<Node> node, Ptr<OutputStreamWrapper> stream) const
+{
+  Simulator::Schedule (printTime, &Ipv6RoutingHelper::Print, this, node, stream);
+}
+
+void
+Ipv6RoutingHelper::PrintRoutingTableEvery (Time printInterval,Ptr<Node> node, Ptr<OutputStreamWrapper> stream) const
+{
+  Simulator::Schedule (printInterval, &Ipv6RoutingHelper::PrintEvery, this, printInterval, node, stream);
+}
+
+void
+Ipv6RoutingHelper::Print (Ptr<Node> node, Ptr<OutputStreamWrapper> stream) const
+{
+  Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
+  Ptr<Ipv6RoutingProtocol> rp = ipv6->GetRoutingProtocol ();
+  NS_ASSERT (rp);
+  rp->PrintRoutingTable (stream);
+}
+
+void
+Ipv6RoutingHelper::PrintEvery (Time printInterval, Ptr<Node> node, Ptr<OutputStreamWrapper> stream) const
+{
+  Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
+  Ptr<Ipv6RoutingProtocol> rp = ipv6->GetRoutingProtocol ();
+  NS_ASSERT (rp);
+  rp->PrintRoutingTable (stream);
+  Simulator::Schedule (printInterval, &Ipv6RoutingHelper::PrintEvery, this, printInterval, node, stream);
+}
+
 } // namespace ns3
--- a/src/internet/helper/ipv6-routing-helper.h	Wed Mar 21 18:51:55 2012 +0100
+++ b/src/internet/helper/ipv6-routing-helper.h	Sat Mar 17 23:23:56 2012 +0100
@@ -22,6 +22,8 @@
 #define IPV6_ROUTING_HELPER_H
 
 #include "ns3/ptr.h"
+#include "ns3/nstime.h"
+#include "ns3/output-stream-wrapper.h"
 
 namespace ns3 {
 
@@ -61,6 +63,56 @@
    * \returns a newly-created routing protocol
    */
   virtual Ptr<Ipv6RoutingProtocol> Create (Ptr<Node> node) const = 0;
+
+  /**
+   * \brief prints the routing tables of all nodes at a particular time.
+   * \param printTime the time at which the routing table is supposed to be printed.
+   * \param stream The output stream object to use
+   *
+   * This method calls the PrintRoutingTable() method of the
+   * Ipv6RoutingProtocol stored in the Ipv6 object, for all nodes at the
+   * specified time; the output format is routing protocol-specific.
+   */
+  void PrintRoutingTableAllAt (Time printTime, Ptr<OutputStreamWrapper> stream) const;
+
+  /**
+   * \brief prints the routing tables of all nodes at regular intervals specified by user.
+   * \param printInterval the time interval for which the routing table is supposed to be printed.
+   * \param stream The output stream object to use
+   *
+   * This method calls the PrintRoutingTable() method of the
+   * Ipv6RoutingProtocol stored in the Ipv6 object, for all nodes at the
+   * specified time interval; the output format is routing protocol-specific.
+   */
+  void PrintRoutingTableAllEvery (Time printInterval, Ptr<OutputStreamWrapper> stream) const;
+
+  /**
+   * \brief prints the routing tables of a node at a particular time.
+   * \param printTime the time at which the routing table is supposed to be printed.
+   * \param node The node ptr for which we need the routing table to be printed
+   * \param stream The output stream object to use
+   *
+   * This method calls the PrintRoutingTable() method of the
+   * Ipv6RoutingProtocol stored in the Ipv6 object, for the selected node
+   * at the specified time; the output format is routing protocol-specific.
+   */
+  void PrintRoutingTableAt (Time printTime, Ptr<Node> node, Ptr<OutputStreamWrapper> stream) const;
+
+  /**
+   * \brief prints the routing tables of a node at regular intervals specified by user.
+   * \param printInterval the time interval for which the routing table is supposed to be printed.
+   * \param node The node ptr for which we need the routing table to be printed
+   * \param stream The output stream object to use
+   *
+   * This method calls the PrintRoutingTable() method of the
+   * Ipv6RoutingProtocol stored in the Ipv6 object, for the selected node
+   * at the specified interval; the output format is routing protocol-specific.
+   */
+  void PrintRoutingTableEvery (Time printInterval, Ptr<Node> node, Ptr<OutputStreamWrapper> stream) const;
+
+private:
+  void Print (Ptr<Node> node, Ptr<OutputStreamWrapper> stream) const;
+  void PrintEvery (Time printInterval, Ptr<Node> node, Ptr<OutputStreamWrapper> stream) const;
 };
 
 } // namespace ns3
--- a/src/internet/model/ipv4-routing-protocol.h	Wed Mar 21 18:51:55 2012 +0100
+++ b/src/internet/model/ipv4-routing-protocol.h	Sat Mar 17 23:23:56 2012 +0100
@@ -142,6 +142,11 @@
    */
   virtual void SetIpv4 (Ptr<Ipv4> ipv4) = 0;
 
+  /**
+   * \brief Print the Routing Table entries
+   *
+   * \param stream the ostream the Routing table is printed to
+   */
   virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const = 0;
 };
 
--- a/src/internet/model/ipv6-list-routing.cc	Wed Mar 21 18:51:55 2012 +0100
+++ b/src/internet/model/ipv6-list-routing.cc	Sat Mar 17 23:23:56 2012 +0100
@@ -23,6 +23,7 @@
 #include "ns3/node.h"
 #include "ns3/ipv6-static-routing.h"
 #include "ipv6-list-routing.h"
+#include "ns3/simulator.h"
 
 NS_LOG_COMPONENT_DEFINE ("Ipv6ListRouting");
 
@@ -270,6 +271,23 @@
 }
 
 void
+Ipv6ListRouting::PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const
+{
+  NS_LOG_FUNCTION (this);
+
+  *stream->GetStream () << "Node: " << m_ipv6->GetObject<Node> ()->GetId ()
+                        << " Time: " << Simulator::Now ().GetSeconds () << "s "
+                        << "Ipv6ListRouting table" << std::endl;
+  for (Ipv6RoutingProtocolList::const_iterator i = m_routingProtocols.begin ();
+       i != m_routingProtocols.end (); i++)
+    {
+      *stream->GetStream () << "  Priority: " << (*i).first << " Protocol: " << (*i).second->GetInstanceTypeId () << std::endl;
+      (*i).second->PrintRoutingTable (stream);
+    }
+  *stream->GetStream () << std::endl;
+}
+
+void
 Ipv6ListRouting::SetIpv6 (Ptr<Ipv6> ipv6)
 {
   NS_LOG_FUNCTION (this << ipv6);
--- a/src/internet/model/ipv6-list-routing.h	Wed Mar 21 18:51:55 2012 +0100
+++ b/src/internet/model/ipv6-list-routing.h	Sat Mar 17 23:23:56 2012 +0100
@@ -104,6 +104,13 @@
   virtual void NotifyRemoveRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse = Ipv6Address::GetZero ());
   virtual void SetIpv6 (Ptr<Ipv6> ipv6);
 
+  /**
+   * \brief Print the Routing Table entries
+   *
+   * \param stream the ostream the Routing table is printed to
+   */
+  virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const;
+
 protected:
   /**
    * \brief Dispose this object.
--- a/src/internet/model/ipv6-routing-protocol.h	Wed Mar 21 18:51:55 2012 +0100
+++ b/src/internet/model/ipv6-routing-protocol.h	Sat Mar 17 23:23:56 2012 +0100
@@ -29,6 +29,7 @@
 #include "ipv6-header.h"
 #include "ipv6-interface-address.h"
 #include "ipv6.h"
+#include "ns3/output-stream-wrapper.h"
 
 namespace ns3 {
 
@@ -171,6 +172,13 @@
    * \param ipv6 the ipv6 object this routing protocol is being associated with
    */
   virtual void SetIpv6 (Ptr<Ipv6> ipv6) = 0;
+
+  /**
+   * \brief Print the Routing Table entries
+   *
+   * \param stream the ostream the Routing table is printed to
+   */
+  virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const = 0;
 };
 
 } // namespace ns3
--- a/src/internet/model/ipv6-static-routing.cc	Wed Mar 21 18:51:55 2012 +0100
+++ b/src/internet/model/ipv6-static-routing.cc	Sat Mar 17 23:23:56 2012 +0100
@@ -18,8 +18,11 @@
  * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
  */
 
+#include <iomanip>
 #include "ns3/log.h"
+#include "ns3/node.h"
 #include "ns3/packet.h"
+#include "ns3/simulator.h"
 #include "ns3/ipv6-route.h"
 #include "ns3/net-device.h"
 
@@ -71,6 +74,26 @@
     }
 }
 
+// Formatted like output of "route -n" command
+void
+Ipv6StaticRouting::PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const
+{
+  NS_LOG_FUNCTION (this);
+  std::ostream* os = stream->GetStream ();
+  if (GetNRoutes () > 0)
+    {
+      *os << "Node: " << m_ipv6->GetObject<Node> ()->GetId ()
+          << " Time: " << Simulator::Now ().GetSeconds () << "s "
+          << "Ipv6StaticRouting table" << std::endl;
+
+      for (uint32_t j = 0; j < GetNRoutes (); j++)
+        {
+          Ipv6RoutingTableEntry route = GetRoute (j);
+          *os << route << std::endl;
+        }
+    }
+}
+
 void Ipv6StaticRouting::AddHostRouteTo (Ipv6Address dst, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse, uint32_t metric)
 {
   NS_LOG_FUNCTION (this << dst << nextHop << interface << prefixToUse << metric);
@@ -369,7 +392,7 @@
   return mrtentry;
 }
 
-uint32_t Ipv6StaticRouting::GetNRoutes ()
+uint32_t Ipv6StaticRouting::GetNRoutes () const
 {
   return m_networkRoutes.size ();
 }
@@ -412,12 +435,12 @@
     }
 }
 
-Ipv6RoutingTableEntry Ipv6StaticRouting::GetRoute (uint32_t index)
+Ipv6RoutingTableEntry Ipv6StaticRouting::GetRoute (uint32_t index) const
 {
   NS_LOG_FUNCTION (this << index);
   uint32_t tmp = 0;
 
-  for (NetworkRoutesI it = m_networkRoutes.begin (); it != m_networkRoutes.end (); it++)
+  for (NetworkRoutesCI it = m_networkRoutes.begin (); it != m_networkRoutes.end (); it++)
     {
       if (tmp == index)
         {
@@ -430,12 +453,12 @@
   return 0;
 }
 
-uint32_t Ipv6StaticRouting::GetMetric (uint32_t index)
+uint32_t Ipv6StaticRouting::GetMetric (uint32_t index) const
 {
   NS_LOG_FUNCTION_NOARGS ();
   uint32_t tmp = 0;
 
-  for (NetworkRoutesI it = m_networkRoutes.begin (); it != m_networkRoutes.end (); it++)
+  for (NetworkRoutesCI it = m_networkRoutes.begin (); it != m_networkRoutes.end (); it++)
     {
       if (tmp == index)
         {
--- a/src/internet/model/ipv6-static-routing.h	Wed Mar 21 18:51:55 2012 +0100
+++ b/src/internet/model/ipv6-static-routing.h	Sat Mar 17 23:23:56 2012 +0100
@@ -132,7 +132,7 @@
    * \brief Get the number or entries in the routing table.
    * \return number of entries
    */
-  uint32_t GetNRoutes ();
+  uint32_t GetNRoutes () const;
 
   /**
    * \brief Get the default route.
@@ -147,14 +147,14 @@
    * \param i index
    * \return the route whose index is i
    */
-  Ipv6RoutingTableEntry GetRoute (uint32_t i);
+  Ipv6RoutingTableEntry GetRoute (uint32_t i) const;
 
   /**
    * \brief Get a metric for route from the static unicast routing table.
    * \param index The index (into the routing table) of the route to retrieve.
    * \return If route is set, the metric is returned. If not, an infinity metric (0xffffffff) is returned
    */
-  uint32_t GetMetric (uint32_t index);
+  uint32_t GetMetric (uint32_t index) const;
 
   /**
    * \brief Remove a route from the routing table.
@@ -236,6 +236,13 @@
   virtual void NotifyRemoveRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse = Ipv6Address::GetZero ());
   virtual void SetIpv6 (Ptr<Ipv6> ipv6);
 
+  /**
+   * \brief Print the Routing Table entries
+   *
+   * \param stream the ostream the Routing table is printed to
+   */
+  virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const;
+
 protected:
   /**
    * \brief Dispose this object.
--- a/src/internet/test/ipv6-list-routing-test-suite.cc	Wed Mar 21 18:51:55 2012 +0100
+++ b/src/internet/test/ipv6-list-routing-test-suite.cc	Sat Mar 17 23:23:56 2012 +0100
@@ -38,6 +38,7 @@
                          GetZero ()) {}
   void NotifyRemoveRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse) {}
   void SetIpv6 (Ptr<Ipv6> ipv6) {}
+  virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const {};
 };
 
 class Ipv6BRouting : public Ipv6RoutingProtocol {
@@ -54,6 +55,7 @@
                          GetZero ()) {}
   void NotifyRemoveRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse) {}
   void SetIpv6 (Ptr<Ipv6> ipv6) {}
+  virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const {};
 };
 
 class Ipv6ListRoutingNegativeTestCase : public TestCase