Selasa, 17 Januari 2012

Access point di NS-2

#
# ======================================================================
# Define options
# ======================================================================
#
  set opt(chan)       Channel/WirelessChannel
  set opt(netif)      Phy/WirelessPhy
  set opt(mac)        Mac/802_11
  set opt(ifq)        Queue/DropTail/PriQueue
  set opt(prop)       Propagation/Shadowing
  set opt(ll)         LL
  set opt(ant)        Antenna/OmniAntenna
  set opt(x)          100       ;# X dimension of the topography
  set opt(y)          100       ;# Y dimension of the topography
  set opt(ifqlen)     50        ;# max packet in ifq
  set opt(seed)       0.0    ;# seed for ns-2 rand()
  set opt(nn)         3        ;# how many wireless nodes are simulated
  set opt(rp)         AODV ;# No Routing protocol
  set opt(stop)       20      ;# simulation time

# UDP Agent
Agent/UDP set packetSize_         2000     ;# now the default maximum segment size is 2000 bytes
                                           ;# so the 1500 byte packet is not fragmented
Mac/802_11 set RTSThreshold_      3000     ;# disable RTS/CTS
Mac/802_11 set ShortRetryLimit_   4
Mac/802_11 set CWMin_             31
Mac/802_11 set CWMax_             1023

# FHSS (IEEE 802.11)
Mac/802_11 set SlotTime_       0.000020
Mac/802_11 set SIFS_           0.000010
Mac/802_11 set PreambleLength_       64
Mac/802_11 set PLCPHeaderLength_    128
Mac/802_11 set PLCPDataRate_      1.0e6
Mac/802_11 set dataRate_          11.0e6
Mac/802_11 set basicRate_         2.0e6

# Physical layer
Phy/WirelessPhy set CPThresh_ 9999 ;# I put a high value so as if 2 stations transmit simultaneously towards the AP a collision would definitely occur
Phy/WirelessPhy set CSThresh_ 9.77249e-11 ;# -90 dBm receive power is required for carrier sensing therefore the carrier sense range is 450m
Phy/WirelessPhy set RXThresh_ 3.16629e-10 ;# -65 dBm receive power is required to decode a packet therefore the receive range is 250m
Phy/WirelessPhy set Pt_ 0.2818
Phy/WirelessPhy set freq_ 2.4e+9



#
# ======================================================================
# Main Program
# ======================================================================
#
  set ns_                       [new Simulator]
  set god_                      [create-god [expr $opt(nn)+1]]
 
  set tracefd                   [open out.tr w]
 
  $ns_ trace-all $tracefd
 
  set tracenam                   [open out.nam w]
  $ns_ namtrace-all-wireless $tracenam $opt(x) $opt(y)
 
  set simDataRate [Mac/802_11 set dataRate_]
#
#-----------------------------------------------------------------------
#
  set topo [new Topography]
  $topo load_flatgrid $opt(x) $opt(y)

  set chan_ [new $opt(chan)]

set namtrace      [open out.nam w]   

$ns_ trace-all $tracefd
$ns_ namtrace-all-wireless $namtrace 100 100

 
  $ns_ node-config -adhocRouting $opt(rp) \
                   -llType $opt(ll) \
                   -macType $opt(mac) \
                   -ifqType $opt(ifq) \
                   -ifqLen $opt(ifqlen) \
           -propType $opt(prop) \
                   -antType $opt(ant) \
                   -phyType $opt(netif) \
           -topoInstance $topo \
                   -wiredRouting ON \
                   -agentTrace ON \
                   -routerTrace OFF \
                   -macTrace ON \
                   -movementTrace ON \
                   -channel $chan_ \
       
#
#------------------------------------------------------------------------
#
  set degrees [expr 360/$opt(nn)]
  set rad [expr $degrees*3.1415927/180]

  $ns_ node-config   -wiredRouting OFF

#CREIAMO UN NODO SPECIFICANDO L'INDIRIZZO DOPO IL METODO NODO COME PARAMETRO]
  set Access_Point [$ns_ node 0]

  $Access_Point random-motion 0

  $Access_Point set X_ 30.0
  $Access_Point set Y_ 30.0
  $Access_Point set Z_ 0.0
  $ns_ at 0 "$Access_Point setdest 30. 30. 1000"
 
  puts "Access Point coord. X=[$Access_Point set X_] Y=X=[$Access_Point set Y_]"     

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

