A Discrete-Event Network Simulator
API
resources-counters.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Author: John Abraham <john.abraham.in@gmail.com>
17  */
18 
19 #include <iostream>
20 
21 #include "ns3/core-module.h"
22 #include "ns3/network-module.h"
23 #include "ns3/internet-module.h"
24 #include "ns3/point-to-point-module.h"
25 #include "ns3/netanim-module.h"
26 #include "ns3/applications-module.h"
27 #include "ns3/point-to-point-layout-module.h"
28 
29 using namespace ns3;
30 
32 
34 struct rgb {
35  uint8_t r;
36  uint8_t g;
37  uint8_t b;
38 };
39 
40 struct rgb colors [] = {
41  { 255, 0, 0 }, // Red
42  { 0, 255, 0 }, // Blue
43  { 0, 0, 255 } // Green
44  };
45 
46 uint32_t resourceId1;
47 uint32_t resourceId2;
51 
52 void modify ()
53 {
54  std::ostringstream oss;
55  oss << "Update:" << Simulator::Now ().GetSeconds ();
56  pAnim->UpdateLinkDescription (0, 1, oss.str ());
57  pAnim->UpdateLinkDescription (0, 2, oss.str ());
58  pAnim->UpdateLinkDescription (0, 3, oss.str ());
59  pAnim->UpdateLinkDescription (0, 4, oss.str ());
60  pAnim->UpdateLinkDescription (0, 5, oss.str ());
61  pAnim->UpdateLinkDescription (0, 6, oss.str ());
62  pAnim->UpdateLinkDescription (1, 7, oss.str ());
63  pAnim->UpdateLinkDescription (1, 8, oss.str ());
64  pAnim->UpdateLinkDescription (1, 9, oss.str ());
65  pAnim->UpdateLinkDescription (1, 10, oss.str ());
66  pAnim->UpdateLinkDescription (1, 11, oss.str ());
67 
68  // Every update change the node description for node 2
69  std::ostringstream node0Oss;
70  node0Oss << "-----Node:" << Simulator::Now ().GetSeconds ();
71  pAnim->UpdateNodeDescription (2, node0Oss.str ());
72  static double size = 2;
73  static uint32_t currentResourceId = resourceId1;
74  pAnim->UpdateNodeSize (2, size, size);
75  pAnim->UpdateNodeImage (3, currentResourceId);
76  size *= 1.1;
77  if (size > 20)
78  size = 1;
79  pAnim->UpdateNodeSize (3, 10, 10);
80  if (currentResourceId == resourceId1)
81  currentResourceId = resourceId2;
82  else
83  currentResourceId = resourceId1;
84 
85  // Every update change the color for node 4
86  static uint32_t index = 0;
87  index++;
88  if (index == 3)
89  index = 0;
90  struct rgb color = colors[index];
91  for (uint32_t nodeId = 4; nodeId < 12; ++nodeId)
92  pAnim->UpdateNodeColor (nodeId, color.r, color.g, color.b);
93 
94  // Update Node Counter for node 0 and node 5, use some random number between 0 to 1000 for value
95  Ptr <UniformRandomVariable> rv = CreateObject<UniformRandomVariable> ();
97  pAnim->UpdateNodeCounter (nodeCounterIdDouble1, 0, rv->GetValue (100.0, 200.0));
98  pAnim->UpdateNodeCounter (nodeCounterIdDouble2, 0, rv->GetValue (300.0, 400.0));
100  pAnim->UpdateNodeCounter (nodeCounterIdDouble1, 5, rv->GetValue (100.0, 200.0));
101  pAnim->UpdateNodeCounter (nodeCounterIdDouble2, 5, rv->GetValue (300.0, 400.0));
102 
103  if (Simulator::Now ().GetSeconds () < 10) // This is important or the simulation
104  // will run endlessly
106 
107 }
108 
109 int main (int argc, char *argv[])
110 {
111  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (512));
112  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("500kb/s"));
113 
114  uint32_t nLeftLeaf = 5;
115  uint32_t nRightLeaf = 5;
116  uint32_t nLeaf = 0; // If non-zero, number of both left and right
117  std::string animFile = "resources_demo.xml" ; // Name of file for animation output
118 
119  CommandLine cmd (__FILE__);
120  cmd.AddValue ("nLeftLeaf", "Number of left side leaf nodes", nLeftLeaf);
121  cmd.AddValue ("nRightLeaf","Number of right side leaf nodes", nRightLeaf);
122  cmd.AddValue ("nLeaf", "Number of left and right side leaf nodes", nLeaf);
123  cmd.AddValue ("animFile", "File Name for Animation Output", animFile);
124 
125  cmd.Parse (argc,argv);
126  if (nLeaf > 0)
127  {
128  nLeftLeaf = nLeaf;
129  nRightLeaf = nLeaf;
130  }
131 
132  // Create the point-to-point link helpers
133  PointToPointHelper pointToPointRouter;
134  pointToPointRouter.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
135  pointToPointRouter.SetChannelAttribute ("Delay", StringValue ("1ms"));
136  PointToPointHelper pointToPointLeaf;
137  pointToPointLeaf.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
138  pointToPointLeaf.SetChannelAttribute ("Delay", StringValue ("1ms"));
139 
140  PointToPointDumbbellHelper d (nLeftLeaf, pointToPointLeaf,
141  nRightLeaf, pointToPointLeaf,
142  pointToPointRouter);
143 
144  // Install Stack
146  d.InstallStack (stack);
147 
148  // Assign IP Addresses
149  d.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"),
150  Ipv4AddressHelper ("10.2.1.0", "255.255.255.0"),
151  Ipv4AddressHelper ("10.3.1.0", "255.255.255.0"));
152 
153  d.BoundingBox (1, 1, 100, 100);
154  // Install on/off app on all right side nodes
155  OnOffHelper clientHelper ("ns3::UdpSocketFactory", Address ());
156  clientHelper.SetAttribute
157  ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
158  clientHelper.SetAttribute
159  ("OffTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
161 
162  for (uint32_t i = 0; i < d.RightCount (); ++i)
163  {
164  // Create an on/off app sending packets to the same leaf right side
165  AddressValue remoteAddress (InetSocketAddress (d.GetLeftIpv4Address (i), 1000));
166  clientHelper.SetAttribute ("Remote", remoteAddress);
167  clientApps.Add (clientHelper.Install (d.GetRight (i)));
168  }
169 
170  clientApps.Start (Seconds (0.0));
171  clientApps.Stop (Seconds (5.0));
172 
173  // Set the bounding box for animation
174 
175 
176  // Create the animation object and configure for specified output
177  pAnim = new AnimationInterface (animFile);
178  // Provide the absolute path to the resource
179  resourceId1 = pAnim->AddResource ("/Users/john/ns3/netanim-3.105/ns-3-logo1.png");
180  resourceId2 = pAnim->AddResource ("/Users/john/ns3/netanim-3.105/ns-3-logo2.png");
181  pAnim->SetBackgroundImage ("/Users/john/ns3/netanim-3.105/ns-3-background.png", 0, 0, 0.2, 0.2, 0.1);
182 
183 
184  // Add a node counter
188 
190 
191  // Set up the actual simulation
193 
194  Simulator::Run ();
195  std::cout << "Animation Trace file created:" << animFile.c_str ()<< std::endl;
197  delete pAnim;
198  return 0;
199 }
200 
ns3::InetSocketAddress
an Inet address class
Definition: inet-socket-address.h:41
rgb::b
uint8_t b
blue
Definition: colors-link-description.cc:37
ns3::AnimationInterface::UINT32_COUNTER
@ UINT32_COUNTER
Definition: animation-interface.h:90
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::AnimationInterface::UpdateNodeColor
void UpdateNodeColor(Ptr< Node > n, uint8_t r, uint8_t g, uint8_t b)
Helper function to update the node color.
Definition: animation-interface.cc:352
ns3::AnimationInterface::UpdateNodeCounter
void UpdateNodeCounter(uint32_t nodeCounterId, uint32_t nodeId, double counter)
Helper function to update a node's counter referenced by the nodeCounterId.
Definition: animation-interface.cc:324
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
resourceId1
uint32_t resourceId1
Definition: resources-counters.cc:46
ns3::PointToPointHelper::SetDeviceAttribute
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
Definition: point-to-point-helper.cc:69
ns3::PointToPointHelper::SetChannelAttribute
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
Definition: point-to-point-helper.cc:75
ns3::AddressValue
AttributeValue implementation for Address.
Definition: address.h:278
ns3::AnimationInterface::SetBackgroundImage
void SetBackgroundImage(std::string fileName, double x, double y, double scaleX, double scaleY, double opacity)
Helper function to set the background image.
Definition: animation-interface.cc:334
rgb
RGB structure.
Definition: colors-link-description.cc:34
ns3::AnimationInterface
Interface to network animator.
Definition: animation-interface.h:76
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
ns3::Ptr< UniformRandomVariable >
ns3::AnimationInterface::UpdateLinkDescription
void UpdateLinkDescription(uint32_t fromNode, uint32_t toNode, std::string linkDescription)
Helper function to update the description for a link.
Definition: animation-interface.cc:368
ns3::Ipv4GlobalRoutingHelper::PopulateRoutingTables
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Definition: ipv4-global-routing-helper.cc:61
resourceId2
uint32_t resourceId2
Definition: resources-counters.cc:47
first.stack
stack
Definition: first.py:41
ns3::Address
a polymophic address class
Definition: address.h:91
nodeCounterIdDouble2
uint32_t nodeCounterIdDouble2
Definition: resources-counters.cc:50
ns3::OnOffHelper
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
ns3::AnimationInterface::AddResource
uint32_t AddResource(std::string resourcePath)
Add a resource such as the path to an image file.
Definition: animation-interface.cc:247
nodeCounterIdUint32
uint32_t nodeCounterIdUint32
Definition: resources-counters.cc:48
second.cmd
cmd
Definition: second.py:35
ns3::AnimationInterface::DOUBLE_COUNTER
@ DOUBLE_COUNTER
Definition: animation-interface.h:91
ns3::AnimationInterface::UpdateNodeSize
void UpdateNodeSize(uint32_t nodeId, double width, double height)
Helper function to update the size of a node.
Definition: animation-interface.cc:344
modify
void modify()
Definition: resources-counters.cc:52
ns3::PointToPointDumbbellHelper
A helper to make it easier to create a dumbbell topology with p2p links.
Definition: point-to-point-dumbbell.h:42
rgb::g
uint8_t g
green
Definition: colors-link-description.cc:36
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
pAnim
AnimationInterface * pAnim
Definition: resources-counters.cc:31
colors
struct rgb colors[]
Definition: resources-counters.cc:40
first.clientApps
clientApps
Definition: first.py:61
rgb::r
uint8_t r
red
Definition: colors-link-description.cc:35
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
nodeCounterIdDouble1
uint32_t nodeCounterIdDouble1
Definition: resources-counters.cc:49
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
ns3::PointToPointHelper
Build a set of PointToPointNetDevice objects.
Definition: point-to-point-helper.h:45
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::Config::SetDefault
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
ns3::AnimationInterface::UpdateNodeImage
void UpdateNodeImage(uint32_t nodeId, uint32_t resourceId)
Helper function to update the image of a node.
Definition: animation-interface.cc:313
ns3::AnimationInterface::UpdateNodeDescription
void UpdateNodeDescription(Ptr< Node > n, std::string descr)
Helper function to update the description for a given node.
Definition: animation-interface.cc:384
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::AnimationInterface::AddNodeCounter
uint32_t AddNodeCounter(std::string counterName, CounterType counterType)
Setup a node counter.
Definition: animation-interface.cc:238
ns3::UniformRandomVariable::GetValue
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
Definition: random-variable-stream.cc:182
ns3::Time::GetSeconds
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:380