forked from keeferrourke/f20-gcc-ns3-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
on-off-app.cc
172 lines (144 loc) · 5.78 KB
/
on-off-app.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation;
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include <inttypes.h>
#include <sysexits.h>
#include "ns3/animation-interface.h"
#include "ns3/aodv-helper.h"
#include "ns3/command-line.h"
#include "ns3/config.h"
#include "ns3/core-module.h"
#include "ns3/double.h"
#include "ns3/dsdv-helper.h"
#include "ns3/internet-stack-helper.h"
#include "ns3/ipv4-address-helper.h"
#include "ns3/ipv4-interface-container.h"
#include "ns3/ipv4-routing-helper.h"
#include "ns3/ipv6-address-helper.h"
#include "ns3/log-macros-enabled.h"
#include "ns3/mobility-helper.h"
#include "ns3/mobility-model.h"
#include "ns3/netanim-module.h"
#include "ns3/node-container.h"
#include "ns3/nstime.h"
#include "ns3/object.h"
#include "ns3/on-off-helper.h"
#include "ns3/packet-socket-address.h"
#include "ns3/packet-socket-factory.h"
#include "ns3/random-variable-stream.h"
#include "ns3/random-walk-2d-mobility-model.h"
#include "ns3/rng-seed-manager.h"
#include "ns3/udp-socket-factory.h"
#include "ns3/wifi-phy.h"
#include "ns3/wifi-standards.h"
#include "ns3/yans-wifi-helper.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("GCCDEMO");
/// \brief This simulation is an interesting first look at mesh networks and
/// how partitions form in the network. It is probably the simplest full
/// demonstration of ns-3 features:
///
/// - Mobility
/// - Ad hoc wifi
/// - Signal propagation loss
/// - IP networking
/// - Network applications
///
int
main (int argc, char *argv[])
{
Time::SetResolution (Time::NS);
uint32_t optNodes = 32;
double optRuntime = 300; // seconds
double optPauseTime = 10; // seconds
double optVelocity = 2; // m/sec
uint32_t seed = 1;
CommandLine cmd;
cmd.AddValue ("seed", "RNG seed", seed);
cmd.AddValue ("nodes", "Number of nodes", optNodes);
cmd.AddValue ("runtime", "Simulation runtime", optRuntime);
cmd.AddValue ("velocity", "Node velocity", optVelocity);
cmd.AddValue ("pause-time", "How long nodes should wait at waypoints",
optPauseTime);
cmd.Parse (argc, argv);
RngSeedManager::SetSeed (seed);
// Instantiate nodes.
NodeContainer adhocNodes;
adhocNodes.Create (optNodes);
// Define mobility model.
Ptr<ConstantRandomVariable> velocityGenerator
= CreateObject<ConstantRandomVariable> ();
velocityGenerator->SetAttribute ("Constant", DoubleValue (optVelocity));
Ptr<ConstantRandomVariable> pauseTimeGenerator
= CreateObject<ConstantRandomVariable> ();
pauseTimeGenerator->SetAttribute ("Constant", DoubleValue (optPauseTime));
ns3::Ptr<ns3::UniformRandomVariable> x
= ns3::CreateObject<ns3::UniformRandomVariable> ();
x->SetAttribute ("Min", ns3::DoubleValue (0.0));
x->SetAttribute ("Max", ns3::DoubleValue (100.0));
ns3::Ptr<ns3::UniformRandomVariable> y
= ns3::CreateObject<ns3::UniformRandomVariable> ();
y->SetAttribute ("Min", ns3::DoubleValue (0.0));
y->SetAttribute ("Max", ns3::DoubleValue (100.0));
auto alloc = ns3::CreateObject<ns3::RandomRectanglePositionAllocator> ();
alloc->SetX (x);
alloc->SetY (y);
MobilityHelper mobility;
mobility.SetPositionAllocator (alloc);
mobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel", "Speed",
PointerValue (velocityGenerator), "Pause",
PointerValue (pauseTimeGenerator),
"PositionAllocator", PointerValue (alloc));
mobility.Install (adhocNodes);
// Set up wifi.
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO);
auto wifiChannel = YansWifiChannelHelper::Default ();
wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
wifiChannel.AddPropagationLoss ("ns3::RangePropagationLossModel", "MaxRange",
DoubleValue (15.0));
wifiPhy.SetChannel (wifiChannel.Create ());
WifiMacHelper wifiMac;
wifiMac.SetType ("ns3::AdhocWifiMac");
WifiHelper wifi;
wifi.SetStandard (WIFI_STANDARD_80211b);
NS_LOG_UNCOND ("Assigning MAC addresses in ad-hoc mode...");
auto adhocDevices = wifi.Install (wifiPhy, wifiMac, adhocNodes);
NS_LOG_UNCOND ("Setting up Internet stacks...");
InternetStackHelper internet;
DsdvHelper dsdv;
internet.SetRoutingHelper (dsdv);
internet.Install (adhocNodes);
Ipv4AddressHelper adhocAddresses;
adhocAddresses.SetBase ("1.1.1.0", "255.255.255.255");
auto adhocInterfaces = adhocAddresses.Assign (adhocDevices);
internet.EnablePcapIpv4All ("captures/on-off-app");
PacketSocketAddress socket;
socket.SetSingleDevice (adhocDevices.Get (0)->GetIfIndex ());
socket.SetPhysicalAddress (adhocDevices.Get (1)->GetAddress ());
socket.SetProtocol (1);
NS_LOG_UNCOND ("Installing network applications...");
OnOffHelper appHelper ("ns3::UdpSocketFactory", Address (socket));
appHelper.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer apps = appHelper.Install (adhocNodes.Get (0));
apps.Start (Seconds (1.0));
apps.Stop (Seconds (optRuntime - 1.0));
NS_LOG_UNCOND ("Running simulation...");
AnimationInterface anim ("animations/gcc-on-off-app.xml");
Simulator::Stop (Seconds (optRuntime));
Simulator::Run ();
Simulator::Destroy ();
NS_LOG_UNCOND ("Done.");
return EX_OK;
}