Replace src/dsr usage of RandomVariable with RandomVariableStream
authorMitch Watrous
Tue, 14 Aug 2012 09:57:49 -0700
changeset 8976 4df4e7cd077f
parent 8975 813554a05d30
child 8977 2bd04c7f5010
Replace src/dsr usage of RandomVariable with RandomVariableStream
src/dsr/model/dsr-options.cc
src/dsr/model/dsr-routing.cc
src/dsr/model/dsr-routing.h
--- a/src/dsr/model/dsr-options.cc	Tue Aug 14 09:37:08 2012 -0700
+++ b/src/dsr/model/dsr-options.cc	Tue Aug 14 09:57:49 2012 -0700
@@ -43,7 +43,6 @@
 #include "ns3/node.h"
 #include "ns3/uinteger.h"
 #include "ns3/trace-source-accessor.h"
-#include "ns3/random-variable.h"
 #include "ns3/udp-header.h"
 #include "ns3/pointer.h"
 #include "ns3/node-list.h"
--- a/src/dsr/model/dsr-routing.cc	Tue Aug 14 09:37:08 2012 -0700
+++ b/src/dsr/model/dsr-routing.cc	Tue Aug 14 09:57:49 2012 -0700
@@ -56,7 +56,6 @@
 #include "ns3/ipv4-l3-protocol.h"
 #include "ns3/ipv4-route.h"
 #include "ns3/trace-source-accessor.h"
-#include "ns3/random-variable.h"
 #include "ns3/icmpv4-l4-protocol.h"
 #include "ns3/adhoc-wifi-mac.h"
 #include "ns3/wifi-net-device.h"
@@ -269,6 +268,9 @@
 DsrRouting::DsrRouting ()
 {
   NS_LOG_FUNCTION_NOARGS ();
+
+  m_uniformRandomVariable = CreateObject<UniformRandomVariable> ();
+
   /*
    * The following Ptr statements created objects for all the options header for DSR, and each of them have
    * distinct option number assigned, when DSR Routing received a packet from higher layer, it will find
@@ -786,7 +788,7 @@
             }
           if (m_maintainBuffer.GetSize () != 0 && m_maintainBuffer.Find (nextHop))
             {
-              Simulator::Schedule (MilliSeconds (UniformVariable ().GetInteger (0,100)),
+              Simulator::Schedule (MilliSeconds (m_uniformRandomVariable->GetInteger (0,100)),
                                    &DsrRouting::SendRerrWhenBreaksLinkToNextHop,this,nextHop,protocol);
             }
         }
@@ -1411,7 +1413,7 @@
                 }
             }
           // Try to send packet from *previously* queued entries from send buffer if any
-          Simulator::Schedule (MilliSeconds (UniformVariable ().GetInteger (0,100)),
+          Simulator::Schedule (MilliSeconds (m_uniformRandomVariable->GetInteger (0,100)),
                                &DsrRouting::SendPacketFromBuffer,this,sourceRoute,nextHop,protocol);
         }
     }
@@ -1544,7 +1546,7 @@
             {
               NS_LOG_DEBUG ("Packet sent by Dsr. Calling PriorityScheduler after some time");
               //packet was successfully sent down. call scheduler after some time
-              Simulator::Schedule (MicroSeconds (UniformVariable ().GetInteger (0, 1000)),
+              Simulator::Schedule (MicroSeconds (m_uniformRandomVariable->GetInteger (0, 1000)),
                                    &DsrRouting::PriorityScheduler,this, i, false);
             }
           else
@@ -1683,7 +1685,7 @@
           if (m_sendBuffer.GetSize () != 0 && m_sendBuffer.Find (destination))
             {
               NS_LOG_DEBUG ("Schedule sending the next packet in send buffer");
-              Simulator::Schedule (MilliSeconds (UniformVariable ().GetInteger (0,100)),
+              Simulator::Schedule (MilliSeconds (m_uniformRandomVariable->GetInteger (0,100)),
                                    &DsrRouting::SendPacketFromBuffer,this,sourceRoute,nextHop,protocol);
             }
         }
@@ -1783,7 +1785,7 @@
           if (m_errorBuffer.GetSize () != 0 && m_errorBuffer.Find (destination))
             {
               NS_LOG_DEBUG ("Schedule sending the next packet in send buffer");
-              Simulator::Schedule (MilliSeconds (UniformVariable ().GetInteger (0,100)),
+              Simulator::Schedule (MilliSeconds (m_uniformRandomVariable->GetInteger (0,100)),
                                    &DsrRouting::SendPacketFromBuffer,this,sourceRoute,nextHop,protocol);
             }
         }
@@ -1941,7 +1943,7 @@
       if (m_maintainBuffer.GetSize () && m_maintainBuffer.Find (nextHop))
         {
           NS_LOG_INFO ("Cancel the packet timer for next maintenance entry");
-          Simulator::Schedule (MilliSeconds (UniformVariable ().GetInteger (0,100)),
+          Simulator::Schedule (MilliSeconds (m_uniformRandomVariable->GetInteger (0,100)),
                                &DsrRouting::CancelPacketTimerNextHop,this,nextHop,protocol);
         }
     }
@@ -2219,6 +2221,14 @@
     }
 }
 
+int64_t
+DsrRouting::AssignStreams (int64_t stream)
+{
+  NS_LOG_FUNCTION (this << stream);
+  m_uniformRandomVariable->SetStream (stream);
+  return 1;
+}
+
 void
 DsrRouting::NetworkScheduleTimerExpire  (MaintainBuffEntry & mb,
                                          uint8_t protocol)
@@ -2729,7 +2739,7 @@
    * This is a forwarding case when sending route requests, a random delay time [0, m_broadcastJitter]
    * used before forwarding as link-layer broadcast
    */
-  Simulator::Schedule (MilliSeconds (UniformVariable ().GetInteger (0, m_broadcastJitter)), &DsrRouting::SendRequest, this,
+  Simulator::Schedule (MilliSeconds (m_uniformRandomVariable->GetInteger (0, m_broadcastJitter)), &DsrRouting::SendRequest, this,
                        packet, m_mainAddress);
 }
 
@@ -2843,7 +2853,7 @@
                                  double hops)
 {
   NS_LOG_FUNCTION (this << packet << source << destination);
-  Simulator::Schedule (Time (2 * m_nodeTraversalTime * (hops - 1 + UniformVariable ().GetValue (0,1))), &DsrRouting::SendReply, this, packet, source, destination, route);
+  Simulator::Schedule (Time (2 * m_nodeTraversalTime * (hops - 1 + m_uniformRandomVariable->GetValue (0,1))), &DsrRouting::SendReply, this, packet, source, destination, route);
 }
 
 void
