# Define options
set opt(umtsType)		NodeB				   ;# Umts node UE/NodeB
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/UmtsNodeb		   ;# MAC type
set opt(ifq)            Queue/DropTail/BsFCQueue   ;# interface queue type
set opt(ll)      	      LL/Nodeb                   ;# Link layer type
set opt(rlc)        	Rlc/UmtsNodeB		   ;# RLC layer type
set opt(ant)            Antenna/OmniAntenna        ;# antenna model
set opt(ifqlen)         5000                       ;# max packet in ifq
set opt(adhocRouting)   NOAH                       ;# routing protocol
set opt(phylayer)       Phy/UmtsNodeB		   ;# nodeB
set opt(x)      		100                         ;# x coordinate of topology
set opt(y)     		100                         ;#y coordinate of topology
set opt(seed)		    0.0                        ;#seed for random number gen.
set opt(cp)             "ex1-traffic"
set opt(sc)             "ex1-scen"
set opt(start)     		0.15
set num_bs_nodes        1
set num_wired_nodes      0
set opt(nn)             2                         ;# number of mobilenodes

Phy/Umts set verbose_ 0
Phy/UmtsNodeB set verbose_ 0

Mac/Umts set verbose_ 0
Mac/UmtsNodeb set verbose_ 0

Rlc/Umts set rlcfragsz_  20
Rlc/Umts set rlcverbose_ 0
Rlc/Umts set rlctime_ 80ms
Rlc/UmtsNodeB set rlcfragsz_  20
Rlc/UmtsNodeB set rlcverbose_ 0
Rlc/UmtsNodeB set rlctime_ 80ms

Queue/DropTail/BsFCQueue set verbose_ 0
Queue/DropTail/FCQueue set verbose_ 0

LL/UE set verbose_ 1
LL/Nodeb set verbose_ 1

set opt(ack)            0
set opt(frag) 			1
set opt(tr)     		"example1.tr"
set opt(namtr)       example1.nam
set opt(stop)   		2.3	 ;# time to stop simulation
set opt(rate)           128k

# ============================================================================
# check for boundary parameters and random seed
if { $opt(x) == 0 || $opt(y) == 0 } {
	puts "No X-Y boundary values given for wireless topology\n"
}
if {$opt(seed) > 0} {
	puts "Seeding Random number generator with $opt(seed)\n"
	ns-random $opt(seed)
}

#remove extrapkt headers else each pkt takes up too much space.
remove-packet-header LDP MPLS Snoop
remove-packet-header Ping TFRC TFRC_ACK
remove-packet-header Diffusion RAP IMEP
remove-packet-header AODV SR TORA IPinIP
remove-packet-header MIP HttpInval
remove-packet-header MFTP SRMEXT SRM aSRM
remove-packet-header mcastCtrl CtrMcast IVS
remove-packet-header Resv UMP Flags

# create simulator instance
set ns_   [new Simulator]

$ns_ set-umts-routing

# Create topography object
set topo   [new Topography]

$ns_ use-newtrace

set tracefd  [open $opt(tr) w]
$ns_ trace-all $tracefd
set namtracefd [open $opt(namtr) w]
$ns_ namtrace-all-wireless $namtracefd $opt(x) $opt(y)
set bwfile [open bw1 w]

# define topology
$topo load_flatgrid $opt(x) $opt(y)

# create God
set god_ [create-god [expr $opt(nn) + $num_bs_nodes]]

#set chan according to new ns
set chan1 [new $opt(chan)]

# Color for nam
$ns_ color 0 black
$ns_ color 1 white
$ns_ color 2 red
$ns_ color 3 green
$ns_ color 4 blue
#$ns_ color 5 chocolate
#$ns_ color 6 tan
#$ns_ color 7 gold
$ns_ color 8 magenta
$ns_ color 9 orchid
$ns_ color 10 SteelBlue
$ns_ color 11 orange
$ns_ color 12 HotPink
$ns_ color 13 brown
#$ns_ color 14 cyan
$ns_ color 15 purple

