Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

/* -*- 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
*/

// Default Network Topology


//
// # #
// *
// *
//
// # WLAN AP UDP Data station
// -----15m-----@ #
//
//
// * * VoIP station
//
// # *
// TCP Data station
//
// Random static environment

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/internet-module.h"
#include "ns3/object.h"
#include "ns3/uinteger.h"
#include "ns3/traced-value.h"
#include "ns3/trace-source-accessor.h"
#include "ns3/type-id.h"
#include "ns3/object-base.h"
#include "ns3/scheduler.h"
#include "ns3/calendar-scheduler.h"
#include "ns3/callback.h"
#include <iostream>

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

void
CourseChange (std::string context, Ptr<const MobilityModel> model)
{
Vector position = model->GetPosition ();
NS_LOG_UNCOND (context <<
" x = " <<
position.x <<
", y = " <<
position.y);
}

//set qos-tag for packets


void
SetTagTid ( uint8_t mytid ,Ptr<const Packet> pkt )
{
QosTag qosTag ;
qosTag.SetTid (mytid);
pkt->AddPacketTag ( qosTag );
}

/*
//Set position of the WifiApNodes
//===========================
static void
SetPosition (Ptr<Node> node, Vector position)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
mobility->SetPosition (position);
} */

int
main (int argc, char *argv[])
{
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
// LogComponentEnable ("Aarfcd", LOG_LEVEL_INFO);

uint32_t nvoipsta = 5; //number of VoIP wireless stations


uint32_t ndatasta = 5; //number of DATA wireless stations
uint32_t nsta = nvoipsta + ndatasta; // number wireless stations
uint32_t nap = 1; //number of AP

// Create 11 nodes
NodeContainer wifiApNode; // create WiFi Ap nodes
wifiApNode.Create (nap);
NodeContainer wifiStaNodes; // create WiFi station nodes
wifiStaNodes.Create (nsta);

// Create & setup wifi channel


YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
phy.SetChannel (channel.Create ());

// Install wireless devices


WifiHelper wifi = WifiHelper::Default ();
wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
//wifi.EnableLogComponents(); //add type of log componnet

QosWifiMacHelper mac = QosWifiMacHelper::Default ();

Ssid ssid = Ssid ("ns-3-ssid");


mac.SetType ("ns3::ApWifiMac",
"Ssid", SsidValue (ssid));

NetDeviceContainer apDevices;
apDevices = wifi.Install (phy, mac, wifiApNode);

mac.SetType ("ns3::StaWifiMac",
"Ssid", SsidValue (ssid),
"ActiveProbing", BooleanValue (true));

NetDeviceContainer staDevices;
staDevices = wifi.Install (phy, mac, wifiStaNodes);

/*Config::Set("/$ns3::NodeListPriv/NodeList/0/$ns3::Node/DeviceList/
0/$ns3::WifiNetDevice/Mac/$ns3::AdhocWifiMac/DcaTxop/$ns3::DcaTxop/
Queue/$ns3::WifiMacQueue/MaxDelay", TimeValue(Seconds(...))); */

//Config::Set("/NodeList/y/DeviceList/y/$ns3::WifiNetDevice/Mac/$ns3::RegularWifiMa
c/VO_EdcaTxopN/Queue/$ns3::WifiMacQueue/MaxPacketNumber", UintegerValue(5));

//Config::Set("/NodeList/y/DeviceList/y/$ns3::WifiNetDevice/Mac/$ns3::RegularWifiMa
c/VO_EdcaTxopN/Queue/$ns3::WifiMacQueue/MaxDelay", TimeValue(Seconds(0.01)));

// Setting mobility model (set positions)


MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::UniformDiscPositionAllocator",
"rho", DoubleValue (15.0),
"X", DoubleValue (0.0),
"Y", DoubleValue (0.0));

mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (wifiStaNodes);

mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
//SetPosition (wifiApNode , Vector (0.0,0.0,0.0));
//SetPosition (wifiApNode.Get(0), Vector (0.0,0.0,0.0));
mobility.Install (wifiApNode);

