Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
- /*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- #include "ns3/command-line.h"
- #include "ns3/config.h"
- #include "ns3/string.h"
- #include "ns3/log.h"
- #include "ns3/yans-wifi-helper.h"
- #include "ns3/ssid.h"
- #include "ns3/mobility-helper.h"
- #include "ns3/on-off-helper.h"
- #include "ns3/yans-wifi-channel.h"
- #include "ns3/mobility-model.h"
- #include "ns3/packet-sink.h"
- #include "ns3/packet-sink-helper.h"
- #include "ns3/tcp-westwood.h"
- #include "ns3/internet-stack-helper.h"
- #include "ns3/ipv4-address-helper.h"
- #include "ns3/ipv4-global-routing-helper.h"
- #include "ns3/core-module.h"
- #include "ns3/network-module.h"
- #include "ns3/csma-module.h"
- #include "ns3/internet-module.h"
- #include "ns3/point-to-point-module.h"
- #include "ns3/applications-module.h"
- #include "ns3/ipv4-global-routing-helper.h"
- #include "ns3/flow-monitor-module.h"
- #include "ns3/flow-monitor-helper.h"
- // Default Network Topology
- //
- // 10.3.0.0
- // n5 n4 n3 n2 n0 ---------------- n1 n6 n7 n8 n9-> server
- // | | | | | point-to-point | | | | |
- // ===================== ====================
- // senders 10.1.0.0 receivers 10.2.0.0
- //n0 is client
- using namespace ns3;
- NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");
- Ptr<PacketSink> sink; /* Pointer to the packet sink application */
- uint64_t lastTotalRx = 0; /* The value of the last total received bytes */
- double total = 0;
- int n = 0;
- class MyApp : public Application
- {
- public:
- MyApp ();
- virtual ~MyApp ();
- /**
- * Register this type.
- * \return The TypeId.
- */
- static TypeId GetTypeId (void);
- void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate);
- private:
- virtual void StartApplication (void);
- virtual void StopApplication (void);
- void ScheduleTx (void);
- void SendPacket (void);
- Ptr<Socket> m_socket;
- Address m_peer;
- uint32_t m_packetSize;
- uint32_t m_nPackets;
- DataRate m_dataRate;
- EventId m_sendEvent;
- bool m_running;
- uint32_t m_packetsSent;
- };
- MyApp::MyApp ()
- : m_socket (0),
- m_peer (),
- m_packetSize (0),
- m_nPackets (0),
- m_dataRate (0),
- m_sendEvent (),
- m_running (false),
- m_packetsSent (0)
- {
- }
- MyApp::~MyApp ()
- {
- m_socket = 0;
- }
- /* static */
- TypeId MyApp::GetTypeId (void)
- {
- static TypeId tid = TypeId ("MyApp")
- .SetParent<Application> ()
- .SetGroupName ("Tutorial")
- .AddConstructor<MyApp> ()
- ;
- return tid;
- }
- void
- MyApp::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
- {
- m_socket = socket;
- m_peer = address;
- m_packetSize = packetSize;
- m_nPackets = nPackets;
- m_dataRate = dataRate;
- }
- void
- MyApp::StartApplication (void)
- {
- m_running = true;
- m_packetsSent = 0;
- if (InetSocketAddress::IsMatchingType (m_peer))
- {
- m_socket->Bind ();
- }
- else
- {
- m_socket->Bind6 ();
- }
- m_socket->Connect (m_peer);
- SendPacket ();
- }
- void
- MyApp::StopApplication (void)
- {
- m_running = false;
- if (m_sendEvent.IsRunning ())
- {
- Simulator::Cancel (m_sendEvent);
- }
- if (m_socket)
- {
- m_socket->Close ();
- }
- }
- void
- MyApp::SendPacket (void)
- {
- Ptr<Packet> packet = Create<Packet> (m_packetSize);
- m_socket->Send (packet);
- if (++m_packetsSent < m_nPackets)
- {
- ScheduleTx ();
- }
- }
- void
- MyApp::ScheduleTx (void)
- {
- if (m_running)
- {
- Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
- m_sendEvent = Simulator::Schedule (tNext, &MyApp::SendPacket, this);
- }
- }
- // static void
- // CwndChange (Ptr<OutputStreamWrapper> stream, uint32_t oldCwnd, uint32_t newCwnd)
- // {
- // NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "\t" << newCwnd);
- // *stream->GetStream () << Simulator::Now ().GetSeconds () << "\t" << oldCwnd << "\t" << newCwnd << std::endl;
- // // }
- // static void
- // RxDrop (Ptr<PcapFileWrapper> file, Ptr<const Packet> p)
- // {
- // NS_LOG_UNCOND ("RxDrop at " << Simulator::Now ().GetSeconds ());
- // file->Write (Simulator::Now (), p);
- // }
- void
- CalculateThroughput ()
- {
- Time now = Simulator::Now (); /* Return the simulator's virtual time. */
- double cur = (sink->GetTotalRx () - lastTotalRx) * (double) 8 / 1e5; /* Convert Application RX Packets to MBits. */
- std::cout << now.GetSeconds () << "s: \t" << cur << " Mbit/s" << std::endl;
- total += cur ;
- n++;
- lastTotalRx = sink->GetTotalRx ();
- Simulator::Schedule (MilliSeconds (100), &CalculateThroughput);
- }
- int
- main (int argc, char *argv[])
- {
- Config::SetDefault ("ns3::TcpL4Protocol::SocketType", StringValue ("ns3::TcpNewReno"));
- Time::SetResolution (Time::NS);
- LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
- LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
- bool useV6 = false ;
- uint nosenders = 4;
- uint noreceivers = 4;
- //empty node containers
- NodeContainer routers, senders, receivers;
- routers.Create(2);
- senders.Create(nosenders);
- receivers.Create(noreceivers);
- PointToPointHelper p2pHR, p2pRR ;
- p2pHR.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
- p2pHR.SetChannelAttribute ("Delay", StringValue ("2ms"));
- p2pRR.SetDeviceAttribute ("DataRate", StringValue ("1.5Mbps"));
- p2pRR.SetChannelAttribute ("Delay", StringValue ("50ms"));
- //Empty containers
- NetDeviceContainer routerDevices = p2pRR.Install(routers);
- NetDeviceContainer leftRouterDevices, rightRouterDevices, senderDevices, receiverDevices;
- //Adding links
- for(uint i = 0 ; i<nosenders ; i++){
- //left side links
- NetDeviceContainer cleft = p2pHR.Install(routers.Get(0),senders.Get(i));
- leftRouterDevices.Add(cleft.Get(0));
- senderDevices.Add(cleft.Get(1));
- //right side links
- NetDeviceContainer cright = p2pHR.Install(routers.Get(1), receivers.Get(i));
- rightRouterDevices.Add(cright.Get(0));
- receiverDevices.Add(cright.Get(1));
- }
- //Install Internet Stack
- InternetStackHelper stack;
- stack.Install(routers);
- stack.Install(senders);
- stack.Install(receivers);
- //Adding IP address
- Ipv4AddressHelper routerIP = Ipv4AddressHelper("10.3.0.0","255.255.255.0");
- Ipv4AddressHelper senderIP = Ipv4AddressHelper("10.1.0.0","255.255.255.0");
- Ipv4AddressHelper receiverIP = Ipv4AddressHelper("10.2.0.0","255.255.255.0");
- Ipv4InterfaceContainer routerIFC, senderIFCs, receiverIFCs, leftRouterIFCs, rightRouterIFCs;
- //assign ipv4
- routerIFC = routerIP.Assign(routerDevices);
- for(uint i = 0; i < nosenders ; ++i) {
- NetDeviceContainer senderDevice;
- senderDevice.Add(senderDevices.Get(i));
- senderDevice.Add(leftRouterDevices.Get(i));
- Ipv4InterfaceContainer senderIFC = senderIP.Assign(senderDevice);
- senderIFCs.Add(senderIFC.Get(0));
- leftRouterIFCs.Add(senderIFC.Get(1));
- //Increment the network number and reset the IP address counter
- //to the base value provided in the SetBase method.
- senderIP.NewNetwork();
- NetDeviceContainer receiverDevice;
- receiverDevice.Add(receiverDevices.Get(i));
- receiverDevice.Add(rightRouterDevices.Get(i));
- Ipv4InterfaceContainer receiverIFC = receiverIP.Assign(receiverDevice);
- receiverIFCs.Add(receiverIFC.Get(0));
- rightRouterIFCs.Add(receiverIFC.Get(1));
- receiverIP.NewNetwork();
- }
- uint16_t sinkPort = 8080;
- Address sinkAddress;
- Address anyAddress;
- std::string probeType;
- std::string tracePath;
- if (useV6 == false)
- {
- sinkAddress = InetSocketAddress (receiverIFCs.GetAddress(0), sinkPort);
- anyAddress = InetSocketAddress (Ipv4Address::GetAny (), sinkPort);
- probeType = "ns3::Ipv4PacketProbe";
- tracePath = "/NodeList/*/$ns3::Ipv4L3Protocol/Tx";
- }
- else
- {
- //sinkAddress = Inet6SocketAddress (interfaces.GetAddress (1,1), sinkPort);
- anyAddress = Inet6SocketAddress (Ipv6Address::GetAny (), sinkPort);
- probeType = "ns3::Ipv6PacketProbe";
- tracePath = "/NodeList/*/$ns3::Ipv6L3Protocol/Tx";
- }
- // topology done
- // UdpEchoServerHelper echoServer(1234);
- // ApplicationContainer serverApps = echoServer.Install (receivers.Get (nosenders-1));
- // serverApps.Start (Seconds (1.0));
- // serverApps.Stop (Seconds (10.0));
- // UdpEchoClientHelper echoClient (senderIFCs.GetAddress (0), 1234);
- // echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
- // echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
- // echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
- // ApplicationContainer clientApps = echoClient.Install (senders.Get (nosenders-1));
- // clientApps.Start (Seconds (2.0));
- // clientApps.Stop (Seconds (10.0));
- //PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", anyAddress);
- //ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (1));
- //sinkApps.Start (Seconds (0.));
- //sinkApps.Stop (Seconds (20.));
- Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
- for(int i=0;i<3;i++)
- {
- sinkAddress = InetSocketAddress (receiverIFCs.GetAddress(i), sinkPort);
- PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", anyAddress);
- ApplicationContainer sinkApps = packetSinkHelper.Install (receivers.Get (i));
- sink = StaticCast<PacketSink>(sinkApps.Get(0));
- OnOffHelper server("ns3::TcpSocketFactory",(InetSocketAddress(receiverIFCs.GetAddress(i),sinkPort)));
- server.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
- server.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
- server.SetAttribute ("PacketSize", UintegerValue (1000));
- server.SetAttribute ("DataRate", StringValue ("100Mbps"));
- ApplicationContainer serverApp = server.Install(senders.Get(i));
- sinkApps.Start (Seconds (0.));
- serverApp.Start(Seconds(1.0));
- }
- // // Ptr<MyApp> app = CreateObject<MyApp>()PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", anyAddress);
- // ApplicationContainer sinkApps = packetSinkHelper.Install (routers.Get (0));
- // sink = StaticCast<PacketSink>(sinkApps.Get(0));
- // OnOffHelper server("ns3::TcpSocketFactory",(InetSocketAddress(routerIFC.GetAddress(0),sinkPort)));
- // server.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
- // server.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
- // server.SetAttribute ("PacketSize", UintegerValue (1000));
- // server.SetAttribute ("DataRate", StringValue ("100Mbps"));
- // ApplicationContainer serverApp = server.Install(routers.Get(1));
- // sinkApps.Start (Seconds (0.));
- // serverApp.Start(Seconds(1.0));;
- // app->Setup (ns3TcpSocket, sinkAddress, 1040, 1000, DataRate ("100Mbps"));
- // app->SetStartTime (Seconds(1.));
- // app->SetStopTime (Seconds (20.));
- Simulator::Schedule(Seconds(1.1),&CalculateThroughput);
- Ptr<FlowMonitor> flowMonitor;
- FlowMonitorHelper flowHelper;
- flowMonitor = flowHelper.InstallAll();
- flowMonitor->CheckForLostPackets();
- Simulator::Stop (Seconds (2));
- Simulator::Run ();
- flowMonitor->SerializeToXmlFile("flowMonitor.xml", false, true);
- Simulator::Destroy ();
- std::cout<<"Average is : "<<(total)/(n)<<std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement