LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   how to write ns code that implements LEACH in a network (https://www.linuxquestions.org/questions/linux-general-1/how-to-write-ns-code-that-implements-leach-in-a-network-804147/)

_man_ouer 04-25-2010 04:47 PM

how to write ns code that implements LEACH in a network
 
hello,
I try to write a code that allows ns to simulate the operation of the leach protocol in a network but I can not find documentation that would allow me to do that.
if anyone can help me by telling me how should be the configuration of nodes and the base.

thank you

_man_ouer 05-02-2010 03:26 PM

Hi i tried to write a code that implement leach but it generate an empty .tr file

could you tell me where is my mistake please

also i know that i had to use a RCAgent but i don't know how to use it

Code:


proc traitement {nodes} {
global ns_ now bs

# les differents noeuds envoient a leur clusterhead
set agent [new Agent/RCAgent/LeachAgent]
$agent set sport_ 0
$agent set dport_ 0
$node_0 attach $agent 0
$node_2 attach $agent 1

set ftp_1 [new Application/FTP]
$ftp_1 attach-agent $agent
$ns at 2,3 "$ftp_1 start"


set tcp_1 [new Agent/TCP]
$tcp_1 set class_ 0
set sink_1 [new Agent/TCPSink]
$ns_ attach-agent $node_2 $tcp_1
$ns_ attach-agent $node_0 $sink_1
$ns_ connect $tcp_1 $sink_1
set ftp_1 [new Application/FTP]
$ftp_1 attach-agent $tcp_1
$ns at 2,3 "$ftp_1 start"

set tcp_2 [new Agent/TCP]
$tcp_2 set class_ 0
set sink_2 [new Agent/TCPSink]
$ns_ attach-agent $node_3 $tcp_2
$ns_ attach-agent $node_0 $sink_2
$ns_ connect $tcp_2 $sink_2
set ftp_1 [new Application/FTP]
$ftp_2 attach-agent $tcp_2
$ns at 2,4 "$ftp_2 start"

set tcp_3 [new Agent/TCP]
$tcp_3 set class_ 0
set sink_3 [new Agent/TCPSink]
$ns_ attach-agent $node_4 $tcp_3
$ns_ attach-agent $node_1 $sink_3
$ns_ connect $tcp_3 $sink_3
set ftp_3 [new Application/FTP]
$ftp_3 attach-agent $tcp_3
$ns at 2,3 "$ftp_3 start"


set tcp_4 [new Agent/TCP]
$tcp_4 set class_ 0
set sink_4 [new Agent/TCPSink]
$ns_ attach-agent $node_5 $tcp_4
$ns_ attach-agent $node_1 $sink_4
$ns_ connect $tcp_4 $sink_4
set ftp_4 [new Application/FTP]
$ftp_1 attach-agent $tcp_4
$ns at 2,5 "$ftp_4 start"

# les differents clusterhead envoient a la BS

set tcp_5 [new Agent/TCP]
$tcp_5 set class_ 0
set sink_5 [new Agent/TCPSink]
$ns_ attach-agent $node_0 $tcp_5
$ns_ attach-agent $base $sink_5
$ns_ connect $tcp_5 $sink_5
set ftp_5 [new Application/FTP]
$ftp_1 attach-agent $tcp_5
$ns at 2,5 "$ftp_5 start"

set tcp_6 [new Agent/TCP]
$tcp_6 set class_ 0
set sink_6 [new Agent/TCPSink]
$ns_ attach-agent $node_1 $tcp_6
$ns_ attach-agent $base $sink_6
$ns_ connect $tcp_6 $sink_6
set ftp_6 [new Application/FTP]
$ftp_6 attach-agent $tcp_6
$ns at 3 "$ftp_6 start"

}


# les options
set val(chan)  Channel/WirelessChannel  ;# channel type
set val(prop)  Propagation/TwoRayGround  ;# radio-propagation model
set val(netif)  Phy/WirelessPhy/LeachWirelessPhy          ;# network interface type
set val(mac)    Mac/802_11                ;# MAC type
set val(ifq)    Queue/DropTail/PriQueue  ;# interface queue type
set val(ll)    RCALinkLayer              ;# link layer type
set val(ant)    Antenna/OmniAntenna      ;# antenna model
set val(ifqlen) 30                        ;# max packet in ifq
set val(nn)    7                          ;#
set val(rp)        leach                          ;
set val(stop)  100;
set val(x)        400
set val(y)        300


set ns [new Simulator]
set tracefd [open manel.tr w]
$ns trace-all $tracefd
set nf [open namleach.nam w]
$ns namtrace-all-wireless $nf $val(x) $val(y)

#topographie
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)

