A Discrete-Event Network Simulator
API
colors-link-description.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 void modify ()
47 {
48  std::ostringstream oss;
49  oss << "Update:" << Simulator::Now ().GetSeconds ();
50  pAnim->UpdateLinkDescription (0, 1, oss.str ());
51  pAnim->UpdateLinkDescription (0, 2, oss.str ());
52  pAnim->UpdateLinkDescription (0, 3, oss.str ());
53  pAnim->UpdateLinkDescription (0, 4, oss.str ());
54  pAnim->UpdateLinkDescription (0, 5, oss.str ());
55  pAnim->UpdateLinkDescription (0, 6, oss.str ());
56  pAnim->UpdateLinkDescription (1, 7, oss.str ());
57  pAnim->UpdateLinkDescription (1, 8, oss.str ());
58  pAnim->UpdateLinkDescription (1, 9, oss.str ());
59  pAnim->UpdateLinkDescription (1, 10, oss.str ());
60  pAnim->UpdateLinkDescription (1, 11, oss.str ());
61 
62  // Every update change the node description for node 2
63  std::ostringstream node0Oss;
64  node0Oss << "-----Node:" << Simulator::Now ().GetSeconds ();
65  pAnim->UpdateNodeDescription (2, node0Oss.str ());
66 
67  // Every update change the color for node 4
68  static uint32_t index = 0;
69  index++;
70  if (index == 3)
71  index = 0;
72  struct rgb color = colors[index];
73  for (uint32_t nodeId = 4; nodeId < 12; ++nodeId)
74  pAnim->UpdateNodeColor (nodeId, color.r, color.g, color.b);
75 
76 
77  if (Simulator::Now ().GetSeconds () < 10) // This is important or the simulation
78  // will run endlessly
80 
81 }
82 
83 int main (int argc, char *argv[])
84 {
85  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (512));
86  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("500kb/s"));
87 
88  uint32_t nLeftLeaf = 5;
89  uint32_t nRightLeaf = 5;
90  uint32_t nLeaf = 0; // If non-zero, number of both left and right
91  std::string animFile = "dynamic_linknode.xml" ; // Name of file for animation output
92 
93  CommandLine cmd (__FILE__);
94  cmd.AddValue ("nLeftLeaf", "Number of left side leaf nodes", nLeftLeaf);
95  cmd.AddValue ("nRightLeaf","Number of right side leaf nodes", nRightLeaf);
96  cmd.AddValue ("nLeaf", "Number of left and right side leaf nodes", nLeaf);
97  cmd.AddValue ("animFile", "File Name for Animation Output", animFile);
98 
99  cmd.Parse (argc,argv);
100  if (nLeaf > 0)
101  {
102  nLeftLeaf = nLeaf;
103  nRightLeaf = nLeaf;
104  }
105 
106  // Create the point-to-point link helpers
107  PointToPointHelper pointToPointRouter;
108  pointToPointRouter.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
109  pointToPointRouter.SetChannelAttribute ("Delay", StringValue ("1ms"));
110  PointToPointHelper pointToPointLeaf;
111  pointToPointLeaf.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
112  pointToPointLeaf.SetChannelAttribute ("Delay", StringValue ("1ms"));
113 
114  PointToPointDumbbellHelper d (nLeftLeaf, pointToPointLeaf,
115  nRightLeaf, pointToPointLeaf,
116  pointToPointRouter);
117 
118  // Install Stack
120  d.InstallStack (stack);
121 
122  // Assign IP Addresses
123  d.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"),
124  Ipv4AddressHelper ("10.2.1.0", "255.255.255.0"),
125  Ipv4AddressHelper ("10.3.1.0", "255.255.255.0"));
126 
127  d.BoundingBox (1, 1, 100, 100);
128  // Install on/off app on all right side nodes
129  OnOffHelper clientHelper ("ns3::UdpSocketFactory", Address ());
130  clientHelper.SetAttribute
131  ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
132  clientHelper.SetAttribute
133  ("OffTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
135 
136  for (uint32_t i = 0; i < d.RightCount (); ++i)
137  {
138  // Create an on/off app sending packets to the same leaf right side
139  AddressValue remoteAddress (InetSocketAddress (d.GetLeftIpv4Address (i), 1000));
140  clientHelper.SetAttribute ("Remote", remoteAddress);
141  clientApps.Add (clientHelper.Install (d.GetRight (i)));
142  }
143 
144  clientApps.Start (Seconds (0.0));
145  clientApps.Stop (Seconds (10.0));
146 
147  // Set the bounding box for animation
148 
149 
150  // Create the animation object and configure for specified output
151  pAnim = new AnimationInterface (animFile);
153 
154  // Set up the actual simulation
156 
157  Simulator::Run ();
158  std::cout << "Animation Trace file created:" << animFile.c_str ()<< std::endl;
160  delete pAnim;
161  return 0;
162 }
a polymophic address class
Definition: address.h:91
AttributeValue implementation for Address.
Definition: address.h:278
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.
Definition: command-line.h:229
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(void)
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:43
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 void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
Hold variables of type string.
Definition: string.h:41
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:379
Hold an unsigned integer type.
Definition: uinteger.h:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
clientApps
Definition: first.py:61
stack
Definition: first.py:41
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:35
RGB structure.
uint8_t b
blue
uint8_t g
green
uint8_t r
red