Issue while implementing MANETS

247 views
Skip to first unread message

Parisha Tarunraj

unread,
Apr 26, 2023, 11:02:07 PM4/26/23
to ns-3-users
I am unable to understand why this "manet-routing-compare" is not working for AODV when i select number of nodes - 5. But it is working fine for number of nodes =20 and above.

//code starts here


#include <fstream>
#include <iostream>
#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/aodv-module.h"
#include "ns3/olsr-module.h"
#include "ns3/dsdv-module.h"
#include "ns3/dsr-module.h"
#include "ns3/applications-module.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/flow-monitor.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/netanim-module.h"

using namespace ns3;
using namespace dsr;
using namespace std;

NS_LOG_COMPONENT_DEFINE ("manet-routing-compare");

class RoutingExperiment
{
public:
  RoutingExperiment ();
  void Run (int nSinks, double txp, std::string CSVfileName);
  //static void SetMACParam (ns3::NetDeviceContainer & devices,
  //                                 int slotDistance);
  std::string CommandSetup (int argc, char **argv);

private:
  Ptr<Socket> SetupPacketReceive (Ipv4Address addr, Ptr<Node> node);
  void ReceivePacket (Ptr<Socket> socket);
  void CheckThroughput ();

  uint32_t port;
  uint32_t bytesTotal;
  uint32_t packetsReceived;

  std::string m_CSVfileName;
  int m_nSinks;
  std::string m_protocolName;
  double m_txp;
  bool m_traceMobility;
  uint32_t m_protocol;
};

RoutingExperiment::RoutingExperiment ()
  : port (9),
    bytesTotal (0),
    packetsReceived (0),
    m_CSVfileName ("AODV.csv"),
    m_traceMobility (false),
    m_protocol (2) // AODV
{
}

static inline std::string
PrintReceivedPacket (Ptr<Socket> socket, Ptr<Packet> packet, Address senderAddress)
{
  std::ostringstream oss;

  oss << Simulator::Now ().GetSeconds () << " " << socket->GetNode ()->GetId ();

  if (InetSocketAddress::IsMatchingType (senderAddress))
    {
      InetSocketAddress addr = InetSocketAddress::ConvertFrom (senderAddress);
      oss << " received one packet from " << addr.GetIpv4 ();
    }
  else
    {
      oss << " received one packet!";
    }
  return oss.str ();
}

void
RoutingExperiment::ReceivePacket (Ptr<Socket> socket)
{
  Ptr<Packet> packet;
  Address senderAddress;
  while ((packet = socket->RecvFrom (senderAddress)))
    {
      bytesTotal += packet->GetSize ();
      packetsReceived += 1;
      NS_LOG_UNCOND (PrintReceivedPacket (socket, packet, senderAddress));
    }
}

void
RoutingExperiment::CheckThroughput ()
{
  double kbs = (bytesTotal * 8.0) / 1000;
  bytesTotal = 0;

  std::ofstream out (m_CSVfileName.c_str (), std::ios::app);

  out << (Simulator::Now ()).GetSeconds () << ","
      << kbs << ","
      << packetsReceived << ","
      << m_nSinks << ","
      << m_protocolName << ","
      << m_txp << ""
      << std::endl;

  out.close ();
  packetsReceived = 0;
  Simulator::Schedule (Seconds (1.0), &RoutingExperiment::CheckThroughput, this);
}

Ptr<Socket>
RoutingExperiment::SetupPacketReceive (Ipv4Address addr, Ptr<Node> node)
{
  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
  Ptr<Socket> sink = Socket::CreateSocket (node, tid);
  InetSocketAddress local = InetSocketAddress (addr, port);
  sink->Bind (local);
  sink->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket, this));

  return sink;
}

std::string
RoutingExperiment::CommandSetup (int argc, char **argv)
{
  CommandLine cmd;
  cmd.AddValue ("CSVfileName", "The name of the CSV output file name", m_CSVfileName);
  cmd.AddValue ("traceMobility", "Enable mobility tracing", m_traceMobility);
  cmd.AddValue ("protocol", "1=OLSR;2=AODV;3=DSDV;4=DSR", m_protocol);
  cmd.Parse (argc, argv);
  return m_CSVfileName;
}

