NS3之一个nodes里面各node次序问题

NS3之一个nodes里面各node次序问题

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

NodeContainer p2pNodes;//完整代码见文章下方
p2pNodes.Create (2);

NodeContainer csmaNodes;
csmaNodes.Add (p2pNodes.Get (1));
csmaNodes.Create (nCsma);//nCsma=3

通过上述语句的前两条语句:
首先创建了含有2个节点的p2pNodes,即n0、n1.他们的序号分别时0、1
p2pNodes.Get (0)的结果就是n0
p2pNodes.Get (1)的结果就是n1
当然如果一个网络拓扑结构只有n0、n1两个node时,那么n0、n1的序号互换也是可以的

然后通过后面三条语句:
语句csmaNodes.Add (p2pNodes.Get (1));执行后,会把p2pNodes中的n1包含到csmaNodes中,此时n1的序号是0。
语句csmaNodes.Create (nCsma);执行后,又创建了3个node:n2、n3、n4。序号分别是:1\2\3(序号0已经被占用了)
csmaNodes.Get (0)就是n1
csmaNodes.Get (1)就是n2
csmaNodes.Get (2)就是n3
csmaNodes.Get (3)就是n4

如果
NodeContainer csmaNodes;
csmaNodes.Add (p2pNodes.Get (1));
csmaNodes.Create (nCsma);//nCsma=3
改为:
NodeContainer csmaNodes;
csmaNodes.Create (nCsma);//nCsma=3
csmaNodes.Add (p2pNodes.Get (1));
那么结果n2、n3、n4的序号就变成了0\1\2.而n1就成了3

完整代码如下

 second.cc Code

 


 

测试代码如下:(待更新)

  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 
 17 #include "ns3/core-module.h"
 18 #include "ns3/network-module.h"
 19 #include "ns3/csma-module.h"
 20 #include "ns3/internet-module.h"
 21 #include "ns3/point-to-point-module.h"
 22 #include "ns3/applications-module.h"
 23 #include "ns3/ipv4-global-routing-helper.h"
 24 
 25 // Default Network Topology
 26 //
 27 //       10.1.1.0
 28 // n0 -------------- n1   n2   n3   n4
 29 //    point-to-point  |    |    |    |
 30 //                    ================
 31 //                      LAN 10.1.2.0
 32 
 33 
 34 using namespace ns3;
 35 
 36 NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");
 37 
 38 int 
 39 main (int argc, char *argv[])
 40 {
 41   bool verbose = true;
 42   uint32_t nCsma = 3;
 43 
 44   CommandLine cmd;
 45   cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
 46   cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
 47 
 48   cmd.Parse (argc,argv);
 49 
 50   if (verbose)
 51     {
 52       LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
 53       LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
 54     }
 55 
 56   nCsma = nCsma == 0 ? 1 : nCsma;
 57 
 58   NodeContainer p2pNodes;
 59   p2pNodes.Create (2);
 60 
 61   NodeContainer csmaNodes;
 62   csmaNodes.Add (p2pNodes.Get (1));
 63   csmaNodes.Create (nCsma);
 64 
 65   PointToPointHelper pointToPoint;
 66   pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
 67   pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
 68 
 69   NetDeviceContainer p2pDevices;
 70   p2pDevices = pointToPoint.Install (p2pNodes);
 71 
 72   CsmaHelper csma;
 73   csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
 74   csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
 75 
 76   NetDeviceContainer csmaDevices;
 77   csmaDevices = csma.Install (csmaNodes);
 78 
 79   InternetStackHelper stack;
 80   stack.Install (p2pNodes.Get (0));
 81   stack.Install (csmaNodes);
 82 
 83   Ipv4AddressHelper address;
 84   address.SetBase ("10.1.1.0", "255.255.255.0");
 85   Ipv4InterfaceContainer p2pInterfaces;
 86   p2pInterfaces = address.Assign (p2pDevices);
 87 
 88   address.SetBase ("10.1.2.0", "255.255.255.0");
 89   Ipv4InterfaceContainer csmaInterfaces;
 90   csmaInterfaces = address.Assign (csmaDevices);
 91 
 92   UdpEchoServerHelper echoServer (9);
 93 
 94   ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));
 95   serverApps.Start (Seconds (1.0));
 96   serverApps.Stop (Seconds (10.0));
 97 
 98   UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);
 99   echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
100   echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.)));
101   echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
102 
103   ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));
104   clientApps.Start (Seconds (2.0));
105   clientApps.Stop (Seconds (10.0));
106 
107   Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
108 
109   pointToPoint.EnablePcapAll ("second");
110   csma.EnablePcap ("second", csmaDevices.Get (1), true);
111 
112   Simulator::Run ();
113   Simulator::Destroy ();
114   return 0;
115 }
second_change.cc Code

测试结果:

lc@lc-Inspiron:~/workspace/ns-allinone-3.22/ns-3.22$ ./waf
Waf: Entering directory `/home/lc/workspace/ns-allinone-3.22/ns-3.22/build'
[ 861/2427] cxx: scratch/second_change.cc -> build/scratch/second_change.cc.1.o
[2392/2427] cxxprogram: build/scratch/second_change.cc.1.o -> build/scratch/second_change
Waf: Leaving directory `/home/lc/workspace/ns-allinone-3.22/ns-3.22/build'
'build' finished successfully (4.504s)

Modules built:
antenna                   aodv                      applications             
bridge                    buildings                 config-store             
core                      csma                      csma-layout              
dsdv                      dsr                       energy                   
fd-net-device             flow-monitor              internet                 
lr-wpan                   lte                       mesh                     
mobility                  mpi                       netanim (no Python)      
network                   nix-vector-routing        olsr                     
point-to-point            point-to-point-layout     propagation              
sixlowpan                 spectrum                  stats                    
tap-bridge                test (no Python)          topology-read            
uan                       virtual-net-device        visualizer               
wave                      wifi                      wimax                    

Modules not built (see ns-3 tutorial for explanation):
brite                     click                     openflow                 

lc@lc-Inspiron:~/workspace/ns-allinone-3.22/ns-3.22$ ./waf --run scratch/second_change
Waf: Entering directory `/home/lc/workspace/ns-allinone-3.22/ns-3.22/build'
Waf: Leaving directory `/home/lc/workspace/ns-allinone-3.22/ns-3.22/build'
'build' finished successfully (2.683s)
At time 2s client sent 1024 bytes to 10.1.2.4 port 9
At time 2.00369s server received 1024 bytes from 10.1.1.1 port 49153
At time 2.00369s server sent 1024 bytes to 10.1.1.1 port 49153
At time 2.00737s client received 1024 bytes from 10.1.1.2 port 9
lc@lc-Inspiron:~/workspace/ns-allinone-3.22/ns-3.22$ ./waf --run scratch/second_change --vis
View Code

有动画演示可以看出两者的序号的不同

 

posted @ 2015-04-05 20:22  LewisLiu  阅读(419)  评论(0)    收藏  举报