A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
dynamic_linknode.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 void modify ()
46 {
47  std::ostringstream oss;
48  oss << "Update:" << Simulator::Now ().GetSeconds ();
49  pAnim->UpdateLinkDescription (0, 1, oss.str ());
50  pAnim->UpdateLinkDescription (0, 2, oss.str ());
51  pAnim->UpdateLinkDescription (0, 3, oss.str ());
52  pAnim->UpdateLinkDescription (0, 4, oss.str ());
53  pAnim->UpdateLinkDescription (0, 5, oss.str ());
54  pAnim->UpdateLinkDescription (0, 6, oss.str ());
55  pAnim->UpdateLinkDescription (1, 7, oss.str ());
56  pAnim->UpdateLinkDescription (1, 8, oss.str ());
57  pAnim->UpdateLinkDescription (1, 9, oss.str ());
58  pAnim->UpdateLinkDescription (1, 10, oss.str ());
59  pAnim->UpdateLinkDescription (1, 11, oss.str ());
60 
61  // After 5 seconds mark node 3 as invisible
62  if (Simulator::Now ().GetSeconds () > 5)
63  {
64  pAnim->ShowNode (3, false);
65  }
66 
67  // Every update change the node description for node 2
68  std::ostringstream node0Oss;
69  node0Oss << "-----Node:" << Simulator::Now ().GetSeconds ();
70  pAnim->UpdateNodeDescription (2, node0Oss.str ());
71 
72  // Every update change the color for node 4
73  static uint32_t index = 0;
74  index++;
75  if (index == 3)
76  index = 0;
77  struct rgb color = colors[index];
78  for (uint32_t nodeId = 4; nodeId < 12; ++nodeId)
79  pAnim->UpdateNodeColor (nodeId, color.r, color.g, color.b);
80 
81 
82  if (Simulator::Now ().GetSeconds () < 10) // This is important or the simulation
83  // will run endlessly
85 
86 }
87 
88 int main (int argc, char *argv[])
89 {
90  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (512));
91  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("500kb/s"));
92 
93  uint32_t nLeftLeaf = 5;
94  uint32_t nRightLeaf = 5;
95  uint32_t nLeaf = 0; // If non-zero, number of both left and right
96  std::string animFile = "dynamic_linknode.xml" ; // Name of file for animation output
97 
98  CommandLine cmd;
99  cmd.AddValue ("nLeftLeaf", "Number of left side leaf nodes", nLeftLeaf);
100  cmd.AddValue ("nRightLeaf","Number of right side leaf nodes", nRightLeaf);
101  cmd.AddValue ("nLeaf", "Number of left and right side leaf nodes", nLeaf);
102  cmd.AddValue ("animFile", "File Name for Animation Output", animFile);
103 
104  cmd.Parse (argc,argv);
105  if (nLeaf > 0)
106  {
107  nLeftLeaf = nLeaf;
108  nRightLeaf = nLeaf;
109  }
110 
111  // Create the point-to-point link helpers
112  PointToPointHelper pointToPointRouter;
113  pointToPointRouter.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
114  pointToPointRouter.SetChannelAttribute ("Delay", StringValue ("1ms"));
115  PointToPointHelper pointToPointLeaf;
116  pointToPointLeaf.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
117  pointToPointLeaf.SetChannelAttribute ("Delay", StringValue ("1ms"));
118 
119  PointToPointDumbbellHelper d (nLeftLeaf, pointToPointLeaf,
120  nRightLeaf, pointToPointLeaf,
121  pointToPointRouter);
122 
123  // Install Stack
124  InternetStackHelper stack;
125  d.InstallStack (stack);
126 
127  // Assign IP Addresses
128  d.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"),
129  Ipv4AddressHelper ("10.2.1.0", "255.255.255.0"),
130  Ipv4AddressHelper ("10.3.1.0", "255.255.255.0"));
131 
132  d.BoundingBox (1, 1, 100, 100);
133  // Install on/off app on all right side nodes
134  OnOffHelper clientHelper ("ns3::UdpSocketFactory", Address ());
135  clientHelper.SetAttribute
136  ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.,Max=1.]"));
137  clientHelper.SetAttribute
138  ("OffTime", StringValue ("ns3::UniformRandomVariable[Min=0.,Max=1.]"));
139  ApplicationContainer clientApps;
140 
141  for (uint32_t i = 0; i < d.RightCount (); ++i)
142  {
143  // Create an on/off app sending packets to the same leaf right side
144  AddressValue remoteAddress (InetSocketAddress (d.GetLeftIpv4Address (i), 1000));
145  clientHelper.SetAttribute ("Remote", remoteAddress);
146  clientApps.Add (clientHelper.Install (d.GetRight (i)));
147  }
148 
149  clientApps.Start (Seconds (0.0));
150  clientApps.Stop (Seconds (10.0));
151 
152  // Set the bounding box for animation
153 
154 
155  // Create the animation object and configure for specified output
156  pAnim = new AnimationInterface (animFile);
158 
159  // Set up the acutal simulation
161 
162  Simulator::Run ();
163  std::cout << "Animation Trace file created:" << animFile.c_str ()<< std::endl;
165  delete pAnim;
166  return 0;
167 }