self-confidence,the source of all the power

导航

NS3的两个例子

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ //Emacs模式行,GPL boilerplate

#include "ns3/core-module.h"       //
#include "ns3/simulator-module.h"
#include "ns3/node-module.h"
#include "ns3/helper-module.h"

// Default Network Topology
//
//       10.1.1.0
// n0 -------------- n1   n2   n3   n4
//    point-to-point  |    |    |    |
//                    ================
//                      LAN 10.1.2.0


using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");

int
main (int argc, char *argv[])
{
bool verbose = true;  //定义变量,用于决定是否开启两个UdpApplication的Logging组件;默认true开启
uint32_t nCsma = 3;   //LAN中另有3个node

CommandLine cmd;
cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); //命令行参数设置是否开启logging

cmd.Parse (argc,argv);

if (verbose)
{
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
}

nCsma = nCsma == 0 ? 1 : nCsma;       //三目运算符还可以这样写。。。。

/********************网络拓扑部分************************/
//创建使用P2P链路链接的2个node
NodeContainer p2pNodes;
p2pNodes.Create (2);

//创建另一个NodeContainer类对象,用于总线(CSMA)网络
NodeContainer csmaNodes;
csmaNodes.Add (p2pNodes.Get (1)); //将之前P2P的NodeContianer的第二个节点(索引1)添加到CSMA的NodeContainer,以获得CSMA device;这个node将会有两个device
csmaNodes.Create (nCsma);         //再创建Bus network上另外的node

//设置传送速率和信道延迟,同first.cc
PointToPointHelper pointToPoint;       //注意使用Helper的固定格式:
//1/helper对象声明及属性设置;
//2/devices对象声明及接收helper对象安装方法的返回列表,安装方法的参数为节点对象!
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

//安装P2P网卡设备到P2P网络节点,同first.cc
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (p2pNodes);

//类似于P2PHelper,CsmaHelper帮助创建和连接CSMA设备及信道
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));   //数据率由channel属性指定,而非Device属性;
//因为CSMA不允许同一信道上有多个不同数据率的设备!
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); //speed-of-light delay???

NetDeviceContainer csmaDevices;
csmaDevices = csma.Install (csmaNodes);

//安装网络协议
InternetStackHelper stack;
stack.Install (p2pNodes.Get (0));     //P2P链路中的第一个节点
stack.Install (csmaNodes);            //P2P链路中的第二个节点包含在csmaNodes中

Ipv4AddressHelper address;                     //两个网段的IP地址类对象
address.SetBase ("10.1.1.0", "255.255.255.0"); //安排P2P网段的地址
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);

address.SetBase ("10.1.2.0", "255.255.255.0"); //安排CSMA网段地址
Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign (csmaDevices);
/********************网络拓扑部分结束*********************/

/**********************应用程序部分*********************/
UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma)); //将Server服务安装在CSMA网段的最后一个节点上,nCsma是可变的,所以不能用3
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);        //同first.cc
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));      //同first.cc
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
/**********************应用程序部分结束*********************/

/****************调用全局路由Helper帮助建立网络路由*******************/
Ipv4GlobalRoutingHelper::PopulateRoutingTables (); //全局路由管理器根据节点产生的链路通告为每个节点建立路由表