# variable to track time
set now [$ns now]

#creat god
create-god $val(nn)

set all_node_liste[list]


proc create_BS { } {
global ns val topo

#la configuration de la station de base
$ns node-config -adgocRouting $val(rp)\
-llType        $val(ll)\
-macType        $val(mac)\
-ifqType        $val(ifq)\
-ifqLen                $val(ifqlen)\
-antType        $val(ant)\
-propType        $val(prop)\
-phyType        $val(netif)\
-channelType        $val(chan)\
-topoInstance        $topo\
-agentTrace        ON\
-routerTrace        ON\
-macTrace        ON\
-mouvementTrace        ON

set base [$ns node]
$base random_motion 0;
$base set X_ 210.0
$base set Y_ 280.0
$base set Z_ 0.0
$ns initial_node_pos $base 30

return $base
}

#create-common-node
proc create-common-node { } {
global ns val bs all_node_liste

#configuration des differents noeuds

$ns node-config -adgocRouting $val(rp)\
-llType        $val(ll)\
-macType        $val(mac)\
-ifqType        $val(ifq)\
-ifqLen                $val(ifqlen)\
-antType        $val(ant)\
-propType        $val(prop)\
-phyType        $val(netif)\
-channelType        $val(chan)\
-topoInstance        $topo\
-agentTrace        ON\
-routerTrace        ON\
-macTrace        ON\
-mouvementTrace        ON

set node_0 [$ns node]
$node_0 random_motion 1;
$node_0 set X_ 130.0
$node_0 set Y_ 130.0
$node_0 set Z_ 0.0

set node_1 [$ns node]
$node_1 random_motion 1;
$node_1 set X_ 300.0
$node_1 set Y_ 150.0
$node_1 set Z_ 0.0

set node_2 [$ns node]
$node_2 random_motion 1;
$node_2 set X_ 100.0
$node_2 set Y_ 90.0
$node_2 set Z_ 0.0

set node_3 [$ns node]
$node_3 random_motion 1;
$node_3 set X_ 220.0
$node_3 set Y_ 70.0
$node_3 set Z_ 0.0

set node_4 [$ns node]
$node_4 random_motion 1;
$node_4 set X_ 350.0
$node_4 set Y_ 200.0
$node_4 set Z_ 0.0

set node_5 [$ns node]
$node_5 random_motion 1;
$node_5 set X_ 270.0
$node_5 set Y_ 100.0
$node_5 set Z_ 0.0

lappend all_node_liste $node_0
lappend all_node_liste $node_1
lappend all_node_liste $node_2
lappend all_node_liste $node_3
lappend all_node_liste $node_4
lappend all_node_liste $node_5

$ns initial_node_pos $node_0 30
$ns initial_node_pos $node_1 30
$ns initial_node_pos $node_2 30
$ns initial_node_pos $node_3 30
$ns initial_node_pos $node_4 30
$ns initial_node_pos $node_5 30

}

#transmettre $all_node_liste
proc transmettre {$all_node_liste} {
global now ns val
while { $now < $val(stop) }
{
incr now
traitement $n_liste
puts "inside transmettre proc $now"
$ns at $now "transmettre n-liste"
}
}


#$ns_ at $val(stop) "stop"
$ns at [expr $val(stop)+0.01] "puts \"NS EXITING...\" ; $ns halt"

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

puts "starting simulation"
$ns run



All times are GMT -5. The time now is 11:33 AM.