UAN aloha problem

507 views
Skip to first unread message

zfree

unread,
Oct 28, 2011, 7:58:19 AM10/28/11
to ns-3-users
hi,all
i made a simulation about UAN simple aloha protocol in ns3,based on
the example of /src/uan/example/uan-cw-example,i think the MAC
protocol was already encapsulated,we do not need consider too much
about its details, so i did not change the code to much, but the
throughput is 0 all the time,i don't why?
can somebody help me?
the code:
#include "aloha-example.h"
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/tools-module.h"
#include "ns3/applications-module.h"

#include <fstream>

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("UanAlohaExample");

Experiment::Experiment ()
: m_numNodes (15),
m_dataRate (80),
m_depth (70),
m_boundary (500),
m_packetSize (32),
m_bytesTotal (0),
m_avgs (3),
m_simTime (Seconds (1000)),
m_gnudatfile ("uan-aloha-example.gpl"),
m_asciitracefile ("uan-aloha-example.asc"),
m_bhCfgFile ("uan-apps/dat/default.cfg")
{
}

void
Experiment::ResetData ()
{
NS_LOG_DEBUG (Simulator::Now ().GetSeconds () << " Resetting
data");
m_throughputs.push_back (m_bytesTotal * 8.0 / m_simTime.GetSeconds
());
m_bytesTotal = 0;
}

void
Experiment::PrintResult (uint32_t ith)
{
NS_ASSERT (m_throughputs.size () == m_avgs);

double avgThroughput = 0.0;
for (uint32_t i=0; i<m_avgs; i++)
{
avgThroughput += m_throughputs[i];
}
avgThroughput /= m_avgs;
m_data.Add (ith, avgThroughput);
m_throughputs.clear ();

////Config::Set ("/NodeList/*/DeviceList/*/Mac/CW", UintegerValue
(ith + m_ithStep));

SeedManager::SetRun (SeedManager::GetRun () + 1);

NS_LOG_DEBUG ("Average for ith=" << ith << " over " << m_avgs << "
runs: " << avgThroughput);
}
void
Experiment::UpdatePositions (NodeContainer &nodes)
{

NS_LOG_DEBUG (Simulator::Now ().GetSeconds () << " Updating
positions");
NodeContainer::Iterator it = nodes.Begin ();
UniformVariable uv (0, m_boundary);
for (; it != nodes.End (); it++)
{
Ptr<MobilityModel> mp = (*it)->GetObject<MobilityModel> ();
mp->SetPosition (Vector (uv.GetValue (), uv.GetValue (), 70.0));
}
}

void
Experiment::ReceivePacket (Ptr<Socket> socket)
{
Ptr<Packet> packet;

while (packet = socket->Recv ())
{
m_bytesTotal += packet->GetSize ();
}
packet = 0;
}

Gnuplot2dDataset
Experiment::Run (UanHelper &uan)
{
uan.SetMac ("ns3::UanMacAloha");
NodeContainer nc = NodeContainer ();
NodeContainer sink = NodeContainer ();
nc.Create (m_numNodes);
sink.Create (1);

PacketSocketHelper socketHelper;
socketHelper.Install (nc);
socketHelper.Install (sink);

#ifdef UAN_PROP_BH_INSTALLED
Ptr<UanPropModelBh> prop =
CreateObjectWithAttributes<UanPropModelBh> ("ConfigFile", StringValue
("exbhconfig.cfg"));
#else
Ptr<UanPropModelIdeal> prop =
CreateObjectWithAttributes<UanPropModelIdeal> ();
#endif //UAN_PROP_BH_INSTALLED
Ptr<UanChannel> channel = CreateObjectWithAttributes<UanChannel>
("PropagationModel", PointerValue (prop));
//Create net device and nodes with UanHelper
NetDeviceContainer devices = uan.Install (nc, channel);
NetDeviceContainer sinkdev = uan.Install (sink, channel);

MobilityHelper mobility;
Ptr<ListPositionAllocator> pos = CreateObject<ListPositionAllocator>
();

{
UniformVariable urv (0, m_boundary);
pos->Add (Vector (m_boundary / 2.0, m_boundary / 2.0, m_depth));
double rsum = 0;

double minr = 2 * m_boundary;

for (uint32_t i = 0; i < m_numNodes; i++)
{
double x = urv.GetValue ();
double y = urv.GetValue ();
double newr = sqrt ((x - m_boundary / 2.0) * (x - m_boundary /
2.0)
+ (y - m_boundary / 2.0) * (y -
m_boundary / 2.0));
rsum += newr;
minr = std::min (minr, newr);
pos->Add (Vector (x, y, m_depth));

}
NS_LOG_DEBUG ("Mean range from gateway: " << rsum / m_numNodes
<< " min. range " <<
minr);

mobility.SetPositionAllocator (pos);
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (sink);

NS_LOG_DEBUG ("Position of sink: "
<< sink.Get (0)->GetObject<MobilityModel> ()-
>GetPosition ());
mobility.Install (nc);

PacketSocketAddress socket;
socket.SetSingleDevice (sinkdev.Get (0)->GetIfIndex ());
socket.SetPhysicalAddress (sinkdev.Get (0)->GetAddress ());
socket.SetProtocol (0);

OnOffHelper app ("ns3::PacketSocketFactory", Address (socket));
app.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable
(1)));
app.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable
(0)));
app.SetAttribute ("DataRate", DataRateValue (m_dataRate));
app.SetAttribute ("PacketSize", UintegerValue (m_packetSize));

