#Setup s TCP connection set tcp [new Agent/TCP] $ns attach

AGENT AND APPLICATION
29 - December – 2014
AGENT AND APPLICATION
Having defined the topology (nodes and links), we should now make traffic flow
through them. To that end, we need to define routing (in particular source and
destination), the agents (protocols), and the applications that use them.
In the next example, we may wish to run an FTP application between node $n0 and
$n4. The Internet protocol used by FTP is TCP. We should first define a TCP agent
between the source node $n0, and the destination node $n4 and then we defined the
FTP application that uses them, as we did in table 1.
#Setup s TCP connection
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
#Setup an FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
Table 1: the definition of an FTP application using a TCP Agent
TCP is a dynamic reliable congestion control protocol. It uses acknowledgements
created by the destination to know whether packets are well received. Lost packets are
interpreted as congestion signals. Thus, TCP requires bidirectional links in order for the
acknowledgements to return to the source.
The type of agent appears in the first line (set tcp [new Agent/TCP])
gives a pointer called "tcp" here to the TCP agent, which is an object in NS.
AGENT AND APPLICATION
29 - December – 2014
The command ($ns attach-agent $n0 $tcp) defines the source node
of the TCP connection.
The command (set
sink
[new
Agent/TCPSink]) defines the
behavior of the destination node of TCP and assigns to it a pointer called sink. As we
mentioned before regarding TCP, the destination node has an active role in the protocol
of generating acknowledgements in order to guarantee that all packets arrive at the
destination.
The command ($ns attach-agent $n4 $sink) defines the destination
node.
The command ($ns
connect $tcp $sink) finally makes the TCP
connection between the source and destination nodes.
If we have several flows, we may wish to distinguish them so that we can identify them
with different colors in the visualization part. This is done by the command ($tcp
set fid_ 1) that assigns to the TCP connection a flow identification of 1.
Once the TCP connection is defined, the FTP application is defined over it. This is
done through the last two lines in table 1.
Note that both the TCP agents and the FTP application are given pointer, where we
called the one for the TCP agent "tcp" (but can use any other name else) and the one
for FTP we called "ftp".
NS is a discrete event based simulation. The Tcl script defines when event should
occur. The initializing command (set ns [new Simulator]) creates an event
scheduler, and events are then scheduled using format ( $ns at <time> <event>). The
scheduler is started when running ns through the command ( $ns run).
In our example presented in table 2, we should schedule the beginning and the end of
the FTP application. This can be done through the command ( $ns at 0.5 "$ftp start")
AGENT AND APPLICATION
29 - December – 2014
and ($ns at 124.0 "$ftp stop"). Thus the FTP will be active from 0.5
till 124.0 (all units are in seconds).
A simple script that runs a single TCP connection over six nodes is presented in Table
2.
#Create a simulator object
set ns [new Simulator]
#Define color for data flow (for NAM)
$ns color 1 Blue
#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
proc finish {} {
global ns tracefile1 namfile
$ns flush-trace
close $tracefile1
close $namfile
exec nam out.nam &
exit 0
}
#create six
set n0 [$ns
set n1 [$ns
set n2 [$ns
nodes
node]
node]
node]
AGENT AND APPLICATION
29 - December – 2014
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
#Setup s TCP connection
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
#Setup an FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 0.5 "$ftp start"
$ns at 124.0 "$ftp stop"
#Call the finish procedure after 125 seconds of
simulation time
$ns at 125.0 "finish"
#Run the simulation
$ns run
Table 2: single TCP connection over six nodes