Skip to content

Instantly share code, notes, and snippets.

@amiribrahim
Created April 20, 2017 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amiribrahim/47b31eb1d3918e21fa6717c7f5098af2 to your computer and use it in GitHub Desktop.
Save amiribrahim/47b31eb1d3918e21fa6717c7f5098af2 to your computer and use it in GitHub Desktop.
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2011-2015 Regents of the University of California.
*
* This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
* contributors.
*
* ndnSIM is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* ndnSIM 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
* ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
**/
// ndn-multiple-strategies.cpp
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/point-to-point-layout-module.h"
#include "ns3/ndnSIM-module.h"
#include "ns3/mobility-module.h"
#include "ns3/config-store-module.h"
#include "ns3/wifi-module.h"
#include "ns3/netanim-module.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
using namespace ns3;
using ns3::ndn::StackHelper;
using ns3::ndn::AppHelper;
using ns3::ndn::GlobalRoutingHelper;
using ns3::ndn::StrategyChoiceHelper;
/**
* This scenario simulates a grid topology (using PointToPointGrid module)
*
* The first six nodes use the best route forwarding strategy, whereas
* the three remaining nodes use the multicast forwarding strategy.
*
* (consumer) -- ( ) ----- ( )
* | | |
* ( ) ------ ( ) ----- ( )
* | | |
* ( ) ------ ( ) -- (producer)
*
* All links are 1Mbps with propagation 10ms delay.
*
* FIB is populated using NdnGlobalRoutingHelper.
*
* Consumer requests data from producer with frequency 100 interests per second
* (interests contain constantly increasing sequence number).
*
* For every received interest, producer replies with a data packet, containing
* 1024 bytes of virtual payload.
*
* To run scenario and see what is happening, use the following command:
*
* NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-multiple-strategies
*/
int
main(int argc, char* argv[])
{
// Setting default parameters for PointToPoint links and channels
Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("1Mbps"));
Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("10ms"));
Config::SetDefault("ns3::DropTailQueue::MaxPackets", StringValue("10"));
// Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
CommandLine cmd;
uint32_t nRows = 5, nCols = 5, nAPs = 8 ,nConsumers = 15;
double xMin = -100.0, xMax = 100.0;
double yMin = -100.0, yMax = 100.0;
Ptr<UniformRandomVariable> randomizer = CreateObject<UniformRandomVariable>();
randomizer->SetAttribute("Min", DoubleValue(-100.0));
randomizer->SetAttribute("Max", DoubleValue(200.0));
//std::cout<<"Before parsing: "<<nRows<<"\n";
cmd.AddValue ("nRows", "Number of router nodes = nRows*nCols", nRows);
cmd.AddValue ("nCols", "Number of router nodes = nRows*nCols", nCols);
cmd.AddValue ("nAPs", "Number of wireless access points", nAPs);
cmd.AddValue ("nConsumers", "Number of consumers", nConsumers);
//cmd.AddValue ("topoType", "Topology type (grid, dumbbell, star)", topoType);
cmd.Parse(argc, argv);
//std::cout<<"After parsing: "<<nRows<<"\n";
// Creating 3x3 topology
PointToPointHelper p2p;
PointToPointGridHelper grid(nRows, nCols, p2p);
grid.BoundingBox(xMin, yMin, xMax, yMax);
//create wifi APs and mobile nodes
NodeContainer wifiAPs , wifiConsumers, routers;
wifiAPs.Create (nAPs);
wifiConsumers.Create (nConsumers);
WifiHelper wifi = WifiHelper::Default ();
wifi.SetStandard(WIFI_PHY_STANDARD_80211a);
wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager", "DataMode",
StringValue("OfdmRate24Mbps"));
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
phy.SetChannel (wifiChannel.Create ());
//phy.Set("TxPowerStart", DoubleValue(5));
//phy.Set("TxPowerEnd", DoubleValue(5));
wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
NqosWifiMacHelper mac = NqosWifiMacHelper::Default ();
Ssid ssid = Ssid ("Cisc825");
//set wifi station nodes/consumers type and installing net devices
mac.SetType ("ns3::StaWifiMac",
"Ssid", SsidValue (ssid),
"ActiveProbing", BooleanValue (false));
NetDeviceContainer consDevices;
consDevices = wifi.Install (phy, mac, wifiConsumers);
//set wifi Access points type and installing net devices
mac.SetType ("ns3::ApWifiMac",
"Ssid", SsidValue (ssid));
NetDeviceContainer apDevices;
apDevices = wifi.Install (phy, mac, wifiAPs);
////////////////////////////////////////////////////
//create mobility model for mobile nodes (Consumers)
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::RandomRectanglePositionAllocator",
"X", StringValue ("ns3::UniformRandomVariable[Min=-100.0|Max=200.0]"),
"Y", StringValue ("ns3::UniformRandomVariable[Min=-100.0|Max=200.0]"));
//mobility.SetPositionAllocator(ConsPositionAlloc);
mobility.SetMobilityModel ("ns3::RandomDirection2dMobilityModel",
"Bounds", RectangleValue (Rectangle (-100.0, 200.0, -100.0, 200.0)),
"Speed", StringValue ("ns3::ConstantRandomVariable[Constant=2]"),
"Pause", StringValue ("ns3::ConstantRandomVariable[Constant=0.2]"));
/*mobility.SetPositionAllocator ("ns3::RandomDiscPositionAllocator",
"X", StringValue ("0.0"),
"Y", StringValue ("0.0"),
"Rho", StringValue ("ns3::UniformRandomVariable[Min=0|Max=50]"));
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Mode", StringValue ("Time"),
"Time", StringValue ("2s"),
"Speed", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"),
"Bounds", StringValue ("-100|100|-100|100"));
*/
mobility.Install (wifiConsumers);
//mobility.SetPositionAllocator("ns3::RandomBoxPositionAllocator", "X", PointerValue(randomizer),
// "Y", PointerValue(randomizer));
mobility.SetPositionAllocator ("ns3::RandomRectanglePositionAllocator",
"X", StringValue ("ns3::UniformRandomVariable[Min=-100.0|Max=200.0]"),
"Y", StringValue ("ns3::UniformRandomVariable[Min=-100.0|Max=200.0]"));
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (wifiAPs);
//testing////////////////////////////////////////////////////////////////////////////////
Ptr<MobilityModel> mob;
Vector pos;
//Ptr<MobilityModel> mob = nodes.Get(i)->GetObject<MobilityModel>();
//Vector pos = mob->GetPosition ();
//std::cout << "POS: x=" << pos.x << ", y=" << pos.y << std::endl;
float gridXMax = grid.GetNode(nRows-1,nCols-1)->GetObject<MobilityModel>()->GetPosition ().x;
float gridYMax = grid.GetNode(nRows-1,nCols-1)->GetObject<MobilityModel>()->GetPosition ().y;
float gridXMin = grid.GetNode(0,0)->GetObject<MobilityModel>()->GetPosition ().x;
float gridYMin = grid.GetNode(0,0)->GetObject<MobilityModel>()->GetPosition ().y;
float xFract = (gridXMax-gridXMin)/(nCols+1);
float yFract = (gridYMax-gridYMin)/(nRows+1);
int gridRow, gridCol; //will use these to set the closest grid block to connect the AP to this grid's router
for(uint32_t i = 0; i < nRows; i++) {
for(uint32_t j = 0; j < nCols; j++) {
routers.Add(grid.GetNode(i,j));
mob = grid.GetNode(i,j)->GetObject<MobilityModel>();
pos = mob->GetPosition ();
std::cout << "Router at row*col"<<i<<"*"<<j<<"pos is : x= " << pos.x << ", y= " << pos.y << std::endl;
}
}
for(uint32_t i = 0; i < nAPs; i++) {
mob = wifiAPs.Get(i)->GetObject<MobilityModel>();
pos = mob->GetPosition ();
//due to the way P2PGridHelper works I'll use xMin/2 and others
gridCol = ((int)round((pos.x)/xFract))-1;
gridRow = ((int)round((pos.y)/yFract))-1;
if(gridCol < 0 )
gridCol = 0;
if(gridRow < 0)
gridRow = 0;
if(gridCol >= nCols )
gridCol = nCols-1;
if(gridRow >= nRows)
gridRow = nRows-1;
//std::cout << "gridRow: "<<gridRow<< ", gridCol: " << gridCol << std::endl;
p2p.Install(wifiAPs.Get(i),grid.GetNode(gridRow, gridCol));
//std::cout << "AP no."<<i<<": x= " << pos.x << ", y= " << pos.y << std::endl;
std::cout << "AP no."<<i<<": Grid row. = " << gridRow << ", Grid col = " << gridCol << std::endl;
}
////////////////////////////////////////////////////////////////////////////////////////////
// Install NDN stack on all nodes
StackHelper ndnHelper;
//both routers and WiFi APs are cache enabled
ndnHelper.SetOldContentStore("ns3::ndn::cs::Lfu", "MaxSize", "1000");
ndnHelper.Install(routers);
ndnHelper.Install(wifiAPs);
// ndnHelper.InstallAll();
//mobile consumers can't cache
ndnHelper.SetOldContentStore("ns3::ndn::cs::Nocache");
ndnHelper.Install(wifiConsumers);
// Installing global routing interface on all nodes
GlobalRoutingHelper ndnGlobalRoutingHelper;
ndnGlobalRoutingHelper.InstallAll();
// ndnGlobalRoutingHelper.Install(routers);
// ndnGlobalRoutingHelper.Install(wifiAPs);
// ndnGlobalRoutingHelper.Install(wifiConsumers);
// Getting containers for the consumer/producer
Ptr<Node> producer = grid.GetNode(nRows-1, nCols-1);
// Install NDN applications
std::string prefix = "/prefix";
//install forwarding strategy ()
StrategyChoiceHelper::Install(routers, prefix,"/localhost/nfd/strategy/best-route");
StrategyChoiceHelper::Install(wifiAPs, prefix,"/localhost/nfd/strategy/best-route");
StrategyChoiceHelper::Install(wifiConsumers, prefix,"/localhost/nfd/strategy/best-route");
AppHelper consumerHelper("ns3::ndn::ConsumerCbr");
consumerHelper.SetPrefix(prefix);
consumerHelper.SetAttribute("Frequency", StringValue("2")); // 100 interests a second
consumerHelper.Install(wifiConsumers);
AppHelper producerHelper("ns3::ndn::Producer");
producerHelper.SetPrefix(prefix);
producerHelper.SetAttribute("PayloadSize", StringValue("1024"));
producerHelper.Install(producer);
ndnGlobalRoutingHelper.AddOrigins(prefix, producer);
AnimationInterface anim ("/home/ns3/animation.xml");
// Calculate and install FIBs
GlobalRoutingHelper::CalculateRoutes();
Simulator::Stop(Seconds(20.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment