Some questions about aodv.cc

80 views
Skip to first unread message

Simon Hu

unread,
Aug 2, 2019, 11:11:36 PM8/2/19
to ns-3-users
In AODV protocol, I notice that there exists a "RERR" packet, I want to understand the complete workflow of AODV protocol, so I want to know how can I implement the case of "link down" to trigger the process of "RERR" packet.

should i modify something in here (below)?
#aodv.cc 
#ver. ns 3.29
WifiMacHelper wifiMac;
wifiMac.SetType ("ns3::AdhocWifiMac");
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
wifiPhy.SetChannel (wifiChannel.Create ());
WifiHelper wifi;
   
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0));



Eric

unread,
Nov 13, 2021, 2:36:18 AM11/13/21
to ns-3-users
I'm curious about this too..
How can I implement "Link Down" when node's energy(battery) is 0?


#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/wifi-module.h" 
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-list-routing-helper.h"
#include "ns3/basic-energy-source.h"
#include "ns3/wifi-radio-energy-model.h"
#include "ns3/energy-module.h"
#include "ns3/yans-wifi-helper.h"

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>

using namespace ns3;

int packetsSent = 0;
int packetsReceived = 0;

void ReceivePacket (Ptr<Socket> socket)
{
  Ptr<Packet> packet;
  while ((packet = socket->Recv ()))
    {
  packetsReceived++;
      std::cout<<"Received packet - "<<packetsReceived<<" and Size is "<<packet->GetSize ()<<" Bytes."<<std::endl;
    }
}

static void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize, 
                             uint32_t pktCount, Time pktInterval )
{
  if (pktCount > 0)
    {
      socket->Send (Create<Packet> (pktSize));
      packetsSent++;
      std::cout<<"Packet sent - "<<packetsSent<<std::endl;
      
      Simulator::Schedule (pktInterval, &GenerateTraffic, 
                           socket, pktSize,pktCount-1, pktInterval);
    }
  else
    {
      socket->Close ();
    }
}

void RemainingEnergy (double oldValue, double remainingEnergy)
{
  NS_LOG_UNCOND (Simulator::Now ().GetSeconds ()
<< " Current remaining energy = " << remainingEnergy << "J");
}

void TotalEnergy (double oldValue, double totalEnergy)
{
  NS_LOG_UNCOND (Simulator::Now ().GetSeconds ()
<< " Total energy consumed by radio = " << totalEnergy << "J");
}



