A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
resources-counters.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Author: John Abraham <john.abraham.in@gmail.com>
5 */
6
7#include "ns3/applications-module.h"
8#include "ns3/core-module.h"
9#include "ns3/internet-module.h"
10#include "ns3/netanim-module.h"
11#include "ns3/network-module.h"
12#include "ns3/point-to-point-layout-module.h"
13#include "ns3/point-to-point-module.h"
14
15#include <iostream>
16
17using namespace ns3;
18
20
21/// RGB structure
22struct Rgb
23{
24 uint8_t r; ///< red
25 uint8_t g; ///< green
26 uint8_t b; ///< blue
27};
28
30 {255, 0, 0}, // Red
31 {0, 255, 0}, // Blue
32 {0, 0, 255}, // Green
33};
34
40
41void
43{
44 std::ostringstream oss;
45 oss << "Update:" << Simulator::Now().GetSeconds();
46 pAnim->UpdateLinkDescription(0, 1, oss.str());
47 pAnim->UpdateLinkDescription(0, 2, oss.str());
48 pAnim->UpdateLinkDescription(0, 3, oss.str());
49 pAnim->UpdateLinkDescription(0, 4, oss.str());
50 pAnim->UpdateLinkDescription(0, 5, oss.str());
51 pAnim->UpdateLinkDescription(0, 6, oss.str());
52 pAnim->UpdateLinkDescription(1, 7, oss.str());
53 pAnim->UpdateLinkDescription(1, 8, oss.str());
54 pAnim->UpdateLinkDescription(1, 9, oss.str());
55 pAnim->UpdateLinkDescription(1, 10, oss.str());
56 pAnim->UpdateLinkDescription(1, 11, oss.str());
57
58 // Every update change the node description for node 2
59 std::ostringstream node0Oss;
60 node0Oss << "-----Node:" << Simulator::Now().GetSeconds();
61 pAnim->UpdateNodeDescription(2, node0Oss.str());
62 static double size = 2;
63 static uint32_t currentResourceId = resourceId1;
64 pAnim->UpdateNodeSize(2, size, size);
65 pAnim->UpdateNodeImage(3, currentResourceId);
66 size *= 1.1;
67 if (size > 20)
68 {
69 size = 1;
70 }
71 pAnim->UpdateNodeSize(3, 10, 10);
72 if (currentResourceId == resourceId1)
73 {
74 currentResourceId = resourceId2;
75 }
76 else
77 {
78 currentResourceId = resourceId1;
79 }
80
81 // Every update change the color for node 4
82 static uint32_t index = 0;
83 index++;
84 if (index == 3)
85 {
86 index = 0;
87 }
88 Rgb color = colors[index];
89 for (uint32_t nodeId = 4; nodeId < 12; ++nodeId)
90 {
91 pAnim->UpdateNodeColor(nodeId, color.r, color.g, color.b);
92 }
93
94 // Update Node Counter for node 0 and node 5, use some random number between 0 to 1000 for value
96 pAnim->UpdateNodeCounter(nodeCounterIdUint32, 0, rv->GetValue(0, 1000));
97 pAnim->UpdateNodeCounter(nodeCounterIdDouble1, 0, rv->GetValue(100.0, 200.0));
98 pAnim->UpdateNodeCounter(nodeCounterIdDouble2, 0, rv->GetValue(300.0, 400.0));
99 pAnim->UpdateNodeCounter(nodeCounterIdUint32, 5, rv->GetValue(0, 1000));
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)
104 { // This is important or the simulation
105 // will run endlessly
107 }
108}
109
110int
111main(int argc, char* argv[])
112{
113 Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(512));
114 Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("500kb/s"));
115
116 uint32_t nLeftLeaf = 5;
117 uint32_t nRightLeaf = 5;
118 uint32_t nLeaf = 0; // If non-zero, number of both left and right
119 std::string animFile = "resources_demo.xml"; // Name of file for animation output
120
121 CommandLine cmd(__FILE__);
122 cmd.AddValue("nLeftLeaf", "Number of left side leaf nodes", nLeftLeaf);
123 cmd.AddValue("nRightLeaf", "Number of right side leaf nodes", nRightLeaf);
124 cmd.AddValue("nLeaf", "Number of left and right side leaf nodes", nLeaf);
125 cmd.AddValue("animFile", "File Name for Animation Output", animFile);
126
127 cmd.Parse(argc, argv);
128 if (nLeaf > 0)
129 {
130 nLeftLeaf = nLeaf;
131 nRightLeaf = nLeaf;
132 }
133
134 // Create the point-to-point link helpers
135 PointToPointHelper pointToPointRouter;
136 pointToPointRouter.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
137 pointToPointRouter.SetChannelAttribute("Delay", StringValue("1ms"));
138 PointToPointHelper pointToPointLeaf;
139 pointToPointLeaf.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
140 pointToPointLeaf.SetChannelAttribute("Delay", StringValue("1ms"));
141
142 PointToPointDumbbellHelper d(nLeftLeaf,
143 pointToPointLeaf,
144 nRightLeaf,
145 pointToPointLeaf,
146 pointToPointRouter);
147
148 // Install Stack
150 d.InstallStack(stack);
151
152 // Assign IP Addresses
153 d.AssignIpv4Addresses(Ipv4AddressHelper("10.1.1.0", "255.255.255.0"),
154 Ipv4AddressHelper("10.2.1.0", "255.255.255.0"),
155 Ipv4AddressHelper("10.3.1.0", "255.255.255.0"));
156
157 d.BoundingBox(1, 1, 100, 100);
158 // Install on/off app on all right side nodes
159 OnOffHelper clientHelper("ns3::UdpSocketFactory", Address());
160 clientHelper.SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
161 clientHelper.SetAttribute("OffTime", StringValue("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
163
164 for (uint32_t i = 0; i < d.RightCount(); ++i)
165 {
166 // Create an on/off app sending packets to the same leaf right side
167 AddressValue remoteAddress(InetSocketAddress(d.GetLeftIpv4Address(i), 1000));
168 clientHelper.SetAttribute("Remote", remoteAddress);
169 clientApps.Add(clientHelper.Install(d.GetRight(i)));
170 }
171
172 clientApps.Start(Seconds(0.0));
173 clientApps.Stop(Seconds(5.0));
174
175 // Set the bounding box for animation
176
177 // Create the animation object and configure for specified output
178 pAnim = new AnimationInterface(animFile);
179 // Provide the absolute path to the resource
180 resourceId1 = pAnim->AddResource("/Users/john/ns3/netanim-3.105/ns-3-logo1.png");
181 resourceId2 = pAnim->AddResource("/Users/john/ns3/netanim-3.105/ns-3-logo2.png");
182 pAnim->SetBackgroundImage("/Users/john/ns3/netanim-3.105/ns-3-background.png",
183 0,
184 0,
185 0.2,
186 0.2,
187 0.1);
188
189 // Add a node counter
196
198
199 // Set up the actual simulation
201
203 std::cout << "Animation Trace file created:" << animFile << std::endl;
205 delete pAnim;
206 return 0;
207}
a polymophic address class
Definition address.h:90
AttributeValue implementation for Address.
Definition address.h:275
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.
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.
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.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
Hold variables of type string.
Definition string.h:45
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
Hold an unsigned integer type.
Definition uinteger.h:34
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
clientApps
Definition first.py:53
stack
Definition first.py:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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 b
blue