1
2# This script describes and illustrates the commands and usage of the
3# NORM ns-2 simulation agent.
4
5proc SimpleNORM {optionList} {
6
7#Some default parameters for NORM
8set groupSize "10"
9set backoffFactor "4.0"
10set sendRate "64kb"
11set duration "120.0"
12set nsTracing 0
13set unicastNacks "off"
14set cc "off"
15
16#Parse optionList for parameters
17set state "flag"
18foreach option $optionList {
19    if {"flag" != $state} {
20        set reset true
21    } else {
22        set reset false
23    }
24    switch -- $state {
25        "flag" {
26            switch -glob -- $option {
27                "unicast" {set unicastNacks "on"}
28                "cc" {set cc "on"}
29                "trace" {set nsTracing 1}
30                default {set state $option}
31            }
32
33        }
34        "gsize" {
35            set groupSize $option
36        }
37        "backoff" {
38            set backoffFactor $option
39        }
40        "rate" {
41            set sendRate $option
42        }
43        "duration" {
44            set duration $option
45        }
46        default {
47            error "simplenorm: bad option: $state"
48        }
49    }
50    if {$reset == "true"} {set state "flag"}
51}
52
53
54# 1) An ns-2 simulator instance is created an configured
55#   for multicast operation with a dense mode (DM) multicast
56#   routing protocol:
57set ns_ [new Simulator -multicast on]
58$ns_ multicast
59
60
61# 2) Trace files are opened and ns and nam
62#    tracing is enabled:
63if {$nsTracing} {
64    set f [open simplenorm.tr w]
65    $ns_ trace-all $f
66    set nf [open simplenorm.nam w]
67    $ns_ namtrace-all $nf
68} else {
69    set f 0
70    set nf 0
71}
72
73set numNodes [expr $groupSize + 1]
74
75# 3) A simple hub and spoke topology is created with
76#    a NORM agent attached to each spoke node
77
78# Node 0 is the hub of our hub & spoke topology
79# Note there is _not_ a NORM agent at the hub.
80set n(0) [$ns_ node]
81
82puts "simplenorm: Creating $numNodes nodes with norm agents ..."
83for {set i 1} {$i <= $numNodes} {incr i} {
84    set n($i) [$ns_ node]
85    set norm($i) [new Agent/NORM]
86    $ns_ attach-agent $n($i) $norm($i)
87}
88
89puts "simplenorm: Creating spoke links ..."
90set linkRate [expr $groupSize * [bw_parse $sendRate]]
91puts "simplenorm: linkRate = [expr $linkRate / 1000.0] kbps"
92for {set i 1} {$i <= $numNodes} {incr i} {
93    $ns_ duplex-link $n(0) $n($i) $linkRate 100ms DropTail
94    $ns_ queue-limit $n(0) $n($i) 100
95    #$ns_ duplex-link-op $n(0) $n($i) orient right
96    $ns_ duplex-link-op $n(0) $n($i) queuePos 0.5
97}
98
99# 4) Configure multicast routing for topology
100set mproto DM
101set mrthandle [$ns_ mrtproto $mproto  {}]
102if {$mrthandle != ""} {
103     $mrthandle set_c_rp [list $n(0)]
104}
105
106# 5) Allocate a multicast address to use
107set group [Node allocaddr]
108
109puts "simplenorm: Configuring NORM agents ..."
110
111# 6) Configure global NORM agent commands (using norm(1))
112$norm(1) debug 2
113#$norm(1) log normLog.txt
114
115# 7) Configure NORM sender agent at node 1
116$norm(1) address $group/5000
117$norm(1) rate [bw_parse $sendRate]
118$norm(1) backoff $backoffFactor
119$norm(1) parity 0
120$norm(1) repeat 50
121$norm(1) interval 0.0
122$norm(1) txloss 10.0
123$norm(1) gsize $groupSize
124$norm(1) cc on
125#$norm(1) trace on
126
127# 8) Configure NORM receiver agents at other nodes
128for {set i 2} {$i <= $numNodes} {incr i} {
129    $norm($i) address $group/5000
130    $norm($i) backoff $backoffFactor
131    $norm($i) rxbuffer 1000000
132    #$norm($i) txloss 10.0
133    $norm($i) gsize $groupSize
134    $norm($i) unicastNacks $unicastNacks
135    #$norm($i) trace
136}
137
138# 9) Start sender and receivers
139$ns_ at 0.0 "$norm(1) start sender"
140for {set i 2} {$i <= $numNodes} {incr i} {
141    $ns_ at 0.0 "$norm($i) start receiver"
142}
143
144$ns_ at 0.0 "$norm(1) sendFile 1000000"
145
146$ns_ at $duration "finish $ns_ $f $nf $nsTracing"
147
148puts "simplenorm: Running simulation (gsize:$groupSize rate:$sendRate backoff $backoffFactor duration:$duration) ..."
149$ns_ run
150
151}
152
153proc finish {ns_ f nf nsTracing} {
154    puts "simplenorm: Done."
155    $ns_ flush-trace
156    if {$nsTracing} {
157	    close $f
158	    close $nf
159    }
160    $ns_ halt
161    delete $ns_
162}
163
164# Run a set of trials with optional command-line parameters
165
166#Usage:
167#ns simplenorm.tcl [gsize <count>][rate <bps>][backoff <k>][duration <sec>][trace]
168
169puts "Running simplenorm: $argv"
170SimpleNORM $argv
171
172