You are on page 1of 10

#############################################################################

# Define simulation options

#############################################################################

set opt(chan) Channel/WirelessChannel ;# channel type

set opt(prop) Propagation/TwoRayGround ;# radio-propagation model

set opt(netif) Phy/WirelessPhy ;# network interface type

set opt(mac) Mac/802_11 ;# MAC type

set opt(ifq) Queue/DropTail/PriQueue ;# interface queue type

set opt(ll) LL ;# link layer type

set opt(ant) Antenna/OmniAntenna ;# antenna model

set opt(ifqlen) 50 ;# max packet in ifq

set opt(row) 1;

set opt(col) 2;

set opt(nn) [expr $opt(row)*$opt(col)] ;# number of mobilenodes

set opt(dist) 100; #distance between nodes

set opt(flow) [lindex $argv 2] ;# number of flows

set opt(sd) [lindex $argv 1] ;# number of shutdowns

set opt(offtime) 4.0 ;# shutdown time

set opt(rp) AODV ;# routing protocol

set opt(MaxX) 300; # X dimension of topography

set opt(MaxY) 400 ;# Y dimension of topography

set opt(stop) 100.0 ;# time of simulation end

set opt(energy) EnergyModel;

set opt(initialenergy) 11000;

set opt(txPower) 0.660;

set opt(rxPower) 0.395;


set opt(idlePower) 0.035;

Mac/802_11 set dataRate_ 2Mb;

Mac/802_11 set basicRate_ 1Mb;

Phy/WirelessPhy set CPThresh_ 10.0;

Phy/WirelessPhy set CSThresh_ 1.559e-11;

Phy/WirelessPhy set bandwidth_ 2e6;

Phy/WirelessPhy set Pt_ 0.2818;

Phy/WirelessPhy set freq_ 914e+6;

Mac/802_11 set dataRate_ 2.0e6;

#############################################################################

# Initialization and configuration

#############################################################################

#===================================

# ns simulator

#===================================

set ns_ [new Simulator]

#===================================

# NAM

#===================================

set nf [open out.nam w]

$ns_ namtrace-all $nf


$ns_ namtrace-all-wireless $nf $opt(MaxX) $opt(MaxY)

set chan_1_ [new $opt(chan)]

#===================================

# Trace File

#===================================

set tracefile [open out.tr w]

$ns_ trace-all $tracefile

#===================================

# Topologie

#===================================

set topo [new Topography]

$topo load_flatgrid $opt(MaxX) $opt(MaxY)

set god [create-god $opt(nn)]

#===================================

# color

#===================================

$ns_ color 1 Blue

$ns_ color 2 Red


$ns_ color 3 Green

$ns_ color 4 Yellow

#===================================

# nodes configuration

#===================================

$ns_ node-config -adhocRouting $opt(rp) \

-llType $opt(ll) \

-macType $opt(mac) \

-ifqType $opt(ifq) \

-ifqLen $opt(ifqlen) \

-antType $opt(ant) \

-propType $opt(prop) \

-phyType $opt(netif) \

-channelType $opt(chan) \

-topoInstance $topo \

-agentTrace ON \

-routerTrace ON \

-macTrace ON \

-movementTrace ON \

-energyModel $opt(energy) \

-initialEnergy $opt(initialenergy) \

-txPower $opt(txPower) \

-rxPower $opt(rxPower) \

-idlePower $opt(idlePower)
#===================================

# nodes initial positions

#===================================

# Define node initial position in nam

for {set i 0} {$i < $opt(row) } { incr i } {

for {set j 0} {$j < $opt(col) } { incr j } {

set nodenum [expr ($i*$opt(col))+($j)]

set node_($nodenum) [$ns_ node $nodenum]

set xpos [expr ($i+1)*$opt(dist)]

set ypos [expr ($j+1)*$opt(dist)]

$node_($nodenum) set X_ $xpos

$node_($nodenum) set Y_ $ypos

$node_($nodenum) set Z_ 0.0

$node_($nodenum) random-motion 0

$ns_ initial_node_pos $node_($nodenum) 20

#=============================================

# Node movement

#=============================================

#for {set i 0} {$i < $opt(nn)} {incr i} {


#$ns_ at 0 "$node_($i) setdest [expr rand()*$opt(MaxY)] [expr rand()*$opt(MaxX)] 0.0"

#}

#=============================================

# Nodes Color

#=============================================

$node_(0) color red

$node_(0) shape circle

$node_([expr $opt(nn)-1]) color green

$ns_ at 0.0 "$node_(0) color red"

$ns_ at 0.0 "$node_(0) label Emetteur"

$ns_ at 0.0 "$node_([expr $opt(nn)-1]) color blue"

$ns_ at 0.0 "$node_([expr $opt(nn)-1]) label Recepteur"

#===================================================

# Transport protocol

#===================================================

set max_fragmented_size 1024

set packetSize [expr $max_fragmented_size+20]

#===================================================

# UDP agent

#===================================================

set src_udp1 [new Agent/myUDP]

$src_udp1 set packetSize_ $packetSize

$src_udp1 set_filename wireless_sd


$ns_ attach-agent $node_(0) $src_udp1

#===================================================

# myEvalvid_Sink agent

#===================================================

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.005

$cbr0 attach-agent $src_udp1

$ns_ at 0.0 "$cbr0 start"

$ns_ at 4.5 "$cbr0 stop"

set dst_udp1 [new Agent/myEvalvid_Sink]

$ns_ attach-agent $node_([expr $opt(nn)-1]) $dst_udp1

$ns_ connect $src_udp1 $dst_udp1

$dst_udp1 set_filename wireless_rd

$dst_udp1 set fid_ 40 # packet color red

$src_udp1 set fid_ 40 # packet color red

set original_file_name st_a01

set trace_file_name video1.dat

set original_file_id [open $original_file_name r]

set trace_file_id [open $trace_file_name w]

set frame_count 0
while {[eof $original_file_id] == 0} {

gets $original_file_id current_line

scan $current_line "%d%s%d%d" seq_ frametype_ nexttime_ length_

set time [expr 1000*40]

if { $frametype_ == "I" } {

set type_v 1

set prio_p 0

if { $frametype_ == "P" } {

set type_v 2

set prio_p 0

if { $frametype_ == "B" } {

set type_v 3

set prio_p 0

if { $frametype_ == "H" } {

set type_v 1

set prio_p 0

puts $trace_file_id "$time $length_ $type_v $seq_ $max_fragmented_size"

incr frame_count

close $original_file_id
close $trace_file_id

set end_sim_time [expr 1.0 * 40 * ($frame_count) / 1000]

puts "$end_sim_time"

set trace_file [new Tracefile]

$trace_file filename $trace_file_name

set video1 [new Application/Traffic/myEvalvid]

$video1 attach-agent $src_udp1

$video1 attach-tracefile $trace_file

$ns_ at 0.0 "$video1 start"

$ns_ at $opt(stop) "$video1 stop"

#===================================

# Termination

#===================================

proc finish {} {

global ns_ tracefile namfile src_udp1 dst_udp1 nf debit

$ns_ flush-trace

close $tracefile

$src_udp1 closefile

$dst_udp1 closefile

puts "running nam..."

exec nam out.nam &

puts "simulation completed"

exit 0
}

for {set i 0} {$i < $opt(nn) } { incr i } {

$ns_ at $opt(stop) "\$node_($i) reset"

$ns_ at $opt(stop) "finish"

$ns_ at $opt(stop) "puts \"done\" ; $ns_ halt"

$ns_ run

You might also like