InternetStackHelper stack;
stack.Install (wifiApNode);
stack.Install (wifiStaNodes);

Ipv4AddressHelper address;
NS_LOG_INFO ("Assign IP Addresses.");
address.SetBase ("10.1.3.0", "255.255.255.0");
Ipv4InterfaceContainer wifiApInterfaces;
wifiApInterfaces = address.Assign (apDevices);
Ipv4InterfaceContainer wifiInterfaces;
wifiInterfaces = address.Assign (staDevices);
//Set application = data transfer ==============================
// UDP receiver ----(ABID FOLLOWING NO. 1 SHOWS 1,2 ABID)
Address remoteAddress1 = InetSocketAddress(wifiInterfaces.GetAddress (1),1000);
PacketSinkHelper receiver1 ("ns3::UdpSocketFactory", remoteAddress1);
ApplicationContainer receiverapp1 =receiver1.Install(wifiStaNodes.Get (1));
receiverapp1.Start (Seconds (2.0));
receiverapp1.Stop (Seconds (10.0));

// UDP Sender ----


OnOffHelper sender1("ns3::UdpSocketFactory", remoteAddress1);
sender1.SetConstantRate( DataRate("2Mb/s") , 1508 );
sender1.SetAttribute ("OnTime", StringValue
("ns3::ConstantRandomVariable[Constant=1]"));
sender1.SetAttribute ("OffTime", StringValue
("ns3::ConstantRandomVariable[Constant=1]"));
ApplicationContainer senderapps1 ;

// FOLLOWING 0 IS FOR NODE 0 AND NODE 1


senderapps1= sender1.Install(wifiStaNodes.Get(0));

