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 
33 struct rgb {
34  uint8_t r;
35  uint8_t g;
36  uint8_t b;
37 };
38 
39 struct rgb colors [] = {
40  { 255, 0, 0 }, // Red
41  { 0, 255, 0 }, // Blue
42  { 0, 0, 255 } // Green
43  };
44 
45 uint32_t resourceId1;
46 uint32_t resourceId2;
50 
51 void modify ()
52 {
53  std::ostringstream oss;
54  oss << "Update:" << Simulator::Now ().GetSeconds ();
55  pAnim->UpdateLinkDescription (0, 1, oss.str ());
56  pAnim->UpdateLinkDescription (0, 2, oss.str ());
57  pAnim->UpdateLinkDescription (0, 3, oss.str ());
58  pAnim->UpdateLinkDescription (0, 4, oss.str ());
59  pAnim->UpdateLinkDescription (0, 5, oss.str ());
60  pAnim->UpdateLinkDescription (0, 6, oss.str ());
61  pAnim->UpdateLinkDescription (1, 7, oss.str ());
62  pAnim->UpdateLinkDescription (1, 8, oss.str ());
63  pAnim->UpdateLinkDescription (1, 9, oss.str ());
64  pAnim->UpdateLinkDescription (1, 10, oss.str ());
65  pAnim->UpdateLinkDescription (1, 11, oss.str ());
66 
67  // Every update change the node description for node 2
68  std::ostringstream node0Oss;
69  node0Oss << "-----Node:" << Simulator::Now ().GetSeconds ();
70  pAnim->UpdateNodeDescription (2, node0Oss.str ());
71  static double size = 2;
72  static uint32_t currentResourceId = resourceId1;
73  pAnim->UpdateNodeSize (2, size, size);
74  pAnim->UpdateNodeImage (3, currentResourceId);
75  size *= 1.1;
76  if (size > 20)
77  size = 1;
78  pAnim->UpdateNodeSize (3, 10, 10);
79  if (currentResourceId == resourceId1)
80  currentResourceId = resourceId2;
81  else
82  currentResourceId = resourceId1;
83 
84  // Every update change the color for node 4
85  static uint32_t index = 0;
86  index++;
87  if (index == 3)
88  index = 0;
89  struct rgb color = colors[index];
90  for (uint32_t nodeId = 4; nodeId < 12; ++nodeId)
91  pAnim->UpdateNodeColor (nodeId, color.r, color.g, color.b);
92 
93  // Update Node Counter for node 0 and node 5, use some random number between 0 to 1000 for value
94  Ptr <UniformRandomVariable> rv = CreateObject<UniformRandomVariable> ();
95  pAnim->UpdateNodeCounter (nodeCounterIdUint32, 0, rv->GetValue (0, 1000));
96  pAnim->UpdateNodeCounter (nodeCounterIdDouble1, 0, rv->GetValue (100.0, 200.0));
97  pAnim->UpdateNodeCounter (nodeCounterIdDouble2, 0, rv->GetValue (300.0, 400.0));
98  pAnim->UpdateNodeCounter (nodeCounterIdUint32, 5, rv->GetValue (0, 1000));
99  pAnim->UpdateNodeCounter (nodeCounterIdDouble1, 5, rv->GetValue (100.0, 200.0));
100  pAnim->UpdateNodeCounter (nodeCounterIdDouble2, 5, rv->GetValue (300.0, 400.0));
101 
102  if (Simulator::Now ().GetSeconds () < 10) // This is important or the simulation
103  // will run endlessly
105 
106 }
107 
108 int main (int argc, char *argv[])
109 {
110  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (512));
111  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("500kb/s"));
112 
113  uint32_t nLeftLeaf = 5;
114  uint32_t nRightLeaf = 5;
115  uint32_t nLeaf = 0; // If non-zero, number of both left and right
116  std::string animFile = "resources_demo.xml" ; // Name of file for animation output
117 
119  cmd.AddValue ("nLeftLeaf", "Number of left side leaf nodes", nLeftLeaf);
120  cmd.AddValue ("nRightLeaf","Number of right side leaf nodes", nRightLeaf);
121  cmd.AddValue ("nLeaf", "Number of left and right side leaf nodes", nLeaf);
122  cmd.AddValue ("animFile", "File Name for Animation Output", animFile);
123 
124  cmd.Parse (argc,argv);
125  if (nLeaf > 0)
126  {
127  nLeftLeaf = nLeaf;
128  nRightLeaf = nLeaf;
129  }
130 
131  // Create the point-to-point link helpers
132  PointToPointHelper pointToPointRouter;
133  pointToPointRouter.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
134  pointToPointRouter.SetChannelAttribute ("Delay", StringValue ("1ms"));
135  PointToPointHelper pointToPointLeaf;
136  pointToPointLeaf.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
137  pointToPointLeaf.SetChannelAttribute ("Delay", StringValue ("1ms"));
138 
139  PointToPointDumbbellHelper d (nLeftLeaf, pointToPointLeaf,
140  nRightLeaf, pointToPointLeaf,
141  pointToPointRouter);
142 
143  // Install Stack
145  d.InstallStack (stack);
146 
147  // Assign IP Addresses
148  d.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"),
149  Ipv4AddressHelper ("10.2.1.0", "255.255.255.0"),
150  Ipv4AddressHelper ("10.3.1.0", "255.255.255.0"));
151 
152  d.BoundingBox (1, 1, 100, 100);
153  // Install on/off app on all right side nodes
154  OnOffHelper clientHelper ("ns3::UdpSocketFactory", Address ());
155  clientHelper.SetAttribute
156  ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
157  clientHelper.SetAttribute
158  ("OffTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
160 
161  for (uint32_t i = 0; i < d.RightCount (); ++i)
162  {
163  // Create an on/off app sending packets to the same leaf right side
164  AddressValue remoteAddress (InetSocketAddress (d.GetLeftIpv4Address (i), 1000));
165  clientHelper.SetAttribute ("Remote", remoteAddress);
166  clientApps.Add (clientHelper.Install (d.GetRight (i)));
167  }
168 
169  clientApps.Start (Seconds (0.0));
170  clientApps.Stop (Seconds (5.0));
171 
172  // Set the bounding box for animation
173 
174 
175  // Create the animation object and configure for specified output
176  pAnim = new AnimationInterface (animFile);
177  // Provide the absolute path to the resource
178  resourceId1 = pAnim->AddResource ("/Users/john/ns3/netanim-3.105/ns-3-logo1.png");
179  resourceId2 = pAnim->AddResource ("/Users/john/ns3/netanim-3.105/ns-3-logo2.png");
180  pAnim->SetBackgroundImage ("/Users/john/ns3/netanim-3.105/ns-3-background.png", 0, 0, 0.2, 0.2, 0.1);
181 
182 
183  // Add a node counter
187 
189 
190  // Set up the acutal simulation
192 
193  Simulator::Run ();
194  std::cout << "Animation Trace file created:" << animFile.c_str ()<< std::endl;
196  delete pAnim;
197  return 0;
198 }
199 
holds a vector of ns3::Application pointers.
void UpdateNodeImage(uint32_t nodeId, uint32_t resourceId)
Helper function to update the image of a node.
an Inet address class
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Hold variables of type string.
Definition: string.h:41
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
uint32_t resourceId1
aggregate IP/TCP/UDP functionality to existing Nodes.
Build a set of PointToPointNetDevice objects.
void UpdateNodeSize(uint32_t nodeId, double width, double height)
Helper function to update the size of a node.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
tuple cmd
Definition: second.py:35
void UpdateNodeColor(Ptr< Node > n, uint8_t r, uint8_t g, uint8_t b)
Helper function to update the node color.
a polymophic address class
Definition: address.h:90
tuple clientApps
Definition: first.py:54
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:341
void SetBackgroundImage(std::string fileName, double x, double y, double scaleX, double scaleY, double opacity)
Helper function to set the background image.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1238
void UpdateNodeCounter(uint32_t nodeCounterId, uint32_t nodeId, double counter)
Helper function to update a node's counter referenced by the nodeCounterId.
uint32_t AddNodeCounter(std::string counterName, CounterType counterType)
Setup a node counter.
Hold an unsigned integer type.
Definition: uinteger.h:44
uint32_t nodeCounterIdDouble2
uint32_t resourceId2
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
uint32_t nodeCounterIdUint32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void UpdateLinkDescription(uint32_t fromNode, uint32_t toNode, std::string linkDescription)
Helper function to update the description for a link.
uint32_t AddResource(std::string resourcePath)
Add a resource such as the path to an image file.
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
AnimationInterface * pAnim
struct rgb colors[]
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
void modify()
tuple stack
Definition: first.py:34
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
AttributeValue implementation for Address.
Definition: address.h:278
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:495
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
Interface to network animator.
uint32_t nodeCounterIdDouble1
A helper to make it easier to create a dumbbell topology with p2p links.
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void UpdateNodeDescription(Ptr< Node > n, std::string descr)
Helper function to update the description for a given node.