├── README.md ├── lab6 ├── README.md ├── combined-packetloss.tr ├── combined-throughput.tr ├── combined.nam ├── combined.tr ├── combined_linear.tcl ├── generate_packetloss.sh ├── generate_throughput.sh ├── packet_loss.awk ├── packet_loss_com.xg ├── packet_loss_tcp.xg ├── packet_loss_udp.xg ├── packetloss_total.awk ├── tcp-packetloss.tr ├── tcp-throughput.tr ├── tcp.nam ├── tcp.tr ├── tcp_linear.tcl ├── throughput.awk ├── throughput_com.xg ├── throughput_tcp.xg ├── throughput_udp.xg ├── udp-packetloss.tr ├── udp-throughput.tr ├── udp.nam ├── udp.tr ├── udp_linear.tcl └── udp_throughput.awk └── lab7 ├── MANET ├── manet.nam ├── manet.tcl ├── manet.tr ├── rss.tr └── rss250.tr ├── VANET ├── dist.tr ├── vanet.nam ├── vanet.tcl └── vanet.tr └── WSN ├── calc_energy.py ├── wsn.nam ├── wsn.tcl └── wsn.tr /README.md: -------------------------------------------------------------------------------- 1 | ##### Each folder has a README.md in it. Go through it for further implementation details. 2 | 3 | ##### Hit the `Star` button on the top-right if you like it :stuck_out_tongue: 4 | -------------------------------------------------------------------------------- /lab6/README.md: -------------------------------------------------------------------------------- 1 | ##### If you find any bug, please feel free to raise an issue. I'll be happy to fix it. Or you can give a PR yourself :) 2 | 3 | + Run `ns tcp_linear.tcl ` where `QType` can be `DropTail`, `RED` or `SFQ` 4 | + Run `xgraph -bar -brw 0.1 tcp-throughput.tr` to get a plot of throughput 5 | + Run `xgraph -bar -brw 0.1 tcp-packetloss.tr` to get a plot of packetloss 6 | 7 | --- 8 | 9 | + Run `ns udp_linear.tcl ` where `QType` can be `DropTail`, `RED` or `SFQ` 10 | + Run `xgraph -bar -brw 0.1 udp-throughput.tr` to get a plot of throughput 11 | + Run `xgraph -bar -brw 0.1 udp-packetloss.tr` to get a plot of packetloss 12 | 13 | --- 14 | 15 | + Run `ns combined_linear.tcl ` where `QType` can be `DropTail`, `RED` or `SFQ` 16 | + Run `xgraph -bar -brw 0.1 combined-throughput.tr` to get a plot of throughput 17 | + Run `xgraph -bar -brw 0.1 combined-packetloss.tr` to get a plot of packetloss 18 | 19 | --- 20 | 21 | + In case you want consolidated graphs i.e. one for throughput and one for packet-loss, do the following 22 | + comment out the line `exec nam .nam &` in the `finish` procedures in the three `tcl` files 23 | ``` 24 | bash generate_throughput.sh 25 | ``` 26 | ``` 27 | bash generate_packetloss.sh 28 | ``` 29 | -------------------------------------------------------------------------------- /lab6/combined-packetloss.tr: -------------------------------------------------------------------------------- 1 | 0.050000000000000003 0 2 | 1.05 154 3 | 2.0499999999999998 399 4 | 3.0499999999999998 605 5 | 4.0499999999999998 756 6 | -------------------------------------------------------------------------------- /lab6/combined-throughput.tr: -------------------------------------------------------------------------------- 1 | 0.050000000000000003 0.0 2 | 1.05 0.10467618619047618 3 | 2.0499999999999998 0.071426848780487801 4 | 3.0499999999999998 0.079139321311475413 5 | 4.0499999999999998 0.054529640740740744 6 | -------------------------------------------------------------------------------- /lab6/combined_linear.tcl: -------------------------------------------------------------------------------- 1 | # Create a new simulator 2 | set ns [new Simulator] 3 | 4 | # Set colors for diffrent flows 5 | $ns color 1 Blue 6 | $ns color 2 Red 7 | 8 | # Open the animator file 9 | set combined_nam_file [open combined.nam w] 10 | $ns namtrace-all $combined_nam_file 11 | 12 | # Open the trace file 13 | set combined_trace_file [open combined.tr w] 14 | $ns trace-all $combined_trace_file 15 | 16 | # Open the file to track throughput for xgraph 17 | set combined_winfile [open combined-throughput.tr w] 18 | 19 | # Open the file to track packet loss for xgraph 20 | set combined_pacfile [open combined-packetloss.tr w] 21 | 22 | # Finish procedure to terminate the program 23 | proc finish {} { 24 | global ns combined_nam_file combined_trace_file combined_winfile combined_pacfile 25 | $ns flush-trace 26 | close $combined_nam_file 27 | close $combined_trace_file 28 | close $combined_winfile 29 | close $combined_pacfile 30 | exec nam combined.nam & 31 | exit 0 32 | } 33 | 34 | # Variable to set number of nodes (Use any value of your choice :D) 35 | set num_nodes 10 36 | 37 | # Queue type taken as command line argument 38 | # Run your program with "ns " 39 | set qtype [lindex $argv 0] 40 | 41 | # Create nodes 42 | for {set i 0} {$i < $num_nodes} {incr i} { 43 | set n($i) [$ns node] 44 | } 45 | 46 | # Establish links between nodes 47 | # TCP requires acknowledgement, so use duplex-links 48 | for {set i 0} {$i < [expr {$num_nodes - 2}]} {incr i} { 49 | $ns duplex-link $n($i) $n([expr {$i + 1}]) 2Mb 10ms $qtype 50 | } 51 | 52 | # Set the last link having lower capacity and greater propagation delay to observe packet drops 53 | $ns duplex-link $n([expr {$num_nodes - 2}]) $n([expr {$num_nodes - 1}]) 1Mb 20ms $qtype 54 | 55 | # Set the queue limit to 8 for the last link 56 | # Default is 50 (pretty large, so you won't observe significant packet drops) 57 | $ns queue-limit $n([expr {$num_nodes - 2}]) $n([expr {$num_nodes - 1}]) 8 58 | 59 | # Set orientaion (just to make it look good) 60 | for {set i 0} {$i < [expr {$num_nodes - 1}]} {incr i} { 61 | $ns duplex-link-op $n($i) $n([expr {$i + 1}]) orient right 62 | } 63 | 64 | # Monitor the queue at the last (for the "queue filling up" animation) 65 | $ns duplex-link-op $n([expr {$num_nodes - 2}]) $n([expr {$num_nodes - 1}]) queuePos 0.5 66 | 67 | # Declare a TCP source 68 | set tcp1 [new Agent/TCP] 69 | $ns attach-agent $n(0) $tcp1 70 | $tcp1 set class_ 1 71 | $tcp1 set fid_ 1 72 | 73 | # Declare a UDP source 74 | set udp1 [new Agent/UDP] 75 | $ns attach-agent $n(2) $udp1 76 | $udp1 set class_ 2 77 | $udp1 set fid_ 2 78 | 79 | # Two sinks for the two sources 80 | set sink1 [new Agent/TCPSink] 81 | set sink2 [new Agent/Null] 82 | 83 | # Last node is the common sink for all sources 84 | $ns attach-agent $n([expr {$num_nodes - 1}]) $sink1 85 | $ns attach-agent $n([expr {$num_nodes - 1}]) $sink2 86 | 87 | # Connect the source with its corresponding sink 88 | $ns connect $tcp1 $sink1 89 | $ns connect $udp1 $sink2 90 | 91 | # Set up FTP over TCP for the connections 92 | set ftp1 [new Application/FTP] 93 | $ftp1 attach-agent $tcp1 94 | $ftp1 set type_ FTP 95 | 96 | # Packet size and interval 97 | # Set it to anything of your choice 98 | set packet_sz 1000 99 | set intvl 0.01 100 | 101 | # Set up CBR over UDP for the connections 102 | set cbr1 [new Application/Traffic/CBR] 103 | $cbr1 attach-agent $udp1 104 | $cbr1 set type_ CBR 105 | $cbr1 set packetSize_ $packet_sz 106 | $cbr1 set interval_ $intvl 107 | 108 | # Procedure to be called every 1 second to track throughput and packetloss 109 | # Set the 'tick' variable on line 115 to a different value to change the delay between recursive calls 110 | proc record {} { 111 | global combined_winfile combined_pacfile packet_sz sink1 sink2 112 | #Get an instance of the simulator 113 | set ns [Simulator instance] 114 | #Set the time after which the procedure should be called again 115 | set tick 1 116 | #How many bytes have been received by the traffic sinks? 117 | set bw [$sink1 set bytes_] 118 | 119 | #Get the current time 120 | set now [$ns now] 121 | 122 | #Calculate the throughput (in MBit/s) and write it to the files 123 | set udp_thru [exec awk -v now=$now -v toNode=9 -v packet_sz=$packet_sz -f udp_throughput.awk combined.tr] 124 | set tcp_thru [expr $bw/$now*8/1000000] 125 | 126 | puts $combined_winfile "$now [expr {$tcp_thru + $udp_thru}]" 127 | 128 | #Calculate packet loss 129 | puts $combined_pacfile "$now [exec awk -v now=$now -v toNode=9 -f packet_loss.awk combined.tr]" 130 | 131 | #Reset the bytes_ values on the traffic sinks 132 | $sink1 set bytes_ 0 133 | 134 | #Re-schedule the procedure 135 | $ns at [expr $now+$tick] "record" 136 | } 137 | 138 | $ns at 0.05 "record" 139 | $ns at 0.1 "$ftp1 start" 140 | $ns at 0.1 "$cbr1 start" 141 | $ns at 4.5 "$ftp1 stop" 142 | $ns at 4.5 "$cbr1 stop" 143 | 144 | $ns at 4.5 "$ns detach-agent $n(0) $tcp1; $ns detach-agent $n([expr {$num_nodes - 1}]) $sink1" 145 | $ns at 4.5 "$ns detach-agent $n(2) $udp1; $ns detach-agent $n([expr {$num_nodes - 1}]) $sink2" 146 | 147 | $ns at 5.0 "finish" 148 | 149 | $ns run 150 | -------------------------------------------------------------------------------- /lab6/generate_packetloss.sh: -------------------------------------------------------------------------------- 1 | rm -rf packet_loss_tcp.xg packet_loss_udp.xg packet_loss_com.xg 2 | 3 | ns tcp_linear.tcl DropTail 4 | awk -v x=1.0 -f packetloss_total.awk tcp.tr >> packet_loss_tcp.xg 5 | ns tcp_linear.tcl RED 6 | awk -v x=3.0 -f packetloss_total.awk tcp.tr >> packet_loss_tcp.xg 7 | ns tcp_linear.tcl SFQ 8 | awk -v x=5.0 -f packetloss_total.awk tcp.tr >> packet_loss_tcp.xg 9 | ns udp_linear.tcl DropTail 10 | awk -v x=1.3 -f packetloss_total.awk udp.tr >> packet_loss_udp.xg 11 | ns udp_linear.tcl RED 12 | awk -v x=3.3 -f packetloss_total.awk udp.tr >> packet_loss_udp.xg 13 | ns udp_linear.tcl SFQ 14 | awk -v x=5.3 -f packetloss_total.awk udp.tr >> packet_loss_udp.xg 15 | ns combined_linear.tcl DropTail 16 | awk -v x=1.5 -f packetloss_total.awk combined.tr >> packet_loss_com.xg 17 | ns combined_linear.tcl RED 18 | awk -v x=3.5 -f packetloss_total.awk combined.tr >> packet_loss_com.xg 19 | ns combined_linear.tcl SFQ 20 | awk -v x=5.5 -f packetloss_total.awk combined.tr >> packet_loss_com.xg 21 | xgraph -bar -brw 0.1 packet_loss_tcp.xg packet_loss_udp.xg packet_loss_com.xg 22 | -------------------------------------------------------------------------------- /lab6/generate_throughput.sh: -------------------------------------------------------------------------------- 1 | rm -rf throughput_tcp.xg throughput_udp.xg throughput_com.xg 2 | 3 | ns tcp_linear.tcl DropTail 4 | awk -v x=1.0 -f throughput.awk tcp.tr >> throughput_tcp.xg 5 | ns tcp_linear.tcl RED 6 | awk -v x=3.0 -f throughput.awk tcp.tr >> throughput_tcp.xg 7 | ns tcp_linear.tcl SFQ 8 | awk -v x=5.0 -f throughput.awk tcp.tr >> throughput_tcp.xg 9 | ns udp_linear.tcl DropTail 10 | awk -v x=1.3 -f throughput.awk udp.tr >> throughput_udp.xg 11 | ns udp_linear.tcl RED 12 | awk -v x=3.3 -f throughput.awk udp.tr >> throughput_udp.xg 13 | ns udp_linear.tcl SFQ 14 | awk -v x=5.3 -f throughput.awk udp.tr >> throughput_udp.xg 15 | ns combined_linear.tcl DropTail 16 | awk -v x=1.5 -f throughput.awk combined.tr >> throughput_com.xg 17 | ns combined_linear.tcl RED 18 | awk -v x=3.5 -f throughput.awk combined.tr >> throughput_com.xg 19 | ns combined_linear.tcl SFQ 20 | awk -v x=5.5 -f throughput.awk combined.tr >> throughput_com.xg 21 | xgraph -bar -brw 0.1 throughput_tcp.xg throughput_udp.xg throughput_com.xg -------------------------------------------------------------------------------- /lab6/packet_loss.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | recv = 0; 3 | gen = 0; 4 | } 5 | 6 | $1=="+" && ($3==0 || $3 == 2 || $3 == 3) && $2 " 40 | set qtype [lindex $argv 0] 41 | 42 | # Create nodes 43 | for {set i 0} {$i < $num_nodes} {incr i} { 44 | set n($i) [$ns node] 45 | } 46 | 47 | # Establish links between nodes 48 | # TCP requires acknowledgement, so use duplex-links 49 | for {set i 0} {$i < [expr {$num_nodes - 2}]} {incr i} { 50 | $ns duplex-link $n($i) $n([expr {$i + 1}]) 2Mb 10ms $qtype 51 | } 52 | 53 | # Set the last link having lower capacity and greater propagation delay to observe packet drops 54 | $ns duplex-link $n([expr {$num_nodes - 2}]) $n([expr {$num_nodes - 1}]) 1Mb 20ms $qtype 55 | 56 | # Set the queue limit to 8 for the last link 57 | # Default is 50 (pretty large, so you won't observe significant packet drops) 58 | $ns queue-limit $n([expr {$num_nodes - 2}]) $n([expr {$num_nodes - 1}]) 8 59 | 60 | # Set orientaion (just to make it look good) 61 | for {set i 0} {$i < [expr {$num_nodes - 1}]} {incr i} { 62 | $ns duplex-link-op $n($i) $n([expr {$i + 1}]) orient right 63 | } 64 | 65 | # Monitor the queue at the last (for the "queue filling up" animation) 66 | $ns duplex-link-op $n([expr {$num_nodes - 2}]) $n([expr {$num_nodes - 1}]) queuePos 0.5 67 | 68 | # Declare three TCP sources 69 | set tcp1 [new Agent/TCP] 70 | $ns attach-agent $n(0) $tcp1 71 | $tcp1 set class_ 1 72 | $tcp1 set fid_ 1 73 | 74 | set tcp2 [new Agent/TCP] 75 | $ns attach-agent $n(2) $tcp2 76 | $tcp2 set class_ 2 77 | $tcp2 set fid_ 2 78 | 79 | set tcp3 [new Agent/TCP] 80 | $ns attach-agent $n(3) $tcp3 81 | $tcp3 set class_ 3 82 | $tcp3 set fid_ 3 83 | 84 | # Three sinks for the three sources 85 | set sink1 [new Agent/TCPSink] 86 | set sink2 [new Agent/TCPSink] 87 | set sink3 [new Agent/TCPSink] 88 | 89 | # Last node is the common sink for all sources 90 | $ns attach-agent $n([expr {$num_nodes - 1}]) $sink1 91 | $ns attach-agent $n([expr {$num_nodes - 1}]) $sink2 92 | $ns attach-agent $n([expr {$num_nodes - 1}]) $sink3 93 | 94 | # Connect the source with its corresponding sink 95 | $ns connect $tcp1 $sink1 96 | $ns connect $tcp2 $sink2 97 | $ns connect $tcp3 $sink3 98 | 99 | # Set up FTP over TCP for the connections 100 | set ftp1 [new Application/FTP] 101 | $ftp1 attach-agent $tcp1 102 | $ftp1 set type_ FTP 103 | 104 | set ftp2 [new Application/FTP] 105 | $ftp2 attach-agent $tcp2 106 | $ftp2 set type_ FTP 107 | 108 | set ftp3 [new Application/FTP] 109 | $ftp3 attach-agent $tcp3 110 | $ftp3 set type_ FTP 111 | 112 | # Procedure to be called every 1 second to track throughput and packetloss 113 | # Set the 'tick' variable on line 119 to a different value to change the delay between recursive calls 114 | proc record {} { 115 | global tcp_winfile tcp_pacfile sink1 sink2 sink3 116 | #Get an instance of the simulator 117 | set ns [Simulator instance] 118 | #Set the time after which the procedure should be called again 119 | set tick 1 120 | #How many bytes have been received by the traffic sinks? 121 | set bw1 [$sink1 set bytes_] 122 | set bw2 [$sink2 set bytes_] 123 | set bw3 [$sink3 set bytes_] 124 | 125 | set bw [expr {$bw1 + $bw2 + $bw3}] 126 | #Get the current time 127 | set now [$ns now] 128 | 129 | #Calculate the throughput (in MBit/s) and write it to the files 130 | puts $tcp_winfile "$now [expr $bw/$now*8/1000000]" 131 | 132 | #Calculate packet loss 133 | puts $tcp_pacfile "$now [exec awk -v now=$now -v toNode=9 -f packet_loss.awk tcp.tr]" 134 | 135 | #Reset the bytes_ values on the traffic sinks 136 | $sink1 set bytes_ 0 137 | $sink2 set bytes_ 0 138 | $sink3 set bytes_ 0 139 | 140 | #Re-schedule the procedure 141 | $ns at [expr $now+$tick] "record" 142 | } 143 | 144 | $ns at 0.05 "record" 145 | $ns at 0.1 "$ftp1 start" 146 | $ns at 0.1 "$ftp2 start" 147 | $ns at 0.1 "$ftp3 start" 148 | $ns at 4.5 "$ftp1 stop" 149 | $ns at 4.5 "$ftp2 stop" 150 | $ns at 4.5 "$ftp3 stop" 151 | 152 | $ns at 4.5 "$ns detach-agent $n(0) $tcp1; $ns detach-agent $n([expr {$num_nodes - 1}]) $sink1" 153 | $ns at 4.5 "$ns detach-agent $n(2) $tcp2; $ns detach-agent $n([expr {$num_nodes - 1}]) $sink2" 154 | $ns at 4.5 "$ns detach-agent $n(3) $tcp3; $ns detach-agent $n([expr {$num_nodes - 1}]) $sink3" 155 | 156 | $ns at 5.0 "finish" 157 | 158 | $ns run 159 | -------------------------------------------------------------------------------- /lab6/throughput.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | bytes_recv = 0; 3 | } 4 | 5 | $1 == "r" && $4 == "9" { 6 | bytes_recv = bytes_recv + $6; 7 | }; 8 | 9 | END { 10 | throughput = (bytes_recv)/(5 * 1000000); 11 | printf("%f %f\n",x, throughput); 12 | }; -------------------------------------------------------------------------------- /lab6/throughput_com.xg: -------------------------------------------------------------------------------- 1 | 1.500000 0.105152 2 | 3.500000 0.104208 3 | 5.500000 0.111432 4 | -------------------------------------------------------------------------------- /lab6/throughput_tcp.xg: -------------------------------------------------------------------------------- 1 | 1.000000 0.079688 2 | 3.000000 0.056392 3 | 5.000000 0.097160 4 | -------------------------------------------------------------------------------- /lab6/throughput_udp.xg: -------------------------------------------------------------------------------- 1 | 1.300000 0.117000 2 | 3.300000 0.112000 3 | 5.300000 0.117600 4 | -------------------------------------------------------------------------------- /lab6/udp-packetloss.tr: -------------------------------------------------------------------------------- 1 | 0.050000000000000003 0 2 | 1.05 455 3 | 2.0499999999999998 927 4 | 3.0499999999999998 1405 5 | 4.0499999999999998 1876 6 | -------------------------------------------------------------------------------- /lab6/udp-throughput.tr: -------------------------------------------------------------------------------- 1 | 0.050000000000000003 0 2 | 1.05 0.012619 3 | 2.0499999999999998 0.0140244 4 | 3.0499999999999998 0.0145902 5 | 4.0499999999999998 0.0148148 6 | -------------------------------------------------------------------------------- /lab6/udp_linear.tcl: -------------------------------------------------------------------------------- 1 | # Create new simulator 2 | set ns [new Simulator] 3 | 4 | # Set colors for different flows 5 | $ns color 1 Blue 6 | $ns color 2 Red 7 | $ns color 3 Yellow 8 | 9 | # Open file for animator 10 | set udp_nam_file [open udp.nam w] 11 | $ns namtrace-all $udp_nam_file 12 | 13 | # Open file for trace 14 | set udp_trace_file [open udp.tr w] 15 | $ns trace-all $udp_trace_file 16 | 17 | # Open the file to track throughput for xgraph 18 | set udp_winfile [open udp-throughput.tr w] 19 | 20 | # Open the file to track packet loss for xgraph 21 | set udp_pacfile [open udp-packetloss.tr w] 22 | 23 | # Finish procedure to terminate the program 24 | proc finish {} { 25 | global ns udp_nam_file udp_trace_file udp_winfile udp_pacfile 26 | $ns flush-trace 27 | close $udp_nam_file 28 | close $udp_trace_file 29 | close $udp_winfile 30 | close $udp_pacfile 31 | exec nam udp.nam & 32 | exit 0 33 | } 34 | 35 | # Variable to set number of nodes (Use any value of your choice :D) 36 | set num_nodes 10 37 | 38 | # Queue type taken as command line argument 39 | # Run your program with "ns " 40 | set qtype [lindex $argv 0] 41 | 42 | # Create nodes 43 | for {set i 0} {$i < $num_nodes} {incr i} { 44 | set n($i) [$ns node] 45 | } 46 | 47 | # Establish links between nodes 48 | for {set i 0} {$i < [expr {$num_nodes - 2}]} {incr i} { 49 | $ns duplex-link $n($i) $n([expr {$i + 1}]) 2Mb 10ms $qtype 50 | } 51 | 52 | # Set the last link having lower capacity and greater propagation delay to observe packet drops 53 | $ns duplex-link $n([expr {$num_nodes - 2}]) $n([expr {$num_nodes - 1}]) 1Mb 20ms $qtype 54 | 55 | # Set the queue limit to 8 for the last link 56 | # Default is 50 (pretty large, so you won't observe significant packet drops) 57 | $ns queue-limit $n([expr {$num_nodes - 2}]) $n([expr {$num_nodes - 1}]) 8 58 | 59 | # Set orientaion (just to make it look good) 60 | for {set i 0} {$i < [expr {$num_nodes - 1}]} {incr i} { 61 | $ns duplex-link-op $n($i) $n([expr {$i + 1}]) orient right 62 | } 63 | 64 | # Monitor the queue at the last (for the "queue filling up" animation) 65 | $ns duplex-link-op $n([expr {$num_nodes - 2}]) $n([expr {$num_nodes - 1}]) queuePos 0.5 66 | #$ns duplex-link-op $n(23) $n(24) queuePos 0.5 67 | 68 | # Declare three UDP sources 69 | set udp1 [new Agent/UDP] 70 | $ns attach-agent $n(0) $udp1 71 | $udp1 set class_ 1 72 | $udp1 set fid_ 1 73 | 74 | set udp2 [new Agent/UDP] 75 | $ns attach-agent $n(2) $udp2 76 | $udp2 set class_ 2 77 | $udp2 set fid_ 2 78 | 79 | set udp3 [new Agent/UDP] 80 | $ns attach-agent $n(3) $udp3 81 | $udp3 set class_ 3 82 | $udp3 set fid_ 3 83 | 84 | # Three sinks for the three sources 85 | set sink1 [new Agent/Null] 86 | set sink2 [new Agent/Null] 87 | set sink3 [new Agent/Null] 88 | 89 | # Last node is the common sink for all sources 90 | $ns attach-agent $n([expr {$num_nodes - 1}]) $sink1 91 | $ns attach-agent $n([expr {$num_nodes - 1}]) $sink2 92 | $ns attach-agent $n([expr {$num_nodes - 1}]) $sink3 93 | 94 | # Connect the source with its corresponding sink 95 | $ns connect $udp1 $sink1 96 | $ns connect $udp2 $sink2 97 | $ns connect $udp3 $sink3 98 | 99 | # Packet size and interval 100 | # Set it to anything of your choice 101 | set packet_sz 1000 102 | set intvl 0.01 103 | 104 | # Set up CBR over UDP for the connections 105 | set cbr1 [new Application/Traffic/CBR] 106 | $cbr1 attach-agent $udp1 107 | $cbr1 set type_ CBR 108 | $cbr1 set packetSize_ $packet_sz 109 | $cbr1 set interval_ $intvl 110 | 111 | set cbr2 [new Application/Traffic/CBR] 112 | $cbr2 attach-agent $udp2 113 | $cbr2 set type_ CBR 114 | $cbr2 set packetSize_ $packet_sz 115 | $cbr2 set interval_ $intvl 116 | 117 | set cbr3 [new Application/Traffic/CBR] 118 | $cbr3 attach-agent $udp3 119 | $cbr3 set type_ CBR 120 | $cbr3 set packetSize_ $packet_sz 121 | $cbr3 set interval_ $intvl 122 | 123 | # Procedure to be called every 1 second to track throughput and packetloss 124 | # Set the 'tick' variable on line 130 to a different value to change the delay between recursive calls 125 | proc record {} { 126 | global udp_winfile udp_pacfile sink1 sink2 sink3 packet_sz 127 | #Get an instance of the simulator 128 | set ns [Simulator instance] 129 | #Set the time after which the procedure should be called again 130 | set tick 1 131 | #Get the current time 132 | set now [$ns now] 133 | 134 | #Calculate the throughput (in MBit/s) and write it to the files 135 | puts $udp_winfile "$now [exec awk -v now=$now -v toNode=9 -v packet_sz=$packet_sz -f udp_throughput.awk udp.tr]" 136 | 137 | #Calculate packet loss 138 | puts $udp_pacfile "$now [exec awk -v now=$now -v toNode=9 -f packet_loss.awk udp.tr]" 139 | 140 | #Re-schedule the procedure 141 | $ns at [expr $now+$tick] "record" 142 | } 143 | 144 | $ns at 0.05 "record" 145 | $ns at 0.1 "$cbr1 start" 146 | $ns at 0.1 "$cbr2 start" 147 | $ns at 0.1 "$cbr3 start" 148 | $ns at 4.5 "$cbr1 stop" 149 | $ns at 4.5 "$cbr2 stop" 150 | $ns at 4.5 "$cbr3 stop" 151 | 152 | $ns at 4.5 "$ns detach-agent $n(0) $udp1; $ns detach-agent $n([expr {$num_nodes - 1}]) $sink1" 153 | $ns at 4.5 "$ns detach-agent $n(2) $udp2; $ns detach-agent $n([expr {$num_nodes - 1}]) $sink2" 154 | $ns at 4.5 "$ns detach-agent $n(3) $udp3; $ns detach-agent $n([expr {$num_nodes - 1}]) $sink3" 155 | 156 | $ns at 5.0 "finish" 157 | 158 | $ns run 159 | -------------------------------------------------------------------------------- /lab6/udp_throughput.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | udp_packets_recv = 0; 3 | } 4 | 5 | $1 == "r" && $2 < now && $4 == toNode && $5 == "cbr" { 6 | udp_packets_recv++; 7 | }; 8 | 9 | END { 10 | udp_throughput = (udp_packets_recv * packet_sz)/(8 * now * 1000000); 11 | print udp_throughput; 12 | }; 13 | -------------------------------------------------------------------------------- /lab7/MANET/manet.nam: -------------------------------------------------------------------------------- 1 | n -t * -s 0 -x 105 -y 105 -Z 0 -z 30 -v circle -c black 2 | n -t * -s 1 -x 390 -y 285 -Z 0 -z 30 -v circle -c black 3 | n -t * -s 2 -x 150 -y 240 -Z 0 -z 30 -v circle -c black 4 | V -t * -v 1.0a5 -a 0 5 | W -t * -x 500 -y 400 6 | A -t * -n 1 -p 0 -o 0x7fffffff -c 30 -a 1 7 | A -t * -h 1 -m 1073741823 -s 0 8 | + -t 0.032821055 -s 1 -d -1 -p message -e 32 -c 2 -a 0 -i 0 -k RTR 9 | - -t 0.032821055 -s 1 -d -1 -p message -e 32 -c 2 -a 0 -i 0 -k RTR 10 | h -t 0.032821055 -s 1 -d -1 -p message -e 32 -c 2 -a 0 -i 0 -k RTR 11 | r -t 0.033801869 -s 2 -d -1 -p message -e 32 -c 2 -a 0 -i 0 -k RTR 12 | + -t 0.178591360 -s 2 -d -1 -p message -e 32 -c 2 -a 0 -i 1 -k RTR 13 | - -t 0.178591360 -s 2 -d -1 -p message -e 32 -c 2 -a 0 -i 1 -k RTR 14 | h -t 0.178591360 -s 2 -d -1 -p message -e 32 -c 2 -a 0 -i 1 -k RTR 15 | r -t 0.179671834 -s 0 -d -1 -p message -e 32 -c 2 -a 0 -i 1 -k RTR 16 | r -t 0.179672174 -s 1 -d -1 -p message -e 32 -c 2 -a 0 -i 1 -k RTR 17 | n -t 0.500000 -s 0 -x 105.000000 -y 105.000000 -U 35.355339 -V 35.355339 -T 4.101219 18 | + -t 0.500000000 -s 0 -d -1 -p tcp -e 40 -c 2 -a 0 -i 2 -k AGT 19 | - -t 0.500000000 -s 0 -d -1 -p tcp -e 40 -c 2 -a 0 -i 2 -k AGT 20 | h -t 0.500000000 -s 0 -d -1 -p tcp -e 40 -c 2 -a 0 -i 2 -k AGT 21 | n -t 0.700000 -s 1 -x 390.000000 -y 285.000000 -U -60.000000 -V 0.000000 -T 5.750000 22 | + -t 1.113402886 -s 0 -d -1 -p message -e 32 -c 2 -a 0 -i 3 -k RTR 23 | - -t 1.113402886 -s 0 -d -1 -p message -e 32 -c 2 -a 0 -i 3 -k RTR 24 | h -t 1.113402886 -s 0 -d -1 -p message -e 32 -c 2 -a 0 -i 3 -k RTR 25 | r -t 1.114383272 -s 2 -d -1 -p message -e 32 -c 2 -a 0 -i 3 -k RTR 26 | + -t 3.500000000 -s 0 -d -1 -p tcp -e 40 -c 2 -a 0 -i 4 -k AGT 27 | - -t 3.500000000 -s 0 -d -1 -p tcp -e 40 -c 2 -a 0 -i 4 -k AGT 28 | h -t 3.500000000 -s 0 -d -1 -p tcp -e 40 -c 2 -a 0 -i 4 -k AGT 29 | W -t 5 30 | -------------------------------------------------------------------------------- /lab7/MANET/manet.tcl: -------------------------------------------------------------------------------- 1 | set routing_protocol [lindex $argv 0] 2 | # set transmission_range [lindex $argv 1] 3 | 4 | set val(chan) Channel/WirelessChannel ;# channel type 5 | set val(prop) Propagation/TwoRayGround ;# radio-propagation model 6 | set val(netif) Phy/WirelessPhy ;# network interface type 7 | set val(mac) Mac/802_11 ;# MAC type 8 | set val(ifq) Queue/DropTail/PriQueue ;# interface queue type 9 | set val(ll) LL ;# link layer type 10 | set val(ant) Antenna/OmniAntenna ;# antenna model 11 | set val(ifqlen) 50 ;# max packet in ifq 12 | set val(nn) 3 ;# number of mobilenodes 13 | set val(rp) $routing_protocol ;# routing protocol 14 | set val(x) 500 ;# X dimension of topography 15 | set val(y) 400 ;# Y dimension of topography 16 | set val(stop) 5 ;# time of simulation end 17 | 18 | $val(netif) set CPThresh_ 10.0 19 | $val(netif) set CSThresh_ 3.65262e-10 ;#250m 20 | $val(netif) set RXThresh_ 3.65262e-10 ;#250m 21 | # $val(netif) set CSThresh_ 2.28289e-11 ;#500m 22 | # $val(netif) set RXThresh_ 2.28289e-11 ;#500m 23 | $val(netif) set Rb_ 2*1e6 24 | $val(netif) set Pt_ 0.2818 25 | $val(netif) set freq_ 914e+6 26 | $val(netif) set L_ 1.0 27 | 28 | set ns [new Simulator] 29 | 30 | set tracefd [open manet.tr w] 31 | set namtrace [open manet.nam w] 32 | set rssfd [open rss250.tr w] 33 | 34 | $ns trace-all $tracefd 35 | $ns namtrace-all-wireless $namtrace $val(x) $val(y) 36 | 37 | set topo [new Topography] 38 | 39 | $topo load_flatgrid $val(x) $val(y) 40 | 41 | create-god $val(nn) 42 | set chan_ [new $val(chan)] 43 | 44 | $ns node-config -adhocRouting $val(rp) \ 45 | -llType $val(ll) \ 46 | -macType $val(mac) \ 47 | -ifqType $val(ifq) \ 48 | -ifqLen $val(ifqlen) \ 49 | -antType $val(ant) \ 50 | -propType $val(prop) \ 51 | -phyType $val(netif) \ 52 | -channel $chan_ \ 53 | -topoInstance $topo \ 54 | -agentTrace ON \ 55 | -routerTrace ON \ 56 | -macTrace OFF \ 57 | -movementTrace ON 58 | 59 | 60 | proc finish {} { 61 | global ns tracefd namtrace 62 | $ns flush-trace 63 | close $tracefd 64 | close $namtrace 65 | # exec nam manet.nam & 66 | exit 0 67 | } 68 | 69 | proc record {} { 70 | global ns rssfd node_ 71 | set tick 0.5 72 | set now [$ns now] 73 | 74 | set srcnode_x [$node_(0) set X_ ] 75 | set srcnode_y [$node_(0) set Y_ ] 76 | 77 | set destnode_x [$node_(1) set X_ ] 78 | set destnode_y [$node_(1) set Y_ ] 79 | 80 | set distance [expr pow(pow($destnode_x - $srcnode_x,2) + pow($destnode_y - $srcnode_y,2),0.5)] 81 | set RSSval [expr 0.2818/$distance] 82 | 83 | puts $rssfd "$now $RSSval" 84 | 85 | $ns at [expr $now+$tick] "record" 86 | } 87 | 88 | for {set i 0} {$i < $val(nn)} {incr i} { 89 | set node_($i) [$ns node] 90 | } 91 | 92 | $node_(0) set X_ 105.0 93 | $node_(0) set Y_ 105.0 94 | $node_(0) set Z_ 0.0 95 | 96 | $node_(1) set X_ 390.0 97 | $node_(1) set Y_ 285.0 98 | $node_(1) set Z_ 0.0 99 | 100 | $node_(2) set X_ 150.0 101 | $node_(2) set Y_ 240.0 102 | $node_(2) set Z_ 0.0 103 | 104 | $ns at 0.5 "$node_(0) setdest 250.0 250.0 50.0" 105 | $ns at 0.7 "$node_(1) setdest 45.0 285.0 60.0" 106 | 107 | set tcp [new Agent/TCP] 108 | set sink [new Agent/TCPSink] 109 | $tcp set class_ 2 110 | 111 | $ns attach-agent $node_(0) $tcp 112 | $ns attach-agent $node_(1) $sink 113 | 114 | $ns connect $tcp $sink 115 | 116 | set ftp [new Application/FTP] 117 | $ftp attach-agent $tcp 118 | $ns at 0.5 "$ftp start" 119 | $ns at 4.5 "$ftp stop" 120 | 121 | for {set i 0} {$i < $val(nn)} {incr i} { 122 | $ns initial_node_pos $node_($i) 30 123 | } 124 | 125 | for {set i 0} {$i < $val(nn)} {incr i} { 126 | $ns at $val(stop) "$node_($i) reset" 127 | } 128 | 129 | $ns at 0.5 "record" 130 | $ns at $val(stop) "$ns nam-end-wireless $val(stop)" 131 | $ns at $val(stop) "finish" 132 | $ns at 5.1 "puts \"NS Exiting\"; $ns halt" 133 | 134 | $ns run 135 | -------------------------------------------------------------------------------- /lab7/MANET/manet.tr: -------------------------------------------------------------------------------- 1 | s 0.032821055 _1_ RTR --- 0 message 32 [0 0 0 0] ------- [1:255 -1:255 32 0] 2 | r 0.033801869 _2_ RTR --- 0 message 32 [0 ffffffff 1 800] ------- [1:255 -1:255 32 0] 3 | s 0.178591360 _2_ RTR --- 1 message 32 [0 0 0 0] ------- [2:255 -1:255 32 0] 4 | r 0.179671834 _0_ RTR --- 1 message 32 [0 ffffffff 2 800] ------- [2:255 -1:255 32 0] 5 | r 0.179672174 _1_ RTR --- 1 message 32 [0 ffffffff 2 800] ------- [2:255 -1:255 32 0] 6 | M 0.50000 0 (105.00, 105.00, 0.00), (250.00, 250.00), 50.00 7 | s 0.500000000 _0_ AGT --- 2 tcp 40 [0 0 0 0] ------- [0:0 1:0 32 0] [0 0] 0 0 8 | r 0.500000000 _0_ RTR --- 2 tcp 40 [0 0 0 0] ------- [0:0 1:0 32 0] [0 0] 0 0 9 | M 0.70000 1 (390.00, 285.00, 0.00), (45.00, 285.00), 60.00 10 | s 1.113402886 _0_ RTR --- 3 message 32 [0 0 0 0] ------- [0:255 -1:255 32 0] 11 | r 1.114383272 _2_ RTR --- 3 message 32 [0 ffffffff 0 800] ------- [0:255 -1:255 32 0] 12 | s 3.500000000 _0_ AGT --- 4 tcp 40 [0 0 0 0] ------- [0:0 1:0 32 0] [0 0] 0 0 13 | r 3.500000000 _0_ RTR --- 4 tcp 40 [0 0 0 0] ------- [0:0 1:0 32 0] [0 0] 0 0 14 | -------------------------------------------------------------------------------- /lab7/MANET/rss.tr: -------------------------------------------------------------------------------- 1 | 0.5 0.00083599569429518479 2 | 1 0.00083657002716162215 3 | 1.5 0.0011332600153659415 4 | 2 0.0014110990823737229 5 | 2.5 0.001848230793162301 6 | 3 0.002590594438015213 7 | 3.5 0.0037609804009133595 8 | 4 0.0042059247782105747 9 | 4.5 0.0030370759507674094 10 | -------------------------------------------------------------------------------- /lab7/MANET/rss250.tr: -------------------------------------------------------------------------------- 1 | 0.5 0.00083599569429518479 2 | 1 0.00083599569429518479 3 | 1.5 0.00091723107475845313 4 | 2 0.00091723107475845313 5 | 2.5 0.00091723107475845313 6 | 3 0.00091723107475845313 7 | 3.5 0.00091723107475845313 8 | 4 0.00091723107475845313 9 | 4.5 0.00091723107475845313 10 | -------------------------------------------------------------------------------- /lab7/VANET/dist.tr: -------------------------------------------------------------------------------- 1 | 0.5 0 224.31652514464992 87.838106131105732 1 295.04974549405733 401.07251862113947 321.12144996710265 2 | 0.5 0 224.31652514464992 87.838106131105732 2 325.82046549106968 64.56350840840652 104.13816209729754 3 | 0.5 0 224.31652514464992 87.838106131105732 3 118.88582008838924 113.97822555805475 108.62292305179136 4 | 0.5 1 295.04974549405733 401.07251862113947 2 325.82046549106968 64.56350840840652 337.91293429445363 5 | 0.5 1 295.04974549405733 401.07251862113947 3 118.88582008838924 113.97822555805475 336.83358164489204 6 | 0.5 2 325.82046549106968 64.56350840840652 3 118.88582008838924 113.97822555805475 212.75281840415849 7 | 1 0 228.16139269141277 112.11406592897792 1 279.18175488717719 393.70663375867747 286.17730800241816 8 | 1 0 228.16139269141277 112.11406592897792 2 328.99842346300426 88.935746696254867 103.46661904829455 9 | 1 0 228.16139269141277 112.11406592897792 3 120.56370131659445 131.39184334387764 109.31100535284209 10 | 1 1 279.18175488717719 393.70663375867747 2 328.99842346300426 88.935746696254867 308.8154692835347 11 | 1 1 279.18175488717719 393.70663375867747 3 120.56370131659445 131.39184334387764 306.54320444087591 12 | 1 2 328.99842346300426 88.935746696254867 3 120.56370131659445 131.39184334387764 212.71472337100536 13 | 1.5 0 232.13701294246371 137.2155801456046 1 251.51949417342462 380.86587575088345 244.42002194421045 14 | 1.5 0 232.13701294246371 137.2155801456046 2 332.28445729304639 114.13683501617218 102.77226808192155 15 | 1.5 0 232.13701294246371 137.2155801456046 3 123.48870747432478 161.74854463815095 111.38366409797518 16 | 1.5 1 251.51949417342462 380.86587575088345 2 332.28445729304639 114.13683501617218 278.68864425909629 17 | 1.5 1 251.51949417342462 380.86587575088345 3 123.48870747432478 161.74854463815095 253.77999751114595 18 | 1.5 2 332.28445729304639 114.13683501617218 3 123.48870747432478 161.74854463815095 214.15541094142313 19 | 2 0 236.04766145775002 161.90687184449231 1 224.30933663295039 368.23498299968008 206.66174711968353 20 | 2 0 236.04766145775002 161.90687184449231 2 335.51678523341349 138.92604478005489 102.08929913200423 21 | 2 0 236.04766145775002 161.90687184449231 3 126.36590992492849 191.60912292284755 113.63234723629208 22 | 2 1 224.30933663295039 368.23498299968008 2 335.51678523341349 138.92604478005489 254.85228225706854 23 | 2 1 224.30933663295039 368.23498299968008 3 126.36590992492849 191.60912292284755 201.96437627262509 24 | 2 2 335.51678523341349 138.92604478005489 3 126.36590992492849 191.60912292284755 215.68401740719 25 | 2.5 0 239.91163960726121 186.30349333444707 1 197.42392184880586 355.7548350914945 174.69677553860532 26 | 2.5 0 239.91163960726121 186.30349333444707 2 338.71053658402207 163.41940486743354 101.41451350172079 27 | 2.5 0 239.91163960726121 186.30349333444707 3 129.20877265980141 221.11331204569518 116.04675018757288 28 | 2.5 1 197.42392184880586 355.7548350914945 2 338.71053658402207 163.41940486743354 238.6521008137411 29 | 2.5 1 197.42392184880586 355.7548350914945 3 129.20877265980141 221.11331204569518 150.93590131897264 30 | 2.5 2 338.71053658402207 163.41940486743354 3 129.20877265980141 221.11331204569518 217.30065810497157 31 | 3 0 243.81702305007803 210.96154209685955 1 170.25039139425735 343.14094454961037 151.27274614400793 32 | 3 0 243.81702305007803 210.96154209685955 2 341.93851354799205 188.17524633807034 100.73252787426337 33 | 3 0 243.81702305007803 210.96154209685955 3 132.08210083630215 250.93368180591929 118.66956136701248 34 | 3 1 170.25039139425735 343.14094454961037 2 341.93851354799205 188.17524633807034 231.28160089135025 35 | 3 1 170.25039139425735 343.14094454961037 3 132.08210083630215 250.93368180591929 99.794777953561137 36 | 3 2 341.93851354799205 188.17524633807034 3 132.08210083630215 250.93368180591929 219.03957445765658 37 | 3.5 0 247.74854234969015 235.78460902989164 1 142.89500914847397 330.44263882312919 141.26006523117562 38 | 3.5 0 247.74854234969015 235.78460902989164 2 345.18809290897013 213.09675984467924 100.0460119837139 39 | 3.5 0 247.74854234969015 235.78460902989164 3 134.9746578577583 280.95361501013798 121.48328331356177 40 | 3.5 1 142.89500914847397 330.44263882312919 2 345.18809290897013 213.09675984467924 233.86437747240629 41 | 3.5 1 142.89500914847397 330.44263882312919 3 134.9746578577583 280.95361501013798 50.118813259405414 42 | 3.5 2 345.18809290897013 213.09675984467924 3 134.9746578577583 280.95361501013798 220.89418523124618 43 | 4 0 250 250 1 115.81675330509175 317.87297453733208 150.37248540252278 44 | 4 0 250 250 2 348.40475208519121 237.76580349660193 99.162345661201968 45 | 4 0 250 250 3 137.83791178714773 310.6694308330965 127.51907257300586 46 | 4 1 115.81675330509175 317.87297453733208 2 348.40475208519121 237.76580349660193 245.9966179212233 47 | 4 1 115.81675330509175 317.87297453733208 3 137.83791178714773 310.6694308330965 23.169429487811264 48 | 4 2 348.40475208519121 237.76580349660193 3 137.83791178714773 310.6694308330965 222.83027871436195 49 | 4.5 0 250 250 1 88.474050846870796 305.18055473916763 170.69131750367842 50 | 4.5 0 250 250 2 350 250 100.0 51 | 4.5 0 250 250 3 140.72912940930476 340.67546329925261 141.9935308532842 52 | 4.5 1 88.474050846870796 305.18055473916763 2 350 250 267.28396080155539 53 | 4.5 1 88.474050846870796 305.18055473916763 3 140.72912940930476 340.67546329925261 63.170260164534227 54 | 4.5 2 350 250 3 140.72912940930476 340.67546329925261 228.07090327861118 55 | -------------------------------------------------------------------------------- /lab7/VANET/vanet.tcl: -------------------------------------------------------------------------------- 1 | set val(chan) Channel/WirelessChannel ;# channel type 2 | set val(prop) Propagation/TwoRayGround ;# radio-propagation model 3 | set val(netif1) Phy/WirelessPhy ;# network interface type 4 | set val(netif2) Phy/WirelessPhy ;# network interface type 5 | set val(mac) Mac/802_11 ;# MAC type 6 | set val(ifq) Queue/DropTail/PriQueue ;# interface queue type 7 | set val(ll) LL ;# link layer type 8 | set val(ant) Antenna/OmniAntenna ;# antenna model 9 | set val(ifqlen) 50 ;# max packet in ifq 10 | set val(nn) 4 ;# number of mobilenodes 11 | set val(rp) AODV ;# routing protocol 12 | set val(x) 500 ;# X dimension of topography 13 | set val(y) 500 ;# Y dimension of topography 14 | set val(stop) 5.0 ;# time of simulation end 15 | 16 | # Simulator Instance Creation 17 | set ns [new Simulator] 18 | 19 | set tracefd [open vanet.tr w] 20 | set namtrace [open vanet.nam w] 21 | set distfd [open dist.tr w] 22 | 23 | $ns trace-all $tracefd 24 | $ns namtrace-all-wireless $namtrace $val(x) $val(y) 25 | # set up topography object 26 | set topo [new Topography] 27 | $topo load_flatgrid $val(x) $val(y) 28 | # general operational descriptor- storing the hop details in the network 29 | create-god $val(nn) 30 | # For model 'TwoRayGround' 31 | 32 | set dist(20m) 4.80696e-07 33 | set dist(26m) 2.84435e-07 34 | set dist(27m) 2.63756e-07 35 | set dist(28m) 2.45253e-07 36 | set dist(25m) 3.07645e-07 37 | set dist(30m) 2.13643e-07 38 | set dist(35m) 1.56962e-07 39 | set dist(50m) 7.69113e-08 40 | set dist(75m) 3.41828e-08 41 | set dist(60m) 5.34106e-08 42 | set dist(70m) 3.92405e-08 43 | set dist(250m) 3.65262e-10 44 | set dist(500m) 2.28289e-11 45 | # unity gain, omni-directional antennas 46 | # set up the antennas to be centered in the node and 1.5 meters above it 47 | Antenna/OmniAntenna set X_ 0 48 | Antenna/OmniAntenna set Y_ 0 49 | Antenna/OmniAntenna set Z_ 1.5 50 | Antenna/OmniAntenna set Gt_ 1.0 51 | Antenna/OmniAntenna set Gr_ 1.0 52 | # Initialize the SharedMedia interface with parameters to make 53 | # it work like the 914MHz Lucent WaveLAN DSSS radio interface 54 | 55 | $val(netif1) set CPThresh_ 10.0 56 | $val(netif1) set CSThresh_ $dist(250m) 57 | $val(netif1) set RXThresh_ $dist(250m) 58 | $val(netif1) set Rb_ 2*1e6 59 | $val(netif1) set Pt_ 0.2818 60 | $val(netif1) set freq_ 914e+6 61 | $val(netif1) set L_ 1.0 62 | 63 | $val(netif2) set CPThresh_ 10.0 64 | $val(netif2) set CSThresh_ $dist(500m) 65 | $val(netif2) set RXThresh_ $dist(500m) 66 | $val(netif2) set Rb_ 2*1e6 67 | $val(netif2) set Pt_ 0.2818 68 | $val(netif2) set freq_ 914e+6 69 | $val(netif2) set L_ 1.0 70 | # set up topography object 71 | set topo [new Topography] 72 | $topo load_flatgrid $val(x) $val(y) 73 | 74 | # Create God 75 | set god_ [create-god $val(nn)] 76 | 77 | set chan_1_ [new $val(chan)] 78 | 79 | # configure node 80 | 81 | $ns node-config -adhocRouting $val(rp) \ 82 | -llType $val(ll) \ 83 | -macType $val(mac) \ 84 | -ifqType $val(ifq) \ 85 | -ifqLen $val(ifqlen) \ 86 | -antType $val(ant) \ 87 | -propType $val(prop) \ 88 | -phyType $val(netif1) \ 89 | -topoInstance $topo \ 90 | -agentTrace ON \ 91 | -routerTrace ON \ 92 | -macTrace OFF \ 93 | -movementTrace ON \ 94 | -channel $chan_1_ \ 95 | 96 | for {set i 0} {$i < 1 } {incr i} { 97 | set node_($i) [$ns node] 98 | $node_($i) color black 99 | set xx [expr rand()*$val(x)] 100 | set yy [expr rand()*$val(y)] 101 | $node_($i) set X_ $xx 102 | $node_($i) set Y_ $yy 103 | #$node_($i) rand-motion 0 ;# disable rand motion 104 | } 105 | 106 | $ns node-config -adhocRouting $val(rp) \ 107 | -llType $val(ll) \ 108 | -macType $val(mac) \ 109 | -ifqType $val(ifq) \ 110 | -ifqLen $val(ifqlen) \ 111 | -antType $val(ant) \ 112 | -propType $val(prop) \ 113 | -phyType $val(netif2) \ 114 | -topoInstance $topo \ 115 | -agentTrace ON \ 116 | -routerTrace ON \ 117 | -macTrace OFF \ 118 | -movementTrace ON \ 119 | -channel $chan_1_ \ 120 | 121 | for {set i 1} {$i < 4 } {incr i} { 122 | set node_($i) [$ns node] 123 | $node_($i) color brown 124 | $node_($i) set X_ [expr rand() * $val(x)] 125 | $node_($i) set Y_ [expr rand() * $val(y)] 126 | } 127 | 128 | $ns at 0.5 "$node_(0) setdest 250.0 250.0 50.0" 129 | $ns at 0.7 "$node_(1) setdest 45.0 285.0 60.0" 130 | $ns at 0.5 "$node_(2) setdest 350.0 250.0 50.0" 131 | $ns at 0.7 "$node_(3) setdest 145.0 385.0 60.0" 132 | 133 | proc finish {} { 134 | global ns tracefd namtrace distfd 135 | $ns flush-trace 136 | close $tracefd 137 | close $distfd 138 | close $namtrace 139 | # exec nam vanet.nam & 140 | exit 0 141 | } 142 | 143 | proc record {} { 144 | global ns distfd node_ val 145 | set tick 0.5 146 | set now [$ns now] 147 | 148 | for {set i 0} {$i < $val(nn)} {incr i} { 149 | for {set j [expr $i+1]} {$j < $val(nn)} {incr j} { 150 | set srcnode_x [$node_($i) set X_] 151 | set srcnode_y [$node_($i) set Y_] 152 | 153 | set destnode_x [$node_($j) set X_] 154 | set destnode_y [$node_($j) set Y_] 155 | 156 | set distance [expr pow(pow($destnode_x - $srcnode_x,2) + pow($destnode_y - $srcnode_y,2),0.5)] 157 | 158 | puts $distfd "$now $i $srcnode_x $srcnode_y $j $destnode_x $destnode_y $distance" 159 | } 160 | } 161 | 162 | $ns at [expr $now+$tick] "record" 163 | } 164 | 165 | set tcp [new Agent/TCP] 166 | set sink [new Agent/TCPSink] 167 | $tcp set class_ 2 168 | 169 | $ns attach-agent $node_(1) $tcp 170 | $ns attach-agent $node_(2) $sink 171 | 172 | $ns connect $tcp $sink 173 | 174 | set ftp [new Application/FTP] 175 | $ftp attach-agent $tcp 176 | $ns at 0.5 "$ftp start" 177 | $ns at 4.5 "$ftp stop" 178 | $ns at 0.5 "record" 179 | 180 | for {set i 0} {$i < $val(nn)} {incr i} { 181 | $ns initial_node_pos $node_($i) 30 182 | } 183 | 184 | for {set i 0} {$i < $val(nn)} {incr i} { 185 | $ns at $val(stop) "$node_($i) reset" 186 | } 187 | 188 | $ns at $val(stop) "$ns nam-end-wireless $val(stop)" 189 | $ns at $val(stop) "finish" 190 | $ns at 5.1 "puts \"NS Exiting\"; $ns halt" 191 | 192 | $ns run 193 | -------------------------------------------------------------------------------- /lab7/WSN/calc_energy.py: -------------------------------------------------------------------------------- 1 | fh = open("wsn.tr", "r") 2 | data = fh.readlines() 3 | nodes = [[] for _ in range(6)] 4 | for line in data: 5 | arr = line.split() 6 | if arr[0] in ['s','r']: 7 | node = arr[2][1] 8 | residual = float(arr[13]) 9 | spent = float(arr[15]) + float(arr[17]) + float(arr[19]) + float(arr[21][:-1]) 10 | time = float(arr[1]) 11 | nodes[int(node)].append([time, residual, spent]) 12 | # print node,time,residual,spent 13 | 14 | for i in range(6): 15 | spent_energy = sum([x[2] for x in nodes[i]]) 16 | try: 17 | residual_energy = nodes[i][len(nodes[i])-1][1] 18 | except: 19 | pass 20 | print "node {0} spent {1}i residual {2}".format(i,spent_energy,residual_energy) 21 | -------------------------------------------------------------------------------- /lab7/WSN/wsn.tcl: -------------------------------------------------------------------------------- 1 | 2 | # Define options 3 | set val(chan) Channel/WirelessChannel ;# channel type 4 | set val(prop) Propagation/TwoRayGround ;# radio-propagation model 5 | set val(netif1) Phy/WirelessPhy ;# network interface type 6 | set val(netif2) Phy/WirelessPhy ;# network interface type 7 | set val(mac) Mac/802_11 ;# MAC type 8 | set val(ifq) Queue/DropTail/PriQueue ;# interface queue type 9 | set val(ll) LL ;# link layer type 10 | set val(ant) Antenna/OmniAntenna ;# antenna model 11 | set val(ifqlen) 50 ;# max packet in ifq 12 | set val(nn) 6 ;# number of mobilenodes 13 | set val(rp) AODV ;# routing protocol 14 | set val(x) 600 ;# X dimension of topography 15 | set val(y) 600 ;# Y dimension of topography 16 | set val(stop) 10.0 ;# time of simulation end 17 | set val(energymodel) EnergyModel ;#Energy set up 18 | 19 | # Simulator Instance Creation 20 | set ns [new Simulator] 21 | 22 | set tracefd [open wsn.tr w] 23 | set namtrace [open wsn.nam w] 24 | 25 | $ns trace-all $tracefd 26 | $ns namtrace-all-wireless $namtrace $val(x) $val(y) 27 | $ns eventtrace-all 28 | 29 | # set up topography object 30 | set topo [new Topography] 31 | $topo load_flatgrid $val(x) $val(y) 32 | 33 | # general operational descriptor- storing the hop details in the network 34 | create-god $val(nn) 35 | 36 | #Transmission range setup 37 | 38 | #********************************** UNITY GAIN, 1.5m HEIGHT OMNI DIRECTIONAL ANTENNA SET UP ************** 39 | 40 | Antenna/OmniAntenna set X_ 0 41 | Antenna/OmniAntenna set Y_ 0 42 | Antenna/OmniAntenna set Z_ 1.5 43 | Antenna/OmniAntenna set Gt_ 1.0 44 | Antenna/OmniAntenna set Gr_ 1.0 45 | 46 | #********************************** SET UP COMMUNICATION AND SENSING RANGE *********************************** 47 | 48 | #default communication range 250m 49 | 50 | # Initialize the SharedMedia interface with parameters to make 51 | # it work like the 914MHz Lucent WaveLAN DSSS radio interface 52 | $val(netif1) set CPThresh_ 10.0 53 | $val(netif1) set CSThresh_ 2.28289e-11 ;#sensing range of 500m 54 | $val(netif1) set RXThresh_ 2.28289e-11 ;#communication range of 500m 55 | $val(netif1) set Rb_ 2*1e6 56 | $val(netif1) set Pt_ 0.2818 57 | $val(netif1) set freq_ 914e+6 58 | $val(netif1) set L_ 1.0 59 | 60 | # Initialize the SharedMedia interface with parameters to make 61 | # it work like the 914MHz Lucent WaveLAN DSSS radio interface 62 | $val(netif2) set CPThresh_ 10.0 63 | $val(netif2) set CSThresh_ 8.91754e-10 ;#sensing range of 200m 64 | $val(netif2) set RXThresh_ 8.91754e-10 ;#communication range of 200m 65 | $val(netif2) set Rb_ 2*1e6 66 | $val(netif2) set Pt_ 0.2818 67 | $val(netif2) set freq_ 914e+6 68 | $val(netif2) set L_ 1.0 69 | 70 | # configure the first 5 nodes with transmission range of 500m 71 | 72 | $ns node-config -adhocRouting $val(rp) \ 73 | -llType $val(ll) \ 74 | -macType $val(mac) \ 75 | -ifqType $val(ifq) \ 76 | -ifqLen $val(ifqlen) \ 77 | -antType $val(ant) \ 78 | -propType $val(prop) \ 79 | -phyType $val(netif1) \ 80 | -channelType $val(chan) \ 81 | -topoInstance $topo \ 82 | -energyModel $val(energymodel) \ 83 | -initialEnergy 10 \ 84 | -rxPower 0.5 \ 85 | -txPower 1.0 \ 86 | -idlePower 0.0 \ 87 | -sensePower 0.3 \ 88 | -agentTrace ON \ 89 | -routerTrace ON \ 90 | -macTrace OFF \ 91 | -movementTrace ON 92 | 93 | # Node Creation 94 | 95 | set energy(0) 1000 96 | 97 | $ns node-config -initialEnergy 1000 \ 98 | -rxPower 0.5 \ 99 | -txPower 1.0 \ 100 | -idlePower 0.0 \ 101 | -sensePower 0.3 102 | 103 | set node_(0) [$ns node] 104 | $node_(0) color black 105 | set xx [expr rand()*400] 106 | set yy [expr rand()*400] 107 | $node_(0) set X_ $xx 108 | $node_(0) set Y_ $yy 109 | 110 | # configure the remaining 5 nodes with transmission range of 200m 111 | 112 | $ns node-config -adhocRouting $val(rp) \ 113 | -llType $val(ll) \ 114 | -macType $val(mac) \ 115 | -ifqType $val(ifq) \ 116 | -ifqLen $val(ifqlen) \ 117 | -antType $val(ant) \ 118 | -propType $val(prop) \ 119 | -phyType $val(netif2) \ 120 | -channelType $val(chan) \ 121 | -topoInstance $topo \ 122 | -energyModel $val(energymodel) \ 123 | -initialEnergy 10 \ 124 | -rxPower 0.5 \ 125 | -txPower 1.0 \ 126 | -idlePower 0.0 \ 127 | -sensePower 0.3 \ 128 | -agentTrace ON \ 129 | -routerTrace ON \ 130 | -macTrace OFF \ 131 | -movementTrace ON 132 | for {set i 1} {$i < $val(nn) } {incr i} { 133 | 134 | set energy($i) [expr rand()*500] 135 | 136 | $ns node-config -initialEnergy $energy($i) \ 137 | -rxPower 0.5 \ 138 | -txPower 1.0 \ 139 | -idlePower 0.0 \ 140 | -sensePower 0.3 141 | set node_($i) [$ns node] 142 | $node_($i) color black 143 | set xx [expr rand()*400] 144 | set yy [expr rand()*400] 145 | $node_($i) set X_ $xx 146 | $node_($i) set Y_ $yy 147 | 148 | } 149 | 150 | proc finish {} { 151 | global ns tracefd namtrace 152 | $ns flush-trace 153 | close $tracefd 154 | close $namtrace 155 | # exec nam wsn.nam & 156 | exit 0 157 | } 158 | 159 | set tcp [new Agent/TCP] 160 | set sink [new Agent/TCPSink] 161 | $tcp set class_ 2 162 | 163 | $ns attach-agent $node_(1) $tcp 164 | $ns attach-agent $node_(5) $sink 165 | 166 | $ns connect $tcp $sink 167 | 168 | set ftp [new Application/FTP] 169 | $ftp attach-agent $tcp 170 | $ns at 0.5 "$ftp start" 171 | $ns at 4.5 "$ftp stop" 172 | 173 | for {set i 0} {$i < $val(nn)} {incr i} { 174 | $ns initial_node_pos $node_($i) 30 175 | } 176 | 177 | for {set i 0} {$i < $val(nn)} {incr i} { 178 | $ns at $val(stop) "$node_($i) reset" 179 | } 180 | 181 | $ns at $val(stop) "$ns nam-end-wireless $val(stop)" 182 | $ns at $val(stop) "finish" 183 | $ns at 5.1 "puts \"NS Exiting\"; $ns halt" 184 | 185 | $ns run 186 | --------------------------------------------------------------------------------