Config::ConnectWithoutContext("/NodeList/1/ApplicationList/0/$ns3::OnOffApplication
/Rx", MakeCallback (&SetTagTid));
Ptr<OnOffApplication> onoffappBE;
onoffappBE = DynamicCast<OnOffApplication>(senderapps1.Get(0));
onoffappBE->TraceConnectWithoutContext("Tx", MakeBoundCallback
(&SetTagTid, AC_BE));
senderapps1.Start(Seconds(2.0))
;
senderapps1.Stop(Seconds(10.0));

//TCP ============================== BELOW IS FOR 3 TCP (ABID)


// first TCP traffic
//TCP receiver--- ABID Following no. 3 shows that 3, 4, 5 ABID
Address remoteAddress2 = InetSocketAddress (wifiInterfaces.GetAddress (3), 2000);
PacketSinkHelper receiver2 ("ns3::TcpSocketFactory", remoteAddress2);
ApplicationContainer receiverapps2 = receiver2.Install (wifiStaNodes.Get (3));
receiverapps2.Start (Seconds (1.0));
receiverapps2.Stop (Seconds (4.0));

//TCP Sender ----


OnOffHelper sender2 ("ns3::TcpSocketFactory" , remoteAddress2);
sender2.SetConstantRate( DataRate("2Mb/s") , 1508 );
sender2.SetAttribute ("OnTime", StringValue
("ns3::ConstantRandomVariable[Constant=1]"));
sender2.SetAttribute ("OffTime", StringValue
("ns3::ConstantRandomVariable[Constant=0]"));
ApplicationContainer senderpps2 ;
// FOLLOWING 2 IS FOR NODE 2 AND NODE 3 AND NODE 4

senderpps2 = sender2.Install (wifiStaNodes.Get (2));

Config::ConnectWithoutContext("/NodeList/3/ApplicationList/1/$ns3::OnOffApplication
/Rx", MakeCallback (&SetTagTid));
Ptr<OnOffApplication> onoffappBK;
onoffappBK = DynamicCast<OnOffApplication>(senderpps2.Get(0));
onoffappBK->TraceConnectWithoutContext("Tx", MakeBoundCallback
(&SetTagTid, AC_BE));
senderpps2.Start (Seconds (1.0));
senderpps2.Stop (Seconds (4.0));
//second TCP traffic
//TCP receiver---
Address remoteAddress3 = InetSocketAddress (wifiInterfaces.GetAddress (4), 3000);
PacketSinkHelper receiver3 ("ns3::TcpSocketFactory", remoteAddress3);
ApplicationContainer receiverapp3 = receiver3.Install (wifiStaNodes.Get (4));
receiverapp3.Start (Seconds (5.0));
receiverapp3.Stop (Seconds (10.0));

//TCP Sender ----

OnOffHelper sender3 ("ns3::TcpSocketFactory" , remoteAddress3);


sender3.SetConstantRate( DataRate("2Mb/s") , 1508 );
sender3.SetAttribute ("OnTime", StringValue
("ns3::ConstantRandomVariable[Constant=1]"));
sender3.SetAttribute ("OffTime", StringValue
("ns3::ConstantRandomVariable[Constant=0]"));
ApplicationContainer senderapp3;
senderapp3 = sender3.Install (wifiStaNodes.Get (2));

Config::ConnectWithoutContext("/NodeList/3/ApplicationList/2/$ns3::OnOffApplication
/Rx", MakeCallback (&SetTagTid));
Ptr<OnOffApplication> onoffappBK2;
onoffappBK2 = DynamicCast<OnOffApplication>(senderapp3.Get(0));
onoffappBK2->TraceConnectWithoutContext("Tx", MakeBoundCallback
(&SetTagTid, AC_BE));
senderapp3.Start (Seconds (5.0));
senderapp3.Stop (Seconds (10.0));

//CBR = VoIP ==============================


//first VoIP traffic
// VoIP receiver---(ABID FOLLOWING NO.6 SHOWS 6,7,8,9,10 ABID)
Address remoteAddress4 = InetSocketAddress(wifiInterfaces.GetAddress (6), 4000);
PacketSinkHelper receiver4 ("ns3::UdpSocketFactory", remoteAddress4);
ApplicationContainer receiverapp4 =receiver4.Install(wifiStaNodes.Get (6));
receiverapp4.Start (Seconds (5.0));
receiverapp4.Stop (Seconds (8.0));

// VoIP Sender ----


OnOffHelper sender4("ns3::UdpSocketFactory", remoteAddress4);
sender4.SetConstantRate( DataRate("68.8Kbps") , 172); // 160 byte(G.711) + 12
byte (RTP hedear) = 172 byte/ for 20ms packet generation intervall is 68.8kbps
sender4.SetAttribute ("OnTime", StringValue
("ns3::ConstantRandomVariable[Constant=1]"));
sender4.SetAttribute ("OffTime", StringValue
("ns3::ConstantRandomVariable[Constant=0]"));
ApplicationContainer senderapps4;
// FOLLOWING 5 IS FOR NODE 5 AND NODE 6,7,8,9

senderapps4 = sender4.Install(wifiStaNodes.Get(5));

Config::ConnectWithoutContext("/NodeList/6/ApplicationList/3/$ns3::OnOffApplication
/Rx", MakeCallback (&SetTagTid));
Ptr<OnOffApplication> onoffappVO;
onoffappVO = DynamicCast<OnOffApplication>(senderapps4.Get(0));
onoffappVO->TraceConnectWithoutContext("Tx", MakeBoundCallback
(&SetTagTid, AC_VO));
senderapps4.Start(Seconds(5.0));
senderapps4.Stop(Seconds(8.0));
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

// Install FlowMonitor on all nodes


/* FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll();*/

Simulator::Stop (Seconds (10.0));

//output================

phy.EnablePcap ("test5", apDevices);


phy.EnablePcap ("test5" , staDevices);
phy.EnableAscii ("test5" , apDevices);
phy.EnableAscii ("test5" , staDevices);
stack.EnableAsciiIpv4 ("test5" , wifiApInterfaces); //output tr file
for trace of internet stack
stack.EnableAsciiIpv4 ("test5" , wifiInterfaces); //output tr file for
trace of internet stack

Simulator::Run ();
Simulator::Destroy ();
return 0;
}

You might also like