$ns_ rtproto DV

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

puts "Configuring NodeB nodes......."
# configure for base-station node
$ns_ node-config -adhocRouting $opt(adhocRouting) \
                 -llType $opt(ll) \
                 -rlcType $opt(rlc) \
                 -macType $opt(mac) \
                 -ifqType $opt(ifq) \
                 -ifqLen $opt(ifqlen) \
                 -antType $opt(ant) \
                 -propType $opt(prop) \
                 -phyType $opt(netif) \
				 -topoInstance $topo \
                 -wiredRouting ON \
				 -agentTrace ON \
                 -routerTrace OFF  \
                 -phyTrace ON  \
				 -movementTrace ON \
				 -channel $chan1 \
			-umtsType $opt(umtsType) \
			-phyLayer $opt(phylayer)



for {set j 0} {$j < $num_bs_nodes} {incr j} {
    set BS($j) [ $ns_ node ]
    $BS($j) random-motion 0               ;# disable random motion
}

#configure for UEs
set opt(phylayer)            Phy/Umts
set opt(umtsType)            UE
set opt(ll)			     LL/UE
set opt(rlc)        	     Rlc/Umts
set opt(mac)                 Mac/Umts
set opt(adhocRouting)        NOAH
set opt(ifq)                 Queue/DropTail/FCQueue

puts "Configuring UE nodes......."
# configure for UE node
$ns_ node-config -wiredRouting OFF \
			-adhocRouting $opt(adhocRouting) \
			-umtsType $opt(umtsType) \
			-llType $opt(ll) \
			-rlcType $opt(rlc) \
			-macType $opt(mac) \
			-phyLayer $opt(phylayer) \
			-ifqType $opt(ifq)

for {set j 0} {$j < $opt(nn)} {incr j} {
    set node_($j) [ $ns_ node ]
    $node_($j) random-motion 0               ;# disable random motion
}

#
# Define node movement model
#
puts "Loading connection pattern..."
source $opt(cp)

#
# Define traffic model
#
puts "Loading scenario file..."
source $opt(sc)

# Define node initial position in nam

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

    # 20 defines the node size in nam, must adjust it according to your scenario
    # The function must be called after mobility model is defined

    $ns_ initial_node_pos $node_($i) 8
}
for {set i 0} {$i < $num_bs_nodes} {incr i} {

    # 20 defines the node size in nam, must adjust it according to your scenario
    # The function must be called after mobility model is defined

    $ns_ initial_node_pos $BS($i) 20
}


puts "Trying to switch on UEs...."


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

# Tell all nodes when the simulation ends
for {set i 0} {$i < $opt(nn) } {incr i} {
    $ns_ at $opt(stop).0 "$node_($i) reset";
}
$ns_ at $opt(stop).0 "$BS(0) reset";

$ns_ at $opt(stop).0002 "puts \" \" ; $ns_ halt"
$ns_ at $opt(stop).0001 "stop"

proc stop {} {
    global ns_ tracefd namtracefd bwfile
    $ns_ flush-trace
    close $tracefd
    close $namtracefd
    close $bwfile
}

# informative headers for CMUTracefile
puts $tracefd "routing=$opt(adhocRouting)
stoptime=$opt(stop) ack=$opt(ack) frag=$opt(frag)"
puts $tracefd "num of mobiles=$opt(nn); x $opt(x) y $opt(y) "
puts $tracefd "seed $opt(seed) prop $opt(prop) ant $opt(ant)"

# One flow
proc calcBW {} {
	global ns_ lm bwfile
	set interval 0.01
	set bw [$lm set bytes_]
	set justnow [$ns_ now]
	puts $bwfile "$justnow [expr $bw/$interval*8]"
	$lm set bytes_ 0
	$ns_ at [expr $justnow+$interval] "calcBW"
}

puts "Starting Simulation..."
$ns_ at $opt(start) "calcBW"
$ns_ run

