Skip to content

Instantly share code, notes, and snippets.

@Anwar05108
Created February 12, 2023 13:01
Show Gist options
  • Save Anwar05108/245692755ab26b7762ddbac8f7b2a67d to your computer and use it in GitHub Desktop.
Save Anwar05108/245692755ab26b7762ddbac8f7b2a67d to your computer and use it in GitHub Desktop.
# simulator
set ns [new Simulator]
# ======================================================================
# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(ant) Antenna/OmniAntenna ;# Antenna type
set val(ll) LL ;# Link layer type
set val(ifq) Queue/DropTail/PriQueue ;# Interface queue type
set val(ifqlen) 50 ;# max packet in ifq
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(rp) DSDV ;# ad-hoc routing protocol
set val(nn) [lindex $argv 0] ;# number of mobilenodes
set val(as) [lindex $argv 1]
# =======================================================================
# trace file
set trace_file [open trace.tr w]
$ns trace-all $trace_file
# nam file
set nam_file [open animation.nam w]
$ns namtrace-all-wireless $nam_file $val(as) $val(as)
# topology: to keep track of node movements
set topo [new Topography]
$topo load_flatgrid $val(as) $val(as) ;# as x as area
# general operation director for mobilenodes
create-god $val(nn)
# node configs
# ======================================================================
# $ns node-config -addressingType flat or hierarchical or expanded
# -adhocRouting DSDV or DSR or TORA
# -llType LL
# -macType Mac/802_11
# -propType "Propagation/TwoRayGround"
# -ifqType "Queue/DropTail/PriQueue"
# -ifqLen 50
# -phyType "Phy/WirelessPhy"
# -antType "Antenna/OmniAntenna"
# -channelType "Channel/WirelessChannel"
# -topoInstance $topo
# -energyModel "EnergyModel"
# -initialEnergy (in Joules)
# -rxPower (in W)
# -txPower (in W)
# -agentTrace ON or OFF
# -routerTrace ON or OFF
# -macTrace ON or OFF
# -movementTrace ON or OFF
# ======================================================================
$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) \
-topoInstance $topo \
-channelType $val(chan) \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace OFF
# create nodes
for {set i 0} {$i < $val(nn) } {incr i} {
set node($i) [$ns node]
$node($i) random-motion 0 ;# disable random motion
$node($i) set X_ [expr (($i % 5 )* 100) + 100]
$node($i) set Y_ [expr (($i / 5 )* 100) + 100]
$node($i) set Z_ 0
$ns initial_node_pos $node($i) 20
}
for {set i 0} {$i < $val(nn)} {incr i} {
$ns at [expr int(20 * rand()) + 10] "$node($i) setdest [expr int(10000 * rand()) % $val(as) + 0.5] [expr int(10000 * rand()) % $val(as) + 0.5] [expr int(100 * rand()) % 5 + 1]"
}
# Traffic
set val(nf) [lindex $argv 2] ;# number of flows
set sink [expr int( rand() * 1000 ) % $val(nn) ]
puts "Sink is $sink"
for {set i 0} {$i < $val(nf)} {incr i} {
while {1 == 1} {
set src [expr int($val(nn) * rand()) % $val(nn)]
# puts "set in while loop src $src"
if {$src != $sink} {
break
}
}
# set src $i
# puts "set in for loop src $i"
# set sink [expr 19 - $i]
# Traffic config
# create agent
set tcp [new Agent/TCP/Reno]
set tcp_sink [new Agent/TCPSink]
# attach to nodes
$ns attach-agent $node($src) $tcp
$ns attach-agent $node($sink) $tcp_sink
# connect agents
$ns connect $tcp $tcp_sink
$tcp set fid_ $i
# Traffic generator
set ftp [new Application/FTP]
# attach to agent
$ftp attach-agent $tcp
# start traffic generation
$ns at 1.0 "$ftp start"
}
# End Simulation
# Stop nodes
for {set i 0} {$i < $val(nn)} {incr i} {
$ns at 50.0 "$node($i) reset"
}
# call final function
proc finish {} {
global ns trace_file nam_file
$ns flush-trace
close $trace_file
close $nam_file
}
proc halt_simulation {} {
global ns
puts "Simulation ending"
$ns halt
}
$ns at 50.0001 "finish"
$ns at 50.0002 "halt_simulation"
# Run simulation
puts "Simulation starting"
$ns run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment