A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
resources_demo.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  // After 5 seconds mark node 3 as invisible
68  if (Simulator::Now ().GetSeconds () > 5)
69  {
70  pAnim->ShowNode (3, false);
71  }
72 
73  // Every update change the node description for node 2
74  std::ostringstream node0Oss;
75  node0Oss << "-----Node:" << Simulator::Now ().GetSeconds ();
76  pAnim->UpdateNodeDescription (2, node0Oss.str ());
77  static double size = 2;
78  static uint32_t currentResourceId = resourceId1;
79  pAnim->UpdateNodeSize (2, size, size);
80  pAnim->UpdateNodeImage (3, currentResourceId);
81  size *= 1.1;
82  if (size > 20)
83  size = 1;
84  pAnim->UpdateNodeSize (3, 10, 10);
85  if (currentResourceId == resourceId1)
86  currentResourceId = resourceId2;
87  else
88  currentResourceId = resourceId1;
89 
90  // Every update change the color for node 4
91  static uint32_t index = 0;
92  index++;
93  if (index == 3)
94  index = 0;
95  struct rgb color = colors[index];
96  for (uint32_t nodeId = 4; nodeId < 12; ++nodeId)
97  pAnim->UpdateNodeColor (nodeId, color.r, color.g, color.b);
98 
99  // Update Node Counter for node 0 and node 5, use some random number between 0 to 1000 for value
100  Ptr <UniformRandomVariable> rv = CreateObject<UniformRandomVariable> ();
102  pAnim->UpdateNodeCounter (nodeCounterIdDouble1, 0, rv->GetValue (100.0, 200.0));
103  pAnim->UpdateNodeCounter (nodeCounterIdDouble2, 0, rv->GetValue (300.0, 400.0));
105  pAnim->UpdateNodeCounter (nodeCounterIdDouble1, 5, rv->GetValue (100.0, 200.0));
106  pAnim->UpdateNodeCounter (nodeCounterIdDouble2, 5, rv->GetValue (300.0, 400.0));
107 
108  if (Simulator::Now ().GetSeconds () < 10) // This is important or the simulation
109  // will run endlessly
110  Simulator::Schedule (Seconds (0.1), modify);
111 
112 }
113 
114 int main (int argc, char *argv[])
115 {
116  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (512));
117  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("500kb/s"));
118 
119  uint32_t nLeftLeaf = 5;
120  uint32_t nRightLeaf = 5;
121  uint32_t nLeaf = 0; // If non-zero, number of both left and right
122  std::string animFile = "resources_demo.xml" ; // Name of file for animation output
123 
124  CommandLine cmd;
125  cmd.AddValue ("nLeftLeaf", "Number of left side leaf nodes", nLeftLeaf);
126  cmd.AddValue ("nRightLeaf","Number of right side leaf nodes", nRightLeaf);
127  cmd.AddValue ("nLeaf", "Number of left and right side leaf nodes", nLeaf);
128  cmd.AddValue ("animFile", "File Name for Animation Output", animFile);
129 
130  cmd.Parse (argc,argv);
131  if (nLeaf > 0)
132  {
133  nLeftLeaf = nLeaf;
134  nRightLeaf = nLeaf;
135  }
136 
137  // Create the point-to-point link helpers
138  PointToPointHelper pointToPointRouter;
139  pointToPointRouter.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
140  pointToPointRouter.SetChannelAttribute ("Delay", StringValue ("1ms"));
141  PointToPointHelper pointToPointLeaf;
142  pointToPointLeaf.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
143  pointToPointLeaf.SetChannelAttribute ("Delay", StringValue ("1ms"));
144 
145  PointToPointDumbbellHelper d (nLeftLeaf, pointToPointLeaf,
146  nRightLeaf, pointToPointLeaf,
147  pointToPointRouter);
148 
149  // Install Stack
151  d.InstallStack (stack);
152 
153  // Assign IP Addresses
154  d.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"),
155  Ipv4AddressHelper ("10.2.1.0", "255.255.255.0"),
156  Ipv4AddressHelper ("10.3.1.0", "255.255.255.0"));
157 
158  d.BoundingBox (1, 1, 100, 100);
159  // Install on/off app on all right side nodes
160  OnOffHelper clientHelper ("ns3::UdpSocketFactory", Address ());
161  clientHelper.SetAttribute
162  ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.,Max=1.]"));
163  clientHelper.SetAttribute
164  ("OffTime", StringValue ("ns3::UniformRandomVariable[Min=0.,Max=1.]"));
166 
167  for (uint32_t i = 0; i < d.RightCount (); ++i)
168  {
169  // Create an on/off app sending packets to the same leaf right side
170  AddressValue remoteAddress (InetSocketAddress (d.GetLeftIpv4Address (i), 1000));
171  clientHelper.SetAttribute ("Remote", remoteAddress);
172  clientApps.Add (clientHelper.Install (d.GetRight (i)));
173  }
174 
175  clientApps.Start (Seconds (0.0));
176  clientApps.Stop (Seconds (5.0));
177 
178  // Set the bounding box for animation
179 
180 
181  // Create the animation object and configure for specified output
182  pAnim = new AnimationInterface (animFile);
183  // Provide the absolute path to the resource
184  resourceId1 = pAnim->AddResource ("/Users/john/ns3/netanim-3.105/ns-3-logo1.png");
185  resourceId2 = pAnim->AddResource ("/Users/john/ns3/netanim-3.105/ns-3-logo2.png");
186  pAnim->SetBackgroundImage ("/Users/john/ns3/netanim-3.105/ns-3-background.png", 0, 0, 0.2, 0.2, 0.1);
187 
188 
189  // Add a node counter
193 
194  Simulator::Schedule (Seconds (0.1), modify);
195 
196  // Set up the acutal simulation
198 
199  Simulator::Run ();
200  std::cout << "Animation Trace file created:" << animFile.c_str ()<< std::endl;
202  delete pAnim;
203  return 0;
204 }
205 
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:18
uint32_t resourceId1
void BoundingBox(double ulx, double uly, double lrx, double lry)
Sets up the node canvas locations for every node in the dumbbell.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
uint32_t resourceId2
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
aggregate IP/TCP/UDP functionality to existing Nodes.
AnimationInterface * pAnim
Build a set of PointToPointNetDevice objects.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
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
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:86
tuple clientApps
Definition: first.py:53
double GetSeconds(void) const
Definition: nstime.h:272
void SetBackgroundImage(std::string fileName, double x, double y, double scaleX, double scaleY, double opacity)
Helper function to set the background image.
struct rgb colors[]
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:46
void modify()
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:177
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
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.
uint32_t nodeCounterIdUint32
uint8_t g
double GetValue(double min, double max)
Returns a random double from the uniform distribution with the specified range.
void AssignIpv4Addresses(Ipv4AddressHelper leftIp, Ipv4AddressHelper rightIp, Ipv4AddressHelper routerIp)
uint32_t nodeCounterIdDouble1
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
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.
hold objects of type ns3::Address
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
uint8_t b
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:435
void InstallStack(InternetStackHelper stack)
Interface to network animator.
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.
uint32_t nodeCounterIdDouble2
void ShowNode(uint32_t nodeId, bool show=true)
Helper function to show/hide a node.
Ipv4Address GetLeftIpv4Address(uint32_t i) const
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void UpdateNodeDescription(Ptr< Node > n, std::string descr)
Helper function to update the description for a given node.
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
int main(int argc, char *argv[])
uint8_t r