set me "139.19.64.109"
set ns [new Simulator]

#$ns use-scheduler RealTime

#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red

#Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure

#read command line arguments

proc getCmdArgu {argc argv} {

global val

for {set i 0} {$i < $argc} {incr i} {

set arg [lindex $argv $i]

if {[string range $arg 0 0] != "-"} continue

set name [string range $arg 1 end]

set val($name) [lindex $argv [expr $i+1]]

}

}

getCmdArgu $argc $argv

set nodeCount $val(numNodes) 


proc finish {} {
    global ns nf
    $ns flush-trace
    #Close the NAM trace file
    close $nf
    #Execute NAM on the trace file
    exec nam out.nam &
    exit 0
}
set node999 [$ns node]

#Agent/UDP set packetSize_ 81920;
Agent/UDP set packetSize_ 1400;

puts "Creating the nodes and the agents with filters"
for {set i 0} {$i < $nodeCount} {incr i} {
    
    #defining the node, and the application for that node
    set node($i) [$ns node]
    set simnet($i) [new Application/Ns2SimNet "Ns2" "localhost" [expr $i+10000]]

    #defining the agents for each node, for live packets capturing and sending
    for {set j 0} {$j < $nodeCount} {incr j} {
	
	# n send udp agents per node, for n destinations...attach them to the node... and attach them to the simnet
	set send_udp_agent($i,$j) [new Agent/UDP]
	$ns attach-agent $node($i) $send_udp_agent($i,$j)
	$simnet($i) attach-udp-send-agent $send_udp_agent($i,$j) $j "localhost:[expr 10000+$j]"
    }
    
    # 1 recv udp agent per node...attach it to the node... and attach them to the simnet
    set recv_udp_agent($i) [new Agent/UDP]
    $ns attach-agent $node($i) $recv_udp_agent($i)
    $simnet($i) attach-udp-rcv-agent $recv_udp_agent($i)
    puts "Node $i created. Send and receive UDP agents created and attached to node $i."
}
puts "Nodes and agents created. Connecting them..."

Queue set limit_ 1000;

#connecting the agents and the nodes
for {set i 0} {$i < $val(numServers)} {incr i} {

    $ns duplex-link $node($i) $node999 1000Mb 0.04ms DropTail 
    set loss_module($i) [new ErrorModel]
    $loss_module($i) set rate_ $val(lossServerSide)
    $loss_module($i) drop-target [$ns set nullAgent_]
    $ns lossmodel $loss_module($i) $node($i) $node999
    
    puts "Connected node $i to others nodes."

    for {set j 0} {$j < $nodeCount} {incr j} {
	puts "attaching send_udp_agent($i,$j) $send_udp_agent($i,$j) to recv_udp_agent($j) $recv_udp_agent($j)"
	$ns connect $send_udp_agent($i,$j) $recv_udp_agent($j)
    }
    puts "Connected agents of node $i to the corresponding destination agents."
}

### Hetergenous connectivity of one replica
set k $val(numServers)
incr k -1
$ns duplex-link $node($k) $node999 $val(bwServerServer)Mb $val(latServerServer)ms DropTail 	

#connecting the agents and the nodes
for {set i $val(numServers)} {$i < $nodeCount} {incr i} {

    $ns duplex-link $node($i) $node999 1000Mb 0.04ms DropTail
    set loss_module($i) [new ErrorModel]
    $loss_module($i) set rate_ $val(lossClientSide)
    $loss_module($i) drop-target [$ns set nullAgent_]
    $ns lossmodel $loss_module($i) $node($i) $node999

    puts "Connected node $i to others nodes."

    for {set j 0} {$j < $nodeCount} {incr j} {
        puts "attaching send_udp_agent($i,$j) $send_udp_agent($i,$j) to recv_udp_agent($j) $recv_udp_agent($j)"
        $ns connect $send_udp_agent($i,$j) $recv_udp_agent($j)
    }
    puts "Connected agents of node $i to the corresponding destination agents."
}



$ns rtproto Static
$ns compute-routes
puts ""
#puts "\nRoute next hops are : "
puts "[$ns dump-routelogic-nh]"
puts "Distances Information"
puts "[$ns dump-routelogic-distance]"

$ns at 0.0 "$simnet(0) start-file \"$val(inputFile)\" \"$val(initFile)\" $val(protocol) $val(f)"



#$ns at 50.0 "$simnet(0) dump-stats"
puts "okey"
$ns run
