编写一个程序,实现下图中的网络拓扑,支持PC0与Tablet PC1之间的互连
源代码
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/csma-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("RipRouting");
int main(int argc, char** argv)
{
CommandLine cmd;
cmd.Parse(argc, argv);
//LogComponentEnable("Rip", LOG_LEVEL_ALL);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
// 创建节点
CsmaHelper csma;
csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NodeContainer pcNodes;
pcNodes.Create(3);
Names::Add("swich1", pcNodes.Get(0));
Names::Add("PC3", pcNodes.Get(1));
Names::Add("Wireless Router0", pcNodes.Get(2));
NodeContainer routerNodes;
routerNodes.Create(3);
std::string name[] = {"R1", "R2", "R3",};
for (uint32_t i = 0; i < routerNodes.GetN(); i ++)
Names::Add(name[i], routerNodes.Get(i));
// 创建拓扑
NetDeviceContainer ndc[6];
NodeContainer net1(pcNodes.Get(0), routerNodes.Get(0)); //swich1 and R1
ndc[0] = csma.Install(net1);
NodeContainer net2(routerNodes.Get(0), routerNodes.Get(1)); //R1 and R2
ndc[1] = csma.Install(net2);
NodeContainer net3(routerNodes.Get(0), routerNodes.Get(2)); //R1 and R3
ndc[2] = csma.Install(net3);
NodeContainer net4(routerNodes.Get(1), pcNodes.Get(1)); //R2 and PC3
ndc[3] = pointToPoint.Install(net4);
NodeContainer net5(routerNodes.Get(1), routerNodes.Get(2)); //R2 and R3
ndc[4] = csma.Install(net5);
NodeContainer net6(routerNodes.Get(2), pcNodes.Get(2)); //R3 and Wireless Router0
ndc[5] = csma.Install(net6);
NodeContainer hub;
hub.Add (pcNodes.Get(0));
NodeContainer spokes;
spokes.Create(3);
Names::Add("PC0", spokes.Get(0));
Names::Add("PC1", spokes.Get(1));
Names::Add("PC2", spokes.Get(2));
NetDeviceContainer star[3];
NodeContainer snet1(pcNodes.Get(0), spokes.Get(0)); //swich1 and PC0
star[0] = csma.Install(snet1);
NodeContainer snet2(pcNodes.Get(0), spokes.Get(1)); //swich1 and PC1
star[1] = csma.Install(snet2);
NodeContainer snet3(pcNodes.Get(0), spokes.Get(2)); //swich1 and PC2
star[2] = csma.Install(snet3);
NodeContainer wifiStaNodes;
wifiStaNodes.Create (2);
Names::Add("Tablet PC0", wifiStaNodes.Get(0));
Names::Add("Tablet PC1", wifiStaNodes.Get(1));
NodeContainer wifiApNode = pcNodes.Get (2);
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
phy.SetChannel (channel.Create ());
WifiHelper wifi;
wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
WifiMacHelper mac;
Ssid ssid = Ssid ("ns-3-ssid");
mac.SetType ("ns3::StaWifiMac", "Ssid", SsidValue (ssid), "ActiveProbing", BooleanValue (false));
NetDeviceContainer staDevices;
staDevices = wifi.Install (phy, mac, wifiStaNodes);
mac.SetType ("ns3::ApWifiMac",
"Ssid", SsidValue (ssid));
NetDeviceContainer apDevices;
apDevices = wifi.Install (phy, mac, wifiApNode);
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (5.0),
"DeltaY", DoubleValue (10.0),
"GridWidth", UintegerValue (3),
"LayoutType", StringValue ("RowFirst"));
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
mobility.Install (wifiStaNodes);
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (wifiApNode);
//InternetStack
RipHelper ripRouting;
ripRouting.ExcludeInterface(routerNodes.Get(0), 1); //排除swich1与R1相连的接口
ripRouting.ExcludeInterface(routerNodes.Get(1), 2); //排除PC3与R2相连的接口
ripRouting.ExcludeInterface(routerNodes.Get(2), 3); //排除Wireless与R3相连的接口
Ipv4ListRoutingHelper listRH;
Ipv4GlobalRoutingHelper globalRouting;
listRH.Add(ripRouting, 20);
listRH.Add(globalRouting, 10);
InternetStackHelper internet;
internet.SetRoutingHelper(listRH); //设置Routers的RIP接口
internet.Install(routerNodes);
InternetStackHelper internetNodes;
internetNodes.Install(pcNodes);
internetNodes.Install(spokes);
internetNodes.Install (wifiStaNodes);
//IP
char const *IPNet[] = {"10.0.0.0", "10.0.1.0", "10.0.2.0", "10.0.3.0", "10.0.4.0", "10.0.5.0"};
Ipv4AddressHelper ipv4;
Ipv4InterfaceContainer iic[6];
for (uint32_t i = 0; i < 6; i ++)
{
ipv4.SetBase(Ipv4Address(IPNet[i]), Ipv4Mask("255.255.255.0"));
iic[i] = ipv4.Assign(ndc[i]);
}
//为PC机添加缺少路由
Ptr<Ipv4StaticRouting> staticRouting;
staticRouting = Ipv4RoutingHelper::GetRouting <Ipv4StaticRouting>(pcNodes.Get(0)->GetObject<Ipv4>()->GetRoutingProtocol());
staticRouting->SetDefaultRoute("10.0.0.2", 1);
staticRouting = Ipv4RoutingHelper::GetRouting <Ipv4StaticRouting>(pcNodes.Get(1)->GetObject<Ipv4>()->GetRoutingProtocol());
staticRouting->SetDefaultRoute("10.0.3.2", 1);
staticRouting = Ipv4RoutingHelper::GetRouting <Ipv4StaticRouting>(pcNodes.Get(2)->GetObject<Ipv4>()->GetRoutingProtocol());
staticRouting->SetDefaultRoute("10.0.5.1", 1);
Ipv4AddressHelper address;
address.SetBase ("10.1.0.0", "255.255.255.0");
Ipv4InterfaceContainer spokesInterfaces;
spokesInterfaces = address.Assign (star[0]);
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer spokesInterfaces1;
spokesInterfaces1 = address.Assign (star[1]);
address.SetBase ("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer spokesInterfaces2;
spokesInterfaces2= address.Assign (star[2]);
address.SetBase ("10.1.3.0", "255.255.255.0");
address.Assign (staDevices);
address.Assign (apDevices);
// 打印路由
RipHelper routingHelper;
Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>(&std::cout);
routingHelper.PrintRoutingTableAt(Seconds(10.0), routerNodes.Get(0), routingStream);
routingHelper.PrintRoutingTableAt(Seconds(10.0), routerNodes.Get(1), routingStream);
//
uint16_t port = 9;
UdpEchoServerHelper echoServer (port);
ApplicationContainer serverApp = echoServer.Install (spokes.Get(0));
Time startTime("1s");
Time stopTime("100s");
serverApp.Start (startTime);
serverApp.Stop (stopTime);
UdpEchoClientHelper echoClient (spokesInterfaces.GetAddress (1), port);
echoClient.SetAttribute ("MaxPackets", UintegerValue (20));
echoClient.SetAttribute ("Interval", StringValue ("1s"));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (wifiStaNodes.Get (0));
Time beginTime("10s");
clientApps.Start (beginTime);
clientApps.Stop (stopTime);
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Simulator::Run();
Simulator::Destroy();
}
运行结果