int
main (int argc, char *argv[])
{
  RoutingExperiment experiment;
  std::string CSVfileName = experiment.CommandSetup (argc,argv);

  //blank out the last output file and write the column headers
  std::ofstream out (CSVfileName.c_str ());
  out << "SimulationSecond," <<
  "ReceiveRate," <<
  "PacketsReceived," <<
  "NumberOfSinks," <<
  "RoutingProtocol," <<
  "TransmissionPower" <<
  std::endl;
  out.close ();

  int nSinks = 10;
  double txp = 7.5;

  experiment.Run (nSinks, txp, CSVfileName);
}

void
RoutingExperiment::Run (int nSinks, double txp, std::string CSVfileName)
{
  Packet::EnablePrinting ();
  m_nSinks = nSinks;
  m_txp = txp;
  m_CSVfileName = CSVfileName;

  int nWifis = 5;

  double TotalTime = 150.0;
  std::string rate ("2048bps");
  std::string phyMode ("DsssRate11Mbps");
  std::string tr_name ("AODV");
  int nodeSpeed = 80; //in m/s
  int nodePause = 0; //in s
  m_protocolName = "protocol";

  Config::SetDefault  ("ns3::OnOffApplication::PacketSize",StringValue ("32"));
  Config::SetDefault ("ns3::OnOffApplication::DataRate",  StringValue (rate));

  //Set Non-unicastMode rate to unicast mode
  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",StringValue (phyMode));

  NodeContainer adhocNodes;
  adhocNodes.Create (nWifis);

  // setting up wifi phy and channel using helpers
  WifiHelper wifi;
  wifi.SetStandard (WIFI_PHY_STANDARD_80211b);

  YansWifiPhyHelper wifiPhy =  YansWifiPhyHelper::Default ();
  YansWifiChannelHelper wifiChannel;
  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
  wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel");
  wifiPhy.SetChannel (wifiChannel.Create ());

  // Add a mac and disable rate control
  WifiMacHelper wifiMac;
  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
                                "DataMode",StringValue (phyMode),
                                "ControlMode",StringValue (phyMode));

  wifiPhy.Set ("TxPowerStart",DoubleValue (txp));
  wifiPhy.Set ("TxPowerEnd", DoubleValue (txp));

  wifiMac.SetType ("ns3::AdhocWifiMac");
  NetDeviceContainer adhocDevices = wifi.Install (wifiPhy, wifiMac, adhocNodes);

  MobilityHelper mobilityAdhoc;
  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 << "]";
  mobilityAdhoc.SetMobilityModel ("ns3::RandomWaypointMobilityModel",
                                  "Speed", StringValue (ssSpeed.str ()),
                                  "Pause", StringValue (ssPause.str ()),
                                  "PositionAllocator", PointerValue (taPositionAlloc));
  mobilityAdhoc.SetPositionAllocator (taPositionAlloc);
  mobilityAdhoc.Install (adhocNodes);
  streamIndex += mobilityAdhoc.AssignStreams (adhocNodes, streamIndex);

  AodvHelper aodv;
  OlsrHelper olsr;
  DsdvHelper dsdv;
  DsrHelper dsr;
  DsrMainHelper dsrMain;
  Ipv4ListRoutingHelper list;
  InternetStackHelper internet;

  switch (m_protocol)
    {
    case 1:
      list.Add (olsr, 100);
      m_protocolName = "OLSR";
      break;
    case 2:
      list.Add (aodv, 100);
      m_protocolName = "AODV";
      break;
    case 3:
      list.Add (dsdv, 100);
      m_protocolName = "DSDV";
      break;
    case 4:
      m_protocolName = "DSR";
      break;
    default:
      NS_FATAL_ERROR ("No such protocol:" << m_protocol);
    }

  if (m_protocol < 4)
    {
      internet.SetRoutingHelper (list);
      internet.Install (adhocNodes);
    }
  else if (m_protocol == 4)
    {
      internet.Install (adhocNodes);
      dsrMain.Install (dsr, adhocNodes);
    }

  NS_LOG_INFO ("assigning ip address");

  Ipv4AddressHelper addressAdhoc;
  addressAdhoc.SetBase ("10.1.1.0", "255.255.255.0");
  Ipv4InterfaceContainer adhocInterfaces;
  adhocInterfaces = addressAdhoc.Assign (adhocDevices);

  OnOffHelper onoff1 ("ns3::UdpSocketFactory",Address ());
  onoff1.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
  onoff1.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));

  for (int i = 0; i < nSinks; i++)
    {
      Ptr<Socket> sink = SetupPacketReceive (adhocInterfaces.GetAddress (i), adhocNodes.Get (i));

      AddressValue remoteAddress (InetSocketAddress (adhocInterfaces.GetAddress (i), port));
      onoff1.SetAttribute ("Remote", remoteAddress);

      Ptr<UniformRandomVariable> var = CreateObject<UniformRandomVariable> ();
      ApplicationContainer temp = onoff1.Install (adhocNodes.Get (i + nSinks));
      temp.Start (Seconds (var->GetValue (100.0,101.0)));
      temp.Stop (Seconds (TotalTime));
    }

  std::stringstream ss;
  ss << nWifis;
  std::string nodes = ss.str ();

  std::stringstream ss2;
  ss2 << nodeSpeed;
  std::string sNodeSpeed = ss2.str ();

  std::stringstream ss3;
  ss3 << nodePause;
  std::string sNodePause = ss3.str ();

  std::stringstream ss4;
  ss4 << rate;
  std::string sRate = ss4.str ();

  //NS_LOG_INFO ("Configure Tracing.");
  //tr_name = tr_name + "_" + m_protocolName +"_" + nodes + "nodes_" + sNodeSpeed + "speed_" + sNodePause + "pause_" + sRate + "rate";

  //AsciiTraceHelper ascii;
  //Ptr<OutputStreamWrapper> osw = ascii.CreateFileStream ( (tr_name + ".tr").c_str());
  //wifiPhy.EnableAsciiAll (osw);
  AsciiTraceHelper ascii;
  MobilityHelper::EnableAsciiAll (ascii.CreateFileStream (tr_name + ".mob"));

  Ptr<FlowMonitor> flowmon;
  FlowMonitorHelper flowmonHelper;
  flowmon = flowmonHelper.InstallAll ();


  NS_LOG_INFO ("Run Simulation.");

  CheckThroughput ();

  Simulator::Stop (Seconds (TotalTime));
  AnimationInterface anim ("manet-routing-compare.xml");
  Simulator::Run ();

