Sie sind auf Seite 1von 12

Code :

# cration d'un simulateur


set ns [new Simulator]
# cration du fichier de trace utilis par lafficheur et indication ns de l'utiliser
set tracefd [open trace.tr w]
set namtrace [open a.nam w]
$ns trace-all $tracefd

$ns namtrace-all-wireless $namtrace 500 400

#Dfinition de la topologie
set topo [new Topography]
$topo load_flatgrid 500 400

#procedure stop
proc stop {} {
global ns tracefd nf namtrace
$ns flush-trace
close $tracefd
close $namtrace
exec nam a.nam &
exit 0

}


#Dfinition des options dun lien sans fil
set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(netif) Phy/WirelessPhy
set val(mac) Mac/802_11
set val(ifq) Queue/DropTail/PriQueue
set val(ll) LL
set val(ant) Antenna/OmniAntenna
set val(ifqlen) 50
set val(nn) 3
set val(rp) AODV
set val(stop) 150
set val(x) 500
set val(y) 400

#creation de god
create-god $val(nn)


#Configuration des noeuds
set chann [new $val(chan)]
$ns node-config -adhocRouting $val(rp) \

$ns node-config -llType $val(ll) \
$ns node-config -macType $val(mac) \

$ns node-config \
-ifqType $val(ifq) \
$ns node-config \
$ns node-config -ifqLen $val(ifqlen) \
$ns node-config -antType $val(ant) \
$ns node-config -propType $val(prop) \
$ns node-config -phyType $val(netif) \

$ns node-config -channel $chann
$ns node-config -topoInstance $topo \
$ns node-config -agentTrace ON \
$ns node-config -routerTrace ON \
$ns node-config -macTrace ON \
$ns node-config -movementTrace OFF


#creation des noeuds
for {set i 0} {$i < $val(nn) } {incr i} {
set node($i) [$ns node ]
$node($i) random-motion 0 ;
}


#positionnement des noeuds
$node(0) set X_ 5.0
$node(0) set Y_ 5.0
$node(0) set Z_ 0.0
$node(1) set X_ 490.0
$node(1) set Y_ 285.0
$node(1) set Z_ 0.0

$node(2) set X_ 150.0
$node(2) set Y_ 240.0
$node(2) set Z_ 0.0

$ns at 10.0 "$node(0) setdest 250.0 250.0 3.0"
$ns at 20.0 "$node(1) setdest 45.0 285.0 5.0"
$ns at 10.0 "$node(2) setdest 480.0 300.0 2.0"


#creation des agents TCP et FTP

set tcp0 [new Agent/TCP]
set tcp1 [new Agent/TCPSink]

$ns attach-agent $node(0) $tcp0
$ns attach-agent $node(1) $tcp1
$ns connect $tcp0 $tcp1

set ftp [new Application/FTP]
$ftp attach-agent $tcp0


#Simulation
$ns at 10.0 "$ftp start"
$ns at 150.0 "stop"
$ns run



Exemple de fichier de trace :


-Le 1
er
champ s : send ou r :recieve
-Le 2eme champ correspond linstant dmission ou de rception
-Le 3eme champ au numro du nud
-Le 4eme champ au niveau du paquet : AGT, MAC, RTR..
-Le 5eme : au numro de squence du paquet
-Le 6eme : au protocole
-Le 7eme : la taille du paquet
Question n2 :
a)
tracage de debit moyen en fonction de temps du simulation :
#!/bin/bash
grep tcp trace.tr | grep MAC > fichier1

grep "^s" fichier1 | grep _0_ | cut -d" " fichier1,2,7,9 | head -n 6612 > fichier2

grep "^r" fichier1 | grep _1_ | cut -d" " -fichier1,2,7,9 > fichier3
join -13 -23 fichier2 fichier3 | cut -d" " -fichier1,3,6,7 | awk '{print echo ($3-$2),$4,$3}' |
awk '{print echo ($2/$1),$3}' > valeurs

Explication :
grep tcp trace: slction des lignes contenant "tcp"
grep MAC : slction des lignes contenant "MAC"
> fichier1:mise des lignes selectionnes dans le fichier fichier1
2eme commande:
grep "^s" fichier1| grep _0_ : On slcitonne les lignes de f1 contenant "s" et "_0_"
| cut -d" " fichier1,2,7,9 | : on extrait les champs suivants: s temps_emission num_sequence taille
head -n 6612 > fichier2 : , puis on garde les 6612 premires lignes pour avoir le
meme nombre de ligne que le dossier fichier3


tracacge :
gnuplot
>plot valeurs using 2 :1 with lines


b) la probabilit de perte = nombre de paquets perdus / nombre total de paquets
nombre de paquets perdus = taille totale des paquets perdus/ taille totale des paquets emis

grep "^D" trace.tr > traced.tr
grep tcp traced.tr > tracedd.tr
On execute ce script:
#! /bin/bash
somme=0;
while IFS=" " read a b c d e f g h i j
do
let somme=$somme+$h;
echo -e "$somme";
done < tracedd.tr
Explications:

Ona un dbit important puis il diminue

