@@ -128,45 +128,71 @@ $ns connect $udp0 $sink
128128# Start sending traffic from udp0
129129$ns at 1.00 " $cbr1 start"
130130
131+ # temporary holder var to carry over values of
132+ # holdtime (lastPktTime_) i.e - Time when the last packet was received
133+ # holdseq (npkts_) i.e - No of packets
134+ # holdrate (bytes_) i.e - no of bytes
131135set holdtime 0
132136set holdseq 0
133137set holdrate1 0
134138
139+ # proc to record all the requried values to calculate throughput and meansure performance
135140proc record {} {
141+ # import all necessary trace file references and holder vars from global namespace
136142global sink f0 f1 f2 holdtime holdseq holdrate1
137143set ns [Simulator instance]
144+ # Record values for every 0.9 seconds
138145set time 0.9
146+ # Store all the instance variables (bytes_,nlost_,lastPktTime_,npkts_)
147+ # into an objects (i.e-bw0,bw1,bw2,bw3) and also get current time ($ns now)
139148set bw0 [$sink set bytes_]
140149set bw1 [$sink set nlost_]
141150set bw2 [$sink set lastPktTime_]
142151set bw3 [$sink set npkts_]
143152set now [$ns now]
144-
153+ # Write the current time and through-put into f0 trace file.
154+ # throughput = (no_of_bytes in current 0.9 sec) + bytes from all prev instances{which we get from holdrate var}) / (2*time in sec)
145155puts $f0 " $now [ expr (($bw0 +$holdrate1 )*8)/(2*$time *1000000)] "
156+
157+ # Here f1 is the trace file for loss
158+ # loss = nlost_{i.e referenced by bw1} / current time duration{which is 0.9 seconds}
146159puts $f1 " $now [ expr $bw1 /$time ] "
160+
161+ # Here we write to the f2 file which traces delay
162+ # if no of packets(bw3) > sum of all prev no of packets
147163if { $bw3 > $holdseq } {
164+ # calculate delay as (lastPktTime_ - previous_lastPktTime)/(current npkts - previous npkts)
148165puts $f2 " $now [ expr ($bw2 - $holdtime )/($bw3 - $holdseq )] "
149166} else {
167+ # else directly calculate and write delay into $f2 as (current npkts - previous npkts)
150168puts $f2 " $now [ expr ($bw3 - $holdseq )] "
151169}
152170
171+ # reset instance var for next 0.9s
153172$sink set bytes_ 0
154173$sink set nlost_ 0
155174
175+ # record lastPktTime(stored in bw2) into holdtime , npkts(stored in bw3) into holdseq and no of bytes byte_(stored in bw0) into holdrate
156176set holdtime $bw2
157177set holdseq $bw3
158178set holdrate1 $bw0
159179
180+ # Recursively call this proc every 0.9 seconds
160181$ns at [expr $now +$time ] " record"
161182}
162183
184+ # Start recording values from 0.0 seconds by calling the "record" proc
163185$ns at 0.0 " record"
186+ # mark sender node with blue square
164187$ns at 1.0 " $node_(4) add-mark m blue square"
188+ # mark reciever node with magenta square
165189$ns at 1.0 " $node_(20) add-mark m magenta square"
190+ # Add lables to sender and receiver
166191$ns at 1.0 " $node_(4) label SENDER"
167192$ns at 1.0 " $node_(20) label RCV"
168193$ns at 0.01 " $ns trace-annotate \" Network Deployment\" "
169194
195+ # Finish proc to close all f0,f1,f2 file handles and run the nam
170196proc finish {} {
171197global ns tracefd f0 f1 f2
172198$ns flush-trace
@@ -178,6 +204,6 @@ exit 0
178204}
179205
180206
181-
207+ # End simulation at 10 seconds
182208$ns at 10 " finish"
183209$ns run
0 commit comments