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  */
17 
18 #include "ns3/core-module.h"
19 #include "ns3/network-module.h"
20 #include "ns3/netanim-module.h"
21 #include "ns3/internet-module.h"
22 #include "ns3/point-to-point-module.h"
23 #include "ns3/applications-module.h"
24 #include "ns3/point-to-point-layout-module.h"
25 
26 // Network topology (default)
27 //
28 //        n2 n3 n4              .
29 //         \ | /                .
30 //          \|/                 .
31 //     n1--- n0---n5            .
32 //          /|\                 .
33 //         / | \                .
34 //        n8 n7 n6              .
35 //
36 
37 
38 using namespace ns3;
39 
40 NS_LOG_COMPONENT_DEFINE ("StarAnimation");
41 
42 int
main(int argc,char * argv[])43 main (int argc, char *argv[])
44 {
45 
46   //
47   // Set up some default values for the simulation.
48   //
49   Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (137));
50 
51   // ??? try and stick 15kb/s into the data rate
52   Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("14kb/s"));
53 
54   //
55   // Default number of nodes in the star.  Overridable by command line argument.
56   //
57   uint32_t nSpokes = 8;
58   std::string animFile = "star-animation.xml";
59   uint8_t useIpv6 = 0;
60   Ipv6Address ipv6AddressBase = Ipv6Address("2001::");
61   Ipv6Prefix ipv6AddressPrefix = Ipv6Prefix(64);
62 
63   CommandLine cmd (__FILE__);
64   cmd.AddValue ("nSpokes", "Number of spoke nodes to place in the star", nSpokes);
65   cmd.AddValue ("animFile",  "File Name for Animation Output", animFile);
66   cmd.AddValue ("useIpv6",   "use Ipv6", useIpv6);
67 
68   cmd.Parse (argc, argv);
69 
70   NS_LOG_INFO ("Build star topology.");
71   PointToPointHelper pointToPoint;
72   pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
73   pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
74   PointToPointStarHelper star (nSpokes, pointToPoint);
75 
76   NS_LOG_INFO ("Install internet stack on all nodes.");
77   InternetStackHelper internet;
78   star.InstallStack (internet);
79 
80   NS_LOG_INFO ("Assign IP Addresses.");
81   if (useIpv6 == 0)
82     {
83       star.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"));
84     }
85   else
86     {
87       star.AssignIpv6Addresses (ipv6AddressBase, ipv6AddressPrefix);
88     }
89 
90   NS_LOG_INFO ("Create applications.");
91   //
92   // Create a packet sink on the star "hub" to receive packets.
93   //
94   uint16_t port = 50000;
95   Address hubLocalAddress;
96   if (useIpv6 == 0)
97     {
98       hubLocalAddress = InetSocketAddress (Ipv4Address::GetAny (), port);
99     }
100   else
101     {
102       hubLocalAddress = Inet6SocketAddress (Ipv6Address::GetAny (), port);
103     }
104   PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", hubLocalAddress);
105   ApplicationContainer hubApp = packetSinkHelper.Install (star.GetHub ());
106   hubApp.Start (Seconds (1.0));
107   hubApp.Stop (Seconds (10.0));
108 
109   //
110   // Create OnOff applications to send TCP to the hub, one on each spoke node.
111   //
112   OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
113   onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
114   onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
115 
116   ApplicationContainer spokeApps;
117 
118   for (uint32_t i = 0; i < star.SpokeCount (); ++i)
119     {
120       AddressValue remoteAddress;
121       if (useIpv6 == 0)
122         {
123           remoteAddress = AddressValue(InetSocketAddress (star.GetHubIpv4Address (i), port));
124         }
125       else
126         {
127           remoteAddress = AddressValue(Inet6SocketAddress (star.GetHubIpv6Address (i), port));
128         }
129       onOffHelper.SetAttribute ("Remote", remoteAddress);
130       spokeApps.Add (onOffHelper.Install (star.GetSpokeNode (i)));
131     }
132   spokeApps.Start (Seconds (1.0));
133   spokeApps.Stop (Seconds (10.0));
134 
135   NS_LOG_INFO ("Enable static global routing.");
136   //
137   // Turn on global static routing so we can actually be routed across the star.
138   //
139   if (useIpv6 == 0)
140     {
141       Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
142     }
143 
144   // Set the bounding box for animation
145   star.BoundingBox (1, 1, 100, 100);
146 
147   // Create the animation object and configure for specified output
148   AnimationInterface anim (animFile);
149 
150   NS_LOG_INFO ("Run Simulation.");
151   Simulator::Run ();
152   Simulator::Destroy ();
153   NS_LOG_INFO ("Done.");
154 
155   return 0;
156 }
157