ApplicationContainer apps = app.Install (nc);
apps.Start (Seconds (0.5));
Time nextEvent = Seconds (0.5);


for(uint32_t ith=1;ith<=10;ith++)
{
for (uint32_t an = 0; an < m_avgs; an++)
{
nextEvent += m_simTime;
Simulator::Schedule (nextEvent, &Experiment::ResetData,
this);
Simulator::Schedule (nextEvent,
&Experiment::UpdatePositions, this, nc);
}
Simulator::Schedule (nextEvent, &Experiment::PrintResult,
this, ith);
}
apps.Stop (nextEvent + m_simTime);

Ptr<Node> sinkNode = sink.Get (0);
TypeId psfid = TypeId::LookupByName ("ns3::PacketSocketFactory");
if (sinkNode->GetObject<SocketFactory> (psfid) == 0)
{
Ptr<PacketSocketFactory> psf =
CreateObject<PacketSocketFactory> ();
sinkNode->AggregateObject (psf);
}
Ptr<Socket> sinkSocket = Socket::CreateSocket (sinkNode, psfid);
sinkSocket->Bind (socket);
sinkSocket->SetRecvCallback (MakeCallback
(&Experiment::ReceivePacket, this));


m_bytesTotal = 0;

std::ofstream ascii (m_asciitracefile.c_str ());
if (!ascii.is_open ())
{
NS_FATAL_ERROR ("Could not open ascii trace file: "
<< m_asciitracefile);
}
uan.EnableAsciiAll (ascii);

Simulator::Run ();
sinkNode = 0;
sinkSocket = 0;
pos = 0;
channel = 0;
prop = 0;
for (uint32_t i=0; i < nc.GetN (); i++)
{
nc.Get (i) = 0;
}
for (uint32_t i=0; i < sink.GetN (); i++)
{
sink.Get (i) = 0;
}

for (uint32_t i=0; i < devices.GetN (); i++)
{
devices.Get (i) = 0;
}
for (uint32_t i=0; i < sinkdev.GetN (); i++)
{
sinkdev.Get (i) = 0;
}

Simulator::Destroy ();
return m_data;
}
}

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

LogComponentEnable ("UanAlohaExample", LOG_LEVEL_ALL);

Experiment exp;

std::string gnudatfile ("alohaexpgnuout.dat");
std::string perModel = "ns3::UanPhyPerGenDefault";
std::string sinrModel = "ns3::UanPhyCalcSinrDefault";

CommandLine cmd;
cmd.AddValue ("NumNodes", "Number of transmitting nodes",
exp.m_numNodes);
cmd.AddValue ("Depth", "Depth of transmitting and sink nodes",
exp.m_depth);
cmd.AddValue ("RegionSize", "Size of boundary in meters",
exp.m_boundary);
cmd.AddValue ("PacketSize", "Generated packet size in bytes",
exp.m_packetSize);
cmd.AddValue ("DataRate", "DataRate in bps", exp.m_dataRate);
cmd.AddValue ("Averages", "Number of topologies to test for each
simulation", exp.m_avgs);
cmd.AddValue ("GnuFile", "Name for GNU Plot output",
exp.m_gnudatfile);
cmd.AddValue ("PerModel", "PER model name", perModel);
cmd.AddValue ("SinrModel", "SINR model name", sinrModel);
cmd.Parse (argc, argv);

ObjectFactory obf;
obf.SetTypeId (perModel);
Ptr<UanPhyPer> per = obf.Create<UanPhyPer> ();
obf.SetTypeId (sinrModel);
Ptr<UanPhyCalcSinr> sinr = obf.Create<UanPhyCalcSinr> ();

UanHelper uan;
UanTxMode mode;
mode = UanTxModeFactory::CreateMode (UanTxMode::FSK, exp.m_dataRate,
exp.m_dataRate, 12000,
exp.m_dataRate, 2,
"Default mode");
UanModesList myModes;
myModes.AppendMode (mode);

uan.SetPhy ("ns3::UanPhyGen",
"PerModel", PointerValue (per),
"SinrModel", PointerValue (sinr),
"SupportedModes", UanModesListValue (myModes));

Gnuplot gp;
Gnuplot2dDataset ds;
ds = exp.Run (uan);

gp.AddDataset (ds);

std::ofstream of (exp.m_gnudatfile.c_str ());
if (!of.is_open ())
{
NS_FATAL_ERROR ("Can not open GNU Plot outfile: " <<
exp.m_gnudatfile);
}
gp.GenerateOutput (of);

per = 0;
sinr = 0;

}

