02-05-2012, 05:12 PM
Simple Simulation Example
nsbyExamples.pdf (Size: 437.63 KB / Downloads: 242)
This section shows a simple NS simulation script and explains what each line does. Example 3 is an
OTcl script that creates the simple network configuration and runs the simulation scenario in Figure 4.
To run this simulation, download "ns-simple.tcl" and type "ns ns-simple.tcl" at your shell prompt.
The following is the explanation of the script above. In general, an NS script starts with making a
Simulator object instance.
set ns [new Simulator]: generates an NS simulator object instance, and assigns it to variable ns
(italics is used for variables and values in this section). What this line does is the following:
m Initialize the packet format (ignore this for now)
m Create a scheduler (default is calendar scheduler)
Select the default address format (ignore this for now)
m
The "Simulator" object has member functions that do the following:
m Create compound objects such as nodes and links (described later)
m Connect network component objects created (ex. attach-agent)
m Set network component parameters (mostly for compound objects)
m Create connections between agents (ex. make connection between a "tcp" and "sink")
m Specify NAM display options
Etc.
Event Scheduler
This section talks about the discrete event schedulers of NS. As described in the Overview section, the
main users of an event scheduler are network components that simulate packet-handling delay or that
need timers. Figure 5 shows each network object using an event scheduler. Note that a network object
that issues an event is the one who handles the event later at scheduled time. Also note that the data path
between network objects is different from the event path. Actually, packets are handed from one network
object to another using send(Packet* p) {target_->recv(p)}; method of the sender and recv(Packet*,
Handler* h = 0) method of the receiver.