A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
colors-link-description.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
35void
37{
38 std::ostringstream oss;
39 oss << "Update:" << Simulator::Now().GetSeconds();
40 pAnim->UpdateLinkDescription(0, 1, oss.str());
41 pAnim->UpdateLinkDescription(0, 2, oss.str());
42 pAnim->UpdateLinkDescription(0, 3, oss.str());
43 pAnim->UpdateLinkDescription(0, 4, oss.str());
44 pAnim->UpdateLinkDescription(0, 5, oss.str());
45 pAnim->UpdateLinkDescription(0, 6, oss.str());
46 pAnim->UpdateLinkDescription(1, 7, oss.str());
47 pAnim->UpdateLinkDescription(1, 8, oss.str());
48 pAnim->UpdateLinkDescription(1, 9, oss.str());
49 pAnim->UpdateLinkDescription(1, 10, oss.str());
50 pAnim->UpdateLinkDescription(1, 11, oss.str());
51
52 // Every update change the node description for node 2
53 std::ostringstream node0Oss;
54 node0Oss << "-----Node:" << Simulator::Now().GetSeconds();
55 pAnim->UpdateNodeDescription(2, node0Oss.str());
56
57 // Every update change the color for node 4
58 static uint32_t index = 0;
59 index++;
60 if (index == 3)
61 {
62 index = 0;
63 }
64 Rgb color = colors[index];
65 for (uint32_t nodeId = 4; nodeId < 12; ++nodeId)
66 {
67 pAnim->UpdateNodeColor(nodeId, color.r, color.g, color.b);
68 }
69
70 if (Simulator::Now().GetSeconds() < 10)
71 { // This is important or the simulation
72 // will run endlessly
74 }
75}
76
77int
78main(int argc, char* argv[])
79{
80 Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(512));
81 Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("500kb/s"));
82
83 uint32_t nLeftLeaf = 5;
84 uint32_t nRightLeaf = 5;
85 uint32_t nLeaf = 0; // If non-zero, number of both left and right
86 std::string animFile = "dynamic_linknode.xml"; // Name of file for animation output
87
88 CommandLine cmd(__FILE__);
89 cmd.AddValue("nLeftLeaf", "Number of left side leaf nodes", nLeftLeaf);
90 cmd.AddValue("nRightLeaf", "Number of right side leaf nodes", nRightLeaf);
91 cmd.AddValue("nLeaf", "Number of left and right side leaf nodes", nLeaf);
92 cmd.AddValue("animFile", "File Name for Animation Output", animFile);
93
94 cmd.Parse(argc, argv);
95 if (nLeaf > 0)
96 {
97 nLeftLeaf = nLeaf;
98 nRightLeaf = nLeaf;
99 }
100
101 // Create the point-to-point link helpers
102 PointToPointHelper pointToPointRouter;
103 pointToPointRouter.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
104 pointToPointRouter.SetChannelAttribute("Delay", StringValue("1ms"));
105 PointToPointHelper pointToPointLeaf;
106 pointToPointLeaf.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
107 pointToPointLeaf.SetChannelAttribute("Delay", StringValue("1ms"));
108
109 PointToPointDumbbellHelper d(nLeftLeaf,
110 pointToPointLeaf,
111 nRightLeaf,
112 pointToPointLeaf,
113 pointToPointRouter);
114
115 // Install Stack
117 d.InstallStack(stack);
118
119 // Assign IP Addresses
120 d.AssignIpv4Addresses(Ipv4AddressHelper("10.1.1.0", "255.255.255.0"),
121 Ipv4AddressHelper("10.2.1.0", "255.255.255.0"),
122 Ipv4AddressHelper("10.3.1.0", "255.255.255.0"));
123
124 d.BoundingBox(1, 1, 100, 100);
125 // Install on/off app on all right side nodes
126 OnOffHelper clientHelper("ns3::UdpSocketFactory", Address());
127 clientHelper.SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
128 clientHelper.SetAttribute("OffTime", StringValue("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
130
131 for (uint32_t i = 0; i < d.RightCount(); ++i)
132 {
133 // Create an on/off app sending packets to the same leaf right side
134 AddressValue remoteAddress(InetSocketAddress(d.GetLeftIpv4Address(i), 1000));
135 clientHelper.SetAttribute("Remote", remoteAddress);
136 clientApps.Add(clientHelper.Install(d.GetRight(i)));
137 }
138
139 clientApps.Start(Seconds(0.0));
140 clientApps.Stop(Seconds(10.0));
141
142 // Set the bounding box for animation
143
144 // Create the animation object and configure for specified output
145 pAnim = new AnimationInterface(animFile);
147
148 // Set up the actual simulation
150
152 std::cout << "Animation Trace file created:" << animFile << std::endl;
154 delete pAnim;
155 return 0;
156}
a polymophic address class
Definition address.h:90
AttributeValue implementation for Address.
Definition address.h:275
Interface to network animator.
void UpdateLinkDescription(uint32_t fromNode, uint32_t toNode, std::string linkDescription)
Helper function to update the description for a link.
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.
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
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.
RGB structure.
uint8_t g
green
uint8_t b
blue