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