--- a/src/dsr/model/dsr-routing.h	Tue Aug 14 09:37:08 2012 -0700
+++ b/src/dsr/model/dsr-routing.h	Tue Aug 14 09:57:49 2012 -0700
@@ -53,6 +53,7 @@
 #include "ns3/ipv4-header.h"
 #include "ns3/ipv4-address.h"
 #include "ns3/traced-callback.h"
+#include "ns3/random-variable-stream.h"
 #include "ns3/ipv4-route.h"
 #include "ns3/timer.h"
 #include "ns3/net-device.h"
@@ -465,6 +466,16 @@
   // / Handle route discovery timer
   void RouteRequestTimerExpire (Ptr<Packet> packet, std::vector<Ipv4Address> address, uint32_t requestId, uint8_t protocol);
 
+ /**
+  * Assign a fixed random variable stream number to the random variables
+  * used by this model.  Return the number of streams (possibly zero) that
+  * have been assigned.
+  *
+  * \param stream first stream index to use
+  * \return the number of stream indices assigned by this model
+  */
+  int64_t AssignStreams (int64_t stream);
+
 protected:
   /*
  *    * This function will notify other components connected to the node that a new stack member is now connected
@@ -634,6 +645,9 @@
   std::vector<Ipv4Address> m_clearList;   // / The node that is clear to send packet to
 
   std::vector<Ipv4Address> m_addresses;   // / The bind ipv4 addresses with next hop, src, destination address in sequence
+
+  /// Provides uniform random variables.
+  Ptr<UniformRandomVariable> m_uniformRandomVariable;  
 };
 }  /* namespace dsr */
 }  /* namespace ns3 */