/****************开启pcap跟踪*******************/
//pointToPoint.EnablePcapAll ("second");        //开启P2PHelper类对象的pcap;"second"为保存文件的前缀名,两句的名称相同了???
//前缀后的节点号是<!全局节点号!>,不用担心名称相同!
//csma.EnablePcap ("second", csmaDevices.Get (1), true); //开启csmaHelper类对象的pcap
//使用csma网段索引为1的设备(第二个)进行sniff,True开启Promiscuous mode
//NodeContainer类对象的Get方法用于获得容器中给定索引下的节点,返回指向请求节点的指针!
//Node类对象的GetId返回节点的全局ID(即节点列表中的索引号)???????
//注意之前使用的Get是NetDevice类的方法,以下使用的是Node类的方法!!
//NetDevice不用取得ID,可以直接使用(已验证);但Node需要进一步查找ID!!(已验证,不使用GetId无法通过)
//所以后边的两句和这样的两句是等效的(已验证)
//  “csma.EnablePcap ("second", csmaDevices.Get (nCsma), 0);”
//  “csma.EnablePcap ("second", csmaDevices.Get (nCsma-1), 0);”
pointToPoint.EnablePcap ("second", p2pNodes.Get (0)->GetId (), 0);
//最后一项为explicitFilename,默认false,不加也可;若true,将prefix作为文件名
//倒数第二项promiscuous,默认false,此处仅想跟踪一个设备,故设为0(false);当有一个节点和设备的promiscuous模式设为true时,CSMA网段其它节点便不再产生trace文件。
csma.EnablePcap ("second", csmaNodes.Get (nCsma)->GetId (), 0, false);
csma.EnablePcap ("second", csmaNodes.Get (nCsma-1)->GetId (), 0, false);

Simulator::Run ();
Simulator::Destroy ();
return 0;
}

 

