Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include ns 3 / core - module.h #include ns 3 / network - module.h #include ns 3 / mobility -

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/internet-module.h"
#include "ns3/applications-module.h"
#include "ns3/aodv-module.h"
#include "ns3/olsr-module.h"// Include the OLSR module
using namespace ns3;
enum RoutingProtocol { AODV, OLSR, HYBRID };
struct RouteEntry {
Ipv4Address dest;
Ipv4Address nextHop;
RoutingProtocol protocol;
RouteEntry(Ipv4Address dest, Ipv4Address nextHop, RoutingProtocol protocol)
: dest(dest), nextHop(nextHop), protocol(protocol){}
};
std::vector routingTable;
bool IsAODVZone(Ptr node){
// Placeholder logic for AODV zone determination
return false;
}
void CustomAODVRouteRequest(Ptr source, Ipv4Address destAddress){
Ptr packet = Create();
Ipv4Header ipvHeader;
ipvHeader.SetSource(source->GetObject()->GetAddress(1,0).GetLocal());
ipvHeader.SetDestination(destAddress);
packet->AddHeader(ipvHeader);
packet->AddHeader(AodvHeader());
source->Send(packet);
}
void CustomAODVRouteReply(Ptr node, RouteEntry route){
Ptr reply = Create();
reply->AddHeader(route);
node->Send(reply);
}
void HybridRoutingLogic(Ptr node, Ipv4Address destAddress){
if (IsAODVZone(node)){
CustomAODVRouteRequest(node, destAddress);
} else {
// Placeholder for OLSR logic
// You can implement your OLSR routing logic here
// For simplicity, this example does not include detailed OLSR logic
}
}
class PacketMonitor {
public:
PacketMonitor() : lostPackets(0){}
void CheckForLostPackets(Ptr packet){
if (!packet->Received()){
lostPackets++;
}
}
int GetLostPackets(){ return lostPackets; }
private:
int lostPackets;
};
int main(){
NodeContainer nodes;
nodes.Create(10);
MobilityHelper mobility;
mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel",
"MinSpeed", StringValue("1.0"),
"MaxSpeed", StringValue("10.0"));
mobility.Install(nodes);
InternetStackHelper internet;
internet.Install(nodes);
AodvHelper aodv;
OlsrHelper olsr;
Ipv4StaticRoutingHelper staticRouting;
Ipv4AddressHelper address;
RoutingProtocol currentProtocol = AODV;
PacketMonitor monitor;
Ipv4Address destAddress("192.168.1.2");
// Assign IP addresses
address.SetBase("192.168.1.0","255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign(nodes);
// Install AODV or OLSR routing
if (currentProtocol == AODV){
aodv.Install(nodes);
} else if (currentProtocol == OLSR){
olsr.Install(nodes);
}
// Install applications
UdpEchoClientHelper clientHelper(interfaces.GetAddress(0),1024);
UdpEchoServerHelper serverHelper(1024);
serverHelper.Install(nodes.Get(9));
clientHelper.Install(nodes.Get(0));
Simulator::Run(10.0);
if (currentProtocol == AODV){
std::cout << "AODV lost packets: "<< monitor.GetLostPackets()
<< std::endl;
} else if (currentProtocol == OLSR){
std::cout << "OLSR lost packets: "<< monitor.GetLostPackets()
<< std::endl;
}
// Switch to AODV at time 5.0s
currentProtocol = AODV;
// Reinstall AODV routing
if (currentProtocol == AODV){
aodv.Install(nodes);
}
// Install applications for the second run
UdpEchoClientHelper clientHelper2(interfaces.GetAddress(0),1024);
UdpEchoServerHelper serverHelper2(1024);
serverHelper2.Install(nodes.Get(9));
clientHelper2.Install(nodes.Get(0));
Simulator::Run(10.0);
if (currentProtocol == AODV){
std::cout << "AODV lost packets: "<< monitor.GetLostPackets()
<< std::endl;
} else if (currentProtocol == OLSR){
std::cout << "OLSR lost packets: "<< monitor.GetLostPackets()
<< std::endl;
}
Simulator::Destroy();
return 0;
} wrte me a working code because this pne is not running. need to run it for ns 3.36.1. I have checked there is no pdr,e2ed, throughput and routing overhead for this code , i needed. Aso for hybrid routign logic else statement for olsr is empy , write the code of it.Print me ip address, mobility, packet generation, current protocol AODV , OLSR, packet received, packet lost, number of nodes for aodsv, olsr rrep, rreq, rerr, routing tablei source adress ,destination address ,next hop, protocol

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions