|
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: Josh Pelkey <jpelkey@gatech.edu> |
|
17 */ |
|
18 |
|
19 #include <iostream> |
|
20 |
|
21 #include "ns3/core-module.h" |
|
22 #include "ns3/simulator-module.h" |
|
23 #include "ns3/node-module.h" |
|
24 #include "ns3/helper-module.h" |
|
25 |
|
26 using namespace ns3; |
|
27 |
|
28 int main (int argc, char *argv[]) |
|
29 { |
|
30 Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (512)); |
|
31 Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("500kb/s")); |
|
32 |
|
33 uint32_t xSize = 5; |
|
34 uint32_t ySize = 5; |
|
35 uint16_t port = 0; |
|
36 std::string animFile; |
|
37 |
|
38 CommandLine cmd; |
|
39 cmd.AddValue ("xSize", "Number of rows of nodes", xSize); |
|
40 cmd.AddValue ("ySize", "Number of columns of nodes", ySize); |
|
41 cmd.AddValue ("port", "Port Number for Remote Animation", port); |
|
42 cmd.AddValue ("animFile", "File Name for Animation Output", animFile); |
|
43 |
|
44 cmd.Parse (argc,argv); |
|
45 if (xSize < 1 || ySize < 1 || (xSize < 2 && ySize < 2)) |
|
46 { |
|
47 NS_FATAL_ERROR ("Need more nodes for grid."); |
|
48 } |
|
49 |
|
50 PointToPointHelper pointToPoint; |
|
51 pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); |
|
52 pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); |
|
53 |
|
54 // Create Grid |
|
55 PointToPointGridHelper grid (xSize, ySize, pointToPoint); |
|
56 |
|
57 // Install stack on Grid |
|
58 InternetStackHelper stack; |
|
59 grid.InstallStack (stack); |
|
60 |
|
61 // Assign Addresses to Grid |
|
62 grid.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"), |
|
63 Ipv4AddressHelper ("10.2.1.0", "255.255.255.0")); |
|
64 |
|
65 |
|
66 OnOffHelper clientHelper ("ns3::UdpSocketFactory", Address ()); |
|
67 clientHelper.SetAttribute |
|
68 ("OnTime", RandomVariableValue (ConstantVariable (1))); |
|
69 clientHelper.SetAttribute |
|
70 ("OffTime", RandomVariableValue (ConstantVariable (0))); |
|
71 ApplicationContainer clientApps; |
|
72 |
|
73 // Create an on/off app sending packets |
|
74 AddressValue remoteAddress (InetSocketAddress (grid.GetIpv4Address (xSize-1,ySize-1), 1000)); |
|
75 clientHelper.SetAttribute ("Remote", remoteAddress); |
|
76 clientApps.Add (clientHelper.Install (grid.GetNode (0,0))); |
|
77 |
|
78 clientApps.Start (Seconds (0.0)); |
|
79 clientApps.Stop (Seconds (1.5)); |
|
80 |
|
81 // Set the bounding box for animation |
|
82 grid.BoundingBox (1, 1, 10, 10); |
|
83 |
|
84 // Create the animation object and configure for specified output |
|
85 AnimationInterface anim; |
|
86 if (port > 0) |
|
87 { |
|
88 anim.SetServerPort (port); |
|
89 } |
|
90 else if (!animFile.empty ()) |
|
91 { |
|
92 anim.SetOutputFile (animFile); |
|
93 } |
|
94 anim.StartAnimation (); |
|
95 |
|
96 // Set up the actual simulation |
|
97 Ipv4GlobalRoutingHelper::PopulateRoutingTables (); |
|
98 |
|
99 std::cout << "Running the simulation" << std::endl; |
|
100 Simulator::Run (); |
|
101 std::cout << "Destroying the simulation" << std::endl; |
|
102 Simulator::Destroy (); |
|
103 std::cout << "Stopping the animation" << std::endl; |
|
104 anim.StopAnimation(); |
|
105 return 0; |
|
106 } |