Thursday 12 May 2016

CELL ZOOMING TCL SCRIPTS NS2

### This simulation is an example of combination of wired and wireless  topologies.
  global opt
  set opt(chan)             Channel/WirelessChannel       ;# set the channel
  set opt(prop)             Propagation/TwoRayGround     ;# define the propagation
  set opt(netif)            Phy/WirelessPhy               ;# set the physical medium as wireless 
  set opt(mac)              Mac/802_11                   ;# Media Acess Control Protocol
  set opt(ifq)              Queue/DropTail/PriQueue       ;# Queue status
  set opt(ll)               LL                            ;# Link Layaer
  set opt(ant)              Antenna/OmniAntenna           ;# Antenna
  set opt(x)                250                                     
  set opt(y)                250 
  set opt(ifqlen)           50  
  set opt(nn)             6                      
  set opt(adhocRouting)   DSDV                     
  set opt(cp)             ""                       
  set opt(sc)             "../mobility/scene/scen-3-test"
  set opt(stop)           20                          
  set num_wired_nodes     2
  set num_bs_nodes        2

#set the simulator instance
  set ns_ [new Simulator]

# set up for hierarchical routing
  $ns_ node-config -addressType hierarchical    ;# set the node object to the have address                                                type as hierarchical
  AddrParams set domain_num_ 3                  ;# number of domains        
  lappend cluster_num 2 1 1                       ;# number of clusters in each domain
  AddrParams set cluster_num_ $cluster_num     
  lappend eilastlevel 1 1 4 4                     ;# number of nodes in each cluster
  AddrParams set nodes_num_ $eilastlevel 
# Creating trace file
  set tracefd  [open firstphase.tr w]
  $ns_ trace-all $tracefd
# Creating  nam file
  set namtracefd [open firstphase.nam w]
  $ns_ namtrace-all $namtracefd
  set topo   [new Topography]
  $topo load_flatgrid $opt(x) $opt(y)
# god needs to know the number of all wireless interfaces
  create-god [expr $opt(nn) + $num_bs_nodes]
#create wired nodes
  set temp {0.0.0 0.1.0}       
  for {set i 0} {$i < $num_wired_nodes} {incr i} {
      set W($i) [$ns_ node [lindex $temp $i]]
            $W($i) color green
            $ns_ at 0.0 "$W($i) color green"
  }   
# Node configuration
  $ns_ node-config -adhocRouting $opt(adhocRouting) \
                   -llType $opt(ll) \
                   -macType $opt(mac) \
                   -ifqType $opt(ifq) \
                   -ifqLen $opt(ifqlen) \
                   -antType $opt(ant) \
                   -propInstance [new $opt(prop)] \
                   -phyType $opt(netif) \
                   -channel [new $opt(chan)] \
                   -topoInstance $topo \
                   -wiredRouting ON \
                   -agentTrace ON \
                   -routerTrace OFF \
                   -macTrace OFF \ 
# Base station creation
  set temp {1.0.0 1.0.1 1.0.2 1.0.3 2.0.1 2.0.2 2.0.3} 
  set BS(0) [$ns_ node [lindex $temp 0]]
  set BS(1) [$ns_ node 2.0.0]
  $BS(0) random-motion 1
  $BS(1) random-motion 1
  $BS(0) set X_ 100.0
  $BS(0) set Y_ 150.0
  $BS(0) set Z_ 0.0
  $BS(1) set X_ 280.0
  $BS(1) set Y_ 130.0
  $BS(1) set Z_ 0.0
#configure for mobile nodes
  $ns_ node-config -wiredRouting OFF
  for {set j 0} {$j < 3} {incr j} {
    set node_($j) [ $ns_ node [lindex $temp \ [expr $j+1]] ]
    $node_($j) base-station [AddrParams addr2id [$BS(0) node-addr]]
     $node_($j) color yellow
            $ns_ at 0.0 "$node_($j) color yellow"
  }
   for {set j 3} {$j < $opt(nn)} {incr j} {
    set node_($j) [ $ns_ node [lindex $temp \ [expr $j+1]] ]
    $node_($j) base-station [AddrParams addr2id [$BS(1) node-addr]]
     $node_($j) color red
            $ns_ at 0.0 "$node_($j) color red"  }
#Procedure for node positions
 proc node_position {i x y z } {
           global ns node_ X_
           $node_($i) set X_ $x
           $node_($i) set Y_ $y
           $node_($i) set Z_ $z }
#set the positions of nodes
 node_position 0   1.0   1.0 0.0
 node_position 1  70.0  70.0 0.0
 node_position 2  40.0  40.0 0.0
 node_position 3 290.0  10.0 0.0
 node_position 4 390.0  30.0 0.0
 node_position 5 320.0  80.0 0.0
#create links between wired and BS nodes
  $ns_ duplex-link $W(0) $W(1) 5Mb 20ms DropTail
  $ns_ duplex-link $W(1) $BS(0) 5Mb 65ms DropTail
  $ns_ duplex-link $W(1) $BS(1) 5Mb 70ms DropTail
  $ns_ duplex-link-op $W(0) $W(1) orient down
  $ns_ duplex-link-op $W(1) $BS(0) orient left-down
  $ns_ duplex-link-op $W(1) $BS(1) orient right-down
 # setup TCP1 connections
  set tcp1 [new Agent/TCP]
  $tcp1 set class_ 2
  set sink1 [new Agent/TCPSink]
  $ns_ attach-agent $node_(0) $tcp1
  $ns_ attach-agent $W(0) $sink1
  $ns_ connect $tcp1 $sink1
  set ftp1 [new Application/FTP]
  $ftp1 attach-agent $tcp1
  $ns_ at 1 "$ftp1 start"
# setup TCP1 connections
  set tcp [new Agent/TCP]
  $tcp set class_ 2
  set sink [new Agent/TCPSink]
  $ns_ attach-agent $node_(4) $tcp
  $ns_ attach-agent $W(0) $sink
  $ns_ connect $tcp $sink
  set ftp [new Application/FTP]
  $ftp attach-agent $tcp
  $ns_ at 9.5 "$ftp start"
 # set the size of the mobile nodes
  for {set i 0} {$i < $opt(nn)} {incr i} {
      $ns_ initial_node_pos $node_($i) 10   }
  # Telling nodes when the simulation ends
  for {set i } {$i < $opt(nn) } {incr i} {
      $ns_ at $opt(stop) "$node_($i) reset";  }
  #stop procedure:
  proc finish {} {    global ns_ tracefd namtracefd
    $ns_ flush-trace
    close $tracefd
    close $namtracefd
    exec nam firstphase.nam &   }
  # Ending nam and the simulation
  $ns_ at $opt(stop) "$ns_ nam-end-wireless $opt(stop)"
  $ns_ at 12.9 "$ftp1 stop"
  $ns_ at 25.5 "$ftp stop"
  $ns_ at $opt(stop) "finish"
  $ns_ at $opt(stop) "puts \"end simulation\"; $ns_ halt"
  puts "Starting Simulation..."
   $ns_ run

No comments:

Post a Comment