//#include <qos-wifi-mac-helper.h>
#include <fstream>
#include <iostream>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/internet-module.h"
#include "ns3/netanim-module.h"
#include "ns3/olsr-module.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/dsdv-module.h"
#include "ns3/ipv4-global-routing-helper.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("AdHocExample");
int
main(int argc,char *argv[])
{
Time::SetResolution (Time::NS);
Config::SetDefault("ns3::TcpL4Protocol::SocketType", StringValue("ns3::TcpNewReno"));
LogComponentEnable ("AdHocExample", LOG_LEVEL_INFO);
// LogComponentEnable ("TcpL4Protocol", LOG_LEVEL_INFO);
LogComponentEnable ("PacketSink", LOG_LEVEL_ALL);
uint32_t nAdHoc=8;
int nodeSpeed = 17; //in m/s
uint32_t acessPoint = 0;
uint32_t SentPackets =0;
uint32_t ReceivedPackets =0;
uint32_t LostPackets =0;
CommandLine cmd;
cmd.AddValue ("nAdHoc", "Number of wifi ad devices", nAdHoc);
cmd.Parse (argc,argv);
NodeContainer AdHocNode;
AdHocNode.Create(nAdHoc);
//set a wifi
WifiHelper wifi;
wifi.SetStandard(WIFI_PHY_STANDARD_80211a); //
wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager","DataMode",StringValue("OfdmRate6Mbps"));
//wifichannle & wifiphy
YansWifiChannelHelper wifiChannel ;
wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
wifiChannel.AddPropagationLoss ("ns3::TwoRayGroundPropagationLossModel",
"SystemLoss", DoubleValue(5),
"HeightAboveZ", DoubleValue(15));
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
// trasmission power
wifiPhy.Set ("TxPowerStart", DoubleValue(40));
wifiPhy.Set ("TxPowerEnd", DoubleValue(40));
wifiPhy.Set ("TxPowerLevels", UintegerValue(1));
wifiPhy.Set ("TxGain", DoubleValue(0));
wifiPhy.Set ("RxGain", DoubleValue(0));
wifiPhy.Set ("EnergyDetectionThreshold", DoubleValue(-61.8));
wifiPhy.Set ("CcaMode1Threshold", DoubleValue(-64.8));
wifiPhy.Set ("ChannelWidth",UintegerValue(40));
//wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11);
wifiPhy.SetChannel (wifiChannel.Create ());
// Add a non-QoS upper mac
WifiMacHelper wifiMac;
// NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
wifiMac.SetType ("ns3::AdhocWifiMac");
NetDeviceContainer AdHocDevices;
AdHocDevices = wifi.Install(wifiPhy,wifiMac,AdHocNode);
//Enable OLSR
OlsrHelper olsr;
// Install the routing protocol
Ipv4ListRoutingHelper list;
list.Add (olsr, 100);
//1 hop range 230m
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (100.0),
"DeltaY", DoubleValue (100.0),
"GridWidth", UintegerValue (4),
"LayoutType", StringValue ("RowFirst"));
std::stringstream ssSpeed;
ssSpeed << "ns3::UniformRandomVariable[Min=10.0|Max=" << nodeSpeed << "]";
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Mode",StringValue("Time"),
"Time",StringValue("10s"),
"Speed", StringValue (ssSpeed.str ()),
"Bounds", RectangleValue (Rectangle (-1000, 1000, -1000, 1000)));
mobility.Install (AdHocNode);
InternetStackHelper Internet;
Internet.Install(AdHocNode);
Ipv4AddressHelper address;
address.SetBase("195.1.1.0","255.255.255.0");
Ipv4InterfaceContainer AdHocIp;
AdHocIp = address.Assign(AdHocDevices);
NS_LOG_INFO ("Create Applications.");
uint16_t port = 9999;
OnOffHelper onOff1("ns3::TcpSocketFactory",Address(InetSocketAddress(AdHocIp.GetAddress(0),port)));
onOff1.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
onOff1.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
onOff1.SetAttribute ("DataRate", DataRateValue (DataRate("10Mbps")));
onOff1.SetAttribute ("PacketSize", UintegerValue (2048));
for (uint32_t i = 0; i < nAdHoc; ++i) {
if (i != acessPoint) {
ApplicationContainer apps1 = onOff1.Install(AdHocNode.Get(i));
apps1.Start(Seconds(1.0));
apps1.Stop(Seconds(50.0));
}
}
PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", Address(InetSocketAddress (Ipv4Address::GetAny(), port)));
ApplicationContainer apps2 = sinkHelper.Install(AdHocNode.Get(0));
apps2.Start(Seconds(0.0));
apps2.Stop(Seconds(50.0));
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
wifiPhy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
wifiPhy.EnablePcap("accesspoint",AdHocDevices);
//Install FlowMonitor on all nodes
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll ();
Simulator::Stop(Seconds(50.0));
AnimationInterface anim ("1.xml");
anim.SetMaxPktsPerTraceFile(99999999999999);
Simulator::Run();
//this is the code shows the result
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier>(flowmon.GetClassifier());
std::map<FlowId,FlowMonitor::FlowStats> stats = monitor->GetFlowStats();
int j=0;
float AvgThroughput = 0;
Time Delay;
Time Jitter;
NS_LOG_UNCOND("--------Results out put ID wise--------" <<std::endl);
for(std::map <FlowId,FlowMonitor::FlowStats> ::const_iterator iter = stats.begin();iter != stats.end();++iter)
{
Ipv4FlowClassifier::FiveTuple t = classifier -> FindFlow(iter->first);
NS_LOG_UNCOND("-------Flow ID:"<< iter-> first);
NS_LOG_UNCOND("Src Addr"<< t.sourceAddress << "Dst Addr"<< t.destinationAddress);
NS_LOG_UNCOND("Sent Packets ="<< iter->second.txPackets);
NS_LOG_UNCOND("Received Packets =" << iter -> second.rxPackets);
NS_LOG_UNCOND("Lost Packets =" <<iter->second.txPackets-iter->second.rxPackets);
NS_LOG_UNCOND("Packet delivery ratio =" <<iter->second.rxPackets*100/iter->second.txPackets << "%");
NS_LOG_UNCOND("Packet loss ratio =" << (iter->second.txPackets-iter->second.rxPackets)*100/iter->second.txPackets << "%");
NS_LOG_UNCOND("Delay =" << iter -> second.delaySum );
NS_LOG_UNCOND("Jitter =" <<iter->second.jitterSum);
NS_LOG_UNCOND("Throughput = " << iter -> second.rxBytes * 8.0 / (iter -> second.timeLastRxPacket.GetSeconds()-iter-> second.timeFirstTxPacket.GetSeconds())/ 1024 << " Kbps ");
//this valuable hold the value for total results
SentPackets = SentPackets +(iter -> second.txPackets);
ReceivedPackets = ReceivedPackets +(iter -> second.rxPackets);
LostPackets = LostPackets + (iter->second.txPackets-iter->second.rxPackets);
AvgThroughput = AvgThroughput + (iter -> second.rxBytes* 8.0 /(iter -> second.timeLastRxPacket.GetSeconds()-iter-> second.timeFirstTxPacket.GetSeconds()) / 1024);
Delay = Delay + (iter -> second.delaySum);
Jitter = Jitter + (iter->second.jitterSum);
j=j+1;// for flow id increase
}
AvgThroughput = AvgThroughput/j;
Delay = Delay / ReceivedPackets ;
NS_LOG_UNCOND("---------Total Results of the simulation-----"<<std::endl);
NS_LOG_UNCOND("Total Send Packets = " << SentPackets);
NS_LOG_UNCOND("Total Received Packets = " << ReceivedPackets);
NS_LOG_UNCOND("Total Lost Packets =" << LostPackets);
NS_LOG_UNCOND("Packet Loss ratio =" << ((LostPackets*100)/SentPackets)<< "%");
NS_LOG_UNCOND("Packet delivery ratio =" << ((ReceivedPackets*100)/SentPackets)<< "%");
NS_LOG_UNCOND("Average Throughput = " << AvgThroughput);
NS_LOG_UNCOND("Delay = " << Delay);
NS_LOG_UNCOND("End to End Jitter delay =" << Jitter);
NS_LOG_UNCOND("Total Flow ID =" << j);
monitor->SerializeToXmlFile("TEXT.xml", true, true);
Simulator::Destroy();
return 0;
}