int main(int argc, char *argv[])
{
  CommandLine cmd;
  cmd.Parse(argc, argv);

  //int nodeSpeed = 2; //in m/s
  //int nodePause = 0; //in s

  //uint32_t size=10;
  //double step=1;
  double totalTime=30;

  int packetSize = 1024;
  int totalPackets = 10;
  double interval = 1; 
  Time interPacketInterval = Seconds (interval);
  
  NodeContainer nodes;
  NetDeviceContainer devices;
  Ipv4InterfaceContainer interfaces;
  
  //std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n";
  nodes.Create (10);
  
  MobilityHelper mobility;
  //mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
  //                         "Bounds", RectangleValue (Rectangle (-100, 100, -100, 100)));

  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");

  /*int64_t streamIndex = 0; // used to get consistent mobility across scenarios

  ObjectFactory pos;
  pos.SetTypeId ("ns3::RandomRectanglePositionAllocator");
  pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=300.0]"));
  pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1500.0]"));

  Ptr<PositionAllocator> taPositionAlloc = pos.Create ()->GetObject<PositionAllocator> ();
  streamIndex += taPositionAlloc->AssignStreams (streamIndex);

  std::stringstream ssSpeed;
  ssSpeed << "ns3::UniformRandomVariable[Min=0.0|Max=" << nodeSpeed << "]";
  std::stringstream ssPause;
  ssPause << "ns3::ConstantRandomVariable[Constant=" << nodePause << "]";
  mobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel",
                                  "Speed", StringValue (ssSpeed.str ()),
                                  "Pause", StringValue (ssPause.str ()),
                                  "PositionAllocator", PointerValue (taPositionAlloc));
  mobility.SetPositionAllocator (taPositionAlloc);

  streamIndex += mobility.AssignStreams (nodes, streamIndex);
  NS_UNUSED (streamIndex); // From this point, streamIndex is unused
  */
  mobility.Install (nodes);

  WifiMacHelper wifiMac;//= = NqosWifiMacHelper::Default ();
  wifiMac.SetType ("ns3::AdhocWifiMac");
  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
  wifiPhy.SetChannel (wifiChannel.Create ());
  WifiHelper wifi;// = WifiHelper::Default ();
  wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
  //wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("DsssRate11Mbps"), "RtsCtsThreshold", UintegerValue (0));
  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
                                "DataMode",StringValue("DsssRate11Mbps"),
                                "ControlMode", StringValue ("DsssRate11Mbps"));
  devices = wifi.Install (wifiPhy, wifiMac, nodes); 
  
  //********* Energy Model *********//
  /* energy source */
  //BasicEnergySourceHelper basicSourceHelper;
  // configure energy source
  //basicSourceHelper.Set("BasicEnergySourceInitialEnergyJ", DoubleValue (10.0));
  // install source
  //EnergySourceContainer sources = basicSourceHelper.Install (nodes);
  /* device energy model */
  //WifiRadioEnergyModelHelper radioEnergyHelper;
  // configure radio energy model
  //radioEnergyHelper.Set("TxCurrentA", DoubleValue(0.0174));
  //radioEnergyHelper.Set("RxCurrentA", DoubleValue(0.0174));
  // install device model
  //DeviceEnergyModelContainer deviceModels = radioEnergyHelper.Install(devices, sources);

  Ptr<BasicEnergySource> energySource = CreateObject<BasicEnergySource>();
  Ptr<WifiRadioEnergyModel> energyModel = CreateObject<WifiRadioEnergyModel>();
  Ptr<EnergySourceContainer> energyContainer = CreateObject<EnergySourceContainer> ();
  energyContainer->Add (energySource);
  energySource->SetInitialEnergy (10);
  energyModel->SetEnergySource (energySource);
  energySource->AppendDeviceEnergyModel (energyModel);
  energyModel->SetTxCurrentA (0.0174);
  nodes.Get(0)->AggregateObject (energySource);

  Ptr<BasicEnergySource> energySource1 = CreateObject<BasicEnergySource>();
  Ptr<WifiRadioEnergyModel> energyModel1 = CreateObject<WifiRadioEnergyModel>();
  Ptr<EnergySourceContainer> energyContainer1 = CreateObject<EnergySourceContainer> ();
  energyContainer1->Add (energySource1);
  energySource1->SetInitialEnergy (5);
  energyModel1->SetEnergySource (energySource1);
  energySource1->AppendDeviceEnergyModel (energyModel1);
  energyModel->SetTxCurrentA (0.0174);
  nodes.Get(1)->AggregateObject (energySource1);

  Ptr<BasicEnergySource> energySource2 = CreateObject<BasicEnergySource>();
  Ptr<WifiRadioEnergyModel> energyModel2 = CreateObject<WifiRadioEnergyModel>();
  Ptr<EnergySourceContainer> energyContainer2 = CreateObject<EnergySourceContainer> ();
  energyContainer2->Add (energySource2);
  energySource2->SetInitialEnergy (10);
  energyModel2->SetEnergySource (energySource2);
  energySource2->AppendDeviceEnergyModel (energyModel2);
  energyModel->SetTxCurrentA (0.0174);
  nodes.Get(2)->AggregateObject (energySource2);

  Ptr<BasicEnergySource> energySource3 = CreateObject<BasicEnergySource>();
  Ptr<WifiRadioEnergyModel> energyModel3 = CreateObject<WifiRadioEnergyModel>();
  Ptr<EnergySourceContainer> energyContainer3 = CreateObject<EnergySourceContainer> ();
  energyContainer3->Add (energySource3);
  energySource3->SetInitialEnergy (10);
  energyModel3->SetEnergySource (energySource3);
  energySource3->AppendDeviceEnergyModel (energyModel3);
  energyModel->SetTxCurrentA (0.0174);
  nodes.Get(3)->AggregateObject (energySource3);

  Ptr<BasicEnergySource> energySource4 = CreateObject<BasicEnergySource>();
  Ptr<WifiRadioEnergyModel> energyModel4 = CreateObject<WifiRadioEnergyModel>();
  Ptr<EnergySourceContainer> energyContainer4 = CreateObject<EnergySourceContainer> ();
  energyContainer4->Add (energySource4);
  energySource4->SetInitialEnergy (10);
  energyModel4->SetEnergySource (energySource4);
  energySource4->AppendDeviceEnergyModel (energyModel4);
  energyModel->SetTxCurrentA (0.0174);
  nodes.Get(4)->AggregateObject (energySource4);

  Ptr<BasicEnergySource> energySource5 = CreateObject<BasicEnergySource>();
  Ptr<WifiRadioEnergyModel> energyModel5 = CreateObject<WifiRadioEnergyModel>();
  Ptr<EnergySourceContainer> energyContainer5 = CreateObject<EnergySourceContainer> ();
  energyContainer5->Add (energySource5);
  energySource5->SetInitialEnergy (10);
  energyModel5->SetEnergySource (energySource5);
  energySource5->AppendDeviceEnergyModel (energyModel5);
  energyModel->SetTxCurrentA (0.0174);
  nodes.Get(5)->AggregateObject (energySource5);

  Ptr<BasicEnergySource> energySource6 = CreateObject<BasicEnergySource>();
  Ptr<WifiRadioEnergyModel> energyModel6 = CreateObject<WifiRadioEnergyModel>();
  Ptr<EnergySourceContainer> energyContainer6 = CreateObject<EnergySourceContainer> ();
  energyContainer6->Add (energySource6);
  energySource6->SetInitialEnergy (10);
  energyModel6->SetEnergySource (energySource6);
  energySource6->AppendDeviceEnergyModel (energyModel6);
  energyModel->SetTxCurrentA (0.0174);
  nodes.Get(6)->AggregateObject (energySource6);

  Ptr<BasicEnergySource> energySource7 = CreateObject<BasicEnergySource>();
  Ptr<WifiRadioEnergyModel> energyModel7 = CreateObject<WifiRadioEnergyModel>();
  Ptr<EnergySourceContainer> energyContainer7 = CreateObject<EnergySourceContainer> ();
  energyContainer7->Add (energySource7);
  energySource7->SetInitialEnergy (10);
  energyModel7->SetEnergySource (energySource7);
  energySource7->AppendDeviceEnergyModel (energyModel7);
  energyModel->SetTxCurrentA (0.0174);
  nodes.Get(7)->AggregateObject (energySource7);

  Ptr<BasicEnergySource> energySource8 = CreateObject<BasicEnergySource>();
  Ptr<WifiRadioEnergyModel> energyModel8 = CreateObject<WifiRadioEnergyModel>();
  Ptr<EnergySourceContainer> energyContainer8 = CreateObject<EnergySourceContainer> ();
  energyContainer8->Add (energySource8);
  energySource8->SetInitialEnergy (10);
  energyModel8->SetEnergySource (energySource8);
  energySource8->AppendDeviceEnergyModel (energyModel8);
  energyModel->SetTxCurrentA (0.0174);
  nodes.Get(8)->AggregateObject (energySource8);

  Ptr<BasicEnergySource> energySource9 = CreateObject<BasicEnergySource>();
  Ptr<WifiRadioEnergyModel> energyModel9 = CreateObject<WifiRadioEnergyModel>();
  Ptr<EnergySourceContainer> energyContainer9 = CreateObject<EnergySourceContainer> ();
  energyContainer9->Add (energySource9);
  energySource9->SetInitialEnergy (10);
  energyModel9->SetEnergySource (energySource7);
  energySource9->AppendDeviceEnergyModel (energyModel9);
  energyModel->SetTxCurrentA (0.0174);
  nodes.Get(9)->AggregateObject (energySource9);

  energySource->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));
  energySource1->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));
  energySource2->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));
  energySource3->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));
  energySource4->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));
  energySource5->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));
  energySource6->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));
  energySource7->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));
  energySource8->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));
  energySource9->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));

  energySource->TraceConnectWithoutContext ("TotalEnergyConsumption", MakeCallback (&TotalEnergy));
  energySource1->TraceConnectWithoutContext ("TotalEnergyConsumption", MakeCallback (&TotalEnergy));
  energySource2->TraceConnectWithoutContext ("TotalEnergyConsumption", MakeCallback (&TotalEnergy));
  energySource3->TraceConnectWithoutContext ("TotalEnergyConsumption", MakeCallback (&TotalEnergy));
  energySource4->TraceConnectWithoutContext ("TotalEnergyConsumption", MakeCallback (&TotalEnergy));
  energySource5->TraceConnectWithoutContext ("TotalEnergyConsumption", MakeCallback (&TotalEnergy));
  energySource6->TraceConnectWithoutContext ("TotalEnergyConsumption", MakeCallback (&TotalEnergy));
  energySource7->TraceConnectWithoutContext ("TotalEnergyConsumption", MakeCallback (&TotalEnergy));
  energySource8->TraceConnectWithoutContext ("TotalEnergyConsumption", MakeCallback (&TotalEnergy));
  energySource9->TraceConnectWithoutContext ("TotalEnergyConsumption", MakeCallback (&TotalEnergy));

  // Pcap
  wifiPhy.EnablePcapAll("ori_src_aodv");

  AodvHelper aodv;
  InternetStackHelper stack;
  stack.SetRoutingHelper (aodv); 
  stack.Install (nodes);

  Ipv4AddressHelper address;
  address.SetBase ("10.0.0.0", "255.0.0.0");
  interfaces = address.Assign (devices);

  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
  
  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("ori_src_aodv.routes", std::ios::out); //
  aodv.PrintRoutingTableAllEvery(Seconds(1),routingStream); // At every 1 seconds

  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
  Ptr<Socket> recvSink = Socket::CreateSocket (nodes.Get (9), tid);
  InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 8080);
  recvSink->Bind (local);
  recvSink->SetRecvCallback (MakeCallback (&ReceivePacket));

  Ptr<Socket> source = Socket::CreateSocket (nodes.Get (0), tid);
  InetSocketAddress remote = InetSocketAddress (interfaces.GetAddress (9,0), 8080);
  source->Connect (remote);
  
  Simulator::Schedule (Seconds (0), &GenerateTraffic, source, packetSize, totalPackets, interPacketInterval);
                       
  std::cout << "Starting simulation for " << totalTime << " s ...\n";
  AnimationInterface anim ("ori_src_aodv.xml");
  /*anim.SetConstantPosition (nodes.Get(0), 0, 40);
  anim.SetConstantPosition (nodes.Get(1), 20, 50);
  anim.SetConstantPosition (nodes.Get(2), 35, 55);
  anim.SetConstantPosition (nodes.Get(3), 20, 30);
  anim.SetConstantPosition (nodes.Get(4), 45, 33);
  anim.SetConstantPosition (nodes.Get(5), 65, 45);
  anim.SetConstantPosition (nodes.Get(6), 63, 55);
  anim.SetConstantPosition (nodes.Get(7), 77, 30);
  anim.SetConstantPosition (nodes.Get(8), 80, 40);
  anim.SetConstantPosition (nodes.Get(9), 100, 50);*/

  /*anim.SetConstantPosition (nodes.Get(0), 0, 50);
  anim.SetConstantPosition (nodes.Get(1), 35, 50);
  anim.SetConstantPosition (nodes.Get(2), 70, 50);
  anim.SetConstantPosition (nodes.Get(3), 105, 50);
  anim.SetConstantPosition (nodes.Get(4), 140, 50);
  anim.SetConstantPosition (nodes.Get(5), 175, 50);
  anim.SetConstantPosition (nodes.Get(6), 210, 50);
  anim.SetConstantPosition (nodes.Get(7), 245, 50);
  anim.SetConstantPosition (nodes.Get(8), 280, 50);
  anim.SetConstantPosition (nodes.Get(9), 315, 50);*/

  anim.SetConstantPosition (nodes.Get(0), 0, 45);
  anim.SetConstantPosition (nodes.Get(1), 35, 75);
  anim.SetConstantPosition (nodes.Get(2), 70, 75);
  anim.SetConstantPosition (nodes.Get(3), 105, 75);
  anim.SetConstantPosition (nodes.Get(4), 140, 75);
  anim.SetConstantPosition (nodes.Get(5), 35, 15);
  anim.SetConstantPosition (nodes.Get(6), 70, 15);
  anim.SetConstantPosition (nodes.Get(7), 105, 15);
  anim.SetConstantPosition (nodes.Get(8), 140, 15);
  anim.SetConstantPosition (nodes.Get(9), 175, 45);

  /*anim.SetConstantPosition (nodes.Get(0), 0, 45);
  anim.SetConstantPosition (nodes.Get(1), 35, 75);
  anim.SetConstantPosition (nodes.Get(2), 70, 75);
  anim.SetConstantPosition (nodes.Get(3), 140, 45);
  anim.SetConstantPosition (nodes.Get(4), 140, 75);
  anim.SetConstantPosition (nodes.Get(5), 35, 15);
  anim.SetConstantPosition (nodes.Get(6), 70, 15);
  anim.SetConstantPosition (nodes.Get(7), 105, 45);
  anim.SetConstantPosition (nodes.Get(8), 140, 15);
  anim.SetConstantPosition (nodes.Get(9), 175, 45);*/

  Ptr<FlowMonitor> flowmon;
  FlowMonitorHelper flowmonHelper;
  flowmon = flowmonHelper.InstallAll ();
  
      
  Simulator::Stop (Seconds (totalTime));
  Simulator::Run ();
  flowmon->SerializeToXmlFile("scratch/aodv-flow.xml", true, true);
  Simulator::Destroy ();

  std::cout<<"\n\n***** OUTPUT *****\n\n";
  std::cout<<"Total Packets sent = "<<packetsSent<<std::endl;
  std::cout<<"Total Packets received = "<<packetsReceived<<std::endl;
  std::cout<<"Packet delivery ratio = "<<(float)(packetsReceived/packetsSent)*100<<" %"<<std::endl;
  
}


2019년 8월 3일 토요일 오후 12시 11분 36초 UTC+9에 simon...@gmail.com님이 작성:
Reply all
Reply to author
Forward
0 new messages