#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/mobility-module.h"
#include "ns3/stats-module.h"
#include "ns3/wifi-module.h"
#include "ns3/internet-module.h"
#include "ns3/olsr-module.h"
#include "ns3/netanim-module.h"
#include <iostream>
NS_LOG_COMPONENT_DEFINE ("MANET");
using namespace ns3;
int nodeSpacing = 100;
int cols = 5;
int numNodes = 25;
int sourceNode = 0;
int destinationNode = 24;
int packetRate = 20;
int packetSize = 500;
bool enablePcap = true;
bool showSimTime = true;
int duration = 600;
int seed = 1;
int run = 1;
void PrintSeconds(void) {
std::cerr << Simulator::Now() << std::endl;
Simulator::Schedule(Seconds(1), &PrintSeconds);
}
void RouteChange(std::string source, uint32_t size) {
std::cout << "Routechange at " << source << ", new size: " << size << std::endl;
}
int main (int argc, char *argv[])
{
LogComponentEnable ("MANET", LOG_LEVEL_INFO);
CommandLine cmd;
cmd.AddValue ("cols", "Columns of nodes", cols);
cmd.AddValue ("numnodes", "Number of nodes", numNodes);
cmd.AddValue ("spacing", "Spacing between neighbouring nodes", nodeSpacing);
cmd.AddValue ("duration", "Duration of simulation", duration);
cmd.AddValue ("seed", "Random seed for simulation", seed);
cmd.AddValue ("run", "Simulation run", run);
cmd.AddValue ("packetrate", "Packets transmitted per second", packetRate);
cmd.AddValue ("packetsize", "Packet size", packetSize);
cmd.AddValue ("sourcenode", "Number of source node", sourceNode);
cmd.AddValue ("destinationnode", "Number of destination node", destinationNode);
cmd.AddValue ("showtime", "Whether or not to show simulation as simulation progresses (default = true)", showSimTime);
cmd.Parse (argc,argv);
int rows = ((int) numNodes / cols) + 1;
Config::SetDefault("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
SeedManager::SetSeed (seed);
SeedManager::SetRun(run);
NodeContainer c;
c.Create (numNodes);
WifiHelper wifi = WifiHelper::Default ();
wifi.SetStandard (WIFI_PHY_STANDARD_80211g);
wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
wifiMac.SetType ("ns3::AdhocWifiMac");
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = wifiPhy;
phy.SetChannel (wifiChannel.Create ());
NetDeviceContainer devices = wifi.Install (phy, wifiMac, c);
if(enablePcap)
wifiPhy.EnablePcap ("MANET", devices);
ns3::OlsrHelper olsr;
InternetStackHelper internet;
internet.SetRoutingHelper(olsr);
internet.Install (c);
Ipv4AddressHelper address;
address.SetBase ("10.0.0.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign (devices);
UdpServerHelper server (4000);
ApplicationContainer apps = server.Install (c.Get(destinationNode));
apps.Start (Seconds (1));
apps.Stop (Seconds (duration - 1));
UdpClientHelper client (interfaces.GetAddress (destinationNode), 4000);
client.SetAttribute ("MaxPackets", UintegerValue (100000000));
client.SetAttribute ("Interval", TimeValue (Seconds(1 / ((double) packetRate))));
client.SetAttribute ("PacketSize", UintegerValue (packetSize));
apps = client.Install (c.Get (sourceNode));
apps.Start (Seconds (1));
apps.Stop (Seconds (duration - 1));
MobilityHelper mobility;
mobility.SetPositionAllocator (
"ns3::GridPositionAllocator",
"MinX", DoubleValue (1.0),
"MinY", DoubleValue (1.0),
"DeltaX", DoubleValue (nodeSpacing),
"DeltaY", DoubleValue (nodeSpacing),
"GridWidth", UintegerValue (cols));
mobility.SetMobilityModel (
"ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue (Rectangle (0,(cols * nodeSpacing) + 1, 0,(rows * nodeSpacing) + 1)),
"Speed", StringValue("ns3::UniformRandomVariable[Min=5.0,Max=10.0]"),
"Distance", DoubleValue(30));
mobility.Install (c);
if(showSimTime)
Simulator::Schedule(Seconds(1), &PrintSeconds);
Simulator::Stop(Seconds(duration));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通