Andrea Sacco

unread,
Oct 30, 2011, 9:53:33 AM10/30/11
to ns-3-users
On 28 Ott, 13:58, zfree <zhan.furu...@gmail.com> wrote:
> hi,all
> i made a simulation about UAN simple aloha protocol in ns3,based on
> the example of /src/uan/example/uan-cw-example,i think the MAC
> protocol was already encapsulated,we do not need consider too much
> about its details, so i did not change the code to much, but the
> throughput is 0 all the time,i don't why?

Can you please post only the code you have changed, from the original
example?

Have you tried to check the your code in debug mode and, see if the
packets reach the destination (receivecallback invoked)?

Regards,
Andrea

zfree

unread,
Oct 30, 2011, 10:00:24 PM10/30/11
to ns-3-users
the change i made :
1.in the header file
1). i remove the definition about the cw,such as
cwMin,cwMax,cwStep,because the sample aloha don't need them
2). i change the function IncrementCw (uint32_t cw) into PrintResult
(uint32_t ith),the ith express the time of simulation
2.in the source file
1). in the function Experiment::Run(UanHelper &uan)
i change the MAC protocol---uan.SetMac ("ns3::UanMacAloha")
i change the circulation
for (uint32_t cw = m_cwMin; cw <= m_cwMax; cw += m_cwStep)
{

for (uint32_t an = 0; an < m_avgs; an++)
{
nextEvent += m_simTime;
Simulator::Schedule (nextEvent, &Experiment::ResetData,
this);
Simulator::Schedule (nextEvent,
&Experiment::UpdatePositions, this, nc);
}
Simulator::Schedule (nextEvent, &Experiment::IncrementCw,
this, cw);
}
into

for(uint32_t ith=1;ith<=10;ith++)
{
for (uint32_t an = 0; an < m_avgs; an++)
{
nextEvent += m_simTime;
Simulator::Schedule (nextEvent, &Experiment::ResetData,
this);
Simulator::Schedule (nextEvent,
&Experiment::UpdatePositions, this, nc);
}
Simulator::Schedule (nextEvent, &Experiment::PrintResult,
this, ith);
}
2).int the main,i remove the some parameters of command,they are about
cw

in addtional,i didn't run the code in debug mode,because i didn't
change other details of the code.Even the change previous is tiny!

thank you,Andrea!

Andrea Sacco

unread,
Nov 9, 2011, 3:27:07 AM11/9/11
to ns-3-users
Hi Zhan,
I've take a look at the code and it seems to be correct.. I've found
there is a problem only with the Aloha protocol.

I'll try to found out what it is and I will let you know. If you need
a piece of code that works, with the Aloha protocol, you can have a
look at the examples in src/uan/test/uan-test.cc

You should find an example with Aloha protocol also in the uan-
framework repo, under src/uan/auv/examples/uan-energy-auv.cc

Best Regards,
Andrea

Andrea Sacco

unread,
Nov 9, 2011, 8:13:06 AM11/9/11
to ns-3-users
Hi Zhan,
there are no problem with the Aloha protocol. You see no througput
during the simulation because your simulation has more than one node,
in saturated transmission. With Aloha MAC, if a node has a packet
ready to transmit, it will be trasmitted immediately. More than one
node transmitting continously in an ideal channel (with no propagation
loss), mean that the transmissions will collide together, giving you a
null throughput.

I you try to modify the number of nodes of the simulation to one (one
node and one sink) you will see a full throughput.
If you want to keep the number of nodes to 15, you have to increase
the datarate of the channel (maybe 10x80) and reduce the OnTime of the
onoff application, reducing the channel occupation.

Hope this help.

Regards,
Andrea

On 9 Nov, 09:27, Andrea Sacco <andrea.sacc...@gmail.com> wrote:
> Hi Zhan,
Reply all
Reply to author
Forward
0 new messages