#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/netanim-module.h"
#include "ns3/mobility-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("nisha-lab1");
int
main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpClient", LOG_LEVEL_INFO);
LogComponentEnable ("UdpServer", LOG_LEVEL_INFO);
NodeContainer nodes;
nodes.Create (2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign (devices);
//Position
MobilityHelper mobility;
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
positionAlloc->Add(Vector(0,0,0));
positionAlloc->Add(Vector(0,50,0));
mobility.SetPositionAllocator(positionAlloc);
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(nodes);
UdpServerHelper Server1 (9);
ApplicationContainer server1Apps = Server1.Install (nodes.Get (1));
server1Apps.Start (Seconds (1.0));
server1Apps.Stop (Seconds (10.0));
UdpServerHelper Server2 (5);
ApplicationContainer server2Apps = Server2.Install (nodes.Get (0));
server2Apps.Start (Seconds (1.0));
server2Apps.Stop (Seconds (10.0));
UdpClientHelper Client1 (interfaces.GetAddress (1), 9);
Client1.SetAttribute ("MaxPackets", UintegerValue (1));
Client1.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
Client1.SetAttribute ("PacketSize", UintegerValue (1024));
UdpClientHelper Client2 (interfaces.GetAddress (0), 5);
Client2.SetAttribute ("MaxPackets", UintegerValue (1));
Client2.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
Client2.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer client1Apps = Client1.Install (nodes.Get (0));
client1Apps.Start (Seconds (2.0));
client1Apps.Stop (Seconds (10.0));
ApplicationContainer client2Apps = Client2.Install (nodes.Get (1));
client2Apps.Start (Seconds (3.0));
client2Apps.Stop (Seconds (8.0));
//For ASCII tracing::
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll(ascii.CreateFileStream("nishalab1.tr"));
//For pcap tracing::
pointToPoint.EnablePcapAll("nishalab1");
//For animation::
AnimationInterface anim ("nishalab1.xml");
//Flow monitoring
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll();
Simulator::Stop (Seconds(10.0));
Simulator::Run ();
//Print per flow statistics
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 iter = stats.begin (); iter != stats.end (); ++iter)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (iter->first);
if ((t.sourceAddress == Ipv4Address("10.1.1.1") && t.destinationAddress == Ipv4Address("10.1.1.2")) || (t.sourceAddress == Ipv4Address("10.1.1.2") && t.destinationAddress == Ipv4Address("10.1.1.1")))
{
NS_LOG_UNCOND("Flow ID: " << iter->first << " Source Addr " << t.sourceAddress << " Destination Addr " << t.destinationAddress);
NS_LOG_UNCOND("Transmitted Packets = " << iter->second.txPackets);
NS_LOG_UNCOND("Received Packets = " << iter->second.rxPackets);
NS_LOG_UNCOND("Delay Sum = " << iter->second.delaySum);
NS_LOG_UNCOND("Jitter Sum = " << iter->second.jitterSum);
NS_LOG_UNCOND("Lost Packets = " << iter->second.lostPackets);
NS_LOG_UNCOND("Throughput: " << iter->second.rxBytes * 8.0 / (8*1024) << " Kbps");
}
}
monitor->SerializeToXmlFile("first-lab", true, true);
Simulator::Destroy ();
return 0;
}