Pour TCP, lorsque un paquet est perdu, le mcanisme de contrle de congestion est initi,qui
diminue la vitesse denvoi des paquets.



Question n4:
set ns [new Simulator]



#number of nodes
set num_wired_nodes 2
set num_mobile_nodes 7
set num_bs_nodes 1 ;# number of base stations
set num_nodes [expr $num_wired_nodes + $num_mobile_nodes + $num_bs_nodes]
set bs_id $num_wired_nodes
# Parameter for wireless nodes
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_11e ;# MAC type
set opt(ifq) Queue/DTail/PriQ ;# interface queue type
set opt(ifqlen) 50
set opt(ll) LL ;# link layer type
set opt(ant) Antenna/OmniAntenna ;# antenna model
set opt(ifqlen) 50 ;# max packet in ifq
set opt(adhocRouting) NOAH ;# routing protocol
set opt(x) 670 ;# X dimension of the topography
set opt(y) 670 ;# Y dimension of the topography
set opt(seed) 0.0 ;# seed for random number gen.
set opt(stop) 250 ;# time to stop simulation

#set up for hierarchical routing
#(needed for routing over a basestation)
$ns node-config -addressType hierarchical
AddrParams set domain_num_ 2 ;# domain number
lappend cluster_num 2 2 ;# cluster number for each domain
AddrParams set cluster_num_ $cluster_num
lappend eilastlevel 1 1 4 4;# number of nodes for each cluster
AddrParams set nodes_num_ $eilastlevel

#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all-wireless $nf $opt(x) $opt(y)
set ntr [open out.tr w]
$ns trace-all $ntr

set chan [new $opt(chan)]
set topo [new Topography]
$topo load_flatgrid $opt(x) $opt(y)

# Create God
create-god [expr $num_mobile_nodes + $num_bs_nodes]

# creating wired nodes
set temp {0.0.0 0.1.0} ;# hierarchical addresses to be used

for {set i 0} {$i < $num_wired_nodes} {incr i} {
set W($i) [$ns node [lindex $temp $i]]

puts "wired node $i created"
$W($i) set X_ [expr $i * 200 + 10]
$W($i) set Y_ [expr $i * 100 + 10]
$W($i) set Z_ 0.0
}




# creating base station

#$ns node-config -adhocRouting $opt(adhocRouting) \
$ns node-config -adhocRouting DSDV \

$ns node-config -llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channel $chan \
-topoInstance $topo \
-wiredRouting ON \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \


set temp {1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3} ;# hier address to be used for
;# wireless domain -movementTrace OFF

set BS(0) [$ns node 1.0.0]
#$BS(0) random-motion 0 ;# disable random motion

puts "Base-Station node $bs_id created"
#provide some co-ord (fixed) to base station node
$BS(0) set X_ 500.0
$BS(0) set Y_ 150.0
$BS(0) set Z_ 0.0





# creating mobile nodes
$ns node-config -wiredRouting OFF


for {set i 0} {$i < $num_mobile_nodes} {incr i} {
set wl_node_($i) [$ns node [lindex $temp [expr $i+1]] ]
#$wl_node_($i) random-motion 0 ;# disable random motion
puts "wireless node $i created ..."
#$wl_node_($i) base-station [AddrParams addr2id [$BS(0) node-addr]]

$wl_node_($i) set X_ [expr $i * 100 + 75]
$wl_node_($i) set Y_ [expr $i * 150 + 80]
$wl_node_($i) set Z_ 0.0

}






#create links between wired and BS nodes
$ns duplex-link $W(0) $BS(0) 5Mb 2ms DropTail
$ns duplex-link $W(1) $BS(0) 5Mb 2ms DropTail

$ns duplex-link-op $W(0) $BS(0) orient down
$ns duplex-link-op $W(1) $BS(0) orient left-down



set tcp1 [new Agent/TCP]
$tcp1 set class_ 2
set sink1 [new Agent/TCPSink]
$ns attach-agent $wl_node_(0) $tcp1
$ns attach-agent $W(0) $sink1
$ns connect $tcp1 $sink1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1

$ns at 170.0 "$ftp1 start"

set tcp2 [new Agent/TCP]
$tcp2 set class_ 2
set sink2 [new Agent/TCPSink]
$ns attach-agent $W(1) $tcp2
$ns attach-agent $wl_node_(2) $sink2
$ns connect $tcp2 $sink2
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ns at 170.0 "$ftp2 start"


# Define node initial position in nam
for {set i 0} {$i < $num_mobile_nodes} {incr i} {
$ns initial_node_pos $wl_node_($i) 20
}

#Tell nodes when the simulation ends
for {set i 0} {$i < $num_mobile_nodes } {incr i} {
$ns at 10.0 "$wl_node_($i) reset";
}



$ns at $opt(stop).0 "$BS(0) reset";

$ns at $opt(stop).0002 "puts \"NS EXITING...\" ; $ns halt"
$ns at $opt(stop).0001 "stop"



proc stop {} {
global ns ntr nf
$ns flush-trace
close $ntr
close $nf
exec nam out.nam &
exit 0

}



$ns run