// Creating a file

   fstream fout;
   // opens an existing csv file or creates a new file.
    fout.open("node_details.csv",ios::out | ios::app);

    fout <<"flowId," << "SAddr," << "DAddr," << "txPackets," << "rxPackets," << "lostPackets," << "droppedPackets,"<< "txBytes," << "rxBytes,"<<"Throughput,"<<"Trust Values,"<<"\n";



 flowmon->CheckForLostPackets ();


  Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmonHelper.GetClassifier ());

  std::map<FlowId, FlowMonitor::FlowStats> stats = flowmon->GetFlowStats ();

  for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)

    {

 Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);

     // if ((t.sourceAddress=="10.1.1.1" && t.destinationAddress == "10.1.1.2"))
        if (i->first>0 )

      {


         // Insert the data to file
     
        fout << i->first << ", "
             << t.sourceAddress << ", "
             << t.destinationAddress << ", "
             << i->second.txPackets << ", "
             << i->second.rxPackets << ", "
             << i->second.lostPackets << ", "
             << i->second.txPackets - (i->second.rxPackets+i->second.lostPackets) << " ,"
             << i->second.txBytes << ", "
             << i->second.rxBytes <<", "
             << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() - i->second.timeFirstTxPacket.GetSeconds())/1024/1024 << ", "
             << (i->second.rxPackets/(float)i->second.txPackets) << ", "
             << "\n";

             
        /*  std::cout << "Flow " << i->first  << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n";
          std::cout << "  Tx Packets:   " << i->second.txPackets << "\n";
          std::cout << "  Rx Packets:   " << i->second.rxPackets << "\n";
          std::cout << "   PacketsLost:   " << i->second.lostPackets << "\n";
          std::cout << "  Tx Bytes:   " << i->second.txBytes << "\n";

          std::cout << "  Rx Bytes:   " << i->second.rxBytes << "\n";

       std::cout << "  Throughput: " << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() - i->second.timeFirstTxPacket.GetSeconds())/1024/1024  << " Mbps\n";*/

      }

     }
