rauf
unread,Nov 23, 2010, 1:08:29 AM11/23/10Sign 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
Hello,
Could you help me with below codes. I am facing problem with flow-
monitor. I want to print packet flow from each node to other (or
source to sink) after every 20 s. That's why flow-monitor has been
inserted into AdvancePosition function in problematic code. It's not
working! But if I use it in the main function after simulation::run(),
it works fine though it's not providing 20s update packet flow. I have
attached both code here. It's urgent :(. I don't know the relationship
of flow-monitor and Simulation::Run().
Problematic code:
#include "ns3/core-module.h"
#include "ns3/simulator-module.h"
#include "ns3/node-module.h"
#include "ns3/helper-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/flow-monitor-module.h"
using namespace ns3;
using namespace std;
NS_LOG_COMPONENT_DEFINE ("n2");
void getdistance (NodeContainer wifiNodes)
{
double dst;
for ( int i = 0; i<3; i++)
{
cout <<"\n";
for (int j =0; j<3; j++)
{
Ptr<Node> a = wifiNodes.Get (i);
Ptr<Node> b = wifiNodes.Get (j);
dst = a->GetObject<MobilityModel> ()->GetDistanceFrom (b-
>GetObject<MobilityModel> ());
cout <<dst<<" ";
}
}
}
static Vector
GetVelocity (Ptr<Node> node)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
return mobility->GetVelocity();
}
static Vector
GetPosition (Ptr<Node> node)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
return mobility->GetPosition();
}
static void
AdvancePosition (NodeContainer wifiNodes)
{
for (int j=0; j<3; j++)
{
if (j==0) cout <<"\n";
Ptr<Node> node = wifiNodes.Get (j);
std::cout << "Node:" <<j<<endl;
std::cout << "Position " << GetPosition (node) << std::endl;
std::cout << "Velocity " << GetVelocity (node) << std::endl;
}
cout <<"Nodes Connectivity:\n";
getdistance(wifiNodes);
cout <<"\n";
cout <<"hi";
//////////Problematic/////////////////////////////////
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.Install(wifiNodes);
monitor->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier>
(flowmon.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor-
>GetFlowStats ();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i =
stats.begin (); i != stats.end (); ++i)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i-
>first);
cout <<"\n\n";
std::cout << "Flow " << i->first << " (" << t.sourceAddress << "
-> " << t.destinationAddress << ")\n";
std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";
}
////////////////////////////////////////////////////
}
int
main (int argc, char *argv[])
{
uint32_t nwifi = 3;
Config::SetDefault ("ns3::OnOffApplication::PacketSize",
UintegerValue (1000));
Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue
("10kb/s"));
CommandLine cmd;
cmd.AddValue ("nwifi", "Number of wifi STA devices", nwifi);
cmd.Parse (argc,argv);
NodeContainer wifiNodes;
wifiNodes.Create (nwifi);
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
phy.SetChannel (channel.Create ());
WifiHelper wifi = WifiHelper::Default ();
wifi.SetStandard(WIFI_PHY_STANDARD_80211a);
wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
NqosWifiMacHelper mac = NqosWifiMacHelper::Default ();
mac.SetType ("ns3::AdhocWifiMac");
NetDeviceContainer wifiDevices;
wifiDevices = wifi.Install (phy, mac, wifiNodes);
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (10.0),
"DeltaY", DoubleValue (20.0),
"GridWidth", UintegerValue (10),
"LayoutType", StringValue ("RowFirst"));
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue (Rectangle (-500, 500, -500, 500)));
mobility.Install (wifiNodes);
InternetStackHelper stack;
//stack.Install (wifiApNode);
stack.Install (wifiNodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.3.0", "255.255.255.0");
Ipv4InterfaceContainer wifiNodesInterfaces;
//Ipv4InterfaceContainer apNodeInterface;
wifiNodesInterfaces = address.Assign (wifiDevices);
//apNodeInterface = address.Assign (apDevices);
OnOffHelper onoff ("ns3::TcpSocketFactory",InetSocketAddress
("255.255.255.255", 1025));
onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable
(10)));
onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable
(0)));
ApplicationContainer apps;
for (int i=0; i<nwifi; i++)
{
apps = onoff.Install (wifiNodes.Get (i));
apps.Start (Seconds (1.0));
apps.Stop (Seconds (10.0));
}
PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress
("10.1.3.1", 1025));
apps = sink.Install (wifiNodes.Get (0)); //node1
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll();
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Simulator::Stop (Seconds (1001.0));
// phy.EnablePcap ("wifi", apDevices.Get (0));
for (int k=0; k<=1000; k=k+20)
Simulator::Schedule (Seconds (k), &AdvancePosition, wifiNodes);
Simulator::Run ();
/*
monitor->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier>
(flowmon.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor-
>GetFlowStats ();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i =
stats.begin (); i != stats.end (); ++i)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i-
>first);
cout <<"\n\n";
std::cout << "Flow " << i->first << " (" << t.sourceAddress << "
-> " << t.destinationAddress << ")\n";
std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";
}
*/////////////////////////////////////////////////////////////
Simulator::Destroy ();
return 0;
}
Running code:
#include "ns3/core-module.h"
#include "ns3/simulator-module.h"
#include "ns3/node-module.h"
#include "ns3/helper-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/flow-monitor-module.h"
using namespace ns3;
using namespace std;
NS_LOG_COMPONENT_DEFINE ("Assignment2");
void getdistance (NodeContainer wifiNodes)
{
double dst;
for ( int i = 0; i<3; i++)
{
cout <<"\n";
for (int j =0; j<3; j++)
{
Ptr<Node> a = wifiNodes.Get (i);
Ptr<Node> b = wifiNodes.Get (j);
dst = a->GetObject<MobilityModel> ()->GetDistanceFrom (b-
>GetObject<MobilityModel> ());
cout <<dst<<" ";
}
}
}
static Vector
GetVelocity (Ptr<Node> node)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
return mobility->GetVelocity();
}
static Vector
GetPosition (Ptr<Node> node)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
return mobility->GetPosition();
}
static void
AdvancePosition (NodeContainer wifiNodes)
{
for (int j=0; j<3; j++)
{
if (j==0) cout <<"\n";
Ptr<Node> node = wifiNodes.Get (j);
std::cout << "Node:" <<j<<endl;
std::cout << "Position " << GetPosition (node) << std::endl;
std::cout << "Velocity " << GetVelocity (node) << std::endl;
}
cout <<"Nodes Connectivity:\n";
getdistance(wifiNodes);
cout <<"\n";
}
int
main (int argc, char *argv[])
{
uint32_t nwifi = 3;
Config::SetDefault ("ns3::OnOffApplication::PacketSize",
UintegerValue (1000));
Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue
("10kb/s"));
CommandLine cmd;
cmd.AddValue ("nwifi", "Number of wifi STA devices", nwifi);
cmd.Parse (argc,argv);
// NodeContainer wifiApNode;
// wifiApNode.Create (1);
NodeContainer wifiNodes;
wifiNodes.Create (nwifi);
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
phy.SetChannel (channel.Create ());
WifiHelper wifi = WifiHelper::Default ();
wifi.SetStandard(WIFI_PHY_STANDARD_80211a);
wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
NqosWifiMacHelper mac = NqosWifiMacHelper::Default ();
mac.SetType ("ns3::AdhocWifiMac");
NetDeviceContainer wifiDevices;
wifiDevices = wifi.Install (phy, mac, wifiNodes);
// mac.SetType ("ns3::NqapWifiMac",
// "Ssid", SsidValue (ssid));
// NetDeviceContainer apDevices;
// apDevices = wifi.Install (phy, mac, wifiApNode);
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (5.0),
"DeltaY", DoubleValue (10.0),
"GridWidth", UintegerValue (5),
"LayoutType", StringValue ("RowFirst"));
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue (Rectangle (-500, 500, -500, 500)));
mobility.Install (wifiNodes);
//mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
//mobility.Install (wifiApNode);
InternetStackHelper stack;
//stack.Install (wifiApNode);
stack.Install (wifiNodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.3.0", "255.255.255.0");
Ipv4InterfaceContainer wifiNodesInterfaces;
//Ipv4InterfaceContainer apNodeInterface;
wifiNodesInterfaces = address.Assign (wifiDevices);
//apNodeInterface = address.Assign (apDevices);
OnOffHelper onoff ("ns3::TcpSocketFactory",InetSocketAddress
("10.1.3.1", 1025));
onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable
(1)));
onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable
(0)));
ApplicationContainer apps;
for (int i=0; i<nwifi; i++)
{
apps = onoff.Install (wifiNodes.Get (i));
apps.Start (Seconds (1.0));
apps.Stop (Seconds (10.0));
}
PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress
("10.1.3.11", 1025));
apps = sink.Install (wifiNodes.Get (0)); //node1
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll();
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Simulator::Stop (Seconds (1001.0));
// phy.EnablePcap ("wifi", apDevices.Get (0));
for (int k=0; k<=1000; k=k+20)
Simulator::Schedule (Seconds (k), &AdvancePosition, wifiNodes);
Simulator::Run ();
///Flowmonitor after simulator::run()
monitor->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier>
(flowmon.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor-
>GetFlowStats ();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i =
stats.begin (); i != stats.end (); ++i)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i-
>first);
cout <<"\n\n";
std::cout << "Flow " << i->first << " (" << t.sourceAddress << "
-> " << t.destinationAddress << ")\n";
std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";
}
Simulator::Destroy ();
return 0;
}