zfree
unread,Oct 28, 2011, 7:58:19 AM10/28/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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;
}