Professional Documents
Culture Documents
Assignment 7
Assignment 7
Assignment 7
#include "ns3/aodv-module.h"
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/mobility-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/wifi-module.h"
#include "ns3/v4ping-helper.h"
#include "ns3/position-allocator.h"
#include "ns3/propagation-loss-model.h"
#include "ns3/applications-module.h"
#include "ns3/v4ping.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/udp-client-server-helper.h"
#include "ns3/udp-server.h"
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
/**
* This script creates 1-dimensional grid topology and then ping last node from the first one:
*
RITESH NAIK NS3 LAB 119CS0141
* [10.0.0.1] <-- step --> [10.0.0.2] <-- step --> [10.0.0.3] <-- step --> [10.0.0.4]
* ping 10.0.0.4
*/
class AodvExample
public:
AodvExample ();
private:
// parameters
uint32_t size;
double step;
double simTime;
bool pcap;
bool printRoutes;
RITESH NAIK NS3 LAB 119CS0141
// network
NodeContainer nodes;
NetDeviceContainer devices;
Ipv4InterfaceContainer interfaces;
Address serverAddress[50];
UdpServer sermon;
WifiMacHelper wifiMac;
private:
};
NS_LOG_COMPONENT_DEFINE ("ManetTest");
RITESH NAIK NS3 LAB 119CS0141
AodvExample test;
test.Run ();
test.Report (std::cout);
return 0;
//-----------------------------------------------------------------------------
AodvExample::AodvExample () :
size (10),
step (5),
simTime (50),
pcap (true),
printRoutes (false)
bool
// LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL);
SeedManager::SetSeed (12345);
RITESH NAIK NS3 LAB 119CS0141
CommandLine cmd;
if (verbose)
return true;
void
AodvExample::Run ()
{
RITESH NAIK NS3 LAB 119CS0141
CreateNodes ();
CreateDevices ();
InstallInternetStack ();
InstallApplications ();
//std::cout << "Starting simulation for " << simTime << " s ...\n";
Simulator::Run ();
Simulator::Destroy ();
void
void
AodvExample::CreateNodes ()
std::cout << "Creating " << (unsigned)size << " nodes with transmission range " << txrange << "m.\n";
nodes.Create (size);
// Name nodes
std::ostringstream os;
std::string line;
ifstream file(topology);
uint16_t i = 0;
double vec[3];
if(file.is_open())
while(getline(file,line))
{
RITESH NAIK NS3 LAB 119CS0141
//std::cout<<line<< '\n';
char *token;
while(token != NULL)
//printf("[%s]\n", token);
vec[i] = atof(token);
i++;
if(i == 3)
//std::cout << "\n" << vec[0] << " " << vec[1] << " " << vec[2] << "\n";
i = 0;
file.close();
else
MobilityHelper mobilityS;
mobilityS.SetPositionAllocator(positionAllocS);
mobilityS.SetMobilityModel("ns3::ConstantPositionMobilityModel"); //whatever it is
mobilityS.Install(nodes);
void
AodvExample::CreateDevices ()
wifiMac.SetType ("ns3::AdhocWifiMac");
wifiChannel.AddPropagationLoss("ns3::RangePropagationLossModel",
WifiHelper wifi;
if (pcap)
}
RITESH NAIK NS3 LAB 119CS0141
void
AodvExample::InstallInternetStack ()
AodvHelper aodv;
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
/*
if (printRoutes)
*/
}
RITESH NAIK NS3 LAB 119CS0141
void
AodvExample::InstallApplications ()
uint16_t i = 0;
uint16_t j = 0;
uint16_t k = 0;
//uint16_t n = 10;
ApplicationContainer apps;
// Create one UdpClient application to send UDP datagrams from node zero to
// node one.
uint32_t maxPacketCount = 3;
uint32_t rxPacketsum = 0;
double Delaysum = 0;
uint32_t txPacketsum = 0;
uint32_t txBytessum = 0;
uint32_t rxBytessum = 0;
uint32_t txTimeFirst = 0;
uint32_t rxTimeLast = 0;
uint32_t lostPacketssum = 0;
RITESH NAIK NS3 LAB 119CS0141
FlowMonitorHelper flowmon;
if (tracing == true)
wifiPhy.EnablePcapAll (outputFilename);
Simulator::Run ();
k = 0;
monitor->CheckForLostPackets ();
rxPacketsum += i->second.rxPackets;
txPacketsum += i->second.txPackets;
txBytessum += i->second.txBytes;
rxBytessum += i->second.rxBytes;
Delaysum += i->second.delaySum.GetSeconds();
lostPacketssum += i->second.lostPackets;
RITESH NAIK NS3 LAB 119CS0141
txTimeFirst += i->second.timeFirstTxPacket.GetSeconds();
rxTimeLast += i->second.timeLastRxPacket.GetSeconds();
k++;
//std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n";
//std::cout << k << " " << " " << rxTimeLast << " " <<txTimeFirst<< "\n\n";
//std::cout<<"rec pkts"<<rxPacketsum<<"\n";
RITESH NAIK NS3 LAB 119CS0141
//std::cout << " Total Tx Packets: " << txPacketsum << "\n";
//std::cout << " Total Rx Packets: " << rxPacketsum << "\n";
std::cout << " Total Packets Lost: " << lostPacketssum << "\n";
std::cout << " Throughput: " << ((rxBytessum * 8.0) / timeDiff)/1024<<" Kbps"<<"\n";
std::cout << " Packets Delivery Ratio: " << (((txPacketsum - lostPacketssum) * 100) /txPacketsum) << "%" <<
"\n";
Simulator::Destroy ();
}
RITESH NAIK NS3 LAB 119CS0141
RITESH NAIK NS3 LAB 119CS0141
RITESH NAIK NS3 LAB 119CS0141
RITESH NAIK NS3 LAB 119CS0141