fout.close();


 flowmon->SerializeToXmlFile("tcp-outdoor", true, true);

//Simulator::Stop (Seconds (simTime));

  Simulator::Destroy ();

  NS_LOG_INFO ("Done.");

}


 


 /* flowmon->SerializeToXmlFile ((tr_name + ".xml").c_str(), true, true);

  Simulator::Destroy ();
}*/

It is giving me the below error:

terminated with the signal SIGSEGV.

Can anyone help me understand , what could be the reason?

A R Meena

unread,
Nov 7, 2023, 9:00:18 AM11/7/23
to ns-3-users
hii @parisha tarunraj 
u get solution of above problem??
I have also same issue.

*Shahid*

unread,
Nov 8, 2023, 2:25:17 AM11/8/23
to ns-3-...@googlegroups.com
Try to decrease area for minimum nodes

--
Posting to this group should follow these guidelines https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting
---
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ns-3-users/e4127492-0bfb-4075-a8f5-aea60044e2cen%40googlegroups.com.
Message has been deleted

A R Meena

unread,
Nov 8, 2023, 4:03:04 AM11/8/23
to ns-3-users
Thank you Mr. Shahid for response.
I will try this.
-AR

A R Meena

unread,
Nov 9, 2023, 4:43:55 AM11/9/23
to ns-3-users
Hii Mr. Shahid,

But why is error come for big area when nodes<=20 .Any specific reason behind it.
-AR

Tommaso Pecorella

unread,
Nov 9, 2023, 5:39:51 AM11/9/23
to ns-3-users
Nodes being disconnected, probably.
However, the simulation should NOT terminate with a segmentation fault. It could be a bug. My suggestion is to use a debugger.

Tommaso Pecorella

unread,
Nov 9, 2023, 5:42:35 AM11/9/23
to ns-3-users
Oh... and make sure it's not a bug that has been fixed already. You're clearly using a not-so-recent ns-3 version (and that's why I can't help in finding the bug).

A R Meena

unread,
Nov 10, 2023, 10:20:08 AM11/10/23
to ns-3-users
I am using ns-3.39 . And i got this error. 
thank you for response Mr. Tommaso Pecorella
-AR

*Shahid*

unread,
Nov 10, 2023, 10:27:01 AM11/10/23
to ns-3-...@googlegroups.com
Try to increase transmission power. It will increase the signal strength and keep the connectivity. 

A R Meena

unread,
Nov 12, 2023, 6:48:17 AM11/12/23
to ns-3-users
I will try this Mr. Shahid.
I have one more issue that .tr file is not opening in tracemetrics and  it's taking too much time. And flowmon file too in NetAnim.
-AR

Tommaso Pecorella

unread,
Nov 12, 2023, 1:04:53 PM11/12/23
to ns-3-users
Tracemetrics is not supported and doesn't work anymore - and that's YEARS ago, not to say decades.
As a matter of fact, a ".tr" file might as well be just a human-readable trace - it depends on the script.
FlowMonitor file and NetAnim are two different things - the fact that both can use XML file doesn't men that they're compatible.

Suggestion: read the tutorial (the official one) and ditch decades-old instructions.

A R Meena

unread,
Nov 14, 2023, 5:54:02 AM11/14/23
to ns-3-users
Thank you Mr. Tammaso Pecorella for response .
But tracemetrics works well for third.tr(for third.cc) , it is too much time for manet-routing-compare.tr(for manet-routing-compare.cc) . Any solution for this problem.
And equally NetAnim runs third.flowmon (or third.xml) very well ,but for  manet-routing-compare.cc  it is showing error.

Tommaso Pecorella

unread,
Nov 14, 2023, 5:47:28 PM11/14/23
to ns-3-users
The file "example/tutoral/third.cc" only generates pcap files, nothing else. Hence, either you're using an extremely old ns-3 version, or you did modify it. In both cases we can't help on that.

Tracemetrics last update was 8 years ago. You've been warned, don't complain if your data are wrong. They might be good, they might be bad. Your problem - not mine.

"for  manet-routing-compare.cc  it is showing error.": I won't spend time either trying to figure out what your problem is. You don't care to explain yourself, I won't care either. Simple.
Reply all
Reply to author
Forward
0 new messages