#CREIAMO I NODI SPECIFICANDO GLI INDIRIZZI 0.0.x CHE ABBIAMO MEMORIZZATO NEL VETTORE addr_domain
      set node_($j) [$ns_ node [expr $j+1]]
      $node_($j) random-motion 0
      $node_($j) set X_ 30
      $node_($j) set Y_ 30
  }

#I NODI MOBILI SONO POSTI A CERCHIO ATTORNO ALL'AP
  for {set j 0} {$j < $opt(nn)} {incr j} {
      set coseno [expr cos([expr $j * $rad])]
      set seno [expr sin([expr $j * $rad])]
      $ns_ at 0 "$node_($j) setdest [expr 30 + 20 * $coseno] [expr 30 + 20 * $seno] 1000."
      puts "placing node $j: X=[format %.2f [expr 30+20*$coseno]] Y=[format %.2f [expr 30+20*$seno] ]"
  }
#
#-------------------------------------------------------------------------
# TRAFFIC
#-------------------------------------------------------------------------
#
  proc attach-cbr-traffic { node sink size rate } {
        global ns_
        set udp [new Agent/UDP]
        $ns_ attach-agent $node $udp
        set traffic [new Application/Traffic/CBR]
        $traffic set packetSize_ $size
        $traffic set rate_ $rate
      $traffic attach-agent $udp
        $ns_ connect $udp $sink
        return $traffic
  }
 
#
#--------------------------------------------------------------------------
#
# Access Point -> Receiver, Mobile Nodes --> Transmitters

 set src_rate [expr ($simDataRate/$opt(nn)+30000)]
 puts "Rate delle sorgenti: [format %.2f $src_rate]"

 for {set j 0} {$j < $opt(nn)} {incr j} {
   set sink_($j) [new Agent/LossMonitor]
   $ns_ attach-agent $Access_Point $sink_($j)
   set src($j) [attach-cbr-traffic $node_($j) $sink_($j) 1500 $src_rate]
   $ns_ at 1.0 "$src($j) start"
   $ns_ at $opt(stop) "$src($j) stop"
 }

#
#Tracing...
 for {set j 0} {$j < $opt(nn)} {incr j} {
   set trace_file_($j) [open bw_node_$j.tr w]
   set bytes_file_($j) [open bytes_rx_$j.tr w]
  }

set total_byte 0

  proc record {node sink sink_f sink_trace_f} {

        global  ns_
        global total_byte
        set time 1.
        set bw_($node) [$sink set bytes_]
        set now [$ns_ now]
        puts $sink_trace_f "[format %.2f $now ] [expr double($bw_($node)*8.)/$time/1000] Kbps"
        incr total_byte $bw_($node)
        $sink set bytes_ 0
        $ns_ at [expr $now+$time] "record {$node} {$sink} {$sink_f} {$sink_trace_f}"
  }
#
#Finishing...
  proc finish {trace_file bytes_file} {
        global ns_ tracefd total_byte opt
        $ns_ flush-trace
        close $trace_file
        close $bytes_file
        close $tracefd
        puts "Total throughput [format %.2f [expr $total_byte*8./($opt(stop)-1)/1000000.]] Mbps"
    exec nam out.nam
        exit 0
  }

   for {set j 0} {$j < $opt(nn)} {incr j} {
        $ns_ at 0.100 "record {$j} {$sink_($j)} {$bytes_file_($j)} {$trace_file_($j)}"
        $ns_ at $opt(stop) " finish {$trace_file_($j)} {$bytes_file_($j)}"
}
#
#------------------------------------------------------------------------------
# Tell nodes when the simulation ends
#------------------------------------------------------------------------------
#
  for {set i 0} {$i < $opt(nn) } {incr i} {
      $ns_ at $opt(stop).001 "$node_($i) reset";
  }
  $ns_ at  $opt(stop).0002 "puts \"NS EXITING!\" ; $ns_ halt"
 


$ns_ run

Tidak ada komentar:

Posting Komentar