version :ns 3.39
//
// Created by manoj on 22/4/24.
//
/*
* 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/CbrSource.h"
#include "ns3/TdmaNetDeviceSp_Ds.h"
#include "ns3/aodv-helper.h"
#include "ns3/aodv-routing-protocol.h"
#include "ns3/applications-module.h"
#include "ns3/core-module.h"
#include "ns3/drand_comm.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/internet-module.h"
#include "ns3/ipv4-routing-table-entry.h"
#include "ns3/mobility-helper.h"
#include "ns3/mobility-module.h"
#include "ns3/netanim-module.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/random-variable-stream.h" // Include for RandomRectanglePositionAllocator
#include "ns3/random-waypoint-mobility-model.h" // Include for RandomWaypointMobilityModel
#include "ns3/simple-channel.h"
#include "ns3/spectrum-channel.h"
#include "ns3/tdma-helper.h"
#include <ns3/olsr-helper.h>
#include <ns3/olsr-routing-protocol.h>
#include <ns3/spectrum-helper.h>
#include "ns3/SP_DS_Common.h"
'
'
'
'
'
int
main(int argc, char* argv[])
{
CommandLine cmd(__FILE__);
cmd.Parse(argc, argv);
Time::SetResolution(Time::NS);
// asd LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
// LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
// LogComponentEnable("CbrSource",LOG_LEVEL_INFO);
// LogComponentEnable("OnOffApplication", LOG_LEVEL_INFO);
PacketMetadata::Enable ();
nodes.Create(10);
//----------------------Setting Mobility
//Mobility for all the nodes.
// double simulationAreaX=300;
// double simulationAreaY=300;
// double nodeSpeed=4;
MobilityHelper mobility;
// Ptr<PositionAllocator> positionAlloc = CreateObject<RandomRectanglePositionAllocator> ();
// positionAlloc->SetAttribute ("X", StringValue ("ns3::UniformRandomVariable[Min=-150.0|Max=150.0]"));
// positionAlloc->SetAttribute ("Y", StringValue ("ns3::UniformRandomVariable[Min=-150.0|Max=150.0]"));
//
// Ptr<RandomVariableStream> speedRandomVariable = CreateObject<UniformRandomVariable>();
// speedRandomVariable->SetAttribute("Min", DoubleValue(0.3));
// speedRandomVariable->SetAttribute("Max", DoubleValue(0.7));
//
// // Set the Speed attribute using a PointerValue
// mobility.SetMobilityModel("ns3::RandomWaypointMobilityModel",
// "Speed", PointerValue(speedRandomVariable),
// "PositionAllocator", PointerValue(positionAlloc));
int width = ceil(sqrt(N));
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (dist),
"DeltaY", DoubleValue (dist),
"GridWidth", UintegerValue (width),
"LayoutType", StringValue ("RowFirst"));
// mobility.SetPositionAllocator ("ns3::RandomBoxPositionAllocator",
// "X", PointerValue (CreateObjectWithAttributes<UniformRandomVariable> ("Min", DoubleValue (0.0), "Max", DoubleValue (dist ))),
// "Y", PointerValue (CreateObjectWithAttributes<UniformRandomVariable> ("Min", DoubleValue (0.0), "Max", DoubleValue (dist ))),
// "Z", PointerValue (CreateObjectWithAttributes<UniformRandomVariable> ("Min", DoubleValue (0.0), "Max", DoubleValue (100.0))));
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); //constant position
mobility.Install (nodes);
//----------------------------------------------------------------------------
UpdateTopology ();
//--------------------------------------------
InitPsd();
for (unsigned int chId = 0; chId < numCh; chId++)
{
//M:SpectrumChannelHelper channelHelper = SpectrumChannelHelper::Default ();
channelList.push_back (channelHelper.Create ());//M:numCh = 6; // number of channels available
}
TdmaNetDeviceHelper tdma;
tdma.SetTxPowerSpectralDensity (txPsd);
tdma.SetNoisePowerSpectralDensity (noisePsd);
tdma.SetNumPhy(numBands);
tdma.SetPhyAttribute ("Rate", DataRateValue (DataRate (PHYtxrate)));
tdma.SetChannelList (channelList);
// Ptr<SimpleChannel> channel= CreateObject<SimpleChannel>();
Ptr<MultiRxChannel> channel= CreateObject<MultiRxChannel>();
// Ptr<SpectrumChannel> channel= CreateObject<SpectrumChannel>();
auto it= nodes.Get(1)->GetObject<MobilityModel>();
NetDeviceContainer devices;
devices = tdma.Install(nodes,channel);
for (uint32_t i=0;i<nodes.GetN();i++)
{
nodes_List[0].Add(nodes.Get(i));//pushing nodes to nodelist
}
//--ALLOCATION OF SLOTS____
//AllocateSlots_Example();
//initialising all cj_estimates for all nodes
//--------------------------------
for(uint32_t i=0;i<nodes.GetN();i++)
{
win_chance_prob[i].nodeId=nodes.Get(i)->GetId();
win_chance_prob[i].slotReserved=0;
win_chance_prob[i].cj_estimate_value=0;
}
//setting all one hop and two hop neighbours knowing to all nodes with their distance ;;for now you are getting with topology
// later need to be modified with routing table;
for(uint32_t i=0;i<nodes.GetN();i++)
{
std::vector<hopnbInfo> hop1info;
std::vector<hopnbInfo> hop2info;
Ptr<TdmaNetDeviceSp_Ds> dev= DynamicCast<TdmaNetDeviceSp_Ds>(nodes.Get(i)->GetDevice(0));
std::vector <uint32_t> hop1Nbs_NodeIds;
std::vector <uint32_t> hop2Nbs_NodeIds;
hop1Nbs_NodeIds=topology[i].hop1Nbs;
hop2Nbs_NodeIds=topology[i].hop2Nbs;
for(int cnt=0;cnt<int(hop1Nbs_NodeIds.size());cnt++)
{
hop1info.push_back(hopnbInfo(hop1Nbs_NodeIds[cnt],Mac48Address::ConvertFrom(nodes.Get(hop1Nbs_NodeIds[cnt])->GetDevice(0)->GetAddress()) ,CalculateDistance(topology[i].pos,topology[hop1Nbs_NodeIds[cnt]].pos)));
// hop1info.push_back(hopnbInfo(hop1Nbs_NodeIds[cnt] ,CalculateDistance(topology[i].pos,topology[hop1Nbs_NodeIds[cnt]].pos)));
// hop1info[cnt].nodeId=hop1Nbs_NodeIds[cnt];
// hop1info[cnt].distance= CalculateDistance(topology[i].pos,topology[hop1Nbs_NodeIds[cnt]].pos);
}
dev->SetOnehopNbs(hop1info);
for(int cnt=0;cnt<int(hop2Nbs_NodeIds.size());cnt++)
{
hop2info.push_back(hopnbInfo(hop2Nbs_NodeIds[cnt],Mac48Address::ConvertFrom(nodes.Get(i)->GetDevice(0)->GetAddress()) ,CalculateDistance(topology[i].pos,topology[hop2Nbs_NodeIds[cnt]].pos)));
// without ac adress
// hop2info.push_back(hopnbInfo(hop2Nbs_NodeIds[cnt],CalculateDistance(topology[i].pos,topology[hop2Nbs_NodeIds[cnt]].pos)));
// hop2info[cnt].nodeId=hop2Nbs_NodeIds[cnt];
// hop2info[cnt].distance= CalculateDistance(topology[i].pos,topology[hop2Nbs_NodeIds[cnt]].pos);
}
dev->SetTwohopnbs(hop2info);
}
//--------------------------------
std::vector<int> current_freeslots=freeSlots_example;
//---to print allocated slots --this i used for static assignment
// printAllocatedSlots(nodes_List[0]);
InternetStackHelper stack;
Ipv4ListRoutingHelper listRouting;
AodvHelper aodv;
OlsrHelper olsrHelper;
listRouting.Add(ipv4StaticRoutingHelper,0); //only used between local interfaces
// listRouting.Add(olsrHelper,10);
listRouting.Add(aodv,10);
// listRouting.Add(olsrHelper,10); //higher priority is preferred
// internet.SetRoutingHelper (listRouting);
stack.SetRoutingHelper(listRouting);
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
interfaces = address.Assign(devices);
//------------------------------------------------------------
/*
//Multicast WRT IP
ipv4MulticastAddr.SetBase ("232.0.0.0","255.255.255.0"); // multicast address generator
SetMulticastAssoc ();
// Set up default multicast routes at all nodes: just to indicate that multicast packets are to be sent to the device
for (unsigned int i = 0; i < 10; i++)
multicast.SetDefaultMulticastRoute (nodes.Get(i),devices.Get(i));
//Now we make various multicast Groups: corresponding to direct and direct+1hop neighbors of each node
UpdateMulticastRoutes();
*/
//-------------------------------------------------------------
//_______________________________Setting apps
std::vector<Ipv4Address> nodeIp(10);
std::vector<App> applist;
std::vector <unsigned int> appCount(10,0);
nodes_Ip_SP_DS.clear();
allNodes_onehopNbrs.resize(10);
// Initialize each element with null values
for (size_t i = 0; i < allNodes_onehopNbrs.size(); ++i) {
allNodes_onehopNbrs[i].nodeId = 0; // Assuming null value for nodeId is 0
// Assuming null value for onehps vector is an empty vector
allNodes_onehopNbrs[i].onehps.clear();
}
nodes_Ip_SP_DS.resize(10,NodeByIP(0,interfaces.GetAddress(0)));
for (unsigned int i = 0; i < 10; i++)
{
nodes_Ip_SP_DS[i]=(NodeByIP((uint32_t)i,interfaces.GetAddress(i)));
nodeIp[i] = interfaces.GetAddress(i);
}
// std::vector<routingtableinfo >route_info_nodes;
route_info_nodes.resize(Sp_DS_numnodes);
for (int i = 0; i < (int)Sp_DS_numnodes; i++)
{
// nodes_Ip_SP_DS[i]=(NodeByIP((uint32_t)i,interfaces.GetAddress(i)));
route_info_nodes[i].local=nodeIp[i] ;
route_info_nodes[i].routeInfo.resize(10);
}
for (int i = 0; i < (int)Sp_DS_numnodes; i++)
{
// nodes_Ip_SP_DS[i]=(NodeByIP((uint32_t)i,interfaces.GetAddress(i)));
for (int j = 0; j < (int)Sp_DS_numnodes; j++)
{
route_info_nodes[i].routeInfo[j].destination=nodeIp[j] ;
}
}
unsigned int numApps = 0;
for (unsigned int i = 0; i < dataSrc.size(); i++)
{
// isUnicast[dataSrc[i]].push_back(true);
applist.emplace_back (dataSrc[i], appCount[dataSrc[i]]++,true, dataDest[i], numApps++);
CreateObject<CbrSource> ("FileTransfer", nodes.Get(dataSrc[i]), nodeIp[dataDest[i]], dataPort + i);
std::cout << "Source IP " << nodeIp[nodes.Get(dataSrc[i])->GetId()] << " Dest IP " << nodeIp[dataDest[i]] << " Port " << dataPort+i << "\n";
Ptr<TdmaNetDeviceSp_Ds> dev0= DynamicCast<TdmaNetDeviceSp_Ds>(nodes.Get(dataSrc[i])->GetDevice(0));
// Simulator::Schedule(appStartTime,&TdmaNetDeviceSp_Ds::SET_TX_WAIT,dev0);
}
for (auto app : applist)
{
nodes.Get (app.src)->GetApplication (app.appOrder)->SetStartTime (appStartTime);//M: inline Time appStartTime = Seconds(30);
std::cout << "At node " << app.src << " app " << app.appOrder << " started at Time " << appStartTime.GetMilliSeconds() << "\n";
nodes.Get (app.src)->GetApplication (app.appOrder)->SetStopTime (simEndTime);
}
for (auto app : applist)
{
Ptr<CbrSource> cbrSource = nodes.Get(app.src)->GetApplication (app.appOrder)->GetObject<CbrSource>();
PacketSinkHelper sinkHelper (cbrSource->GetSocketTypeId().GetName(),cbrSource->GetPeerAddress());
// sinkHelper.Install (nodes.Get (app.dest));
// The constructor of PacketSinkHelper allows you to specify the socket type and address for the packet sink,
// making it adaptable to different communication scenarios.
if (app.isUnicast)
{
sinkHelper.Install (nodes.Get (app.dest));
//std::cout << "Sink installed for appId = " << app.appId << " src = " << app.src << " at node = " << app.dest << "\n";
}
else
// for (unsigned int jt = N*node2Net[app.src]; jt< N+N*node2Net[app.src]; jt++)//topology[allNodes.Get(app.src)->GetId()].allNbs) //
for (unsigned int jt = 0; jt< 10; jt++)//topology[allNodes.Get(app.src)->GetId()].allNbs) //
{
sinkHelper.Install(nodes.Get(jt));
//std::cout << "Sink installed for appId = " << app.appId << " src = " << app.src << " at node = " << jt << "\n";
}
}
//scheduling control phase for all the nodes
for(uint32_t i=0;i<nodes.GetN();i++)
{
Ptr<TdmaNetDeviceSp_Ds> dev= DynamicCast<TdmaNetDeviceSp_Ds>(nodes.Get(i)->GetDevice(0));
Simulator::Schedule(Seconds(4),&TdmaNetDeviceSp_Ds::startSPDS,dev);
}
AnimationInterface anim("TDMATEST.xml");
// anim.EnablePacketMetadata();
// Flowmonitor
Ptr<FlowMonitor> flowmon;
FlowMonitorHelper flowmonHelp;
flowmon= flowmonHelp.InstallAll();
// tdma.EnablePcapAll("TDMATEST");
//_____________________________________________________________
Simulator::Stop (simEndTime);
flowmon->SerializeToXmlFile(("TDMAAODV.flowmon"),false,false);
Simulator::Destroy();
return 0;
}
i have attached my xml file also
what else i can attachÂ
should i attach my NETDEVICE AND SPECTRUMPHY Also???