The source of the file is the NS2 simple-wireless.tcl (~NS\TCL\EX\SIMPLE-WIRELESS.TCL): # Defines the options, setting some of the properties that the simulation requires. Set Val (chan) channel/wirelesschannel; # Wireless channel type set Val (prop) Propagation/tworayground; # Infinite signal Transfer model set Val (netif) P file source is NS2 with SIMPLE-WIRELESS.TCL (~NS\TCL\EX\SIMPLE-WIRELESS.TCL):
# define options and set some properties that the simulation needs.
Set Val (chan) channel/wirelesschannel; # Wireless Channel type
Set Val (prop) Propagation/tworayground; # Infinite signal transmission model
Set Val (netif) phy/wirelessphy; # Physical Layer Type
Set Val (Mac) mac/802_11; # mac Layer Type
Set Val (IFQ) queue/droptail/priqueue; # interface Queue type
Set Val (ll) ll; # link Layer Type
Set Val (ant) Antenna/omniantenna; # antenna type
Set Val (Ifqlen) 50; # Queue Length
Set Val (nn) 2 ;# the number of mobile nodes
Set Val (RP) dsdv ;# Routing Protocol
Set Val (x) 500 x dimensions of the ;# topology
Set Val (y) 500; # The Y dimension of the topology
# Main Program
# Initialize Global variables
#
#建立一个Simulator对象的实例并赋值给变量ns_
Set ns_ [New Simulator]
#打开一个名为simple. tr file (if not present, create), set file writable, variable tracefd point to the file
Set TRACEFD [Open simple.tr W]
#调用Simulator类的trace-all method to write the entire simulation process into the simple.tr trace file
$ns _ Trace-all $TRACEFD
Set NAMTRACEFD [Open Simple.nam W]
#在拓扑范围内建立无线nam跟踪
$ns _ namtrace-all-wireless $namtracefd $val (x) $val (y)
#建立一个Topography对象, the object will ensure that the nodes move within the bounds of the topological boundary.
Set topo [New topography]
#设定模拟所采用的场景的长宽尺寸
$topo Load_flatgrid $val (x) $val (y)
#
# Build a God object. God object is mainly used for performance evaluation of routing protocols, it stores the total number of nodes, the shortest path between nodes and other information. The Mac object of the node invokes the God object, so the object is still established even if it is not used.
#
Create-god $val (NN)
#
# Set up mobile nodes with a specified number of [$val (NN)] and connect them to the channel.
# Two nodes are created here: node (0), node (1)
# Configure Nodes
$ns _ node-config-adhocrouting $val (RP) \
-lltype $val (LL) \
-mactype $val (MAC) \
-ifqtype $val (IFQ) \
-ifqlen $val (Ifqlen) \
-anttype $val (ant) \
-proptype $val (prop) \
-phytype $val (netif) \
-channeltype $val (chan) \
-topoinstance $topo \
-agenttrace on \
-routertrace on \
-mactrace off \
-movementtrace off
#建立两个节点, the random motion function of the node is closed, that is, the motion of the node is fully programmed.
For {set I 0} {$i < $val (NN)} {incr i} {
Set Node_ ($i) [$ns _ node]
$node _ ($i) random-motion 0;
}
#
# Specifies the initial x,y coordinates for the mobile node, where the z coordinates are 0
#
$node _ (0) Set X_ 5.0
$node _ (0) Set Y_ 2.0
$node _ (0) Set z_ 0.0
$node _ (1) Set X_ 390.0
$node _ (1) Set Y_ 385.0
$node _ (1) Set z_ 0.0
# There are some simple motions
# Node_ (1) in 50.0s, 15.0m/s velocity to coordinate (25.0,20.0) movement
$ns _ at 50.0 "$node _ (1) setdest 25.0 20.0 15.0"
# node_ (0) in 10.0s, 1.0m/s velocity to coordinate (20.0,18.0) movement
$ns _ at 10.0 "$node _ (0) setdest 20.0 18.0 1.0"
# Node_ (1) in 100.0s, 15.0m/s velocity to coordinate (490.0,480.0) movement
$ns _ at 100.0 "$node _ (1) setdest 490.0 480.0 15.0"
#设置节点间的流量
#创建一个TCP源代理对象tcp
Set TCP [New AGENT/TCP]
#class_是agent类中定义的整型变量, used for sorting, and can then be used for coloring. This program is useless.
$tcp Set Class_ 2
#创建分组的接收代理对象sink
Set sink [New Agent/tcpsink]
#将源代理对象tcp绑定到节点0
$ns _ attach-agent $node _ (0) $tcp
#将接收代理对象sink绑定到节点1
$ns _ Attach-agent $node _ (1) $sink
#在源代理对象tcp和接收代理对象sink之间建立连接
$ns _ Connect $tcp $sink
#在TCP连接上创建一个FTP流量模拟器, because TCP does not generate its own traffic
Set FTP [New APPLICATION/FTP]
$ftp attach-agent $tcp
#在10.0s start ftp traffic simulator
$ns _ at 10.0 "$ftp start"
#
#模拟结束时告知节点
#
For {set I 0} {$i < $val (NN)} {incr i} {
#在模拟结束前 (150.0s) resets all objects in the node
$ns _ at 150.0 "$node _ ($i) reset";
}
Call the stop procedure when #告知Simulator对象在150.0s
$ns _ at 150.0 "Stop"
Output prompt statement and terminate dispatcher #在150.01s
$ns _ at 150.01 "puts \ ns exiting...\"; $ns _ Halt "
#stop过程
Proc Stop {} {
#声明全局变量
Global Ns_ TRACEFD NAMTRACEFD
#清空跟踪的缓冲区
$ns _ Flush-trace
#关闭文件
Close $TRACEFD
Close $NAMTRACEFD
#启动nam
exec Nam Simple.nam &
Exit 0
}
Puts "starting simulation ..."
#启动调度器
$ns _ Run