2、加入wifi网络

  1. /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */  
  2. /* 
  3.  * This program is free software; you can redistribute it and/or modify 
  4.  * it under the terms of the GNU General Public License version 2 as 
  5.  * published by the Free Software Foundation; 
  6.  * 
  7.  * This program is distributed in the hope that it will be useful, 
  8.  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  9.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  10.  * GNU General Public License for more details. 
  11.  * 
  12.  * You should have received a copy of the GNU General Public License 
  13.  * along with this program; if not, write to the Free Software 
  14.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
  15.  */  
  16. #include "ns3/core-module.h"  
  17. #include "ns3/simulator-module.h"  
  18. #include "ns3/node-module.h"  
  19. #include "ns3/helper-module.h"  
  20. #include "ns3/wifi-module.h"  
  21. #include "ns3/mobility-module.h"  
  22. // Default Network Topology  
  23. //  
  24. //   Wifi 10.1.3.0  
  25. //                 AP     
  26. //  *    *    *    *  
  27. //  |    |    |    |    10.1.1.0  
  28. // n5   n6   n7   n0 -------------- n1   n2   n3   n4  
  29. //                   point-to-point  |    |    |    |  
  30. //                                   ================  
  31. //                                     LAN 10.1.2.0  
  32. using namespace ns3;  
  33. NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample");  
  34. int   
  35. main (int argc, char *argv[])  
  36. {  
  37.   bool verbose = true;  
  38.   uint32_t nCsma = 3;  
  39.   uint32_t nWifi = 3;  
  40.   CommandLine cmd;  
  41.   cmd.AddValue ("nCsma""Number of \"extra\" CSMA nodes/devices", nCsma);  
  42.   cmd.AddValue ("nWifi""Number of wifi STA devices", nWifi);  
  43.   cmd.AddValue ("verbose""Tell echo applications to log if true", verbose);  
  44.   cmd.Parse (argc,argv);  
  45.   if (verbose)  
  46.     {  
  47.       LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);  
  48.       LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);  
  49.     }  
  50.   NodeContainer p2pNodes;  
  51.   p2pNodes.Create (2);  
  52.   PointToPointHelper pointToPoint;  
  53.   pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));  
  54.   pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));  
  55.   NetDeviceContainer p2pDevices;  
  56.   p2pDevices = pointToPoint.Install (p2pNodes);  
  57.   NodeContainer csmaNodes;  
  58.   csmaNodes.Add (p2pNodes.Get (1));  
  59.   csmaNodes.Create (nCsma);  
  60.   CsmaHelper csma;  
  61.   csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));  
  62.   csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));  
  63.   NetDeviceContainer csmaDevices;  
  64.   csmaDevices = csma.Install (csmaNodes);  
  65.   NodeContainer wifiStaNodes;  
  66.   wifiStaNodes.Create (nWifi);  
  67.   NodeContainer wifiApNode = p2pNodes.Get (0);  
  68.   YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();  
  69.   YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();  
  70.   phy.SetChannel (channel.Create ());  
  71.   WifiHelper wifi = WifiHelper::Default ();  
  72.   wifi.SetRemoteStationManager ("ns3::AarfWifiManager");  
  73.   NqosWifiMacHelper mac = NqosWifiMacHelper::Default ();  
  74.     
  75.   Ssid ssid = Ssid ("ns-3-ssid");  
  76.   mac.SetType ("ns3::NqstaWifiMac",   
  77.     "Ssid", SsidValue (ssid),  
  78.     "ActiveProbing", BooleanValue (false));  
  79.   NetDeviceContainer staDevices;  
  80.   staDevices = wifi.Install (phy, mac, wifiStaNodes);  
  81.   mac.SetType ("ns3::NqapWifiMac",   
  82.     "Ssid", SsidValue (ssid));  
  83.   NetDeviceContainer apDevices;  
  84.   apDevices = wifi.Install (phy, mac, wifiApNode);  
  85.   MobilityHelper mobility;  
  86.   mobility.SetPositionAllocator ("ns3::GridPositionAllocator",  
  87.     "MinX", DoubleValue (0.0),  
  88.     "MinY", DoubleValue (0.0),  
  89.     "DeltaX", DoubleValue (5.0),  
  90.     "DeltaY", DoubleValue (10.0),  
  91.     "GridWidth", UintegerValue (3),  
  92.     "LayoutType", StringValue ("RowFirst"));  
  93.   mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",  
  94.     "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));  
  95.   mobility.Install (wifiStaNodes);  
  96.   mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");  
  97.   mobility.Install (wifiApNode);  
  98.   InternetStackHelper stack;  
  99.   stack.Install (csmaNodes);  
  100.   stack.Install (wifiApNode);  
  101.   stack.Install (wifiStaNodes);  
  102.   Ipv4AddressHelper address;  
  103.   address.SetBase ("10.1.1.0""255.255.255.0");  
  104.   Ipv4InterfaceContainer p2pInterfaces;  
  105.   p2pInterfaces = address.Assign (p2pDevices);  
  106.   address.SetBase ("10.1.2.0""255.255.255.0");  
  107.   Ipv4InterfaceContainer csmaInterfaces;  
  108.   csmaInterfaces = address.Assign (csmaDevices);  
  109.   address.SetBase ("10.1.3.0""255.255.255.0");  
  110.   address.Assign (staDevices);  
  111.   address.Assign (apDevices);  
  112.   UdpEchoServerHelper echoServer (9);  
  113.   ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));  
  114.   serverApps.Start (Seconds (1.0));  
  115.   serverApps.Stop (Seconds (10.0));  
  116.   UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);  
  117.   echoClient.SetAttribute ("MaxPackets", UintegerValue (1));  
  118.   echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.)));  
  119.   echoClient.SetAttribute ("PacketSize", UintegerValue (1024));  
  120.   ApplicationContainer clientApps =   
  121.     echoClient.Install (wifiStaNodes.Get (nWifi - 1));  
  122.   clientApps.Start (Seconds (2.0));  
  123.   clientApps.Stop (Seconds (10.0));  
  124.   Ipv4GlobalRoutingHelper::PopulateRoutingTables ();  
  125.   Simulator::Stop (Seconds (10.0));  
  126.   pointToPoint.EnablePcapAll ("third");  
  127.   phy.EnablePcap ("third", apDevices.Get (0));  
  128.   csma.EnablePcap ("third", csmaDevices.Get (0), true);  
  129.   Simulator::Run ();  
  130.   Simulator::Destroy ();  
  131.   return 0;  
  132. }  

 

posted on 2011-02-22 16:11  漩涡鸣人  阅读(4303)  评论(0编辑  收藏  举报