Amruth Gudigar
unread,May 30, 2024, 12:48:03 AM5/30/24Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ns-3-users
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/internet-module.h"
#include "ns3/applications-module.h"
#include "ns3/olsr-helper.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/wifi-module.h"
#include "ns3/netanim-module.h"
#include "ns3/ipv4-routing-table-entry.h"
#include "ns3/ipv4-static-routing.h"
#include "ns3/ipv4-list-routing.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/node-container.h"
#include "ns3/application-container.h"
#include "ns3/ipv4-address.h"
#include "ns3/csma-module.h"
#include "ns3/trace-helper.h"
#include "ns3/flow-monitor-module.h"
#include <cassert>
#include <fstream>
#include <iostream>
#include <string>
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("OLSR-Example");
void PrintRoutingTable (Ptr<Node> node)
{
//Print Routing Table
Ipv4StaticRoutingHelper routingHelper;
Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> (&std::cout);
routingHelper.PrintRoutingTableAt (Seconds (10.0), node, routingStream);
//Print IP Address
Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
Ipv4Address address = ipv4->GetAddress (1, 0).GetLocal ();
std::cout << "Node " << node->GetId () << " IP Address: " << address << std::endl;
}
int main(int argc, char* argv[]) {
// Allow the user to override any of the defaults and the above
// DefaultValue::Bind ()s at run-time, via command-line arguments
CommandLine cmd;
cmd.Parse (argc, argv);
NS_LOG_UNCOND("\n-------------------------------------------\n OLSR Simulator Testing\n-------------------------------------------\n");
Time::SetResolution(Time::NS);
LogComponentEnable("OLSR-Example", LOG_LEVEL_INFO);
LogComponentEnable("InternetStackHelper", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
//LogComponentEnable("OnOffApplication", LOG_LEVEL_INFO);
LogComponentEnable("PacketSink", LOG_LEVEL_INFO);
// Create 3 nodes
NodeContainer nodes;
nodes.Create(3);
// Set up mobility with no mobility
MobilityHelper mobility;
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(nodes);
// Set node positions
Ptr<Node> n0 = nodes.Get(0);
Ptr<Node> n1 = nodes.Get(1);
Ptr<Node> n2 = nodes.Get(2);
Ptr<MobilityModel> locN0 = n0->GetObject<MobilityModel>();
Ptr<MobilityModel> locN1 = n1->GetObject<MobilityModel>();
Ptr<MobilityModel> locN2 = n2->GetObject<MobilityModel>();
locN0->SetPosition(Vector(0, 0, 0));
locN1->SetPosition(Vector(100, 0, 0));
locN2->SetPosition(Vector(200, 0, 0));
// setting up wifi phy and channel using helpers
WifiHelper wifi;
wifi.SetStandard (WIFI_STANDARD_80211b);
YansWifiPhyHelper wifiPhy;
YansWifiChannelHelper wifiChannel;
wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
wifiChannel.AddPropagationLoss ("ns3::RangePropagationLossModel","MaxRange",DoubleValue(110.0));
wifiPhy.SetChannel (wifiChannel.Create ());
// Add a mac and disable rate control
WifiMacHelper wifiMac;
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode",StringValue ("DsssRate11Mbps"),
"ControlMode",StringValue ("DsssRate1Mbps"));
wifiMac.SetType ("ns3::AdhocWifiMac");
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
// Install internet stack with OLSR
Ipv4ListRoutingHelper list;
InternetStackHelper internet;
OlsrHelper olsr;
list.Add (olsr,100);
internet.SetRoutingHelper(list); // has effect on the internet stack installed
internet.Install(nodes);
NS_LOG_INFO("Assigning IP Address");
Ipv4AddressHelper ipv4;
ipv4.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces;
interfaces = ipv4.Assign(devices);
// UdpEchoServerHelper echoServer(9);
// ApplicationContainer serverApps = echoServer.Install(nodes.Get(2));
// serverApps.Start(Seconds(1.0));
// serverApps.Stop(Seconds(30.0));
// UdpEchoClientHelper echoClient(interfaces.GetAddress(2), 9);
// echoClient.SetAttribute("MaxPackets", UintegerValue(10));
// echoClient.SetAttribute("Interval", TimeValue(Seconds(0.002336)));
// echoClient.SetAttribute("PacketSize", UintegerValue(1460));
// ApplicationContainer clientApps = echoClient.Install(nodes.Get(0));
// clientApps.Start(Seconds(1.0));
// clientApps.Stop(Seconds(30.0));
// // Populate routing tables
// Ipv4GlobalRoutingHelper::PopulateRoutingTables();
// Install CBR application
// uint16_t port = 9;
// OnOffHelper onoff ("ns3::UdpSocketFactory", InetSocketAddress (interfaces.GetAddress (2), port));
// onoff.SetConstantRate (DataRate ("1Mbps"));
// ApplicationContainer apps = onoff.Install (nodes.Get (0));
// apps.Start (Seconds (1.0));
// apps.Stop (Seconds (30.0));
// PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
// apps = sink.Install (nodes.Get (2));
// apps.Start (Seconds (1.0));
// apps.Stop (Seconds (30.0));
NS_LOG_INFO("Create Applications.");
uint16_t port = 9; // Discard port (RFC 863)
OnOffHelper onoff1("ns3::UdpSocketFactory", InetSocketAddress(interfaces.GetAddress(0), port));
onoff1.SetConstantRate(DataRate("448kb/s"));
ApplicationContainer onOffApp1 = onoff1.Install(nodes.Get(2));
onOffApp1.Start(Seconds(1.0));
onOffApp1.Stop(Seconds(30.0));
// // Create a similar flow from n3 to n1, starting at time 1.1 seconds
// OnOffHelper onoff2("ns3::UdpSocketFactory", InetSocketAddress(nodes.GetAddress(0), port));
// onoff2.SetConstantRate(DataRate("448kb/s"));
// ApplicationContainer onOffApp2 = onoff2.Install(nodes.Get(3));
// onOffApp2.Start(Seconds(1.0));
// onOffApp2.Stop(Seconds(30.0));
// // Create packet sinks to receive these packets
// PacketSinkHelper sink("ns3::UdpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), port));
// NodeContainer sinks = NodeContainer(nodes.Get(4), nodes.Get(1));
// ApplicationContainer sinkApps = sink.Install(sinks);
// sinkApps.Start(Seconds(1.0));
// sinkApps.Stop(Seconds(30.0));
NS_LOG_INFO ("Configure Tracing.");
// Results and Metrics
std::string tracePath = "scratch/OLSR";
AsciiTraceHelper ascii;
wifiPhy.EnableAsciiAll (ascii.CreateFileStream (tracePath + "/OLSR-Example-trace.tr"));
wifiPhy.EnablePcapAll (tracePath + "/OLSR-Example-pcap");
AnimationInterface anim ( tracePath + "/OLSR-Example-animation.xml");
Simulator::Schedule(Seconds(30.0), &PrintRoutingTable, nodes.Get(0));
Simulator::Schedule(Seconds(30.0), &PrintRoutingTable, nodes.Get(1));
Simulator::Schedule(Seconds(30.0), &PrintRoutingTable, nodes.Get(2));
NS_LOG_INFO ("Run Simulation.");
// Run simulation
Simulator::Stop(Seconds(50.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}
I am trying many ways to create application , still i am not create a application please help me