ad hoc network simulation

911 views
Skip to first unread message

Shagor Chowdhury

unread,
Sep 29, 2011, 1:24:44 PM9/29/11
to ns-3-users
Hello Users,
I am trying to implement a mobile ad hoc network with 100 nodes and
using CSMA/CA protocol. But the trace files and flow monitor do not
show any activity at all. They are totally blank. I need some urgently
about that. I have the following code:

#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/csma-helper.h"
#include "ns3/applications-module.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/ipv4-global-routing-helper.h"

using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("first_broadcast");


int
main (int argc, char *argv[])
{

int numNodes = 100;
std::string phyMode ("DsssRate11Mbps");
double txp = 7.5;
int nodeSpeed = 20;
double TotalTime = 20.0;
std::string rate ("2048bps");

NodeContainer manetNodes;
manetNodes.Create (numNodes);

//NodeContainer nCsma;
//nCsma.Add (manetNodes.Get (1));
//nCsma.Create (mCsma);

Config::SetDefault
("ns3::WifiRemoteStationManager::NonUnicastMode",StringValue
(phyMode));

WifiHelper manetWifi;
manetWifi.SetStandard (WIFI_PHY_STANDARD_80211b);

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


NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
manetWifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode",StringValue (phyMode),
"ControlMode",StringValue (phyMode));

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

wifiMac.SetType ("ns3::AdhocWifiMac");
NetDeviceContainer manetDevices = manetWifi.Install (wifiPhy,
wifiMac, manetNodes);
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));

//csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Dix"));
//csma.SetDeviceAttribute ("FrameSize", UintegerValue (2000));


NetDeviceContainer csmaDevices = csma.Install(manetNodes);
MobilityHelper mobilityManet;
Ipv4ListRoutingHelper list;

InternetStackHelper internet;
internet.SetRoutingHelper (list);
internet.Install (manetNodes);

NS_LOG_INFO ("assigning ip address");

Ipv4AddressHelper addressManet;
addressManet.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer manetInterfaces;
manetInterfaces = addressManet.Assign (manetDevices);

ObjectFactory pos;
pos.SetTypeId ("ns3::RandomRectanglePositionAllocator");
pos.Set ("X", RandomVariableValue (UniformVariable (0.0, 50.0)));
pos.Set ("Y", RandomVariableValue (UniformVariable (0.0, 50.0)));

Ptr<PositionAllocator> taPositionAlloc = pos.Create ()-
>GetObject<PositionAllocator> ();
mobilityManet.SetMobilityModel ("ns3::RandomWaypointMobilityModel",
"Speed", RandomVariableValue
(UniformVariable (0.0, nodeSpeed)),
"Pause", RandomVariableValue
(ConstantVariable (0)),
"PositionAllocator", PointerValue
(taPositionAlloc));
mobilityManet.SetPositionAllocator (taPositionAlloc);
mobilityManet.Install (manetNodes);

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

uint32_t port = 100;

OnOffHelper onoff1 ("ns3::UdpSocketFactory",Address ());
onoff1.SetAttribute ("OnTime", RandomVariableValue
(ConstantVariable (1)));
onoff1.SetAttribute ("OffTime", RandomVariableValue
(ConstantVariable (0)));

for (int i = 0; i <= 9; i++)
{

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

UniformVariable var;
ApplicationContainer temp = onoff1.Install (manetNodes.Get (i));
temp.Start (Seconds (var.GetValue (0.1,1.0)));
temp.Stop (Seconds (TotalTime));
}



AsciiTraceHelper ascii;
Ptr<OutputStreamWrapper> osw = ascii.CreateFileStream
("myfirstmanet.tr");
wifiPhy.EnableAsciiAll (osw);
std::ofstream os;
MobilityHelper::EnableAsciiAll (os);

FlowMonitorHelper flowmonHelper;
Ptr<FlowMonitor> flowmon = flowmonHelper.InstallAll();
flowmon-> SetAttribute("DelayBinWidth", DoubleValue (0.001));
flowmon-> SetAttribute("JitterBinWidth", DoubleValue (0.001));
flowmon-> SetAttribute("PacketSizeBinWidth", DoubleValue(20));

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
wifiPhy.EnablePcapAll ("manet");
csma.EnablePcap ("manet", true, true);

NS_LOG_INFO ("Run Simulation.");


Simulator::Stop (Seconds (TotalTime));
Simulator::Run ();

flowmon->SerializeToXmlFile (("myfirstmanet.flowmon"), false, false);
Simulator::Destroy ();
}

Can anyone suggest what I am doing wrong?
Thank you,
Shagor.

Laurent R.

unread,
Sep 30, 2011, 11:12:30 AM9/30/11
to ns-3-...@googlegroups.com
Hi,
Defining the desired trace level for your log component should help.
e.g. at the beginning of your script in your main (), you may want to insert something like:

LogComponentEnable ("first_broadcast", LOG_LEVEL_INFO);

(or whatever log level you need)
Regards
Laurent

Shagor Chowdhury

unread,
Sep 30, 2011, 8:22:30 PM9/30/11
to ns-3-users
Hey,
I tried it but it still gives me blank trace files and flow monitor
files. In the terminal it gives me the following message:
"assigning ip address
Run Simulation."
Here is the code again;

#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/csma-helper.h"
#include "ns3/applications-module.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/ipv4-global-routing-helper.h"

using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("first_broadcast");


int
main (int argc, char *argv[])
{

int numNodes = 100;
std::string phyMode ("DsssRate11Mbps");
double txp = 7.5;
int nodeSpeed = 20;
double TotalTime = 20.0;
std::string rate ("2048bps");

LogComponentEnable ("first_broadcast", LOG_LEVEL_ALL);
pos.Set ("X", RandomVariableValue (UniformVariable (0.0, 500.0)));
pos.Set ("Y", RandomVariableValue (UniformVariable (0.0, 500.0)));
Thanks Shagor.



On Sep 30, 11:12 am, "Laurent R." <laurent.reynaud.3x1...@gmail.com>
wrote:

Laurent R.

unread,
Oct 1, 2011, 6:40:43 AM10/1/11
to ns-3-...@googlegroups.com
Ok.
1- I tried your script, but I replaced your AdHocWifiMac by a ApWifiMac configuration (if needed, see "second.cc" in the example scripts for the relevant configuration)
Then, it works correctly and the .pcap / .tr traces are correctly filled.

2- Alternatively I kept your default WiFi configuration, but also used a routing protocol in your script.
E.g. with OLSR (but any routing protocol will do),

(...)
#include "ns3/olsr-helper.h"
(...)
OlsrHelper olsr;
Ipv4StaticRoutingHelper staticRouting;
Ipv4ListRoutingHelper list;
list.Add (staticRouting, 0);
list.Add (olsr, 10);
(...)

Also works ok.
I suggest you choose either way, depending on your needs.
Regards

Shagor Chowdhury

unread,
Oct 1, 2011, 10:52:08 PM10/1/11
to ns-3-users
Thank you very much for the help. That worked for me. So my next task
is to setup a retransmission probability for each received packet. For
example, each time a node receives a packet it will retransmit with a
probility of 0.5. DO you know any examples or any way I can achieve
that?
Thank again for your suggestion. I used the olsr routing protocol.
Shagor.

On Oct 1, 6:40 am, "Laurent R." <laurent.reynaud.3x1...@gmail.com>
wrote:

gaurav garg

unread,
Feb 4, 2013, 10:13:55 PM2/4/13
to ns-3-...@googlegroups.com
Dear Sir

I try to execute the following above code ie.




#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/csma-helper.h"
#include "ns3/applications-module.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/mobility-helper.h"
  wifiMac.SetType ("ns3::ApWifiMac");
 Config::SetDefault  ("ns3::OnOffApplication::PacketSize",StringValue("64"));

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

 uint32_t port = 100;

  OnOffHelper onoff1 ("ns3::UdpSocketFactory",Address ());
  onoff1.SetAttribute ("OnTime", RandomVariableValue(ConstantVariable  (1)));
  onoff1.SetAttribute ("OffTime", RandomVariableValue(ConstantVariable (0)));


  for (int i = 0; i <= 9; i++)
    {

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

      UniformVariable var;
      ApplicationContainer temp = onoff1.Install (manetNodes.Get (i));
      temp.Start (Seconds (var.GetValue (0.1,1.0)));
      temp.Stop (Seconds (TotalTime));
    }


 AsciiTraceHelper ascii;
Ptr<OutputStreamWrapper> osw = ascii.CreateFileStream("myfirstmanet.tr");

  wifiPhy.EnableAsciiAll (osw);
  std::ofstream os;
  MobilityHelper::EnableAsciiAll(os);

  FlowMonitorHelper flowmonHelper;
 Ptr<FlowMonitor> flowmon = flowmonHelper.InstallAll();
 flowmon-> SetAttribute("DelayBinWidth", DoubleValue (0.001));
 flowmon-> SetAttribute("JitterBinWidth", DoubleValue (0.001));
 flowmon-> SetAttribute("PacketSizeBinWidth", DoubleValue(20));

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
wifiPhy.EnablePcapAll ("manet");
  csma.EnablePcap ("manet", true, true);

  NS_LOG_INFO ("Run Simulation.");


  Simulator::Stop (Seconds (TotalTime));
  Simulator::Run ();

flowmon->SerializeToXmlFile (("myfirstmanet.flowmon"), false, false);
  Simulator::Destroy ();
}



but it generates the following error::
Waf: Entering directory `/home/monu/ns-allinone-3.16/ns-3.16/build'
[645/691] cxx: adhoc.cc -> ../build/scratch/adhoc.cc.4.o
../scratch/adhoc.cc: In function ‘int main(int, char**)’:
../scratch/adhoc.cc:131:36: error: no matching function for call to ‘ns3::MobilityHelper::EnableAsciiAll(std::ofstream&)’
../scratch/adhoc.cc:131:36: note: candidate is:
In file included from ./ns3/mobility-module.h:17:0,
                 from ../scratch/adhoc.cc:7:
./ns3/mobility-helper.h:249:15: note: static void ns3::MobilityHelper::EnableAsciiAll(ns3::Ptr<ns3::OutputStreamWrapper>)
./ns3/mobility-helper.h:249:15: note:   no known conversion for argument 1 from ‘std::ofstream {aka std::basic_ofstream<char>}’ to ‘ns3::Ptr<ns3::OutputStreamWrapper>’
Waf: Leaving directory `/home/monu/ns-allinone-3.16/ns-3.16/build'
Build failed


Plz help me to get out of it!!


Reply all
Reply to author
Forward
0 new messages