int main (int argc, char *argv[])
{
// ---------- Simulation Variables ------------------------------------------
double SimTime = 130.0;
double SinkStartTime = 30.0;
double SinkStopTime = 130.0;
double AppStartTime = 30.0;
double AppStopTime = 130.0;
string AppPacketRate ("2Mbps");
Config::SetDefault ("ns3::OnOffApplication::PacketSize",StringValue ("1000"));
Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue (AppPacketRate));
string LinkRate ("11Mbps");
string LinkDelay ("0ms");
string phyMode ("ErpOfdmRate12Mbps");
// ---------- Nodes --------------------------------------------------------
int n_nodes = 2;
int sinkNode = n_nodes-1;
int sourceNode = 0;
NodeContainer nodes;
nodes.Create (n_nodes);
// ---------- Network Setup ------------------------------------------------
PointToPointHelper p2p;
p2p.SetDeviceAttribute ("DataRate", StringValue (LinkRate));
p2p.SetChannelAttribute ("Delay", StringValue (LinkDelay));
OlsrHelper olsr;
Ipv4ListRoutingHelper list;
list.Add (olsr, 10);
InternetStackHelper internet;
internet.SetRoutingHelper (list);
internet.Install (NodeContainer::GetGlobal ());
Ipv4AddressHelper ipv4_n;
ipv4_n.SetBase ("192.168.1.0", "255.255.255.0");
NodeContainer n_links = NodeContainer (nodes.Get (sourceNode), nodes.Get (sinkNode));
NetDeviceContainer n_devs = p2p.Install (n_links);
ipv4_n.Assign (n_devs);
ipv4_n.NewNetwork ();
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
// ---------- Wifi setup ---------------------------------------------------
WifiHelper wifi;
wifi.SetStandard (WIFI_PHY_STANDARD_80211g);
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
wifiPhy.Set ("RxGain", DoubleValue (0) );
wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO);
YansWifiChannelHelper wifiChannel;
wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
wifiChannel.AddPropagationLoss ("ns3::RangePropagationLossModel"
,"MaxRange", DoubleValue (50.0)
);
wifiPhy.SetChannel (wifiChannel.Create ());
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode",StringValue (phyMode),
"ControlMode",StringValue (phyMode)
);
wifiMac.SetType ("ns3::AdhocWifiMac");
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
// ---------- Allocate Node Positions --------------------------------------
MobilityHelper mobility_n;
Ptr<ListPositionAllocator> positionAlloc_n = CreateObject<ListPositionAllocator> ();
positionAlloc_n->Add (Vector (0.0, 0.0, 0.0));
positionAlloc_n->Add (Vector (250.0, 0.0, 0.0));
mobility_n.SetPositionAllocator (positionAlloc_n);
mobility_n.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility_n.Install (nodes);
// ---------- Create CBR Flow -------------------------------------
uint16_t port = 80;
PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
ApplicationContainer apps_sink = sink.Install (nodes.Get (sinkNode)); // sink is installed on last node
apps_sink.Start (Seconds (SinkStartTime));
apps_sink.Stop (Seconds (SinkStopTime));
Ptr<UniformRandomVariable> x = CreateObject<UniformRandomVariable> ();
x->SetAttribute ("Min", DoubleValue (0));
x->SetAttribute ("Max", DoubleValue (1));
double rn = x->GetValue ();
Ptr<Node> n = nodes.Get (sinkNode);
Ptr<Ipv4> ipv4 = n->GetObject<Ipv4> ();
Ipv4InterfaceAddress ipv4_int_addr = ipv4->GetAddress (1, 0);
Ipv4Address ip_addr = ipv4_int_addr.GetLocal ();
OnOffHelper onoff ("ns3::TcpSocketFactory", InetSocketAddress (ip_addr, port)); // traffic flows from node[i] to node[j]
onoff.SetConstantRate (DataRate (AppPacketRate));
ApplicationContainer apps = onoff.Install (nodes.Get (sourceNode)); // traffic sources are installed on all nodes
apps.Start (Seconds (AppStartTime + rn));
apps.Stop (Seconds (AppStopTime));
// ---------- Simulation Monitoring ----------------------------------------
FlowMonitorHelper flow;
Ptr<FlowMonitor> flow_nodes = flow.InstallAll();
Simulator::Stop (Seconds (SimTime));
Simulator::Run ();
flow_nodes->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flow.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = flow_nodes->GetFlowStats ();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
std::cout.precision(8);
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";
std::cout << " Throughput: " << i->second.rxBytes * 8.0 / (AppStopTime - AppStartTime) / 1000 << " Kbps\n";
std::cout << " Rx Segments: " << i->second.rxPackets << "\n";
std::cout << " Lost packets: " << i->second.lostPackets << "\n";
std::cout << " Packet Delivery Fraction: " << (float) i->second.rxBytes / i->second.txBytes * 100 << " %\n";
std::cout << " Average End-to-end Delay: " << i->second.delaySum / i->second.rxPackets / 1000 << " us\n";
}
Simulator::Destroy ();
return 0;
}
OnOffHelper onoff ("ns3::TcpSocketFactory", InetSocketAddress (ip_addr2, port)); // traffic flows from node[i] to node[j]