# ===========================================================
# NS2 All-in-One Simulation Script
# Covers: Topologies, MAC, Routing, TCP, Application Traffic
# ===========================================================
# Create a simulator object
set ns [new Simulator]
# Open trace files
set tracefile [open allinone.tr w]
set namfile [open allinone.nam w]
$ns trace-all $tracefile
$ns namtrace-all $namfile
# Define finish procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
puts "Simulation completed. Run NAM or parse trace file for analysis."
exec nam allinone.nam &
exit 0
}
# -----------------------------
# 1. Network Topology Creation
# -----------------------------
# We'll create a hybrid topology: some wired, some wireless
# Create nodes
for {set i 0} {$i < 6} {incr i} {
set n($i) [$ns node]
}
# Point-to-point links
$ns duplex-link $n(0) $n(1) 2Mb 10ms DropTail
$ns duplex-link $n(1) $n(2) 2Mb 10ms DropTail
# Star topology (n3 central)
for {set i 4} {$i <= 5} {incr i} {
$ns duplex-link $n(3) $n($i) 1Mb 20ms DropTail
}
# Connect n2 and n3 for mesh behavior
$ns duplex-link $n(2) $n(3) 1.5Mb 15ms DropTail
# -----------------------------
# 2. MAC Protocol Configuration
# -----------------------------
# For demonstration, we'll assign MAC types for wireless simulation part
Mac/802_11 set dataRate_ 2Mb
Phy/WirelessPhy set freq_ 2.4e9
# -----------------------------
# 3. Routing Protocol Measurement
# -----------------------------
# We'll pick AODV for this simulation
set val(rp) AODV
$ns node-config -adhocRouting $val(rp) \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-topoInstance [new Topography] \
-agentTrace ON \
-routerTrace ON \
-macTrace ON
# -----------------------------
# 4. TCP/IP Analysis
# -----------------------------
# TCP connection between n0 and n5
set tcp1 [new Agent/TCP/Reno]
set sink1 [new Agent/TCPSink]
$ns attach-agent $n(0) $tcp1
$ns attach-agent $n(5) $sink1
$ns connect $tcp1 $sink1
# FTP over TCP
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns at 1.0 "$ftp1 start"
$ns at 5.0 "$ftp1 stop"
# -----------------------------
# 5. Application Protocol Analysis
# -----------------------------
# CBR over UDP between n1 and n4
set udp1 [new Agent/UDP]
set null1 [new Agent/Null]
$ns attach-agent $n(1) $udp1
$ns attach-agent $n(4) $null1
$ns connect $udp1 $null1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 512
$cbr1 set interval_ 0.05
$cbr1 attach-agent $udp1
$ns at 2.0 "$cbr1 start"
$ns at 6.0 "$cbr1 stop"
# -----------------------------
# Run Simulation
# -----------------------------
$ns at 10.0 "finish"
$ns run