diff --git a/examples/aodv_random_disc.cc b/examples/aodv_random_disc.cc new file mode 100644 index 000000000..72f40f5a5 --- /dev/null +++ b/examples/aodv_random_disc.cc @@ -0,0 +1,214 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2009 IITP RAS + * + * 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 + * + * This is an example script for AODV manet routing protocol. + * + * Authors: Pavel Boyko + */ + +#include "ns3/aodv-module.h" +#include "ns3/core-module.h" +#include "ns3/common-module.h" +#include "ns3/node-module.h" +#include "ns3/helper-module.h" +#include "ns3/mobility-module.h" +#include "ns3/contrib-module.h" +#include "ns3/wifi-module.h" +#include "ns3/v4ping-helper.h" +#include +#include + +using namespace ns3; + +/** + * \brief Test script. + */ +class AodvExample +{ +public: + AodvExample (); + /// Configure script parameters, \return true on successful configuration + bool Configure (int argc, char **argv); + /// Run simulation + void Run (); + /// Report results + void Report (std::ostream & os); + +private: + ///\name parameters + //\{ + /// Number of nodes in disc + uint32_t size; + /// Radius of disc, meters + double radius; + /// Simulation time, seconds + double totalTime; + /// Write per-device PCAP traces if true + bool pcap; + //\} + + ///\name network + //\{ + NodeContainer nodes; + NodeContainer node1; + NetDeviceContainer devices; + NetDeviceContainer device1; + Ipv4InterfaceContainer interfaces; + Ipv4InterfaceContainer interface1; + //\} + +private: + void CreateNodes (); + void CreateDevices (); + void InstallInternetStack (); + void InstallApplications (); +}; + +int main (int argc, char **argv) +{ + AodvExample test; + if (! test.Configure(argc, argv)) + NS_FATAL_ERROR ("Configuration failed. Aborted."); + + test.Run (); + test.Report (std::cout); + return 0; +} + +//----------------------------------------------------------------------------- +AodvExample::AodvExample () : + size (64), + radius (50), + totalTime (10), + pcap (true) +{ +} + +bool +AodvExample::Configure (int argc, char **argv) +{ + // Enable AODV logs by default. Comment this if too noisy + // LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL); + + SeedManager::SetSeed(12345); + CommandLine cmd; + + cmd.AddValue ("pcap", "Write PCAP traces.", pcap); + cmd.AddValue ("size", "Number of nodes.", size); + cmd.AddValue ("time", "Simulation time, s.", totalTime); + cmd.AddValue ("radius", "Radius of disc, m", radius); + + cmd.Parse (argc, argv); + return true; +} + +void +AodvExample::Run () +{ + CreateNodes (); + CreateDevices (); + InstallInternetStack (); + InstallApplications (); + + std::cout << "Starting simulation for " << totalTime << " s ...\n"; + + Simulator::Stop (Seconds (totalTime)); + Simulator::Run (); + Simulator::Destroy (); +} + +void +AodvExample::Report (std::ostream &) +{ +} + +void +AodvExample::CreateNodes () +{ + std::cout << "Creating " << (unsigned)size << " nodes in disc " << radius << " m.\n"; + nodes.Create (size); + node1.Create(1); + // Name nodes + for (uint32_t i = 0; i < size; ++i) + { + std::ostringstream os; + os << "node-" << i; + Names::Add (os.str (), nodes.Get (i)); + } + + MobilityHelper mobility; + Ptr positionAlloc = CreateObject(); + positionAlloc->SetRho(UniformVariable (0.0, radius)); + positionAlloc->SetTheta(UniformVariable (0.0, 6.2830)); + positionAlloc->SetX(0.0); + positionAlloc->SetY(0.0); + mobility.SetPositionAllocator(positionAlloc); + mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); + mobility.Install (nodes); + + Ptr pos = CreateObject(); + pos->Add(Vector(150.0 + radius, 0.0, 0.0)); + mobility.SetPositionAllocator(pos); + mobility.Install (node1); +} + +void +AodvExample::CreateDevices () +{ + NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); + wifiMac.SetType ("ns3::AdhocWifiMac"); + YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); + YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); + wifiPhy.SetChannel (wifiChannel.Create ()); + WifiHelper wifi = WifiHelper::Default (); + wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("wifia-6mbs")); + devices = wifi.Install (wifiPhy, wifiMac, nodes); + device1 = wifi.Install (wifiPhy, wifiMac, node1); + + if (pcap) + { + wifiPhy.EnablePcapAll (std::string ("aodv")); + } +} + +void +AodvExample::InstallInternetStack () +{ + AodvHelper aodv; + // you can configure AODV attributes here using aodv.Set(name, value) + InternetStackHelper stack; + stack.SetRoutingHelper (aodv); + stack.Install (nodes); + stack.Install(node1); + Ipv4AddressHelper address; + address.SetBase ("10.0.0.0", "255.0.0.0"); + interfaces = address.Assign (devices); + interface1 = address.Assign (device1); + +} + +void +AodvExample::InstallApplications () +{ + V4PingHelper ping (interface1.GetAddress(0)); + ping.SetAttribute ("Verbose", BooleanValue (true)); + + ApplicationContainer p = ping.Install (nodes.Get (0)); + p.Start (Seconds (0)); + p.Stop (Seconds (totalTime)); +} + diff --git a/examples/wscript b/examples/wscript index 7fb4a5de1..25c62b588 100644 --- a/examples/wscript +++ b/examples/wscript @@ -7,6 +7,9 @@ def build(bld): obj = bld.create_ns3_program('aodv2') obj.source = 'aodv2.cc' + obj = bld.create_ns3_program('aodv_crash') + obj.source = 'aodv_random_disc.cc' + # obj = bld.create_ns3_program('hello-simulator') # obj.source = 'hello-simulator.cc' #