Last active
August 29, 2015 13:58
-
-
Save bkamapantula/9982330 to your computer and use it in GitHub Desktop.
Example that shows flooding routing using CBR traffic and UDP agent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# doesn't work as expected | |
# Create a simulator | |
set ns [new Simulator] | |
set MESSAGE_PORT 42 | |
# reads the first nine command line arguments | |
set loss_rate [lindex $argv 0] | |
set sink [lindex $argv 1] | |
set input_file [lindex $argv 2] | |
set num_nodes [lindex $argv 3] | |
set linktype [lindex $argv 4] | |
set traceFile [lindex $argv 5] | |
set packetSize [lindex $argv 6] | |
set bw [lindex $argv 7] | |
set transTime [lindex $argv 8] | |
# prints the same | |
puts "Loss_rate: $loss_rate, sink: $sink, input file: $input_file, nodes: $num_nodes, link: $linktype, trace file: $traceFile, packet size: $packetSize" | |
puts "Bandwidth: $bw, Tranmission time: $transTime, Queue limit [lindex $argv 9]" | |
# all packet trace: | |
set mytrace [open $traceFile w] | |
$ns trace-all $mytrace | |
#create a NAM trace file | |
set fname [file tail $input_file] | |
if {[string match "*.txt" $fname]} { | |
set fname [string range $fname 0 end-4] | |
} | |
set p ".nam" | |
set namfilename $fname | |
set namefilepath "nams/" | |
set namfile $namefilepath$namfilename$p | |
set myNAM [open $namfile w] | |
$ns namtrace-all $myNAM | |
Class Agent/MessagePassing/Flooding -superclass Agent/MessagePassing | |
Agent/MessagePassing/Flooding instproc send_message {size msgid msg} { | |
$self instvar messages_seen node_ | |
global ns MESSAGE_PORT sink | |
$ns trace-annotate "Node [$node_ node-addr] is sending {$msgid:$msg}" | |
lappend messages_seen $msgid | |
if { "[$node_ node-addr]" != $sink} { | |
$self send_to_neighbors -1 $MESSAGE_PORT $size "$msgid:$msg" | |
} | |
} | |
Agent/MessagePassing/Flooding instproc send_to_neighbors {skip port size data} { | |
$self instvar node_ | |
global sink | |
foreach x [$node_ neighbors] { | |
set addr [$x set address_] | |
if {$addr != $skip && "[$node_ node-addr]" != $sink} { | |
$self sendto $size $data $addr $port | |
} | |
} | |
} | |
Agent/MessagePassing/Flooding instproc recv {source sport size data} { | |
$self instvar messages_seen node_ | |
global ns | |
# extract message ID from message | |
set message_id [lindex [split $data ":"] 0] | |
if {[lsearch $messages_seen $message_id] == -1} { | |
lappend messages_seen $message_id | |
$ns trace-annotate "Node [$node_ node-addr] received {$data} from $source, message #$message_id" | |
$self send_to_neighbors $source $sport $size $data | |
} else { | |
$ns trace-annotate "Node [$node_ node-addr] received redundant copy of message #$message_id from $source" | |
} | |
} | |
#Create Nodes | |
for {set i 0} {$i < $num_nodes-1} {incr i} { | |
set n($i) [$ns node] | |
} | |
set fp [open $input_file r] | |
set content [read $fp] | |
close $fp | |
set records [split $content "\n"] | |
set count 0 | |
foreach rec $records { | |
set element [split $rec " "] | |
foreach number $element { | |
incr count | |
if { $count % 2 == 1 } { | |
if {[regexp {[0-9]+} $number src]} { | |
continue | |
#puts "Source = $src" | |
} | |
} | |
if { $count % 2 == 0 } { | |
if {[regexp {[0-9]+} $number dest]} { | |
#puts "Destination = $dest" | |
set loss_module [new ErrorModel] | |
$loss_module set rate_ $loss_rate | |
$loss_module unit pkt | |
$loss_module ranvar [new RandomVariable/Uniform] | |
$loss_module drop-target [new Agent/Null] | |
if {$linktype == 1} { | |
$ns duplex-link $n($src) $n($dest) $bw $transTime DropTail | |
} | |
if {$linktype == 0} { | |
$ns simplex-link $n($src) $n($dest) 1Mb 1.0ms DropTail | |
} | |
$ns link-lossmodel $loss_module $n($src) $n($dest) | |
if {$argc == 10} { | |
set ql [lindex $argv 9] | |
$ns queue-limit $n($src) $n($dest) $ql | |
} | |
} | |
} | |
} | |
} | |
puts "Packet Size -> $packetSize - Script 1" | |
# attach a new Agent/MessagePassing/Flooding to each node on port $MESSAGE_PORT | |
for {set i 0} {$i < $num_nodes-1} {incr i} { | |
set a($i) [new Agent/MessagePassing/Flooding] | |
$n($i) attach $a($i) $MESSAGE_PORT | |
$a($i) set messages_seen {} | |
#set udp($i) [new Agent/UDP] | |
#$ns attach-agent $n($i) $udp($i) | |
set cbr($i) [new Application/Traffic/CBR] | |
$cbr($i) set type_ CBR | |
$cbr($i) set packet_size_ 500 | |
$cbr($i) set rate_ 0.6mb | |
$cbr($i) set random_ false | |
$cbr($i) attach-agent $a($i) | |
} | |
for { set l 0 } { $l < $num_nodes-1} { incr l } { | |
if { $l == $sink } { | |
continue; | |
} | |
set t1 [expr $l + 0.1] | |
set t2 [expr $l + 0.12] | |
set mid1 [expr $l + 1] | |
set mid2 [expr $l + 2] | |
#$ns at $t1 "$a($l) send_message $packetSize $mid1 {$l message}" | |
#$ns at $t2 "$a($l) send_message $packetSize $mid2 {$l message}" | |
$ns at $t1 "$cbr($l) start" | |
} | |
$ns at 120.10 "finish" | |
#define procedure finish | |
proc finish {} { | |
global ns mytrace myNAM | |
$ns flush-trace | |
close $mytrace | |
close $myNAM | |
#exec nam $myNAM & | |
exit 0 | |
} | |
$ns run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment