Assign random OnOff-values to different OnOff-Applications in a for-loop

957 views
Skip to first unread message

stefanhe...@googlemail.com

unread,
Jan 20, 2013, 3:31:47 PM1/20/13
to ns-3-...@googlegroups.com

Hey everybody!

I have started to work with ns3 2 days ago and I am playing a little bit with NetAnim and the star-animation.cc example at the moment. The follwoing code is an excerpt from this example. My aim is to assign every node and its OnOff-Application radom values for their On- and Offtime, respectively. Or even different values. My idea was to use the index i for this task, but I have not found a way to do so.

 
  OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
  onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
  onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));

  ApplicationContainer spokeApps;

  for (uint32_t i = 0; i < star.SpokeCount (); ++i)
    {
      AddressValue remoteAddress;
      remoteAddress = AddressValue(InetSocketAddress (star.GetHubIpv4Address (i), port));

      onOffHelper.SetAttribute ("Remote", remoteAddress);
      spokeApps.Add (onOffHelper.Install (star.GetSpokeNode (i)));
    }

This one replacing the for-loop would work, but I think that there must be a better way. Especially using the loop.

  {
  onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
  onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=2]"));

  AddressValue remoteAddress;
  remoteAddress = AddressValue(InetSocketAddress (star.GetHubIpv4Address (0), port));

  onOffHelper.SetAttribute ("Remote", remoteAddress);
  spokeApps.Add (onOffHelper.Install (star.GetSpokeNode (0)));
  }

  {
  onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=3]"));
  onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=4]"));

  AddressValue remoteAddress;
  remoteAddress = AddressValue(InetSocketAddress (star.GetHubIpv4Address (1), port));

  onOffHelper.SetAttribute ("Remote", remoteAddress);
  spokeApps.Add (onOffHelper.Install (star.GetSpokeNode (1)));
  }

  {
  onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=4]"));
  onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=5]"));

  AddressValue remoteAddress;
  remoteAddress = AddressValue(InetSocketAddress (star.GetHubIpv4Address (2), port));

  onOffHelper.SetAttribute ("Remote", remoteAddress);
  spokeApps.Add (onOffHelper.Install (star.GetSpokeNode (2)));
  }
 
Thanks for every help and greets!

John Abraham

unread,
Jan 21, 2013, 1:03:03 AM1/21/13
to ns-3-...@googlegroups.com
Maybe something like below gives the attached picture

johnabra-mac:ns-3-dev johnabraham$ hg diff
diff -r 23373993f110 src/netanim/examples/star-animation.cc
--- a/src/netanim/examples/star-animation.cc Mon Dec 10 10:54:28 2012 -0800
+++ b/src/netanim/examples/star-animation.cc Sun Jan 20 22:01:56 2013 -0800
@@ -49,7 +49,7 @@
   Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (137));
 
   // ??? try and stick 15kb/s into the data rate
-  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("14kb/s"));
+  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("1400kb/s"));
 
   //
   // Default number of nodes in the star.  Overridable by command line argument.
@@ -69,7 +69,7 @@
 
   NS_LOG_INFO ("Build star topology.");
   PointToPointHelper pointToPoint;
-  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
+  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("3Mbps"));
   pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
   PointToPointStarHelper star (nSpokes, pointToPoint);
 
@@ -110,13 +110,17 @@
   // Create OnOff applications to send TCP to the hub, one on each spoke node.
   //
   OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
-  onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
-  onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
 
   ApplicationContainer spokeApps;
 
   for (uint32_t i = 0; i < star.SpokeCount (); ++i)
     {
+      std::ostringstream oss1;
+      oss1 << "ns3::ConstantRandomVariable[Constant=" << (i+1)%2 << "]";
+      std::ostringstream oss2;
+      oss2 << "ns3::ConstantRandomVariable[Constant=" << i << "]";
+      onOffHelper.SetAttribute ("OnTime", StringValue (oss1.str ())); 
+      onOffHelper.SetAttribute ("OffTime", StringValue (oss2.str ())); 
       AddressValue remoteAddress;
       if (useIpv6 == 0)
         {



--
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/ns-3-users/-/1Cn-B-HTX1AJ.
To post to this group, send email to ns-3-...@googlegroups.com.
To unsubscribe from this group, send email to ns-3-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ns-3-users?hl=en.

Screen Shot 2013-01-20 at 10.01.38 PM.png
Reply all
Reply to author
Forward
0 new messages