انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

NS-2 Initialization and Termination

الكلية كلية تكنولوجيا المعلومات     القسم قسم شبكات المعلومات     المرحلة 2
أستاذ المادة علاء عبد الحسين مهدي كريم       16/12/2015 22:15:30
NS-2 Initialization and Termination 13 - December – 2015
NS-2 Initialization and Termination:
The network simulator ns-2 starts with the command "set ns [new
Simulator", which is the first line in the Tcl script. This line declares a new
variable "ns" using the "set" command, you can call this variable as you wish, but
in general people declares it as "ns", because it is an instance of the
"Simulator" class, so it is an object. The code "[new Simulator]" is indeed
the instantiation of the class Simulator using the reserved word "new". So using
this new variable "ns", we can use all the methods of the class "Simulator",
that we will see later.
In order to have output files with data on the simulation (trace files) or files
used for visualization (nam files), we need to create these files using "open"
command.
1. #Open the Trace file
2. Set tracefile1 [open out.tr w]
3. $ns trace-all $tracefile1
4. #Open the NAM trace file
5. Set namfile [open out.nam w]
6. $ns namtrace-all $namfile
The above code creates a data trace file called "out.tr" and a nam
visualization trace file (for the NAM tool) called "out.nam". Within the Tcl script,
these files are not called explicitly by their names, but instead by pointers that are
declared above and called "tracefile1" and "namfile" respectively.
The first and fourth lines in the previous code are only comments, they are not
simulation commands. Remark that they begin with a "#" symbol.
The second line opens the file "out.tr" to be used for writing, declared
with the letter "w".
NS-2 Initialization and Termination 13 - December – 2015
The third line uses a simulation method called trace-all that has as
parameter the name of the file where the traces will go. With this simulator command,
we will trace all the events in a specific format that we will explain later.
The last line tells the simulator to record all simulation traces in NAM input
format. It also gives the file name that the trace will be written later through the
command "$ns flush-trace" (as we will see in the procedure finish
below). In our case, this will be the file pointed at by the pointer "$namfile".
The termination of the program is done through the use of the "finish" procedure.
? #Define a finish procedure to terminate the
program.
? proc finish {} {
? global ns tracefile1 namfile
? $ns flush-trace
? close $tracefile1
? close $namfile
? exec nam out.nam &
? exit 0
? }
The word "proc" declares a procedure in this case called "finish", and
without arguments. The word "global" is used to tell that we are using variables
declared outside the procedure. The simulator method "flush-trace" will dump
the trace on the respective file. The Tcl command "close" closes the trace and nam
files defined before. The Tcl command "exec" executes the nam program for
visualization.
NS-2 Initialization and Termination 13 - December – 2015
Remark that we pass the real name of the file of traces to nam and not the
pointer namfile, because it is an external command.
The command "exit" ends the application and returns the number 0 as status
to the system. Zero is the default for a clean exit. Other values can be used to say that
this is an exit because something failed.
At the end of the ns program, we should call the procedure "finish" and
specify at which time the termination should occur. For example: the command "$ns
at 125.0 "finish"" will be used to call "finish" at time 125 sec. Indeed,
the "at" method of the simulator allows us to schedule events explicitly.
Finally, the simulation can then begin using the command: "$ns run".
Definition of a Network Links and Nodes:
The way to define a node is: set n0 [$ns node].We created a node
that is pointed by the variable n0. When we shall refer to that node in the script, we
shall thus write $n0.
Once we define several nodes, we can define the links that connect them. An
example of a definition of a link is:
$ns duplex-link $n0 $n2 10Mb 10ms DropTail
Which means that nodes $n0 and $n2 are connected using a bi-directional link that
has 10ms of propagation delay and a capacity of 10Mb/sec for each direction.
To define a directional link instead of a bi-directional one, we should replace
"duplex-link" by "simplex-link".
In ns-2, an output queue of a node is implemented as a part of each link whose
input is that node. The definition of the link then includes the way to handle the
overflow at that queue. In our case, if the buffer capacity of the output queue is
exceeded then the last packet to arrive is dropped (DropTail option). Many
alternative options exist, such as the RED (Random Early Discard)
mechanism, the FQ (Fair Queueing), the DRR (Deficit Round Robin), the SFQ
NS-2 Initialization and Termination 13 - December – 2015
(Stochastic Fair Queuing), and the CBQ (which including a priority and a round-robin
scheduler).
As an example of simple network, consider the one depicted in the figure below. This
network id defined the script given in table of code below.
#Create a simulator object
set ns [new Simulator]
#Open the Trace file
set tracefile1 [open out.tr w]
$ns trace-all $tracefile1
#Open the NAM trace file
set namfile [open out.nam w]
$ns namtrace-all $namfile
#Define a finish procedure
NS-2 Initialization and Termination 13 - December – 2015
proc finish {} {
global ns tracefile1 namfile
$ns flush-trace
close $tracefile1
close $namfile
exec nam out.nam &
exit 0
}
#create six nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
#Create a link between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns simplex-link $n2 $n3 0.3Mb 100ms DropTail
$ns simplex-link $n3 $n2 0.3Mb 100ms DropTail
$ns duplex-link $n3 $n4 0.5Mb 40ms DropTail
$ns duplex-link $n3 $n5 0.5Mb 30ms DropTail
#Call the finish procedure after 5 seconds of
simulation time
$ns at 5.0 "finish"
NS-2 Initialization and Termination 13 - December – 2015
#Run the simulation
$ns run

المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .