View difference between Paste ID: vckgaJzQ and v80siiq8
SHOW: | | - or go back to the newest paste.
1
#Exemplo dado em aula pelo professor.
2
#A documentação do ns2 possui uma boa explicação sobre cada uma das funções utiliza
3
#das aqui. Comando:
4
#	$ man ns
5
6
# Inicializa Flags
7
set run_nam 0
8
9
#Create a simulator object
10
set ns [new Simulator]
11
12
#Define different colors for data flows
13
$ns color 1 Blue
14
$ns color 2 Red
15
16
# ALTERADO #1
17
#Open the nam trace file
18
#set nf [open out-ex2.nam w]
19
#$ns namtrace-all $nf
20
# PARA #1
21
#Cria arquivos de saida
22
set f [open out.tr w]
23
$ns trace-all $f
24
set nf [open out.nam w]
25
$ns namtrace-all $nf
26
# END #1
27
28
# ALTERADO #2
29
#Define a ’finish’ procedure
30
#proc finish {} {
31
#	global ns nf
32
#	$ns flush-trace
33
#	#Close the trace file
34
#	close $nf
35
#	#Execute nam on the trace file
36
#	exit 0
37
#}
38
# PARA #2
39
#Procedimento de finalizacao
40
proc finish {} {
41-
    close $f#D6D6D6#3B3B3B
41+
42
    $ns flush-trace
43
    close $nf
44
    close $f
45
46
    if {$run_nam} {
47
      puts "running nam..."
48
      exec nam out.nam
49
    }
50
    exit 0
51
}
52
# END #2
53
54
#Create four nodes
55
set n0 [$ns node]
56
set n1 [$ns node]
57
set n2 [$ns node]
58
set n3 [$ns node]
59
60
#Create links between the nodes
61
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
62
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
63
$ns duplex-link $n3 $n2 1Mb 10ms SFQ
64
#'op' define uma operacao para o link, que no caso é uma definição de 
65
#posicionamento dele. 
66
$ns duplex-link-op $n0 $n2 orient right-down
67
$ns duplex-link-op $n1 $n2 orient right-up
68
$ns duplex-link-op $n2 $n3 orient right
69
70
#Monitor the queue for the link between node 2 and node 3
71
$ns duplex-link-op $n2 $n3 queuePos 0.5
72
73
#Create a CBR agent and attach it to node n0
74
set cbr0 [new Agent/CBR]
75
$ns attach-agent $n0 $cbr0
76
$cbr0 set packetSize_ 500
77
$cbr0 set interval_ 0.005
78
$cbr0 set fid_ 1
79
80
#Create a CBR agent and attach it to node n1
81
set cbr1 [new Agent/CBR]
82
$ns attach-agent $n1 $cbr1
83
$cbr1 set packetSize_ 500
84
$cbr1 set interval_ 0.005
85
$cbr1 set fid_ 2
86
87
#Create a Null agent (a traffic sink) and attach it to node n3
88
set null0 [new Agent/Null]
89
$ns attach-agent $n3 $null0
90
91
#Connect the traffic sources with the traffic sink
92
$ns connect $cbr0 $null0
93
$ns connect $cbr1 $null0
94
95
#Schedule events for the CBR agents
96
$ns at 0.5 "$cbr0 start"
97
$ns at 1.0 "$cbr1 start"
98
$ns at 4.0 "$cbr1 stop"
99
$ns at 4.5 "$cbr0 stop"
100
101
#Call the finish procedure after 5 seconds of simulation time
102
$ns at 5.0 "finish"
103
104
#Run the simulation
105
$ns run