Anda di halaman 1dari 158

NS

(Network Simulator 2

'%

'( & !" #

$% #

mehdi_vojdani@yahoo.com
)' " *

+ !,

-.

/0 12 3

7-8

&456

4;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ns
35;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1=

:9
3 *> :<

42;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ns ) ' '% 4*# = @ 0 6 A :?


55;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Xgraph

% C

(D 6 + :B

GE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ns ) 3
J9;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;I!
3

!> ) (.!D

H!6 # L M.

) F$% :E

1)
1 "

H . 1 :G

(D 6

-.

:K

NJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;I!
9PK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;I!
9<P;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;I!
9?E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;I!

I!

O >#) + :J

Q1"1R# H ) MobileIP + :N

9?<;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;I!

*>

31 )
31 )

9?N;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;I!

H!6 # 1 " 4 6 + :9P


3 2 L M.
*> )

1 " 4 6 + :99

1 )) .

("' :9<

9BJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ns ) S )

>!. = :9?

L 456

ns
WinXP
;' $ % 1D

2 '! 0 1D

)6

TM6 ' ) '5 2 U V. ) L

;' ) ) Cygwin W5

(D 6 4>

$ 0 1

2 '! 0 W5 )

) cygwin.exe

1D

ns
'

'.

X ' ) '5 2

-. Z1$Y 1 ! 2 ;'! 0 U V. )

;'! 0 -.

6 [!=

;' $

6 [!= @ 1 ^ All ' \$]

;' $ R0 c:/cygwin /usr/local )


# '! 0 )

4*

' $ !(# 1D

) ns-allinone-2.29.2.tar.gz ' L

) tar xvfz ns-allinone-2.29.2.tar.gz

0 % ) Cygwin

;' 0 _) C )10` \ (6 )

Ha ) install W5

(D 6 1% ls )1.

'

/]R

6\ 6

)4 6

) ns-allinone-2.29 \ (6 ns W5
;'! 0 ) ) ./install )1.

;' '3 1C @' W5

bR

;'! 0

)0

& 1 '3 1C

;'! 0 U V. ) Advanced 2

.6) My Computer

+ = 61 W5

properties \$]

W5

'Y

'Y

10

;'! 0 U V.
'!

$.]!

%1# ;'! 0 ) ) 0 @ $3 ) )

3 !!d. I!e #
3

) Environment Varible $0 bR

2 ;'!3 ) 6 ) Edit $0

0 U V. ) path L

;' $ '% I3 ; ) '0 3 0


/usr/local/ns-allinone-2.29/bin
/usr/local/ns-allinone-2.29/tcl8.4.11/unix
/usr/local/ns-allinone-2.29/tk8.4.11/unix
;' $

'3

) 4$^

4* )

11

;'! 0 ) )

)1.

.6) Cygwin '

'Y 2 )

Export LD_LIBRARY_PATH=/usr/local/ns-allinone-2.29/otcl-1.11
Export LD_LIBRARY_PATH=/usr/local/ns-allinone-2.29/lib
Export TCL_LIBRARY_PATH=/usr/local/ns-allinone-2.29/tcl8.4.11/library

12

;\ ns-simple.tcl 4 6

I $ % %

) $

;' $ !Cg ns-simple.tcl 4 6 ) ) 3'0 @ '! 1#


!Cg C:\Cygwin\usr\local 4*

'

h) )

) 1$

' 4 0 456 !$3


Y ' $

;'! 0 L > )
;'
@1a

'3 1V @

!> H 0 I ) '5

/usr/local \ (6

(. ) L f

3 '0

!Cg /usr/local ) )
( 2 '0

Lf L

L ;:'! 0

0 % ) Cygwin '.

8 C !a 0 ' '!3 1C ;'! 0 ) ) ns ns-simple.tcl )1.


;\]! @ 0 XWin

%
)

13

4 6 \ (6

) ;'

C:\cygwin\usr\X11R6\bin C

; 0 '!3 1C '3

+=

$ '

.6) My Computer

U1C !a $3 2 ' $ % ) Xwin.exe

'! 0 \6 ) ! )

)' 3

3 d!= ) 0 \

*$ .>"

14

) C:\cygwin\bin \ (6

(D 6 '

$ ;'

.6 ) 0

(. 1R 0 0 \ 4!"

d!=

;'! 0 R0 C:\cygwin\usr\X11R6\bin
) $ 4*$ ' 1#

@ 6 / # ;'! 0

) X11 '! 1. ns-simple.tcl L f @ 0

'Y I3 '
;' 0 4

export DISPLAY=:0.0
;'! 0 % ) ns ns-simple.tcl )

15

'3 1C @

>! L i

)18

' '3 1C 3 j nam + = L

16

&ns ) 0 c
0 \ tcl 4 6

tclscript @ ) 0 '! 0 , ) 1C ) 0 ns <tclscript> )1.

'!"1# ) 3 % C

0 \ tcl \R *

.]
;'

!a $3 ;' 0

nam

!>

kV

' c

) $

!>

trace 4 6 H \

1)

*$ 0 ' 0

&(network animiator)nam c
0 \ nam trace 4 6 H
L i
@

)18 '! 1#
)

nam-file

$ 0 \ tcl

% ) nam<nam-file> )1.
!> H % C 48

l15V *> @ 0 ) 0 ' $

'3

$!M.]

) nam + =

) nam '! 1#
\

'

+ ns T 1#

4* ) ;' $ '3
;' '3 1C

17

&Tcl \R *
3 ) \ 6

'! 1#

$ ;I! 0

;I ) `2
nam

1C !"

(.R *

example1.tcl ) @ 1C L f

% C . 1
;'!3 ) '

% C 4 6 H bR

)$

!"

I! 0

template H

$#

;'!] 1 ) 1C
+ )

(m )

!> c 1

3 4> 0 '! 0 %1# ;' ' 3 1C .V ) 4 6

trace

!"
'. )

).
H

'. )

;I! 0

set ns [new Simulator]


set nf [open out.nam w]
$ns namtrace-all $nf
;\ nam c

trace 4 6 .]

finish L ) @ 0 6 A 'Y 2

proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
+

) 0 a 3'0 '!$(- 0 ' '3 1C ) *

. ;'! > Z

3'0 n)

13'

$
;' 3

;' 0 %

!> 5.0 ! o ) ) finish L ) 0 ' 12

!>

'Y TC

$ns at 5.0 "finish"


; 0 I!3 1C c

!>

C ) ;\ ns ) 3 'C) ' >

at )1.

18

$ns run
4 6 H % C ;'! 0 %

ns example1.tcl )1.

0 !Cg ) example1.tcl '! 1#

;' '3 1C out.nam


&' $

-. template 1 " @ 1 ^ '

0' $

#Create a simulator object


set ns [new Simulator]
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}

# Insert your own code for topology creation


# and agent definitions, etc. here

#Call the finish procedure after 5 seconds simulation time


$ns at 5.0 "finish"
#Run the simulation
$ns run

'3

) 4 0 '0

19

&
p!

q Otcl ]- H

V .0 q 3 ' ) ' @
+

)0 0 )
')

1%

!>
V .0 ) 0

) 3 .] L M.
+ 0 a
h)

(.R *

!>

*("1" F 1#

%
*> !

a 0 ' 1 H!6 # F
! )0

H 2 )

) ns '

*>

3 ) 'M

ns

;\

-.

) *>

' )'>

"

-.

*> !

!>

4* ) 0 1 $3
*>

D.V

*>

Yr

Q1"1=1# ;' 0 % ' ' 0


*

CZ

*("1" " Q ;' 3 $# C @


!

) $Y

' $ +

a ' 0c

3 !]

*("1" q *> H
s >! W

& '3

+ ))0

' 1.!

X
X

1C

ns

("1%

) 0 0 !!Y#

@1a ' 0
+

!]

))0

2 'C) )18

' 0 + ' '%

*("1"
$^

H '3 1V ) 0 0 .

! t V .0

*("1"

>! @ 1#

*>

'
ns

' '%

H . 1. 9

-.

'!u!= \

3 ' )

-.

;'.6

-# a

+ .<

*$ ) 0
;' 0
!>

-.
@

'0

20

') )
@ =

!>

' )

8) 0 )

;' 0

L Y6 W

(*>

!>

+!. @'

H 3 0 1

nam Z f ;' .]3

3 ' ) t!D0 ' *! L > )


Yr

1C 6

!> @

' ) '>

q' '

'>

1 a q 1$

*>
1C 6

Yr p !

V .0 q 3 ' ) ' @

0 + L ) H @ 1.!

!> ) 3 2 n 7# @'
&

) 0 @ @ 1 ^ OTcl

q Otcl ]- H

) ns I!.-2 )1r $3

1 a '3'! @

; 0 + MD @ 1.!

)%@

% C 4 6 '^ ' a ' )

;\

*>

ns )

0\

1 a CZ

1$ H

4* L f ;\

0 3 ) 'M ) 3 !d. @ 1.!

# Writing a procedure called "test"


proc test {} {
set a 43
set b 27
set c [expr $a + $b]
set d [expr [expr $a - $b] * $c]
for {set k 0} {$k < 10} {incr k} {
if {$k < 5} {
puts "k < 5, pow = [expr pow($d, $k)]"
} else {
puts "k >= 5, mod = [expr $d % $k]"
}
}
}
# Calling the "test" procedure created above
test

'!D0 $D0 ; !2
) >^ ]- # 1

) L )

'Y

v^ expr Q ; 1

-. L ) H c
-. @

@ 0 kV

Y# @ ' !!d.

proc $D0

) 'M H k!5V#
;' 0 %

set

!]-# ) 4 M

21

) 1C '

. 1 L ) '! 1#

1C 4 6 ) ; 1

& 1 '3 1C

4*

;\C = I!3 1C
h 0H 1 ^F # 0 +
instvar Q
0 1

-.

)1.

F #H

; 0 I!3 1C

% C)

% CLf

1C 6 $3

! u$3 ;\ 1 I!3 1C h 0 4C ) ) F #

v^ '

'

Y# 4> )

1$ !!d.

;'! 0

-. @
F # 2 \

(.R * ) I3 puts $D0

. 1

1C 6 @

w)
$3

-. instproc Q

!=

1$ !!d.
;1

# Create a child class of "mom" called "kid"

H +

Class $D0
Y#

w) ) superclass Q ;\ C++ @ ) this ' $3 self $D0. 1

# Create a class call "mom" and


# add a member function call "greet"
Class mom
mom instproc greet {} {
$self instvar age_
puts "$age_ years old mom say:
How are you doing?"
}

TM6 2

Y%

)
+

$3 h 0

instproc ' $3
@

)'= h 0 )

-. )'= h 0 @ 0 kV

22

# and overide the member function "greet"


Class kid -superclass mom
kid instproc greet {} {
$self instvar age_
puts "$age_ years old kid say:
What's up, dude?"
}
# Create a mom and a kid object set each age
set a [new mom]
$a set age_ 45
set b [new kid]
$b set age_ 15
# Calling member function "greet" of each object
$a greet
$b greet

e )1 F #

1$

1D%

1 ^ F 1#

1C 6

; 1 '3 1C

-.

% C I! 0 % ) Z \R *

&H !" H
_ ) $ns at 5.0 finish 4> \

.(

$ns run 4> '


;I! 0

Y# (

! new

1$ +

) \$]
! H !" H

2 L ;I] 1

$3

Y#

3'0 0 '
) 2

2
.

%1#

+ ) ;'! 0

23

set n0 [$ns node]


set n1 [$ns node]
;I! 0

45. I3

) 2

'Y TC) L ;' *!

+ ) n0 n1 2

Z )1.

$ns duplex-link $n0 $n1 1Mb 10ms DropTail


-. DropTail 8

0'0

;' . 1

45. I3

) 1Megabit '

) 4 0 '0 ;'! 0 %

#Create a simulator object


set ns [new Simulator]
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}
#Create two nodes
set n0 [$ns node]
set n1 [$ns node]
#Create a duplex link between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail

(=

6X

H !"

!Cg example1a.tcl

2 Z TC )
)4 6

;' 0

24

#Call the finish procedure after 5 seconds of simulation time


$ns at 5.0 "finish"
#Run the simulation
$ns run
; 1 '3 1C

4*

nam )

% C % b=

&
ns ) ;I

H
0L )

n0 2

n1 2

n0 2

0 0 I!3 1C + agent

L ) I!3 1C
H

'Y 2 ) ;' 1

;' 0
#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

\6 ) )

;' .]3
L )
n1 )

L )
(" f .>"
agent

agent H

25

constant ) \ o \! x
;1

kV

0 CBR H!6 # ' 0 +

: ! o ) \0 = \]

'

;' 0

L )

+ n1 2

0' 0

+ UDP agent H Z O1rC


-. :bit rate

\0 = ' 'Y O1rC ) ;' 0


H!6 # .6 2

NULL agent H

TC

set null0 [new Agent/Null]


$ns attach-agent $n1 $null0
;I! 0

45. I3

) 2

$ns connect $udp0 $null0


;I! 0

kV

$ns at 5.0 "finish" TC [!= ) H!6 # L )

1# c

$ns at 0.5 "$cbr0 start"


$ns at 4.5 "$cbr0 stop"
;' $
#Create a simulator object
set ns [new Simulator]
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}

'3

) ) 4 0 '0 L

26

#Create two nodes


set n0 [$ns node]
set n1 [$ns node]
#Create a duplex link between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
#Create a Null agent (a traffic sink) and attach it to node n1
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0
#Schedule events for the CBR agent
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run

) H!6 #

3 ) 1$ C + ) @ 1#

.>" ;' '!3 1C

) % C nam )

1$

b=

; 0 '3

27

:Topology Q1"1=1#
L ;'!

Q1"1R#

' >$3 '

$ 3

!> ) ' 0 + )
;I!

!>

1)

H $

2 B I!3 1C

D> L f )
'Y L f )

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
;' 0

+ 3 2 ! 6 X H !" ?

'0

$ns duplex-link $n0 $n2 1Mb 10ms DropTail


$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n3 $n2 1Mb 10ms DropTail
) 3 L . 0 C '!3 1V 2 ; 1 '3 1V U `% @ ' a % C nam ) '! 0 %
)

'! 0 6 A )

3'0 b= ;'! 0 kV

\R * ) '! 1#

!Cg ) 3'0
'!

2L

[ $ 1!

;'! 0 %
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up

28

$ns duplex-link-op $n2 $n3 orient right


; 1 '3 1C

% C '! 0 % ) nam '! 0 % )

&:Events 3 'C)
l 5.C n1 n0

3 2

) (

I! 0 + CBR H!6 # UDP agent

I!3 1C

\$]

;I!3' n3 2 NULL agent H bR ;I!3


#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
#Create a UDP agent and attach it to node n1
set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
# Create a CBR traffic source and attach it to udp1

29

set cbr1 [new Application/Traffic/CBR]


$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0
45. Null agent

;I! 0

) CBR agent

$ns connect $udp0 $null0


$ns connect $udp1 $null0
CBR ! u3 ;' 0

1.

ByE ! o )

'0L )

PyE > o ) L CBR agent 0 I ) '5

c
;' 0

1. ByP ! o )

agent

c ) 9yP ! o )

$ns at 0.5 "$cbr0 start"


$ns at 1.0 "$cbr1 start"
$ns at 4.0 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"

n2 2 bR

\0 = <PP H !"
n2

n1 n2

H!6 # n0,n1

3 ! o 3 ) \-2 @ 1#
n0

' n3 n2 ! '
)1.!

1% n2 2 \$
H !"

' # > 7

! o ) \!
@ %

% \R * H!6 #

; 0 '3 1C L

PyJ '

(= @1a .>" ;' '3 1C \!

\ {) *

3 2

n3 2 \$

2
) H!6 #

(= b= ; 0 ' 3 1C L ) \ EPP

9yG ' n3 n2 ! D0 '

3 nam ) ;' ' 3 1C z` (.0 = C b= \

'

(= b= ;' '3 1C
! o

\!

;'! 0 H!D0 ) ( @ 0
; 0 I!3 1C @ ! (

( @ 0 $.

) )0 )

30

& ( % ) `2 @
;'! 0 6 A 3 CBR agent

Y#

TC

$udp0 set class_ 1


$udp1 set class_ 2
j . flow id

;\
;'! 0 6 A :

!> I!e # ' 0 +

!>

class_ fid_ . ) =
'Y )

3'0 *# L

$ns color 1 Blue


$ns color 2 Red
;'! 0

) % 1$

;\

4* ) ; !2

e ) flow 3

!!d. n3 n2 ! '

) '%

(= F 1# '! 0

'3

( ) 3'0

0 )1X @ $3

& 8 H @ 0 )1.!
;'! 0 6 A n2 , n3 H !" !
$ns duplex-link-op $n2 $n3 queuePos 0.5

8 H @ 0 )1.!

TC '0 ' TM6 $

31

; 1 '3 1C

) 0 I *! *6 ;'! >! I3 ) (.0 = @' z` '! 1#


@ %

(.0 = TM6 0 \ s )

(stochastic fair
% 4* ) 0

s)
;'!3 !!d# )

#
1

TC ) 0

3
;'! 0

% e7" ' a 'Y

. ;' '!3 1C ) 8 ) (.0 = @'

fairness s ) ' '3 1C z`


z`7

!>

(.0 =

@ %

3 .] z`

;:' 0

-. n2 ,n3 ! H !"

z`

#Create a simulator object


set ns [new Simulator]
#Define different colors for data flows

) ) [V

$
)18
) L

queuing)SFQ
; 1 '3 1C

&I . 1

4*

4 0 '0 L

32

$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
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#Create links between the nodes
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n3 $n2 1Mb 10ms SFQ
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
#Monitor the queue for the link between node 2 and node 3
$ns duplex-link-op $n2 $n3 queuePos 0.5
#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$udp0 set class_ 1
$ns attach-agent $n0 $udp0

33

# Create a CBR traffic source and attach it to udp0


set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
#Create a UDP agent and attach it to node n1
set udp1 [new Agent/UDP]
$udp1 set class_ 2
$ns attach-agent $n1 $udp1
# Create a CBR traffic source and attach it to udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1
#Create a Null agent (a traffic sink) and attach it to node n3
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0
#Connect the traffic sources with the traffic sink
$ns connect $udp0 $null0
$ns connect $udp1 $null0
#Schedule events for the CBR agents
$ns at 0.5 "$cbr0 start"
$ns at 1.0 "$cbr1 start"
$ns at 4.0 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run

34

35

456

1=
|)

3 *> ) ; 0 I!3 1C ) 0 3 2

3 *>
C )18 )
;)

!]

1% !

1=
C@*

3 *>

' '3 1C + '. )


0 + ) 2 \-3

'0 ;

Q1"1=1# H +

Q1"1=1# ! $3 ;\ `2 I!3 1Cexample3.tcl ) 1C 'Y L f


I!3 1C + ) ) 0

) *3 ) H

I ] 2
;' 0

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


set n($i) [$ns node]
}
;' ) ' '. ) @'
; ' '!u!= +

\ Z 3 2 'Y# Y
&|)

) ) [V

$0 .>" ;' 0

Y#
45. I3

! Tcl ) @ !e

!a

Q1"1=1# )18

) |)

3 Q1"1=1#

!Cg n() ) ) ) (

3 ) 0 '!
) 2 \-3 @ $3

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


$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}
;' 0 45. I3 :\ 45. L 2 0 ) C 2 % 3 2

%1#
'0 L

$# for MD

36

re- $0 2 " '

W!+^ $0 4* '. ) nam ) \

*$ '! 0

;' '3 1C

% ) \R *

0 .

!> 4* '!3 ) 6 ) layout

&H !"
;' 0 L ) )

n(3) 2 n(0) 2 0 I!3 1C

#Create a UDP agent and attach it to node n(0)


set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
set null0 [new Agent/Null]

'Y 2 )

37

$ns attach-agent $n(3) $null0


$ns connect $udp0 $null0
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
3 2 )
2 !
L )

+ ) 0

? P 2

-#
Y !]

-. 4>

(.$] ) 0 ' .]3

( $3

# #10 H!6 # 0 ' '!3 1C '! 0 % ) \R *

0 < 9 2 ! H !" @ ) 0 I! 0

6 A ' '% \!D H


;'.6

)0

!> Z
2;1

L ;' 0

down ! o H

3 '0
-.

)1>^ < 9

:'

-. H!6 #

$ns rtmodel-at 1.0 down $n(1) $n(2)


$ns rtmodel-at 2.0 up $n(1) $n(2)
L Y6 <yP ! o )

L Y6 !, ) < 9 ! TC
;\6) ' 3 1C !

!> 9yP ! o )
' )

;\]! D*

? P 2
;'! 0

'3

) 0 TC

(.0 = ! o H
nam ) ) } -#

n)
) b= ;I! 0
4* )

38

'.

TC L

; 0 '3 1C 4

) 4*

1=

!]

1 a 0

;'! 0 6 A
$ns rtproto DV
3 2 !]
-.

? P

3 2 ! H!6 # 0 ' '!3 1C


;' $

'3

) % 1$

!>

#Tell the simulator to use dynamic routing

'Y 1C \R *

!> ! o ) '! 0 % ) \R *

4* ) ;' '3 1C 4 G 4,5

; ' . 1 4 0 )1r example3.tcl Y


#Create a simulator object
set ns [new Simulator]

I!3 1C

L f 4 0 '0

39

$ns rtproto DV
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf

#Define a 'finish' procedure


proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}
#Create seven nodes
for {set i 0} {$i < 7} {incr i} {
set n($i) [$ns node]
}

#Create links between the nodes


for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}
#Create a UDP agent and attach it to node n(0)
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
#Create a Null agent (a traffic sink) and attach it to node n(3)

40

set null0 [new Agent/Null]


$ns attach-agent $n(3) $null0
#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0
#Schedule events for the CBR agent and the network dynamics
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n(1) $n(2)
$ns rtmodel-at 2.0 up $n(1) $n(2)
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run

41

42

1 456

ns ) ' '% 4*# = @ 0 6 A


\ )
\ 2

2 H @ ) 0 0 I!3 1C 6 A ) ping protocol


\6) @

@ 1#

)0

;' 2 F

' '% 4*# = H

) @ I3 ' !2 @

456

' 0 L ) \0 = H 3 2
; 0 > 7 ) round-trip

;\6 2

@ 1#

\]!

!=

.( ' ~ ) 0 '0 0 \ kV
&:header '

) T>#

0 I! 0

kV

) ' '% ping \0 = '

) .C

.>"

4 6H +

'. ' '% '

4 6
;

'3 1C L M.

struct hdr_ping {
char ret;
double send_time;
};
1 '

ping = 2

> 7 ) 'Y

0 '

.]

)L )

' ping ' !2

'. 6 z X

.] #)18 ) ret .0) 0

W]a send_time !!d. ;' '3 1C

3) 0\

;' '3 1C > 7 \ 2


;' 0

Y#Agent h 0

@1^

) PingAgent h 0

\6) @
'0 *#

43

class PingAgent : public Agent {


public:
PingAgent();
int command(int argc, const char*const* argv);
void recv(Packet*, Handler*);
protected:
int off_ping_;
};
Y# c1
3 !!d.

) recv() command()

\o )
'

\ ;' '3 1C

3'.

'0

Y# ) '

-. ping \0 = H '
;\

off_ping_

' 3 1C _ @
&'

C ) Z1$Y
e

/*
* File: Header File for a new 'Ping' Agent Class for the ns
*
network simulator
*
*/

#ifndef ns_ping_h
#define ns_ping_h
#include "agent.h"
#include "tclcl.h"
#include "packet.h"
#include "address.h"
#include "ip.h"

struct hdr_ping {
char ret;

PingAgent() F #
;' '

D7 !

) ping.h 4 0 '0

'7
)

44

double send_time;
};

class PingAgent : public Agent {


public:
PingAgent();
int command(int argc, const char*const* argv);
void recv(Packet*, Handler*);
protected:
int off_ping_;
};

#endif
&C++
3'0

) ;' !

) ( 4 0 )1r 0 \]!

Z '
;'! 0

'
'3

Y# C++ tcl
) ping '

static class PingHeaderClass : public PacketHeaderClass {


public:
PingHeaderClass() : PacketHeaderClass("PacketHeader/Ping",
sizeof(hdr_ping)) {}
} class_pinghdr;

static class PingClass : public TclClass {


public:
PingClass() : TclClass("Agent/Ping") {}
TclObject* create(int, const char*const*) {
return (new PingAgent());
}
} class_ping;

h 0

3'0

3'0 ! O >#)
C++ O >#)

45

Tcl ) I3 0 ' 0

:bind 45. '!M

)1X 3 !!d. 0 \ PingAgent h 0 '


;'

'0 'Y \$]


4 C++ ) I3

h.

PingAgent::PingAgent() : Agent(PT_PING)
{
bind("packetSize_", &size_);
bind("off_ping_", &off_ping_);
}
) ;1

% PingAgent h 0
:\ Agent/Ping h 0
) )1.

'!=

r F # 2

F # '0 ;:Agent::command() \"

H 0 1

1C 6

command() F #

1$ H pa 0 I! 0 S 6 \ $pa send F #

ping agent agent H

)1r ;I! 0 L )
) ( $2)

Tcl )1.

) ping

(.0 = 0 I ) '5

0\

0 '!3 1C : +# parse ) command )1.


0 '!3 1C L )

\"
XC
$

= h 0 ) command F #

; ' . 1 '0 ) TC TC @ !A1# \


int PingAgent::command(int argc, const char*const* argv)
{
if (argc == 2) {
if (strcmp(argv[1], "send") == 0) {
// Create a new packet
Packet* pkt = allocpkt();
// Access the Ping header for the new packet:
hdr_ping* hdr = (hdr_ping*)pkt->access(off_ping_);
// Set the 'ret' field to 0, so the receiving node knows
// that it has to generate an echo packet
hdr->ret = 0;
// Store the current time in the 'send_time' field
hdr->send_time = Scheduler::instance().clock();
// Send the packet
send(pkt, 0);
// return TCL_OK, so the calling function knows that the
// command has been processed

Z1X command()

46

return (TCL_OK);
}
}
// If the command hasn't been processed by PingAgent()::command,
// call the command() function for the base class
return (Agent::command(argc, argv));
}
'

ret 'D!6 2 ;I!3

' ' )

\0 = H .

0 ' *! kV

0 '

ret 2 ;' '3 1C 9

ret 'D!6 ' '3 1C L )

\ 2

\6) @

) ;' '3 1C s

W]a @ $3 \0 =
!!Y# tcl )

) ' . 6 ) $ NodeShift h) ;' '3 1C

;'3
;'

1C 6 tcl F # H : 1

) D$^ recv() #

) '

) 0 T 1#
)0

!= ! recv() '. @ 4C ) 0 ping.cc 4 0 4 6

/*
* File: Code for a new 'Ping' Agent Class for the ns
*
network simulator
* Author: Marc Greis (greis@cs.uni-bonn.de), May 1998
*
*/

#include "ping.h"

static class PingHeaderClass : public PacketHeaderClass {


public:
PingHeaderClass() : PacketHeaderClass("PacketHeader/Ping",
sizeof(hdr_ping)) {}
} class_pinghdr;

static class PingClass : public TclClass {


public:

47

PingClass() : TclClass("Agent/Ping") {}
TclObject* create(int, const char*const*) {
return (new PingAgent());
}
} class_ping;

PingAgent::PingAgent() : Agent(PT_PING)
{
bind("packetSize_", &size_);
bind("off_ping_", &off_ping_);
}

int PingAgent::command(int argc, const char*const* argv)


{
if (argc == 2) {
if (strcmp(argv[1], "send") == 0) {
// Create a new packet
Packet* pkt = allocpkt();
// Access the Ping header for the new packet:
hdr_ping* hdr = (hdr_ping*)pkt->access(off_ping_);
// Set the 'ret' field to 0, so the receiving node knows
// that it has to generate an echo packet
hdr->ret = 0;
// Store the current time in the 'send_time' field
hdr->send_time = Scheduler::instance().clock();
// Send the packet
send(pkt, 0);
// return TCL_OK, so the calling function knows that the
// command has been processed
return (TCL_OK);
}
}
// If the command hasn't been processed by PingAgent()::command,
// call the command() function for the base class
return (Agent::command(argc, argv));
}

48

void PingAgent::recv(Packet* pkt, Handler*)


{
// Access the IP header for the received packet:
hdr_ip* hdrip = (hdr_ip*)pkt->access(off_ip_);
// Access the Ping header for the received packet:
hdr_ping* hdr = (hdr_ping*)pkt->access(off_ping_);
// Is the 'ret' field = 0 (i.e. the receiving node is being pinged)?
if (hdr->ret == 0) {
// Send an 'echo'. First save the old packet's send_time
double stime = hdr->send_time;
// Discard the packet
Packet::free(pkt);
// Create a new packet
Packet* pktret = allocpkt();
// Access the Ping header for the new packet:
hdr_ping* hdrret = (hdr_ping*)pktret->access(off_ping_);
// Set the 'ret' field to 1, so the receiver won't send another echo
hdrret->ret = 1;
// Set the send_time field to the correct value
hdrret->send_time = stime;
// Send the packet
send(pktret, 0);
} else {
// A packet was received. Use tcl.eval to call the Tcl
// interpreter with the ping results.
// Note: In the Tcl code, a procedure 'Agent/Ping recv {from rtt}'
// has to be defined which allows the user to react to the ping
// result.
char out[100];
// Prepare the output to the Tcl interpreter. Calculate the round
// trip time
sprintf(out, "%s recv %d %3.1f", name(),
hdrip->src_ >> Address::instance().NodeShift_[1],
(Scheduler::instance().clock()-hdr->send_time) * 1000);
Tcl& tcl = Tcl::instance();
tcl.eval(out);
// Discard the packet
Packet::free(pkt);
}
}

49

' ping 2 ) $
. 1 F #

0 \ tcl.eval()
'0 1 a 0 I! !

;' ' 3 1C [

1C 6 \$]
( V ) ;1

L R 0 ns *

1 )

1C 6recv F # 0

.$( 1

1C 6 . ) = @ 1 ^ \ 2

[!= (D 6

1 a 0 I! !> '

\6) @

4> ; 1
& Z

\ 6

2 i

Z '! 0 6 A ' '% agent H *

;' )

. `2 '! 0 L $^
H ) ) (

) 1C

7!A1#

) 1C

!!d# '! 1#

. )

ns F>

!!d# 0 I! 0

(D 6
( != ;'! 0

;: !, #ifdef

!!d# C $
-. I3 ' '% \0 =
comment

-.

;' ) ` ns
$ + ) ;\ packet.h 4 6 [

! ping agent

2I)

+ ) ;: !, PT_TELNET PT_TCP 4f '! 0 '!= )


enum O1rC

'Y# packet.h ' [

\0 = c1 H

@Q) ) ;'! 0 6 A ) PT_PING

;: 1 L $^ @ )

# !!d# 3'Y '

' '% @Q)

(*# = ID

D.V
1

3'0

!!d#

0L

- Y# '! 1#
) ' '%

- Y#

!> packet_t{}

enum packet_t {
PT_TCP,
PT_UDP,
......
// insert new packet types here
PT_TFRC,
PT_TFRC_ACK,
PT_PING, // packet protocol ID for our ping-agent
PT_NTYPE // This MUST be the LAST one
};
;'! 0 [
class p_info {
public:
p_info() {

) \ Ping 4

0 D 6 @ $3 ) ) p_info() '

! u$3

50

name_[PT_TCP]= "tcp";
name_[PT_UDP]= "udp";
...........
name_[PT_TFRC]= "tcpFriend";
name_[PT_TFRC_ACK]= "tcpFriendCtl";
name_[PT_PING]="Ping";
name_[PT_NTYPE]= "undefined";
}
.....
}
!, ) 0 '!3

+ ) make depend )1.

'

make )1.

;' 1
!

6 [!=
;'! 0 _) 4 6

3 ) 'M

$# 4 6

) ; 0[

) ) Asent/Ping

4> 0 '!

4 R 04 6

0\

*$

)15

! ) tcl/lib/ns-default.tcl 4 6 '

6 [!= \0 = ' I!e #

TC ;\

! u$3
Y# Tcl

'

Agent/Ping set packetSize_ 64


4 6 '. \]!" ) tcl/lib/ns-packet.tcl 4 6 ) ' '% ping

(.0 =

) H '

; 1 '3 1C

! u$3

!> 3'0 *# ;'! 0 _)

{ SRMEXT off_srm_ext_}
{ Ping off_ping_ }} {
set cl PacketHeader/[lindex $pair 0]
(D 6 \]!"

) ping.o 4 6 $ ;

'!3 1C + Makefile 4 6 ) 0 \
;'! 0
;'

sessionhelper.o delaymodel.o srm-ssm.o \


srm-topo.o \
ping.o \
$(LIB_DIR)int.Vec.o $(LIB_DIR)int.RVec.o \

!!d# !!d#

6 A ) ns
!> '

$ \]!"

51

$(LIB_DIR)dmalloc_support.o \
ns )1.0

;'!

) make / # ns )

4 R 0 )

'

&Tcl '0
0
'3 1C

I!3 1C @

I!3 1C @

1C 6 C++ '0 ) recv() F # ' )

) Ping agent

Tcl L f H

4 0 '0 L

ping echo \0 = H 0 . recv L ) 1 a


;'

Agent/Ping instproc recv {from rtt} {


$self instvar node_
puts "node [$node_ id] received ping answer from \
$from with round-trip-time $rtt ms."
}
1 ^ !!d.
I

0 ping @

!.

0\
0\

0 ' '% !a ;\ I(6 4

2 @ :node id

2 )$

.6 2

Agent = h 0

&' . 1 $
#Create a simulator object
set ns [new Simulator]
#Open a trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0

Z ) '

.-2 '0
node_

ping.tcl 4 0 '0

52

}
#Create three nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
#Connect the nodes with two links
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
#Define a 'recv' function for the class 'Agent/Ping'
Agent/Ping instproc recv {from rtt} {
$self instvar node_
puts "node [$node_ id] received ping answer from \
$from with round-trip-time $rtt ms."
}
#Create two ping agents and attach them to the nodes n0 and n2
set p0 [new Agent/Ping]
$ns attach-agent $n0 $p0
set p1 [new Agent/Ping]
$ns attach-agent $n2 $p1
#Connect the two agents
$ns connect $p0 $p1
#Schedule events
$ns at 0.2 "$p0 send"
$ns at 0.4 "$p1 send"
$ns at 0.6 "$p0 send"
$ns at 0.6 "$p1 send"
$ns at 1.0 "finish"
#Run the simulation
$ns run

53

-# a I! * 1 \6 )

'Y ) ) 'M ret 'D!6 ) 2 ' 0 '!=

+# ) 0

' # $

0 L ;
.6 '3 1C

) %1. I! * 9 ) \!

2 @1a 0 ' 3 1Cping I3


:;' '3 1C TD,

0 )1r $pa send $node \0 = L )


) ping agent H

pa

1# @ ' ) (.0 = $~
!$3 \ 2 \6) @

L 5# @ ' :\
;' )

'!u!= ) 0

*6 & ' ] 1

' '3 1V @ L )

) 3 '0 C '! 1#

2 H node
e

0I0

\6
! u$3

1 ping agent H pa
'. ) .>" ; '. ) node

54

55

) (a 456

Xgraph
48
(.R *
-.
+

3z 2 +
output
17 I!3 1C
-D.V

' 0 I! #
(D 6 I! 1#
L f ) ;I! 0

( ) 0\

% C

(D 6 +

H 0 \ Xgraph !*= ns-allinone


1 a 0

I!3 1C @

[V

-. Xgraph

) (

0 %1# '

.* ;I!3 @

;)

) ;1

-.

^1$+ bR

I! 0 + tcl

! ) H!6 #

3 '0 +

1% Xgraph

& H!6 #
;I! 0 + '

(.$]

!>

% C

(D 6

3 F>

Q1"1=1#

Q1"1=1#

$3 4>

56

;I

) ) ( 4>

(.$] ) 0 ' .]3

3'0 @ $3

!>

3'0

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
$ns duplex-link $n0 $n3 1Mb 100ms DropTail
$ns duplex-link $n1 $n3 1Mb 100ms DropTail
$ns duplex-link $n2 $n3 1Mb 100ms DropTail
$ns duplex-link $n3 $n4 1Mb 100ms DropTail

6 A # I] 1 ' L ) H

'

'.

I! 0 T>A ) n2 n0,n1
;' 0 #

3 F>
) 3 F>

(*!6 # 0 I ) '5
3 ' 0 + H!6 # @ 0

proc attach-expoo-traffic { node sink size burst idle rate } {


#Get an instance of the simulator
set ns [Simulator instance]
#Create a UDP agent and attach it to the node
set source [new Agent/UDP]
$ns attach-agent $node $source
#Create an Expoo traffic agent and set its configuration parameters
set traffic [new Application/Traffic/Exponential]
$traffic set packetSize_ $size
$traffic set burst_time_ $burst
$traffic set idle_time_ $idle
$traffic set rate_ $rate
# Attach traffic source to the traffic generator

57

$traffic attach-agent $source


#Connect the source and the sink
$ns connect $source $sink
return $traffic
}
H!6 # ' !2 H!6 # sink H 2 { & !2
( $ F 1#
\ 1!=
*

idle burst
!$A 2 H

[!=

L )

H!6 # F>
*!=

;:H!6 # ' . 6 ' 2 '3 1C

2 'a

0 :H!6 # F> H

3 peak x
L ) n4 2

\0 = '

) ( T 1# '

'

# '!u!= L )

3j

> 0 :H!6 # '

+ Traffic/Expoo

3 . ) = I !0
H!6 # F>

;I! 0

!$A H!6 # F>

:' 0 )

handler

H!6 # @ 0 !$A 4f \

H!6 # ' 0 +

;\ @ X

+ ) H!6 # F> H '. Z L ) ) ;:peak rate _ x

'Y I !0

0 I!e # ) ) '

. )=GL )

3 -!j )

3 F> k!5V#

L )

+ H!6 # bR

I! 0
;:I

H bR ;I! 0

' 1 45. I3
! .

H L ) C )

+ 17

1C L f H

L ;\ ' 0 ! .
-. n3 n1 n0

' 1#
3 2

0
+

sink I! 0

0 + '. ) 0 I! 0

set sink0 [new Agent/LossMonitor]


set sink1 [new Agent/LossMonitor]
set sink2 [new Agent/LossMonitor]
$ns attach-agent $n4 $sink0
$ns attach-agent $n4 $sink1
$ns attach-agent $n4 $sink2
set source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k]
set source1 [attach-expoo-traffic $n1 $sink1 200 2s 1s 200k]
set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k]
(

;I

0
;' 1

-. H!6 #
-. '

3 sink @ 1 ^
(= >]7

Agent/LossMonitor !
' 1#

0' 0

!Cg ) '! )

Lf
(.

)
) 'M

58

: % C
;I

)0

D>

(.R * ) )

O1rC ;I !0

(D 6 ) 3

) % C4 6

T>A
@1 0 I3

set f0 [open out0.tr w]


set f1 [open out1.tr w]
set f2 [open out2.tr w]
;I

!> finish L ) )

+ ))0

;' 1

.] rM H ) (D 6

proc finish {} {
global f0 f1 f2
#Close the output files
close $f0
close $f1
close $f2
#Call xgraph to display the results
exec xgraph out0.tr out1.tr out2.tr -geometry 800x400 &
exit 0
}
0\

*$ ;I

! ) Xgraph *D I .]> ) (D 6 TM6 finish L ) )

1C 6 . [ $
;'
;'] 1

800*400 '
% C

(D 6

Xgraph + = ' 0 '!


) 3

0 I!] 1 ) " ) I! 1#

proc record {} {
global sink0 sink1 sink2 f0 f1 f2
#Get an instance of the simulator
set ns [Simulator instance]
#Set the time after which the procedure should be called again
set time 0.5
#How many bytes have been received by the traffic sinks?
set bw0 [$sink0 set bytes_]

'5 $
L

59

set bw1 [$sink1 set bytes_]


set bw2 [$sink2 set bytes_]
#Get the current time
set now [$ns now]
#Calculate the bandwidth (in MBit/s) and write it to the files
puts $f0 "$now [expr $bw0/$time*8/1000000]"
puts $f1 "$now [expr $bw1/$time*8/1000000]"
puts $f2 "$now [expr $bw2/$time*8/1000000]"
#Reset the bytes_ values on the traffic sinks
$sink0 set bytes_ 0
$sink1 set bytes_ 0
$sink2 set bytes_ 0
#Re-schedule the procedure
$ns at [expr $now+$time] "record"
}
)

0 '3 1C > 7 ) '


' 0 !!d# '+ H!6 #

(= bR ;' 1C
3 sink ) bytes_
;' 0

) H!6 # sink
3) 'M *
' @

' \6 )

'Y# L )
:MBit/s

[!= % C 4 6
) ( bR ;\ 1 '3 1C (

10

&

!>

&I! 0 ' >


$ns at 0.0 "record"
$ns at 10.0 "$source0 start"
$ns at 10.0 "$source1 start"
$ns at 10.0 "$source2 start"
$ns at 50.0 "$source0 stop"
$ns at 50.0 "$source1 stop"
$ns at 50.0 "$source2 stop"
$ns at 60.0 "finish"
$ns run

(.

3 'C) I! 1#

(
%
L

60

'>

) 1C ! o PyE 3

L ) GP ! o ) ;' 1

)15 ) 1C @ q@

1. EPyP )

0c

b=

'

1C 6 record L ) '. )

9PyP ! o ) H!6 #

3 F> bR ; 0 '3 1C
;1

&\

'

) example4.tcl

#Create a simulator object


set ns [new Simulator]
#Open the output files
set f0 [open out0.tr w]
set f1 [open out1.tr w]
set f2 [open out2.tr w]
#Create 5 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
#Connect the nodes
$ns duplex-link $n0 $n3 1Mb 100ms DropTail
$ns duplex-link $n1 $n3 1Mb 100ms DropTail
$ns duplex-link $n2 $n3 1Mb 100ms DropTail
$ns duplex-link $n3 $n4 1Mb 100ms DropTail
#Define a 'finish' procedure
proc finish {} {
global f0 f1 f2
#Close the output files
close $f0
close $f1
close $f2
#Call xgraph to display the results
exec xgraph out0.tr out1.tr out2.tr -geometry 800x400 &
exit 0
}

1C 6 finish
)Lf ' 4 0

61

#Define a procedure that attaches a UDP agent to a previously created node


#'node' and attaches an Expoo traffic generator to the agent with the
#characteristic values 'size' for packet size 'burst' for burst time,
#'idle' for idle time and 'rate' for burst peak rate. The procedure connects
#the source with the previously defined traffic sink 'sink' and returns the
#source object.
proc attach-expoo-traffic { node sink size burst idle rate } {
#Get an instance of the simulator
set ns [Simulator instance]
#Create a UDP agent and attach it to the node
set source [new Agent/UDP]
$ns attach-agent $node $source
#Create an Expoo traffic agent and set its configuration parameters
set traffic [new Application/Traffic/Exponential]
$traffic set packetSize_ $size
$traffic set burst_time_ $burst
$traffic set idle_time_ $idle
$traffic set rate_ $rate
# Attach traffic source to the traffic generator
$traffic attach-agent $source
#Connect the source and the sink
$ns connect $source $sink
return $traffic
}

#Define a procedure which periodically records the bandwidth received by the


#three traffic sinks sink0/1/2 and writes it to the three files f0/1/2.
proc record {} {
global sink0 sink1 sink2 f0 f1 f2
#Get an instance of the simulator
set ns [Simulator instance]
#Set the time after which the procedure should be called again
set time 0.5
#How many bytes have been received by the traffic sinks?

62

set bw0 [$sink0 set bytes_]


set bw1 [$sink1 set bytes_]
set bw2 [$sink2 set bytes_]
#Get the current time
set now [$ns now]
#Calculate the bandwidth (in MBit/s) and write it to the files
puts $f0 "$now [expr $bw0/$time*8/1000000]"
puts $f1 "$now [expr $bw1/$time*8/1000000]"
puts $f2 "$now [expr $bw2/$time*8/1000000]"
#Reset the bytes_ values on the traffic sinks
$sink0 set bytes_ 0
$sink1 set bytes_ 0
$sink2 set bytes_ 0
#Re-schedule the procedure
$ns at [expr $now+$time] "record"
}
#Create three traffic sinks and attach them to the node n4
set sink0 [new Agent/LossMonitor]
set sink1 [new Agent/LossMonitor]
set sink2 [new Agent/LossMonitor]
$ns attach-agent $n4 $sink0
$ns attach-agent $n4 $sink1
$ns attach-agent $n4 $sink2
#Create three traffic sources
set source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k]
set source1 [attach-expoo-traffic $n1 $sink1 200 2s 1s 200k]
set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k]
#Start logging the received bandwidth
$ns at 0.0 "record"
#Start the traffic sources
$ns at 10.0 "$source0 start"
$ns at 10.0 "$source1 start"
$ns at 10.0 "$source2 start"
#Stop the traffic sources
$ns at 50.0 "$source0 stop"
$ns at 50.0 "$source1 stop"
$ns at 50.0 "$source2 stop"

63

#Call the finish procedure after 60 seconds simulation time


$ns at 60.0 "finish"
#Run the simulation
$ns run
4*

!> @

3 ) 1$

' '3 1C 3 j Xgraph e7" ' a 'Y '! 0

% )

!> $

0 .

; 1 '3 1C

) ) 0.1Mbit/s ' peak ) L @ %

@ %)

!!d# ) record L ) ) time ) 'M $


;'!3 !!d# 9yP bR
*
I )

.* ;\ I(
)

;'

'!3 !!d# Py9

D!C time

H gnuplot

2 L
W

! u$3 ' 1#

3 burst '

0.3Mbit/s )

0 )1X @ $3

1 ) 0.2Mbit/s

f ; .6 '3 1C } -# # !!d# 0 ' '!3 1C '!3

) 'M @ 0 '!=
'1

!>

31 )

+ record L )

) 0 '! 0 %1#
0 % C
;' 1

(D 6

-. :) 1$

64

65

I+ = 456

ns ) 3
F$% '

!>

!>

*> s
! ) ) 3

= )1e

) F$%

) % C

0\

@ 1#

;)

1% 3 % C ) F$%

;)

. !

)0

3 .] z`

@'! ) q@' ) q@' _) C

! ;' 1

'

)18

.] 3

(.!D

-D.V

s ) .>" ; 1 s
&1

*!= *>

!> H

.$(

( ) ;' 0 )

= 'Y # \V ) D 6 ) 'Y
>!. = tracing s )

^ X & trace !

Q1"1=1# ) 3 2 ' $3 trace ! ; 0 ) F$% @ 1#

ns )

D! 1 9
): 8)

& ' #) >^ trace


trace ) : 8 Z1$Y 3 .]

;' 0
;' 0

trace ) : 8 Z1$Y 3 .] _ C

&Trace/Deque

trace 3 .] z`

&Trace/Drop

trace ) 'Y 2 ) 3 .] \6 )

&Trace/Recv

;' 0
;' 0
;' 0
;' 0
@

' zg .]

&Trace/Enque

!Cg ) ) @
) .]

'

.]>3 '

.] _ C @

' RED I. )1 " T 1# .] z` @

&SnoopQueue/In
&SnoopQueue/Out
&SnoopQueue/EDrop
;' 0

!Cg ) z`

66

!,
!

'

. 6

;' 3

.6 )

(.! q 3 .] 'Y# '

+ ) s) $ 4$^ ( %

-D.V

) I3

&Monitoring !

(.!$0 !

3 .]

) I3 ' 1#

3)1.!

D!

;' 0

<

& ' #) >^


(

trace ! \$] ) 0

3 Snooper T 1# ' \ '


;' 0

.] z` k!V #

(.!D

QueueMonitor

(.!D

&QueueMonitor

3 1$
'

2 \6 ) ) :' )
&QueueMonitorED

^
;)

' 1#

QueueMonitor/ED

(.!D

&QueueMonitorED/Flowmon

^
;' 0 '

T 1#

3 @ %

O1

^ X

! ) Red I!#)1 " T 1#

2 2 '% )1r q @ % 3 h

^ X

&QueueMonitorED/Flow

1.7

\ ' QueueMonitorED/Flowmon

;1

&Trace !
trace
;\ ) )

!815C 0 )

1% .] 3 ) n .

>!. =

!!Y# ' 0

'!"1# ) .] 0

(" ^ T 1# \!815C

) $Y

H trace

'

.'3

.] '

.]
' 0

>!. =

.* 5V

H 9

) .] c1 <

kV

.1
.\

! )1 .] L M. @
;1

!!Y#

\!$0

. '0

-. Multicast .C) F 1# > 7


&Trace

kV

) .]
:T

!. =

' ?

W]a B
(* !" ) $Y

67

' 4!* # Connector

;\

'Y#

H !" ;' '

Y# (* !" )

Trace + )1e

3 2 )

; DropHead,Ttl,Link,Queue,Head ;' 0
;\ Simple-Link H !" D8 h 0 ; 1 L Y6 ((* !"

h 0
-.

Monitoring Trace ' L Y6 @

Y# ) H !"

) !!d. =

'. trace + )1e

)@ '

) 0 \ trace init-monitor L )

)
;1

4 6

) (

Dequeue ;' 0

!Cg ) 3 .] @'

) 3 .] .C ' )
;' 0
q'

Receive , Dequeue , Enqueue, Drop c1

% C

.-2 trace !

O1

^ X Drop q' 0
'Y ;\

;'3

) "]

'0

4* ;' 0

O1

'0

;' *
;' 0

)
)

trace ) 8

'

!!Y#
^ X
) '(

S 6 Trace L ) ! u$3

6 A H !" ) queue

-.

O1

^ X Receive ;' 0

8H '0

6 A H !" O1

&'
;' 0

) 3 .] @' _) C

' T># H !"

6 A queue b= ) Dequeue

'0

^ X ( # Enquque c1 ;'. 6

O1

!Cg ) 'Y 2 T 1# 3 .] \6 )
+

trace ! trace L )

queue target

)1 trace L $^

4> ) Enqueue
8 ! ) Drop

H !" )

8^

3 .] 0

5 ^ &Enqt_

' *! trace ) % C

3 .] 0

5 ^ &Dept_

trace ) ' I2

3 .] 0

5 ^ &Drpt_

'0

68

;' 0

;' 0

trace ) 'Y

'0
)

5^

2 T 1# ' \6 )

3 !!d.

3 .] 0

5 ^ &Rcvt_

) ) ' a ) tracing ) 0 2

' 0 L Y6 H !"

&trace 4 6 \ 6
;' 3

) 1

+ trace-all )1.

0 trace 4 6 \ 6

D!

tracing !
)0

H$0 F 1# @

)0

3)1.

-. @ ' @ 1#

)'

L '%

*$0 F 1#

.-2 4> ) 0 trace !


;

;' 0

4 6 )

) trace !

dst src ! :type c1


3) 'M ' 1#

trace

c1 ;' 2

trace- L ) T 1#

0 '

$3 O1

^ XF #

H & create-trace{type file src dst}


! .

&flush-trace{}

'0

) Recv,Drop,Dequeue, Enqueue

69

) '

1 trace \ )

!!Y# )1.

callback{ns command}

;1
create-trace F # T 1# > 0 trace

& Drop-trace{n1 n2 traceobject}

n2 n1 2 ! ' .C ' )

;1

3 .] '5M \

' .C

&trace !

-.

)* ]13

]1

3 \R * ) @ 1#

(* !" ) trace@ 1$ L Y6

(r
) 3)1.

]1

(r

$Simple-link trace <ns> <file> <optionali op>


O1
;1
@

trace % C ; )
.C nam c1

nam animator )

) L 5# :' 0

trace 4 6 H

L Y6

) nam ) 'M op 2 ; 1

*!6 2 )15 ) H !" ) '

) trace

H !"
!>

. 1 '

-# @ 1#

)1.

!!Y# 4 6 ) H !"

nam 4 6

-.
;

$simple-link nam trace <ns> <file>


;1

. 1 4 6)

% C '0

' ) H !" ) ) nam tracing )1.

$simple-link trace dyndmic <ns> <file> <optional:op>


nam ) 'M

!!Y#
;\

'0

'
*!

) :H !" @' U C *!
-#

O1

trace \ 6 ;' 0

-# l15V ) tracing )1.


' ) ) c1

op

70

& (-8 trace O1

$ns trace-all <file>


.1

. 1 '

!!Y# 4 6 ) @ % C ' 0

L Y6 (* !" $3

) ) trace )1.

$ns namtrace-all <file>


.1

. 1 '

!!Y# 4 6 ) @ % C ' 0

L Y6 (* !" $3

) ) nam c1

Trace

$ ns trace queue <n1> <n2> <optional file>


+ n2 n1 !

;' 0

M#

.6 2 log )1e

trace

H @ 6

$ns namtrace queue <n1> <n2> <optional: file>


;' 0

L Y6 ) nma tracing *

% \ 4> 4f @ 6

Monitoring !

-.
)

)1
^ X

!>

% C

!2 ) \!D !

^ X
. '1

) F$% )1e
6 A *>

0 ' .]3 !
Q1"1=1#

^1$+ 3)1!.
'Y ! !

' !2

71

'

. 6

3 .]

$#

(3 \] 'Y# q '

.C ' )

*$ monitoring '

)\

3 \] 'Y# q ' \6 )

(3 \] 'Y#

.-2 0 )1r $3. ' ) ) 3 . ) =

! !

;l C
[t0,t1]

intergrator

D8 6 H ) ' 1#

-.

)1.!

( %

H (

O1

! W]

1 L $^

3 .] 'Y#

1" ^

;' 0 > 7 ) 8 '

^ X

Queue Monitoring
; ) Init-monitor

L )

" ) :H !" D8 h 0 simple-link h 0 monitoring %


;' 0

trace ! '

s) 2 Queue monitor

;'3
(

Snoop Queue ;' 0

' .]3 h .

2' 2

) ( Queue monitor 0
T 1# 0

3 !!d. ;'

)1.!

) 0'0

;' .]3 O1
Queue

-. Monitor Snoop !

'0

!> @

+ H !"

(.
)

!Cg

Y" X

3 !!d. ) )
-.

!>
b

'Y# ' 1 c

'1

1
% Cs

*$

3 ) 'M monitor

' '2

' _) C

'

.C ' )

' :Pkts_

(.

'Y#:barrival_

(3 \] 'Y#:pdeparture_

' _) C
.C ' )

' &Size

(3 \] 'Y#:parrival_

8 .6 )

'

@ 1#

snoop queue ' a \

8 .6 )

.]

8H

Queue monitor

45.

3 .] 'Y# ' 1 c

Queue Monitor H
;' .]3

Queue Monitoring

) 3 .] _ C

3 !!d. ; 1$

) ) 8 { )1.!

'Y#:bdeparture_

(.

(3 \] 'Y# :pdrops_
(.

'Y# :bdrops_

72

3 !!d.

.1

> 7 \

' L

T 1#:bytesInt_

. 1

> 7 .]

' L

T 1#: PktsInt_

# 8 ) Monitoring @' L Y6 @

1C 6 @

3 !!d.

T 1# ' '

2 ) 'M
;'

&QueueMonitoring/ED
6 @'
& \

= XC

3 .] .C ' )

) >^

! \ )

8 5C

3 !!d. ; 1 4~

\ QueueMonitor '

$# Red I. )1 " T 1# 3 .] @' .C ' )

RED I. )1 " T 1# ' .C ' )

3 .] 'Y# &Epdrops_

RED I. )1 " T 1# ' .C ' )

3\

'Y# &Ebdrops_

QueueMonitor/ED/FlowMon

'. (

' '

L ) ( % ' 0 )1.!

!= 31D6 O1
.] @

'Y
'0

) F$%
% a

H!6 # !

) F$% 1D6 W]

n3 n0 2 TCP *!6 # \
;I! 0 trace ) n3 n2 2

'

DY. .] 0 ' 0
)

NS-2 ) Monitoring Trace


) n3 n1

lC

0
!!Y#

^ X 0 1
-.

(" f

.C

Q1"1=1# '! 0 S 6

! H!6 # I!3 C

;\ L ) L

73

;1

W]. (

2 )

:w

'1

()

. 1 )1e

4 6

)1.

set tf [open out.tr w]


set nf [open out.nam w]
'

!!Y# 4 6 ) \

(* !" $3 ) 3 ' ) $# 4

; )

4 6

0@ % C

'

) trace 'Y )1.

'

' ) ! namtrace ! u$3 ; 1

% C

. 1 ' .-2 \ 6

$ns trace-all $tf


$ns namtrace-all $nf
nam \ ( )

'1

.] (D 6 bR

'

. 1 (D 6 4C

3 trace
;1

^ X finish L ) )
% out.nam 4 6

proc finish {} {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam out.nam&
exit 0
}
H !" f H !" H

3 ' ) ( # I!3 1V 2 ;' 0


;I! 0

-.

)1.!

trace-queue )1.

) (* !" $3 ) 3 ' ) $# trace-all )1.


@ 1#

I! 0 )1.!

) n2 n1 2

74

$ns trace-queue n2 n3 $tf


@ 1#

-. monitoring

0 I! 0

; ) \ ' ) !,

I! 0 )1.!

' _) C

' I2

-# $3 I!3 1V 2 '

3 .]

'Y# L f 3 .]

&L f @ 1 ^ ; 0

-. l C

-.

O1

3 )1e

^ X

@ 1#

^ X

( !M ) 3 .] @' I2 x
D.V

;; !,

.-2 0 )1X @ $3

D.V

( !M )
( !M ) 3 .]

3 )`2 x
)

'Y#
& 1$ L f

;'! 0
'

(=

-.

. `2 4 6 H ) '

-. @ ! n3 n2 ! H !"

7!A1# z`

) monitoring @ 1$ L Y6
;1

.6 2 ' W

)1.
-.

0 '!
'0 )

%1#

1$ @ 1 ^

3' = ) 3 .] @' I2 x

H !"

set aggr_thr_file [open thr_.dat w]


set aggr_drops_file [open drops_genC4mt.dat w]
set ft
;' 2

2 )

'0

L Y6 n3 n2 H !"

set mon_ref [$ns monitor-queue $n2 $n3 $ft]


$mon_ref set-delay-samples [new Samples]
set link_ref [$ns link $n1 $n2]
set throughput_samples_($link_ref) [new Samples]
set drops_samples_($link_ref) [new Samples]
$throughput_samples_($link_ref) reset
$drops_samples_($link_ref) reset
$ns at 0.0 "record"

) ) monitor-queue

75

proc record {} {
global n1 n2 aggr_thr_file aggr_drops_file
global ns t_u_time_ sent_bytes_ throughput_samples_ drops_samples_
global dropped_bytes_
#get an instance of the simulator
set ns [Simulator instance]
#set time after which this proc should be called again
set time 0.1
#byts no go through the queue

;' 2

H !"

)1.

set nbr [$ns link $n1 $n2]


;' 2

) '

!!Y# H qMonitor

2 )

)1.

(.

'Y#

)1.

(.

'Y#

)1.

set qmon [$nbr set qMonitor_]


;' 0

U ]. transf_byte !!d.

' 2

) H !"

' _) C

set transf_bytes [$qmon set bdepartures_]


;' 0

U ]. new_drops !!d.

' 2

) H !"

' .C ' )

Set new_drops [$qmon set bdrops_]


set simtime [$ns now]
;' 1

reset 3 !!d.

) ' ) 'M t_u_time !!d. 1

if { ![info exists t_u_time_($nbr)]} {


set p_simtime 0.0

) 2 )

!"

76

set used_bw 0.0


set drop_rate 0.0
set sent_bytes_($nbr) $transf_bytes
set dropped_bytes_($nbr) $new_drops
set t_u_time_($nbr) $simtime
}
else {
;1

3 ) 'M '3

) D>

!2 1$ @

0p_simtime !!d.

)1.

set p_simtime [set t_u_time_($nbr)]


!Cg t_u_time !!d. ) A @

; 2

)1.

-. @ ! )1.

set t_u_time_($nbr) $simtime


; 2

> 7

!2 1$

!'

(=

set used_bw [expr (($transf_bytes-$sent_bytes_($nbr))*8)/($simtime$p_simtime)]


set sent_bytes_($nbr) $transf_bytes
; 2

> 7

!2 1$

! 3 .] @' I2 x

)1.

Set drop_rate [expr (($new_drops-$dropped_bytes_($nbr))*8)/($simtime$p_simtime)]


set dropped_bytes_($nbr) $new_drops
set t_u_time_($nbr) $simtime
}
;' 1

!C '

!!Y#

3 ) ) drop_rate used_bw

$throughput_samples_($nbr) newpoint $used_bw


$drops_samples_($nbr) newpoint $drop_rate

)1.

77

!Cg 4 6 ) drop_rate used_bw I3

;' 1

puts $aggr_thr_file "$used_bw"


puts $aggr_drops_file "$simtime $drop_rate"
set now [$ns now]
;1

1C 6 )

'Y

D8 6 )

^ X )

$%

Z L )

)1.

) \ ()

$ns at [expr $now+$time] "recoord


&
;'] 1

D 6 ) ) ( % O1

^ X

'0 ; ) \ ' ( % W]

Lf

^ X @ 1#

set flow_stat [open flow_mon.stat w]


monitor-per0flow

)1.

) flowmon1

)1.

set flowmon1 [$ns makeflowmon Fid]


) H !"

flowmon1 W!# #

;' 0

\ 1!= n3 n2 ! H !"
;' 0

) F$% @ % c1 W]

$ns attach-fmon [$ns link $n2 $n3] $flowmon1 0]


) '

.-2 > 0 . 6

% C W!# #

'0

45. 4 6

) flowmon1
;1

$flowmon_10 attach $flow_stat

);\
. 14 6

78

) I!3 1V 2 ; 1
) *# e )1

. 1 4 6 4C

flowmon1

3' = ) ) )1.

I! 1#

I !

% C '

!!Y# @

^ X flowmon1

)1.

kC

31 =
;I! 0

$ns at $finish-time "$flowmon_10 dump


&@ % O1
@ %'

'. )1e

; 1$

-. I!M.] )1r Flow Monitor


&L f @ 1 ^ ; 1$

; 0

-.

Flow Monitoring

^ X ) F$%

@ 1#

'1

!Cg @

O1

3 !!d.

-. @

-.

@ 1#

! u+$3

3 . ) = bR

3 !!d. @ % 3

1
MD

!!Y#
)

foreach f [$fmon flows] {


set flow_arrs [$f set barrivals_]
set flow_deps [$f set bdepartures_]
set flow_drops [$f set bdrops_]
}
@ 1 ^ ; 1$

-. @

^ X

bR

1$ 1+.]% classifier T 1# ) e )1 @ % @ 1#

! u$3
&L f

set fclassifier [$fmon classifier]


set flow1 [$fclassifier lookup auto 0 0 1]
set flow_arrs [$flow1 set barrivals_]

&Loss Monitor L f
0

) F$%

0 )

^ X
;1

@#
z !Y#

Y# LossMonitor c1

1
)18

' !2 agent 2

' !2 '! 0 S 6 L f @ 1 ^ ; 1$

-.

79

$ns set sink0 [new agent/LossMonitor]


. ! !

' '

!!Y# U # ) ) ) ' !2 T 1# ' \6 )

(.

'Y#

L ) )18

proc record {} {
global sink0 f1
#Get an instance of the simulator
set ns [Simulator instance]
#Set the time after which the procedure should be called again
set time 0.5
; !2

sink0 ' !2 T 1# ' \6 )

(.

)'M bw0 !!d.

'Y#

)1. )

set bw0 [$sink0 set bytes_]


#Get the current time
set now [$ns now]
;'] 1

'

> 7 Mbs )18

!!Y# 4 6 ) ' 0

)'

(=

)1.

)1.

!2 1$ @

puts $f0 "$now [expr $bw0/$time*8/1000000]"


# '3'! )
'Y

-8 ) '3

!2 1$ @

# DY6 @

) ' !2 .6 )
' \6 )

(.

(.

'Y# 0 byte_ !!d.

'Y# !d.

'Y

.'3 @
$sink0 set bytes_ 0
#Re-schedule the procedure
;1

1C 6

!2 1$

$ns at [expr $now+$time] "record"


}

L )

time @

'

'Y 1

v^

)1.

80

; 1$

-. W!# # !$3 @ 1#

! ) LossMonitor

3 !!d.

81

I!

; node_(1) node_(0)
\0

'7

3 >" \$

456

1)

3 2 Y \ n 7.

'7 ) I3 " V z X
I3 " V

$! )

0 I ) I!

0 \0

! (.0 = 0 I

I3 \$

3 2

4> ;' ' 3 1C drop z` (.0 = ' > I3 1 )


3 *>

5V

Q1"1R#

)
2

!> \]V $! ) ;' 0

-. TCP O >#)

I!3 1C !A1# ns ) I!

. 500*500 '7 )

0 \0

'7

0 #)18 )
!a *

3 1 )

H . 1

! ;' 0

) 2 ;' 1

L ># '

F>% )

7-8 ' a I! 0

\0
!

@ )
!>

&(link layer)LL
0\

6X

) '(

L )

(recvtarget()) 3'5M
;' .]3 ' L )

;'

.6 2 linkDely h 0

\6 )
'! )

0 \ C++ h 0 H H !" Z

:sendtarget() 3 '5M

(.0 = @ 0 )

L )

sendto() recnfrom()

class LL : public LinkDelay {


public:
LL();
virtual void recv(Packet* p, Handler* h);
virtual Packet* sendto(Packet* p, Handler* h = 0);
virtual Packet* recvfrom(Packet* p);
inline int seqno() return seqno_;
inline int ackno() return ackno_;
inline int macDA() return macDA_;

'% 2 )
3'. ;' 0

82

inline Queue *ifq() return ifq_;


inline NsObject* sendtarget() return sendtarget_;
inline NsObject* recvtarget() return recvtarget_;
protected:
int command(int argc, const char*const* argv);
void handle(Event* e) recv((Packet*)e, 0);
inline virtual int arp (int ip_addr) return ip_addr;
int seqno_; // link-layer sequence number
int ackno_; // ACK received so far
int macDA_; // destination MAC address
Queue* ifq_; // interface queue
NsObject* sendtarget_; // for outgoing packet
NsObject* recvtarget_; // for incoming packet
LanRouter* lanrouter_; // for lookups of the next hop
};
;' $

'3

) H !" Z

1$

'

*!= H

set ll_ [new LL]


set ifq_ [new Queue/DropTail]
$ll_ lanrouter [new LanRouter $ns $lan] # LanRouter is one object
# per LAN
$ll_ set delay_ $delay # link-level overhead
$ll_ set bandwidth_ $bw # bandwidth
$ll_ sendtarget $mac # interface queue at the sender side
$ll_ recvtarget $iif # input interface of the receiver
...
3IP h)
LL ;' 1
) #Z

ARP L' @
L ) LL

'

1% I!

I!

3 2 !

Routing Agent T 1# ' 1 _) C

(.0 = ! u$3 ;' 0


;

~ ) #Z

-# H TM6 I!

3 2)

(.0 = $# "1$Y )1r ;\ MAC

0 ) '( interface queue ) )

'3 1C node_enter_point

) @ MAC Z bR ;'3

(.0 =

MAC Z

& (Address Resolution Protocol)ARP

83

0' 2

) * !6 h)

ARP ; )

\0 = h)

'0

\6 ) h) 4 '>#

>

(. 1C) H !" Z 4*# =

@ 0 broadcast ' > 1%1 h)

\ '

!!d# )18 ) ' 0

) '(

) \0 = H h)

2 ;:\

1%1 \0 = '

0 '0

-. Ha10 \0 = 6

!= ~ns/tcl/lib/ns-mobilenode.tcl ~ns/arp ) ARPTable h 0 ; 0 '3 1C

% ) ' '%
;'

&Interface Queue
8 '. ) ) Z \ 1"
;'! 0 '3

(.0 =

'

!= (. 1"

) ~ns/priqueue . !

^ X

;' *!

PriQueue

!] 4*# =

>!. = (.0 = { .D!6

! u$3 ;'3

&MAC Layer
T 1# 0 \ (distributed coordination function)DCF 1 "
1 "

V= H#

@ 0 s12 s )

RTS/CTS/DATA/ACK

(" )
3

4*# =

MAC 4*# = F

1 "

; ) ) ~ns/mac802_11 C ) Mac802_11 h 0 ;' 0

Z
!=CMU

@ ;'

-. broadcast

!= ) ;' 0

(" )

DATA

* !6 L 0

-.

&Tap Agent
'3

+ (

[!= MAC 4*# = '

) ) { .D!6 4$^ *

Tap h 0

3Agent

; ) ) ~ns/mac ) Tap h 0 ; 0 '3 1C \6 ) ) ( $3


&Network Interfaces
0 .

) T ) ;'3

\6 )
I!

L 0

) .

5# L'

n 7.
T)

3 *> ) 0 ' .]3 meta data


) . L' T 1# '

s12 s )

) 3

L ) ' I!

;'
^ X '

3 2

) 6 \V T ) H 0 *>

!= Phy/wirelessPhy h 0
3 4C

(>]a ;' 0

;: !, _1 L1X L ! L M.
3 *> ) ; 1

-. L ) '

-.

)' 4f ' 1
!!Y# )1e

I!
(.0 = L )
-.

' !2 *> T ) )

84

direct-sequence

DSSS 1 ) T ) L'

;\

6 5 @ 1# - '

;' .]3 1%1 ~ns/wireless-phy \ (6 ) h 0

L )

!Cg qL 0 @ 0

!$V# ) spread-spectrum

;'

&Radio Propagation Model


L1 6 Two ray Ground ) 48 16
Two ray Ground ) '!

'

1/(r*r) L1 6 Free-space H
)1C

! zX

L'

-. 1/(r*r*r*r)

2 (] 0 6 \ (6 ) 0 )1X @ $3 ;' 0
;1

48 16

! 1%

)1C

o) L !
&Antenna

;' 0

-. n 7.

3 2

)L 0 (

gain

'
;'! 0 Y%
n 7.

TDMA

802.11 * 0 '

!= n 7.

H omni-directional . H
~ns/antenna

3 *>

. !

^ X

+ MAC

3 Z

NS ) MAC Z

3 2

4*# =
;\

&MAC 802.11 4*# =


;'! 0 Y%

~ns/mac_802.11

. !
&

.* ;' 0

) multi hop T!7

-.

2'a
\

!"

^ X @'

!A1# $0 4*# =

F>% ) 4> )

= (Time Division Multiple Access)TDMA 4*# =


I!]M# \ '!= 4*# =
!= L ) 1 3 s )

0\

'!

-V
.

0 )1X @ $3

e) %
; ' .6

) '
e) @

85

-.

(.0 = \6 )

L ) \ )c

3 time slot TDMA ) 802.11 4*# = b*^

D.V

*> ) TDMA 4*# = )1 ) ;' 12

TDMA frame H 3 time slot


) ) ~ns/mac-tdma

;'! 0

&n 7
(D*# =

;I

-. ns ) n 7.

0 @! ) '1

'0 3 ;' 0
3'0 '! 1#

3 *> )

!]

3 *> ) 0

I!

(D*# = D.V c 1

+ 4*# = c1 ) (a

;' .]3 tora dsdv,dsr,aodv


&DSDV
I3 1 )

) 0

3 ]$3 1

\0 = ; .6 '3 1C } -# 1
3 !]

!] s )

e )1 !]
2 'Y# 0

0 '0
!] @ '

+ *>
kV

) 4*# =

!!d# 0 .

!]

'5M H

)1= 1

)$

( d!= 4*# =

!] L '%

.$0
)
]$3

. ! W!# # ) $

'0 ' \>o !]

0
C

0 2 2 ;' '3 1C U V. )

.$0

0 '3 1C )

\6 )

\0 = L '%
;\

) )

.(

:' 6
)

) ) ;:' .]3
# L '%

-. ' )

2 ;\

!] 0 ' 0

-.
)L )

!]

cache ) ' 1 \>o L '% ) 0

3 !] ) 'M

;' '3 1C 1D%

3 32 !

) W!# # ) $ ; ' ' 3 1C

@ ]* '5M H

;'5M @
) dmux

3 !] )

) ;' ' 3 1C

'3 1C \ ( ! ) 2 'Y# .6) !


]1

L ># ]$3 n 7.

) @ W!# # ) $

L )

<EE

'Y #Z

0
)1=

Z ) ;:C++

!= ~ns/tcl/mobility/dsdv.tcl

'

&DSR
\ o n 7.

3 2 0 '!

%1# ;\ Dynamic Routing Protocol

;' 1 ) routing agent T 1# 2 T 1# ' \6 )


'

!]
$#

4*# =

^ X

\0 = 2 1 Ha ' F>

' > '5M h)

'! )

(.0 = $# ' n 7.
!]

^ X

-V 4*# =

3 2)

1 @ -.

\0 = 3 DSR agent )

\0 = 2 ; 0 '3 1C L ) 1D%

) ) \0 =

86

^ X

2 ' 0 \ 1C)

:routing agent

!]

!]

(.0 = s

]$3 2 ; 0 '3 1C broadcast 1C

^ X

3 ]$3

<EE )1= DSR + ) ; 0 '3 1C L ) @

= \6 )

;' ) ) ~ns/tcl/mobility/dsr.tcl ~ns/dsr

(D 6 ) DSR ) ) . !

^ X ;' 0

-.

& TORA
tora

R0 H

h1*Y H !"

QUERY d!= H ) ) '5M H

'5M h) 4
2H

2 3 ) ;\

'5M \

# \0 =

;)

) UPDATE \0 = '5M 1C '5M h)


0 '!
'5M

%1# ; 0 ' 3 1C )
)

) '5M h)

2;1

link reversal I. )1 "

!]

.]

! '5M h)

) '( \ '5M

!]

3 2 !

% '5M
broadcast )

'0 @ ^ 0 '0
'

) '5M h)

) ; 0 '3 1C L ) ' . 6

.$0 L1X @ $3 0 @

) CLEAR \0 = '5M h)

1.7 @ 0 n =

L1*# =

2H . ; 1

' ) bR ; 0 '3 1C X ) *> '

) 1C L '% !

>X ( ) L '%
' 0 \ 1C)

2 1 \6

; 0 '3 1C L ) L '%
(.0 = m$r 4 17#
))

0 '

-.

IMEP (Internet MANET Encapsulation Protocol) ) tora s )


i \0 = H

4*# =

:ad hoc ) \ o kV

3 2

) ;1

@*

"

-.

3 2

]$3 L 5#

( d!=

; ) ) ~ns/tcl/mobility/tora.tcl ~ns/tora ) . !

)15

!!d#

!]

'3

[3 0

HELLO W]a

^ X ;'3

&AODV
$3 .>" 0 ' )
;' 0 $

) '5M
)

!] 3 2 0 \]!

M AODV ' ) ) >.Y

Route Request RREQ 4


'

' .]3 UDP

( d!= c1

0 '0
( d!=

Z 0\

D8 6 )

3 !] 0 L 5# H
-.

(* !" ) '(

c1

!] c1

(. O M ) ;: 1
0

'%

$
( d!=

-.

4*# =

;\ Route Error RERR Route Reply RREP


;' .]3 "1$Y IP '

@ $3 (

87

U 1% ) '5M T 1# W!# # ) $
'0

kV

-.

3 !]

C a H ) W!# # ) $ ; 1

.] H ) )
H

;' 0

1%1

!] H .] '5
( )

' '3 1C '

! F

) '0

) 3

L ) ' 0 \ 1C)
2 0

;' .]3 #

L ) 1%1 '5M

3 h)

' . 6 RREP '5M bR ;: )

!]

^ X \ 1C)

D.V '8 M
$#

( )

.2) W!# # ) $

3 !]
#

( d!=

) L 5#

^ X

)18

( d!= 2 ;:' 3' RREP ! '5M


;\

.6) !
'0

L 5# @ 0 1

z` )

RREQ d!= H ' 1#

U V.

' 0 [V= broadcast

2 ' 1 \6 )

) '. \]! >.Y

$ ) I!M.] !,
(.!D

sequence number

' 3 1C )

.$] 0

. @
]$3 H

H )

!] 1

3 2

)15 Hello
Hello

]$3 H
%1.

2H 0

L Y6 ) !] @ 0

]$3

\6 ) ) RERR d!= 0 F> H ;' 0 \6 ) RERR d!= H 0

'0

route discovery

beacon

' 1#

'5M

s) 2 ) !] 0

*$ ' .]3 L Y6 !] H

L ) ) RERR d!= H 1C ' !

;' 0
;'3

route

%1.

!] L '%

# ' '3 1C ) *# L )

0\

'0 0

) RREQ

0 1
;'3

D7

Aodv

'5M W!# # ) $ H

) 1C L '%

% AODV s ) ;' 0 L ) )

(* !" @ 0 )

0 \ DSDV DSR 4*# =


hop by hop routing

0 ' 3 1C \0

+ )

(.!D

>!0 # F

) 4*# =

DSR 4*# = maintenance

3 2 $# ; 0

-. DSDV 4*# =
;' )

n 7.

2H ; 1

\6 '

;' ) ) ns/aodv and ns/tcl/lib/ns-lib.tc ) . !

^ X

) ) simple-wirless.tcl 4 6 L f

) 0)

0 I!3 1C

MAC Z (Interface Queue)IFQ 8 T ) (link layer)LL O >#) Z 4f 1C


;\ L ! \6 )

L M.

*> p % 4
I!

L 0

88

) . L' q . c1 4f

'! 0 \

3 . )=
3'0

;I! 0 kV

7!A1# ) ; 0 kV

'

) *> % c 1 '
) !, ad hoc

!>

'. )

!] 4*# = c1 q1 )

; ' .-2 !!d. 3


;\ I!

(.R *

TM6

)1>

0 \ val() '

-.

3 !!d.

Y#

5.V
0

#
============================================================
==========
# Define options
#
============================================================
==========
set val(chan)
Channel/WirelessChannel ;# channel type
set val(prop)
Propagation/TwoRayGround ;# radio-propagation model
set val(ant)
Antenna/OmniAntenna
;# Antenna type
set val(ll)
LL
;# Link layer type
set val(ifq)
Queue/DropTail/PriQueue ;# Interface queue type
set val(ifqlen)
50
;# max packet in ifq
set val(netif)
Phy/WirelessPhy
;# network interface type
set val(mac)
Mac/802_11
;# MAC type
set val(rp)
DSDV
;# ad-hoc routing protocol
set val(nn)
2
;# number of mobilenodes
;I! 0
set ns_
;I! 0
.

+ ) simulator

1$ H main )

D8 \$] ) L

[new Simulator]
1C 6 ) trace-all() L )

) (

3) 'M I ) '5 0

0
3 . )=

) simple.tr 4 6 ' :trace @ 0 L >

>!. = )1e

3 !!d. @ 4C ) NS ) 0 \

I! 0 @ ! '

D 64 6
;I! 0

set tracefd [open simple.tr w]


$ns_ trace-all $tracefd

kV

I!

89

x,y

kV

.>" ;\ 500*500 I!.-2 0 )1X @ $3 ;I! 0

kV

) 1C *>

Q1"1R#

' I3 L

; (x=500,y=500) 1
set topo [new Topography]
$topo load_flatgrid 500 500
1

) '% ) 'M H @ 1.! ;' '3 1C 9 H!*-# grid )1# ' $3 + )

. ) = @ 1 ^ H!*-#

Q1DR#

; 0 L ) Z ) load_flatgrid
) ) god

; 0 I!3 1C +

create-god $val(nn)
^ X

!Cg

) ) 0

-#

L '% n 7.
hop

'Y

!a $3

2 ;' 0

) '(

X) ; 1

'Y 2

;1

@1 0 I3 ; ) ' ^ X

2 3

-. L M.

1 "

'Y [V

!@
)

-.

^ X b= '
)

1C 6 MAC ! T 1# n 7.

0 '!= !!d# %

3 2 q *> q T!7 \"

!> c

( # 1$ H TM6 0 \
;I )

-.

) `2)
;' '

X ) god

'

god

3 2 'Y#

!>
~)

! God (General Operations Director) D0 L $^ ' 0 \ '3

0\

"1$Y

13

1 "

(D 6

Lf ) 'a 3 ; 1
'Y 2 L M.

1 "

3 2 ) ' 1#
)1C $ )

-. 'Y

ns2.1b6 @Q)

-.

(D 6

Lf

(.!D

-. ; 1

^ ;\

) ) God

'Y
31 "
$

'

!>

0 I! 0 *6

API .>" 0 0 I!3 1C + ) n 7.


; 0 I!3 1C

; 0

Z1$Y

.$0

Y# ~ns/tcl/mobility/com.tcl) create-god L )

'

3 2 +

!> ) (.0)

! )1 hop 'Y#

@'! )

[!= L -.

) )

-.

\]! 1%1 ns2.1b5 @Q) ) ' '%

3 2

3 API
3API & .*

90

Y# 4

API

;I! 0 '

*!= ) ( ' I! 0 + ) ( *

!, IFQ q MAC layer qlink layer q

[!= ) 3 2 0 \

!] 4*# = c1 q : !,
; 0

Y#

>#

]D \V# f

'. )

3 h) c1

*!= API @ 1#

' $3 ) '

;\

# $ns_ node-config -addressingType flat or hierarchical or expanded


#
-adhocRouting DSDV or DSR or TORA
#
-llType
LL
#
-macType
Mac/802_11
#
-propType "Propagation/TwoRayGround"
#
-ifqType
"Queue/DropTail/PriQueue"
#
-ifqLen
50
#
-phyType
"Phy/WirelessPhy"
#
-antType
"Antenna/OmniAntenna"
#
-channelType "Channel/WirelessChannel"
#
-topoInstance $topo
#
-energyModel "EnergyModel"
#
-initialEnergy (in Joules)
#
-rxPower
(in W)
#
-txPower
(in W)
#
-agentTrace ON or OFF
#
-routerTrace ON or OFF
#
-macTrace
ON or OFF
#
-movementTrace ON or OFF

; addressingType: flat %
0 on ) routerTtrace agentTrace

.>" \ NULL 6 [!= )1r


3 ) 'M

! u$3 ;I

qAGT agentTrace ) 'M trace 4 6 ) .' $ trace


2 ;'

kV

; ' kV

3 'D!6

!$+ = )

M 'D!6 !

MAC
@

) !.C

3) 'M

-. flat 6 [!= ) 'M @ $3

0 on ) ( $3 '! 1#

macTrace

RTR

3 2 L M. 1 on

) n 7.

&\
# Configure nodes
$ns_ node-config -adhocRouting $val(rp) \

4*

$#

3 2

+#

$ ;I

routerTrace ) 'M
movementTrace
'

*!= L f )

91

-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-topoInstance $topo \
-channelType $val(chan) \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace OFF

!!d# ) 'Y# 2

@ 1#

) )

for MD ) @1a .>" 0 I! 0

+ ) n 7.

:nn number of node ) 'M !!d#


for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node ]
$node_($i) random-motion 0
}
\^

\ 1 I!3 1C
;I!3

) ) 3 2 \!Y 1

L M.

\!Y 1 H (

b= I

)0 c

;# disable random motion

' L Y6 !, + ) 3 2
0 + n 7.

3 2

6 5# \0

0 L ;:\0

#
# Provide initial (X,Y, for now Z=0) co-ordinates for node_(0) and node_(1)
#
$node_(0) set X_ 5.0
$node_(0) set Y_ 2.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 390.0
$node_(1) set Y_ 385.0

!]

92

$node_(1) set Z_ 0.0


;I

3 2 L M. >" ;(390,385) ) 1 2

1 '3 1C (5.2) \!Y 1 ) '. ) 0 2

#
# Node_(1) starts to move towards node_(0)
#
$ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0"
$ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0"
# Node_(1) then starts to move away from node_(0)
$ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0"
!> 50 ! o ) 0 \
\^

!] !!d#

# )1.

$ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0 )1.


! a ; 0 '3 1C \0

15m/s \^
&I! 0

x=25,y=20 '5M

;' .]3 n 7.

3 2 L M.

! ) *!6 # L

# TCP connections between node_(0) and node_(1)


set tcp [new Agent/TCP]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns_ attach-agent $node_(0) $tcp
$ns_ attach-agent $node_(1) $sink
$ns_ connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns_ at 10.0 "$ftp start"
; 0 '3 1C + 0 2 F>
I!3 1C kV

) n 7.

3 2 *> % @ 0 reset

>!

2
1# @

! TCP L 5# H ) 0
31)

' $3 L
; 0

93

#
# Tell nodes when the simulation ends
#
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at 150.0 "$node_($i) reset";
}
$ns_ at 150.0001 "stop"
$ns_ at 150.0002 "puts \"NS EXITING...\" ; $ns_ halt"
proc stop {} {
global ns_ tracefd
close $tracefd
}
$ns_ halt" is called at
) @ 3 2 ' '3 1C 1.
;' '3 1C

1C 6 trace 4 6 .] @ 0 " C

!> 150 ! o )

!>

stop{} L ) ; 0 ' 3 1C reset 0 150.0002s

&\ 1 I!3 1C 1 )

4 6 C ) )

!> c

)1.

I3 C )

puts "Starting Simulation..."


$ns_ run
;' ' ) [ $

'Y

6 H ) 3 2 .>" ' ) [ $

) 1)

' 4 04 6L

simple-wireless.tcl
# A simple example for wireless simulation
#
===========================================================
===========
# Define options
#
===========================================================
===========
set val(chan)
Channel/WirelessChannel ;# channel type
set val(prop)
Propagation/TwoRayGround ;# radio-propagation model
set val(netif)
Phy/WirelessPhy
;# network interface type
set val(mac)
Mac/802_11
;# MAC type
set val(ifq)
Queue/DropTail/PriQueue ;# interface queue type

94

set val(ll)
set val(ant)
set val(ifqlen)
set val(nn)
set val(rp)

LL
;# link layer type
Antenna/OmniAntenna
;# antenna model
50
;# max packet in ifq
2
;# number of mobilenodes
DSDV
;# routing protocol

#
===========================================================
===========
# Main Program
#
===========================================================
===========

#
# Initialize Global Variables
#
set ns_
[new Simulator]
set tracefd [open simple.tr w]
$ns_ trace-all $tracefd
# set up topography object
set topo
[new Topography]
$topo load_flatgrid 500 500
#
# Create God
#
create-god $val(nn)
#
# Create the specified number of mobilenodes [$val(nn)] and "attach" them
# to the channel.
# Here two nodes are created : node(0) and node(1)
# configure node
$ns_ node-config -adhocRouting $val(rp) \

95

-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace OFF
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node]
$node_($i) random-motion 0
}

;# disable random motion

#
# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes
#
$node_(0) set X_ 5.0
$node_(0) set Y_ 2.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 390.0
$node_(1) set Y_ 385.0
$node_(1) set Z_ 0.0
#
# Now produce some simple node movements
# Node_(1) starts to move towards node_(0)
#
$ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0"
$ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0"
# Node_(1) then starts to move away from node_(0)
$ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0"

96

# Setup traffic flow between nodes


# TCP connections between node_(0) and node_(1)
set tcp [new Agent/TCP]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns_ attach-agent $node_(0) $tcp
$ns_ attach-agent $node_(1) $sink
$ns_ connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns_ at 10.0 "$ftp start"
#
# Tell nodes when the simulation ends
#
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at 150.0 "$node_($i) reset";
}
$ns_ at 150.0 "stop"
$ns_ at 150.01 "puts \"NS EXITING...\" ; $ns_ halt"
proc stop {} {
global ns_ tracefd
$ns_ flush-trace
close $tracefd
}
puts "Starting Simulation..."
$ns_ run

97

98

I.-3 456

!> ) (.!D

H!6 # L M.
I!

kV

1)

) 0

scen-3-test

'7 H 4C
L M. 4 6 H

Z M. 0 I 0 % ) I!

(D 6 +

*>

1)

2 ? Z M. 0 0 I!3 1C Y L f

!~ %;' 1

-.

D> [V )

) ' a 3 ;'

+ 1 '

6 5# L M. scen-3-test 4 6 ;'

(D 6 ! a;\ ~ns/tcl/mobility/scene h) ) L M.
G [V ) L M.

(D 6

;\ 670m*670m Q1"1R# H 4C )

@1^ 4 6

1 "

(D 6 ) h .

\ ns F 1#

+ setdest 2 L M. ' 0 + CMU

-.

@ 1#

; 0 I!3 1C
H!6 # 1 " 4 6 H
1%1

W5
1 )

'!3 1C
\R *

' 1#

! ( 0'1

+ n 7.

H!6 # F>% )

(.0 = 0 0 + n 7.
. !

3 !a '! 0 '3

;\ cbr-3-test @

2 ? ! TCP CBR
)4 6

)15 CMU TCP/CBR H!6 # ' 0 +

)
)

3 2 ! ! 3 H!6 # 3 2 L M.

~ns/tcl/mobility/scene h) ) ! 4 6

.$]

( % @ 1#

2 ;' 1 L ) 1D%
-.

' 1#

; 0 I!3 1C

0'1
4 6

^
' 1C
;\

) \6 ) qL ) ' 1#

*!6 #

31 " ! a ;\6 2

) ) ( E [V ) 0 ' 1

99

wireless1.tcl ) @
C \R * c
@

!!d# I 0 + 9 \$] ) 0 ) simple-wireless.tcl I ) '5


)

. q LL,MAC 4f ' 1 '

: !,

seed ) 'M H q x,y

6 5# '^ ' 0 +

;' ' \]!"

Y# 0 D>

3 !!d.

'7 q 2 L M. 4 6 q O >#)
) 0I

0 6A

^ ;I ) `
1 "'

!> @ 0

3 . )=

1.

set val(chan)
Channel/WirelessChannel
set val(prop)
Propagation/TwoRayGround
set val(netif)
Phy/WirelessPhy
set val(mac)
Mac/802_11
set val(ifq)
Queue/DropTail/PriQueue
set val(ll)
LL
set val(ant)
Antenna/OmniAntenna
set val(x)
670 ;# X dimension of the topography
set val(y)
670 ;# Y dimension of the topography
set val(ifqlen)
50
;# max packet in ifq
set val(seed)
0.0
set val(adhocRouting) DSR
set val(nn)
3
;# how many nodes are simulated
set val(cp)
"../mobility/scene/cbr-3-test"
set val(sc)
"../mobility/scene/scen-3-test"
set val(stop)
2000.0
;# simulation time
!] 4*# = @ 1 ^ (dynamic source routing)DSR
;I! 0
! u$3 ; 1
I

0 + 0 )

.6 !!d# ? n 7.

3 2 'Y#

-. (destination sequence distance vector)DSDV

@ 0 trace
*>

! u$3

wireless1-out.tr 4 6 simulator 1$

*!6 2

)18

#I

)0

'Y

) ! ) (network animator)nam
;'3 @

set tracefd [open wireless1-out.tr w]


$ns_ trace-all $tracefd

;# for wireless traces

set namtrace [open wireless1-out.nam w]


;# for nam tracing
$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)

L i

)18

100

) val(cp) val(sc)

> 0 O >#)

1 "

(D 6

3 2 L -. :n 7.

3 2 +

;' ' 3 1C

-.

F>

'Y 'Y 2 )
)15 ' '

Y#

#
# Define node movement model
#
puts "Loading connection pattern..."
source $val(cp)
#
# Define traffic model
#
puts "Loading scenario file..."
source $val(sc)
& 1 '3 1C

!>

2 L M. )1.

) scen-3-test 4 6 2

0 ' I!3 1C '! 0 e

$ns_ at 50.000000000000 "$node_(2) setdest 369.463244915743 \


170.519203111152 3.371785899154"
(368.4,170.5) '5M

!> 50 ! o ) 0 \

< 2

;' I!3 1C

TC

!>

rC 4 6

I!.-2 4> [V ) 0 )1X @ $3


3.37m/s \^

! u$3 ; 0 '3 1C \0

$god_ set-dist 1 2 2
#10 0 \
god

'

3 2 ! !]

# #10 > 7

6 5# waypoint I. )1 "
8 5.C @

8 5.C

-. hop

;1

-.

) L M.

^ X

) god

# #10 god

^ X

^ X
1 "
' 1#

~)

; ) _ !.

;'

) @

) `2 )
2

@ 1#

)1.

TC

< 9 2 ! !]
0 '

!>

#
X)

) G E [V ) 0 setdest

(D 6 : 1

! u$3 scen-3-test 4f (D 6

;' 0

;' 0 ) `2
' 1#

(~ns/indep-utilities/cmu-scen-gen/setdest/calcdest) ' 12

S 6 ' a calcdest

;1

-. god

^ X

'

+ L M.

calcdest @
1 "

(D 6 !]-#

101

2 ; 0 '3 1C 4$^ \ )
@

' > W

) `2) 4 6 3 4*
. )

\ 6

.( ' 0 ) '

) 4 6 ad-hockey

) $

\ 6 2 @1a )
2 ;\

) L M.

0 + $ 0 ) L M.

1 " 4 6H

@ ! ) @

ad-hockey

!Cg bR

;' '3 1C + calcdest


12 3

!]

0' 0

> 7

1 )

;'! 0 L $^ !

)18 )

'

1C b= ; '

@ 1#

0\

1%1 L M.

(*# = )

!!d#

!( !] L1X 4!D7#

\ ) \ 6

) 3 2 ! hop 'Y#

e6 8 Y
1 "

% C 4 6 ' 1V

# #10 setdest calcdest

!> ) ) . L' X C
) I3

) I3 1 )

'7

monarch Q = T 1# '

god

0 !] L1X

^ X

&4
L M.

'3

% C)
)

1 "H )

^ X q'0

-.

!]

-. ad-hoc *>

'

CMU 0 # ^ X

-. @ *

Q1"1R# !!d# x @ 0 kV
( V

(*# = : ^ X

'

b 06
">

;1
' a 3 ;I! 0
)

6A )

.!] @' I2

(D 6 '

; ' a CMUTrace \0 = 3

]$3 ) ) 4 0 god

1 " 4 6 O1rC ) )

3'0 nam ) 3 2 !" @ * @ 0 kV


H!6 # [ $ ;\

'3

-. ' 1#

wireless1.tcl 4 6

4 nam ) 3 2 L M. TM6 0 '!


;' '

>!. = 1 3 I!

)
%1#
31 )

# Define node initial position in nam


for {set i 0} {$i < $val(nn)} {incr i} {
# 20 defines the node size in nam, must adjust it according to your
# scenario size.
# The function must be called after mobility model is defined
$ns_ initial_node_pos $node_($i) 20
}
&\ ns_run TC 4> TM6 0 I! 0

6 A ) CMUTrace 4 6

^ X

3'

102

puts $tracefd "M 0.0 nn $val(nn) x $val(x) y $val(y) rp $val(adhocRouting)"


puts $tracefd "M 0.0 sc $val(sc) cp $val(cp) seed $val(seed)"
puts $tracefd "M 0.0 prop $val(prop) ant $val(ant)"

1 "

(D 6 0 ' 1

) \R *

)1.

0 !Cg ) wireless1.tcl 4 6 L

m$r
/ # L ;'

1%1 Z ) '

.-2 6 [!=

;'

!!d# \R *

3 )1.* ) L M.

' $!

1 "

O >#)

;'! 0 %
ns wireless1.tcl
4 6 nam % C 4 6
!!d# X C

wireless1-out.tr

; 0 '!3 1C '3

) 2

CMUTrace % C 4 6 % @' 4 0

\0

nam 4 6

AgentTrace, RouteTrace, MacTrace )


1 I3 1 )

'7 _) C < P 2 4 6

! u$3 ; 0 '3 1C 1

3 \ < P

'Y

;' '3 1C wireless1-out.nam

on/off

3 ) 'M

trace

% Cq

) ; 0 '3 1C L $^ ) \R * 4C movementTrace
3 1 )

'7 ) 9 2 ; 0 ' 1V 1
; 0 '3 1C

&'! 0

I3

!] ) < P !

(.0 =

) wireless1.tcl 4 0 4 6

) L

wireless1.tcl
# A simple example for wireless simulation
#
===========================================================
===========

103

# Define options
#
===========================================================
===========
set val(chan)
Channel/WirelessChannel
set val(prop)
Propagation/TwoRayGround
set val(netif)
Phy/WirelessPhy
set val(mac)
Mac/802_11
set val(ifq)
Queue/DropTail/PriQueue
set val(ll)
LL
set val(ant)
Antenna/OmniAntenna
set val(x)
670 ;# X dimension of the topography
set val(y)
670 ;# Y dimension of the topography
set val(ifqlen)
50
;# max packet in ifq
set val(seed)
0.0
set val(adhocRouting) DSR
set val(nn)
3
;# how many nodes are simulated
set val(cp)
"../mobility/scene/cbr-3-test"
set val(sc)
"../mobility/scene/scen-3-test"
set val(stop)
400.0
;# simulation time
#
===========================================================
==========
# Main Program
#
===========================================================
===========
#
# Initialize Global Variables
#
# create simulator instance
set ns_

[new Simulator]

# setup topography object

104

set topo [new Topography]


# create trace object for ns and nam
set tracefd
[open wireless1-out.tr w]
set namtrace [open wireless1-out.nam w]
$ns_ trace-all $tracefd
$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)
# define topology
$topo load_flatgrid $val(x) $val(y)
#
# Create God
#
set god_ [create-god $val(nn)]
#
# define how node should be created
#
#global node setting
$ns_ node-config -adhocRouting $val(adhocRouting) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace OFF \
-macTrace OFF
#
# Create the specified number of nodes [$val(nn)] and "attach" them

105

# to the channel.
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node]
$node_($i) random-motion 0
}

;# disable random motion

#
# Define node movement model
#
puts "Loading connection pattern..."
source $val(cp)
#
# Define traffic model
#
puts "Loading scenario file..."
source $val(sc)
# Define node initial position in nam
for {set i 0} {$i < $val(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) 20
}

#
# Tell nodes when the simulation ends
#
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at $val(stop).0 "$node_($i) reset";
}
$ns_ at $val(stop).0002 "puts \"NS EXITING...\" ; $ns_ halt"

106

puts $tracefd "M 0.0 nn $val(nn) x $val(x) y $val(y) rp $val(adhocRouting)"


puts $tracefd "M 0.0 sc $val(sc) cp $val(cp) seed $val(seed)"
puts $tracefd "M 0.0 prop $val(prop) ant $val(ant)"
puts "Starting Simulation..."
$ns_ run

107

I. 3 456

I!
;' 0
*>

*>

I!

O >#) +

>!. = wireless LAN multihop ad-hoc


0'

I!3 1C W!0 # I!

-.

3 LAN

*> H

wireless1.tcl 4 6 %

>!0 #

!>

( V )

0 + I!

) 4>

) b= ;' 0

;\ `2 I!3 1Cwireless2.tcl I!

3 *>

Q1"1R# 0 '

L ) .] n 7. !,
I

4>

L 5# 1 )

( V ) '

ZF 1

*>

[V

3 2 n 7.
4 6

)@

C ) \

31)
*$

) ;' ' 45. I!


3 2@ ) 0 0
I!3 1C !!d# ) D>

108

I!

getaway ' $3 =
) Lf

Q1"1R#

I!3 1C W(0) , W(1)

0\

3 2 ;' ' 45. BS

.]
4* ;' 3

2 c1 ! \0 = L >#

I!

>!0 # 1 )

.]

2H

node0,1,2 I!

I!

% '0

4$^ I!

@
3 1

;'3
'. ;I! 0

) , ) 1C ) 0 wireless1.tcl 4 6 c

@ ' I!

I!

!!d# )

!>

3 2 ! CBR TCP
1# @

) 0 0 I!3 1C

0 I!3 1V

-. opt() )

) 3 !!d. \]!"

X >#)

-.

val() )

1 " 4 6

-.

;\

!!d# DSDV

.6

D> O >#)

!!d# @ 0 Ha L

ad-hoc

!] 4*# =

! u$3 ;' '3 1C

+ ) 0 '!

\]# \R * ) TM6 @

.
'7

\ ;
1>

!!d#
I!3 1C

Z1X !!d.

set opt(adhocRouting) DSDV


set opt(cp)
""
;# cp file not used
set opt(stop) 300
;# time to stop simulation
&I
set opt(ftp1-start)
set opt(ftp2-start)

Y# 1

) TCP

( %c

! u$3

160.0
170.0
;' 1

-.

.]

I!

3 2 'Y#

Y#

O1rC

set num_wired_nodes
2
set num_bs_nodes
1
1

! (.0 = W!# # )

Q1"1R# )
1D%

Z 5#

>#
=

]D

I!

3 2

) L ) L '%

(.0 = I ) ' I]M.]


;' 1

!]

)18

0I)

Z 3

ns ) U

!]

Z 5#

;3 2

) link 1(- I!]!

!] 3 ]$3 ! ad-hoc

!> W!0 #

D8 [V ) L

^ X ;I! 0
!6 d% L 5#

-. I!
1 ah

3 2 ) 0 'a 3 ; 1
!]

(*# = 1D%

I!

3
Y ;' .]3

-. I!
) L ) L '%

23
-.

109

L 5# 1

! 0 0 I!3 1C

) 0 >#

]D ) .C

-.

.]

I!

I!

@ ! + ) 0 cluster

1(- ) : 1

3 2 ! (.0 = L >#

3 1

3 1 ;' 0

O1rC set ns [new Simulator] TC 'Y b= ;' '

;'! 0 6 A )

b=
)

Y# ' ) [ $

$ns_ node-config -addressType hierarchical


AddrParams set domain_num_ 2
;# number of domains
lappend cluster_num 2 1
;# number of clusters in each
;#domain
AddrParams set cluster_num_ $cluster_num
lappend eilastlevel 1 1 4
;# number of nodes in each cluster
AddrParams set nodes_num_ $eilastlevel ;# for each domain
Y# >#

]D

Q1"1R# bR

H 3) 3 .
1 )
)

0 'Y# ;:I!

3.

1 4C

.
;:'! 0

3 trace

!>

0)

2 ) (a
Z 4*
0 '!

I!

) 3 1

1 ) L

]D

3 '1

+ I!

) '
I!

) trace )
I!e # *> 4* @'

set tracefd [open wireless2-out.tr w]


set namtrace [open wireless2-out.nam w]
$ns_ trace-all $tracefd
$ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y)

'Y# ; 1

Y# 2 1

0' '

\ ; 0 I!3 1C I!e # )

;I! 0

'. ) Z O1rC )

H 3 ) 3 2 'Y# 'Y TC ; )

1% :I!

( 0\

) 2

Q1"1R#

1 ) 0 ) )
0

]D h) c1

3 2

\ W#
.

*!= >#

I!

Y# ) 3 .

Y# % C 4 6 @ $3

WL '

'

# 2 :I!

1 1 4 0 ' 0

I!

'

I! 0

1% .

H 3)

3 1
0 9 :I!

2H 0 )

Y# Q1"1R#

;' )

) tracing

!>
3 1
-# ;' 1
) nam

1%

'Y 2 )
\

*$

. 1 wireless2-out.tr
! u$3 ;' 1

110

$ 3 2
+

$# +

'!

\ ;I! 0 + )

.]

I!

qI!

O1rC create-god $opt(nn) TC 'Y b= ;'! 0 L ) '

3 2'

) 2 3 >#

]D h)

;'! 0 6 A I!

3 2

# create wired nodes


set temp {0.0.0 0.1.0}
;# hierarchical addresses to be used
for {set i 0} {$i < $num_wired_nodes} {incr i} {
set W($i) [$ns_ node [lindex $temp $i]]
}
;'
=

@
.]

) 0 I! 0 '

3 2 )

;\

node- ) !.C I!e # 4 '>#


*!= ) I!
'
$#

.]

!"

'

!* \

3 2

*>

'! )

-. I!

'! )

(.0 =

n 7.
=

;'
domain0

b= :9 P ' .6 2 )
)

1%

@ !

1
'% .

1 H TM6 I!

I!

%
0

I!
!

) ;I! 0
) I!

' )
=

.]

@ ! ) W#
;I ) I!]!

.]

I!

$!e # 0 \
!] @ 0 ON
3 1 +

.]

2 ' I!

+ 17 L f

2 .

)
Z'0

D.V

3 *>

_) C

*>

]D

3 2

2 0 \ I( ) !]

]D

) =

3 2 ) ;' '3 1C @ !

!] ad-hoc )15 I!

3 2

O >#)

0 I!3 1C '

1 ) n 7.

.]

3 2 1 @ $3 )

!> H b= ;' 0

!>

.$]

! gateway

3 2

$# 0 )1r 1 ' 3 1C I!

>#
3 *>

)18 wiredRouting ON config

'Y ; !2

I!

.]

' '% API '0

*!= 4

&' 0 L $^ )
; 2'a

; 0 I!3 1C I!e # ) wiredRouting OFF bR

(.0 = 0 )1r '

I!

Z ( ) 0 ' .]3 1

;' ' 3 1C L )
I!

BS(0) 2 )1X !$3 ;' ' 3 1C

.] @ 1 ^

0 n 7.

!]

2 +

3 node-config
=

0I)

3 2 + bR

; 1 4 '>#

3 2

-.

*!= ) 2 ) .C

domain1 I!

0 1

@!

2L
*>
( )

111

h)

) ;\

* ( h)

'.

' .]3 1 H ) 3 2 $3 b= .
;'! 0

Base-station: 1(second domain,1).0(cluster


WL
node#1
:
1.0.1(second
WL
node#2
:
WL node#3 : 1.0.3(fourth node)
.] ; I!3 )

'% 1

;I!

) 2

) ) I!
1

Y (>#

2
]D

I! 1#
Z

'3

) (

0 H ! u$3
=

.]

0).0(first node in
node
in
1.0.2(third

I!3 ) P 1 @ $3 ) ) I!
I! 0 W!0 # I3 ) 3 .

# configure for base-station node


$ns_ node-config adhocRouting $opt(adhocRouting) \
-llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channelType $opt(chan) \
-topoInstance $topo \
-wiredRouting ON \
-agentTrace ON \
-routerTrace OFF \
-macTrace OFF
#create base-station node
set temp {1.0.0 1.0.1 1.0.2 1.0.3} ;# hier address to be used for
;# wireless domain
set BS(0) [ $ns_ node [lindex $temp 0]]
$BS(0) random-motion 0
;# disable random motion
#provide some co-ordinates (fixed) to base station node
$BS(0) set X_ 1.0
$BS(0) set Y_ 2.0
$BS(0) set Z_ 0.0

0 I! 1#

I!

cluster)
cluster)
node)

I! 1#
Q1"1R#

112

# create mobilenodes in the same domain as BS(0)


# note the position and movement of mobilenodes is as defined
# in $opt(sc)
# Note there has been a change of the earlier AddrParams
# function set-hieraddr to addr2id.
#configure for mobilenodes
$ns_ node-config wiredRouting OFF
# now create mobilenodes
for {set j 0} {$j < $opt(nn)} {incr j} {
set node_($j) [ $ns_ node [lindex $temp \
[expr $j+1]] ]
$node_($j) base-station [AddrParams addr2id \
[$BS(0) node-addr]] ;# provide each mobilenode with
;# hier address of its base-station
}
2 node_(0) I!
2
! TCP H!6 #
0 45. I3 ) = .]
;I! 0

3 2

+ ) node_(2) W(1) 2 ! ! u$3 W(0) I!

#create links between wired and BS nodes


$ns_ duplex-link $W(0) $W(1) 5Mb 2ms DropTail
$ns_ duplex-link $W(1) $BS(0) 5Mb 2ms DropTail
$ns_ duplex-link-op $W(0) $W(1) orient down
$ns_ duplex-link-op $W(1) $BS(0) orient left-down
# setup TCP 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 $opt(ftp1-start) "$ftp1 start"

I!

113

set tcp2 [new Agent/TCP]


$tcp2 set class_ 2
set sink2 [new Agent/TCPSink]
$ns_ attach-agent $W(1) $tcp2
$ns_ attach-agent $node_(2) $sink2
$ns_ connect $tcp2 $sink2
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ns_ at $opt(ftp2-start) "$ftp2 start"
% c

n 7.

\ ns

1$^

3 2

^ X .!

! u$3 ' '!3 1C ) I!


!= 1 3 nam ) I!
TCP

1# cp , sc F>
0'

'

) %

(D 6 1 '3 1C wireless1.tcl 4 6 ' $3 '0 !M


) wireless2.tcl

3 2 ! H!6 # '! 0 % ) 1 )
3 2 ! H!6 # I 0 @ !

2 ; 0

3'0

C '

) ) ( '

> 0 )1X @ $3 ) I!

.>" ;:ns
"+ )
3 2 n 7#

L 5# 170 ! o ) ;' '3 1C + W(0) node_(0) 2 ! TCP L 5# 160 ! o ) ;: '


;' '3 1C +
;'! 0

" V \(% )

) wireless2.tcl 4 0 '0

wireless2.tcl
# simulation of a wired-cum-wireless scenario consisting of 2 wired nodes
# connected to a wireless domain through a base-station node.
#
===========================================================
===========
# Define options
#
===========================================================
===========
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/802_11
;# MAC type
set opt(ifq)
Queue/DropTail/PriQueue ;# interface queue type

114

set opt(ll)
LL
;# link layer type
set opt(ant)
Antenna/OmniAntenna
;# antenna model
set opt(ifqlen)
50
;# max packet in ifq
set opt(nn)
3
;# number of mobilenodes
set opt(adhocRouting) DSDV
;# routing protocol
set opt(cp)
set opt(sc)

""
;# connection pattern file
"../mobility/scene/scen-3-test" ;# node movement file.

set opt(x) 670


set opt(y) 670
set opt(seed) 0.0
set opt(stop) 250
set opt(ftp1-start)
set opt(ftp2-start)

;# x coordinate of topology
;# y coordinate of topology
;# seed for random number gen.
;# time to stop simulation
160.0
170.0

set num_wired_nodes
2
set num_bs_nodes
1
#
===========================================================
=================
# 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)
}
# create simulator instance
set ns_ [new Simulator]
# set up for hierarchical routing
$ns_ node-config -addressType hierarchical
AddrParams set domain_num_ 2
;# number of domains
lappend cluster_num 2 1
;# number of clusters in each domain
AddrParams set cluster_num_ $cluster_num

115

lappend eilastlevel 1 1 4
;# number of nodes in each cluster
AddrParams set nodes_num_ $eilastlevel ;# of each domain
set tracefd [open wireless2-out.tr w]
set namtrace [open wireless2-out.nam w]
$ns_ trace-all $tracefd
$ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y)
# Create topography object
set topo [new Topography]
# define topology
$topo load_flatgrid $opt(x) $opt(y)
# create God
create-god [expr $opt(nn) + $num_bs_nodes]
#create wired nodes
set temp {0.0.0 0.1.0}
;# hierarchical addresses for wired domain
for {set i 0} {$i < $num_wired_nodes} {incr i} {
set W($i) [$ns_ node [lindex $temp $i]]
}
# configure for base-station node
$ns_ node-config -adhocRouting $opt(adhocRouting) \
-llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channelType $opt(chan) \
-topoInstance $topo \
-wiredRouting ON \
-agentTrace ON \
-routerTrace OFF \
-macTrace OFF
#create base-station node

116

set temp {1.0.0 1.0.1 1.0.2 1.0.3} ;# hier address to be used for wireless
;# domain
set BS(0) [$ns_ node [lindex $temp 0]]
$BS(0) random-motion 0
;# disable random motion
#provide some co-ord (fixed) to base station node
$BS(0) set X_ 1.0
$BS(0) set Y_ 2.0
$BS(0) set Z_ 0.0
# create mobilenodes in the same domain as BS(0)
# note the position and movement of mobilenodes is as defined
# in $opt(sc)
#configure for mobilenodes
$ns_ node-config -wiredRouting OFF
for {set j 0} {$j < $opt(nn)} {incr j} {
set node_($j) [ $ns_ node [lindex $temp \
[expr $j+1]] ]
$node_($j) base-station [AddrParams addr2id \
[$BS(0) node-addr]]
}
#create links between wired and BS nodes
$ns_ duplex-link $W(0) $W(1) 5Mb 2ms DropTail
$ns_ duplex-link $W(1) $BS(0) 5Mb 2ms DropTail
$ns_ duplex-link-op $W(0) $W(1) orient down
$ns_ duplex-link-op $W(1) $BS(0) orient left-down
# setup TCP 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]

117

$ftp1 attach-agent $tcp1


$ns_ at $opt(ftp1-start) "$ftp1 start"
set tcp2 [new Agent/TCP]
$tcp2 set class_ 2
set sink2 [new Agent/TCPSink]
$ns_ attach-agent $W(1) $tcp2
$ns_ attach-agent $node_(2) $sink2
$ns_ connect $tcp2 $sink2
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ns_ at $opt(ftp2-start) "$ftp2 start"

# source connection-pattern and node-movement scripts


if { $opt(cp) == "" } {
puts "*** NOTE: no connection pattern specified."
set opt(cp) "none"
} else {
puts "Loading connection pattern..."
source $opt(cp)
}
if { $opt(sc) == "" } {
puts "*** NOTE: no scenario file specified."
set opt(sc) "none"
} else {
puts "Loading scenario file..."
source $opt(sc)
puts "Load complete..."
}
# Define initial node 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) 20

118

}
# Tell all nodes when the simulation ends
for {set i } {$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 EXITING...\" ; $ns_ halt"
$ns_ at $opt(stop).0001 "stop"
proc stop {} {
global ns_ tracefd namtrace
# $ns_ flush-trace
close $tracefd
close $namtrace
}
# informative headers for CMUTracefile
puts $tracefd "M 0.0 nn $opt(nn) x $opt(x) y $opt(y) rp \
$opt(adhocRouting)"
puts $tracefd "M 0.0 sc $opt(sc) cp $opt(cp) seed $opt(seed)"
puts $tracefd "M 0.0 prop $opt(prop) ant $opt(ant)"
puts "Starting Simulation..."
$ns_ run

119

120

I( 456

I!
@ $3 I ) '5
) I!

Q1"1R# H ) MobileIP +

I
L ;I 0 + ) =

1 @ $3 1 )

! ForeginAgent(FA)

.]

-.

) I!

*> L 5# 4> \$] )

) ) MobileIP

) ;I!3 Y 1# ' '% 1 )


HomeAgent(HA)

I!

.]

>!. = D>

! u$3 I ) W1

1)
W0 2
;I )

121

' ) [ $
4 ^
1

#Z 5# ! a ;\ 45. FA HA Y %) C

4* )

1% MobileHost(MH) 2

C4 ^ ! 0 )
MH 0 . ;I!

) HA
;:' $

+ MH
'3

(.0 = L ) I!3 1C

FA 1

!!d#

set opt(nn)
set opt(stop)
sc cp

(D 6

!!d# )
1
250

3 2 ! TCP CBR
sc 4 6 ) ;' 1 L ) 1D%

) ' 1% 4 6

!> @ = @

n 7.

\0

%) C

_) C HA 1

Y 1C

3'0 $# )
3 2 'Y#

FA

)0

@*

"M

) Y# \$] ) ;' '

! ) TCP L 5# 0 I ) '5

( % cp 4 6 0 )

1C @1a L f

;I! 0 $

-.

I 0

-. 4> ) 0

1 )

(.0 = 0

0 + n 7.

Q1"1R# H 4C ) 3 2

6 5# L M.

) \6 ) qL ) ' 1#

""
""
;I! 0

set opt(ftp1-start)

;' 0

;# just one MH

;\
set opt(cp)
set opt(sc)

2 { ;\

) wireless2.tcl

0 I!3 1C [

I! 0 + MH L M.

3 !] MobileIP 4*# = T 1# 0 I! 0 '3

4* ) 0 )1X @ $3 1

1C ) 0 ) !A1# )18
;I!3

roaming n 7.

MH W0 ! TCP @ % H 0 I ) '5

.>" ;\ `2 I!3 1C wireless3.tcl


@!

C 4 ^ W1 I!

100.0

kV

) TCP@ % c

)1.

122

.]

32

-. 1 )

I!3
Y

!!d#

)1.

) I!

)1r num_bs_nodes !!d.

3 2 'Y# $!M.] 3 2 'Y# !!d# )

' '3 1C

)15 HA,FA

>!. = 4$^ +

;' '3 1V
set num_wired_nodes
]D
1

Y#

1 H 0

O1rC ' I!e # W#


-#

I! 0
;I!3

D]D

h) \ 6 '

4$^ wireless2.tcl ' $3 %


!!d# ) .

3 . )=@

+ ns

1$ 0 TC < 'Y

) ;'! 0 6 A ) H Q1"1R# W#
r

I! 0

FA

kV

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 2 1
;# number of nodes in each cluster
AddrParams set nodes_num_ $eilastlevel ;# of each domain
& 0 I!3 1C I!e # ) nam ns trace

(D 6 'Y \$] )

set tracefd [open wireless3-out.tr w]


set namtrace [open wireless3-out.nam w]
$ns_ trace-all $tracefd
$ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y)
;I ) ' '

) `2

1 ) ;' '3 1C
(

3 h)

:< 9

Py9yP 0.0.0 @ $3 I!

0 1 ' 3 1C .

:P 1

I!

) FA =

1 H

Q1"1R#

3 2 h) I!.-2 4> [V ) 0 )1X @ *3 )

0 H# @ $3 ) 0 I ) ) MH n 7.

!> ) 0 ' a 3 ;I ) <yPyP h)


;

0 I!

.] H

' 3 1C !] !!d# (.0 = 1 a 0 ' I!3 1C

I!

2 HA =

.]

9 I!

1 ) ;\ 1.0.1 1.0.0

0 '3 1C \0

FA 1

MH

123

.* ; 0 I!3 1C + ) FA HA
>X

3 2 ) .C

# I! 0 on ) MobileIP

.]

(# 2H
O1

flag '

0\

' '

> I!

'! 0 %1# + ) '

;' mobileIP ON ) !.C


# Configure for ForeignAgent and HomeAgent nodes
$ns_ node-config -mobileIP ON \
-adhocRouting $opt(adhocRouting) \
-llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channelType $opt(chan) \
-topoInstance $topo \
-wiredRouting ON \
-agentTrace ON \
-routerTrace OFF \
-macTrace OFF
# Create HA and FA
set HA [$ns_ node 1.0.0]
set FA [$ns_ node 2.0.0]
$HA random-motion 0
$FA random-motion 0
#provide some co-ord (fixed) to these base-station nodes.
$HA set X_ 1.000000000000
$HA set Y_ 2.000000000000
$HA set Z_ 0.000000000000
$FA set X_ 650.000000000000
$FA set Y_ 600.000000000000
$FA set Z_ 0.000000000000

3 2
0
-.

124

> 0 )1X @ $3 ; 0 I!3 1C + ) mobilehost

' I 0 )

) HA ' L

C 4 ^ @1^

;I! 0 OFF n 7.

COA care-of-address @

;' 12

I3 MH 0
h)

1
!> ) ; 1

!!d# FA

'0

'!= !!d#

.]

MH 2 ; '

mip.{cc,h}, mip-reg.{cc,h}, tcl/lib/{ns-

Y# n 7.

2 MH ! ' \>o (beacon) 2


DY. COA
(D 6 '! 1#
;' $ '3

) L

[!= ) wiredRouting ) !.C

0 ) h) H MH 2 ;I! 0

k!5V# MH

COA ' 0 '!= L M. FA 1

2 +

:MH n 7. @ !

( !
" 0

.] h) :\ @ 4C ) @1 0

MH 4C COA
. !

^ X

!"

)18

HA

; 0 '3 1C '!=

) mip.tcl, ns-wireless-mip.tcl}

# configure for mobilehost


$ns_ node-config -wiredRouting OFF
# create mobilehost that would be moving between HA and FA.
# note address of MH indicates its in the same domain as HA.
set MH [$ns_ node 1.0.1]
set node_(0) $MH
set HAaddress [AddrParams set-hieraddr [$HA node-addr]]
[$MH set regagent_] set home_agent_ $HAaddress
# movement of the MH
$MH set Z_ 0.000000000000
$MH set Y_ 2.000000000000
$MH set X_ 2.000000000000
# MH starts to move towards FA (640, 610) at a speed of 20m/s
$ns_ at 100.000000000000 "$MH setdest 640.000000000000 610.000000000000
20.000000000000"
# and goes back to HA (2, 2) at a speed of 20 m/s
$ns_ at 200.000000000000 "$MH setdest 2.000000000000 2.000000000000
20.000000000000"
;\C ' I!3 1C ) ) TCP L 5#

0 I!3 1C + ) FA/HA I!

3 2 ! 3link

125

# create links between wired and BaseStation nodes


$ns_ duplex-link $W(0) $W(1) 5Mb 2ms DropTail
$ns_ duplex-link $W(1) $HA 5Mb 2ms DropTail
$ns_ duplex-link $W(1) $FA 5Mb 2ms DropTail
$ns_ duplex-link-op $W(0) $W(1) orient down
$ns_ duplex-link-op $W(1) $HA orient left-down
$ns_ duplex-link-op $W(1) $FA orient right-down
# setup TCP connections between a wired node and the MobileHost
set tcp1 [new Agent/TCP]
$tcp1 set class_ 2
set sink1 [new Agent/TCPSink]
$ns_ attach-agent $W(0) $tcp1
$ns_ attach-agent $MH $sink1
$ns_ connect $tcp1 $sink1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns_ at $opt(ftp1-start) "$ftp1 start"
3 )' 3 $
Y

'

.]

0 1

0\

*$ '! 0

;\ wireless2.tcl ' $3 1 )

' $!

) "warning: Route to base_stn not known: dropping pkt" ' $3

;'! 0 '3

1 \ 1 )1r \
$ kV

% )1)

MH

*$ 0 .6)
' L )

3 1

(.0 = )18

) 0'

.]

1 H

MH 2 0 \

' \>o @ 1 2

"

1 @

'3 1C + wireless3-out.nam wireless3-out.tr

(D 6 % @' 4 0 'Y ;' 1 L ) '

trace 4 6 ) ; 1

@ $ !

0 '!= !!d# H!6 #

) n 7. @ ! L M. nam

; 0 '3 1C !!d# MH 1
;' $

wireless3.tcl

'3

!!d# )

+0

% C ;'

" 1 a 0 ' I!3 1C

) wireless3.tcl 4 0 1 )

126

# simulation of a wired-cum-wireless topology running with mobileIP


#
===========================================================
===========
# Define options
#
===========================================================
===========
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/802_11
;# MAC type
set opt(ifq) Queue/DropTail/PriQueue
;# interface queue type
set opt(ll) LL
;# link layer type
set opt(ant) Antenna/OmniAntenna
;# antenna model
set opt(ifqlen)
50
;# max packet in ifq
set opt(nn)
1
;# number of mobilenodes
set opt(adhocRouting) DSDV
;# routing protocol
set opt(cp)
set opt(sc)

""
""

;# cp file not used


;# node movement file.

set opt(x) 670


set opt(y) 670
set opt(seed) 0.0
set opt(stop) 250
set opt(ftp1-start)

;# x coordinate of topology
;# y coordinate of topology
;# random seed
;# time to stop simulation
100.0

set num_wired_nodes
2
#set num_bs_nodes
2 ; this is not really used here.
#
===========================================================
===========
# check for boundary parameters and random seed
if { $opt(x) == 0 || $opt(y) == 0 } {

127

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)
}
# create simulator instance
set ns_ [new Simulator]
# set up for hierarchical routing
$ns_ node-config -addressType 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 2 1
;# number of nodes in each cluster
AddrParams set nodes_num_ $eilastlevel ;# of each domain
set tracefd [open wireless3-out.tr w]
set namtrace [open wireless3-out.nam w]
$ns_ trace-all $tracefd
$ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y)
# Create topography object
set topo [new Topography]
# define topology
$topo load_flatgrid $opt(x) $opt(y)
# create God
# 2 for HA and FA
create-god [expr $opt(nn) + 2]
#create wired nodes
set temp {0.0.0 0.1.0}
;# hierarchical addresses
for {set i 0} {$i < $num_wired_nodes} {incr i} {
set W($i) [$ns_ node [lindex $temp $i]]
}

128

# Configure for ForeignAgent and HomeAgent nodes


$ns_ node-config -mobileIP ON \
-adhocRouting $opt(adhocRouting) \
-llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channelType $opt(chan) \
-topoInstance $topo \
-wiredRouting ON \
-agentTrace ON \
-routerTrace OFF \
-macTrace OFF
# Create HA and FA
set HA [$ns_ node 1.0.0]
set FA [$ns_ node 2.0.0]
$HA random-motion 0
$FA random-motion 0
# Position (fixed) for base-station nodes (HA & FA).
$HA set X_ 1.000000000000
$HA set Y_ 2.000000000000
$HA set Z_ 0.000000000000
$FA set X_ 650.000000000000
$FA set Y_ 600.000000000000
$FA set Z_ 0.000000000000
# create a mobilenode that would be moving between HA and FA.
# note address of MH indicates its in the same domain as HA.
$ns_ node-config -wiredRouting OFF
set MH [$ns_ node 1.0.1]
set node_(0) $MH
set HAaddress [AddrParams addr2id [$HA node-addr]]
[$MH set regagent_] set home_agent_ $HAaddress

129

# movement of the MH
$MH set Z_ 0.000000000000
$MH set Y_ 2.000000000000
$MH set X_ 2.000000000000
# MH starts to move towards FA
$ns_ at 100.000000000000 "$MH setdest 640.000000000000 610.000000000000
20.000000000000"
# goes back to HA
$ns_ at 200.000000000000 "$MH setdest 2.000000000000 2.000000000000
20.000000000000"
# create links between wired and BaseStation nodes
$ns_ duplex-link $W(0) $W(1) 5Mb 2ms DropTail
$ns_ duplex-link $W(1) $HA 5Mb 2ms DropTail
$ns_ duplex-link $W(1) $FA 5Mb 2ms DropTail
$ns_ duplex-link-op $W(0) $W(1) orient down
$ns_ duplex-link-op $W(1) $HA orient left-down
$ns_ duplex-link-op $W(1) $FA orient right-down
# setup TCP connections between a wired node and the MobileHost
set tcp1 [new Agent/TCP]
$tcp1 set class_ 2
set sink1 [new Agent/TCPSink]
$ns_ attach-agent $W(0) $tcp1
$ns_ attach-agent $MH $sink1
$ns_ connect $tcp1 $sink1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns_ at $opt(ftp1-start) "$ftp1 start"
# source connection-pattern and node-movement scripts
if { $opt(cp) == "" } {
puts "*** NOTE: no connection pattern specified."
set opt(cp) "none"
} else {
puts "Loading connection pattern..."

130

source $opt(cp)
}
if { $opt(sc) == "" } {
puts "*** NOTE: no scenario file specified."
set opt(sc) "none"
} else {
puts "Loading scenario file..."
source $opt(sc)
puts "Load complete..."
}
# Define initial node 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) 20
}
# Tell all nodes when the siulation ends
for {set i 0} {$i < $opt(nn) } {incr i} {
$ns_ at $opt(stop).0 "$node_($i) reset";
}
$ns_ at $opt(stop).0 "$HA reset";
$ns_ at $opt(stop).0 "$FA reset";
$ns_ at $opt(stop).0002 "puts \"NS EXITING...\" ; $ns_ halt"
$ns_ at $opt(stop).0001 "stop"
proc stop {} {
global ns_ tracefd namtrace
close $tracefd
close $namtrace
}
# some useful headers for tracefile
puts $tracefd "M 0.0 nn $opt(nn) x $opt(x) y $opt(y) rp \
$opt(adhocRouting)"

131

puts $tracefd "M 0.0 sc $opt(sc) cp $opt(cp) seed $opt(seed)"


puts $tracefd "M 0.0 prop $opt(prop) ant $opt(ant)"
puts "Starting Simulation..."
$ns_ run

132

I3 456

I!

2 ! H!6 # 1 )

31 )

H!6 # 1 " 4 6 +

' 0 + \R *

-.

CBR TCP Z 5# )

' 1#

4 ~ns/indep-utils/cmu-scen-gen h) ) ' 0 + \R * ;' 1

h.

n 7. I!

3 2 ! L 5#

(*!6 # +

@ 1#

f0 ' q 3 2 'Y# q:CBR TCP H!6 # L 5# c1 0 \


0 x H q 6 5# seed @ 0 kV
'3 1C

4*

)1.

CBR L 5# \"

b= ;I! 0 kV

) 1

.C

n 7.

cbrgen.tcl @

;' 12

Z H!6 # 4 6 H

) q 0 + (
-. CBR

6 5# H!6 #
1 '3 1C

+ ) ; 0

! @ 1#

3
-.

0 #Z 5# 'Y#

(.0 = !

> 7
;1

ns cbrgen.tcl [-type cbr|tcp] [-nn nodes] [-seed seed] [-mc connections]


[-rate rate]
;' 1

! o 180 ) 'M
;' $ Y%

f0 '

6 5#

cbrgen.tcl \R *

)15 TCP/CBR
H!6 # ' 0 +

Z 5#

!= ) ) . !

(
^ X

133

seed ) 'M

! u$3 9yP

L 5# J f0 '

2 9P ! CBR L 5# 4 6 H
;'! 0 / # )

Lf
)1.

ByP x

ns cbrgen.tcl -type cbr -nn 10 -seed 1.0 -mc 8 -rate 4.0 > cbr-10-test
; 1 '3 1C

!> CBR Z 5#

* :CBR ' 0 +

% C cbr-10-test 4 6 )

b=

#
# 2 connecting to 3 at time 82.557023746220864
#
set udp_(0) [new Agent/UDP]
$ns_ attach-agent $node_(2) $udp_(0)
set null_(0) [new Agent/Null]
$ns_ attach-agent $node_(3) $null_(0)
set cbr_(0) [new Application/Traffic/CBR]
$cbr_(0) set packetSize_ 512
$cbr_(0) set interval_ 0.25
$cbr_(0) set random_ 1
$cbr_(0) set maxpkts_ 10000
$cbr_(0) attach-agent $udp_(0)
$ns_ connect $udp_(0) $null_(0)
$ns_ at 82.557023746220864 "$cbr_(0) start"
F! cbr-10-test 4 6
)

E J 2

'

(. ) ; 1
+

+ <

Z 5# 'Y# c1$+

?
'1

2 ! UDP L 5# H Z ) W!# #

'

U V. P9P

3 2 !

: 3' . 6
;' )

'3 1C

!> L f H ;I !2

e ) tcp ) type %

) TCP Z 5# +

4>

1 " ' $3
;1

ns cbrgen.tcl -type tcp -nn 25 -seed 0.0 -mc 8 > tcp-25-test

134

;' '3 1C

!> tcp-15-test 4 6

1$ L 5# H

#
# 5 connecting to 7 at time 163.0399642433226
#
set tcp_(1) [$ns_ create-connection TCP $node_(5) TCPSink $node_(7) 0]
$tcp_(1) set window_ 32
$tcp_(1) set packetSize_ 512
set ftp_(1) [$tcp_(1) attach-source FTP]
$ns_ at 163.0399642433226 "$ftp_(1) start"

135

I3

I!

) ) ns W5

31 )

3 2 L M.

1 "4 6 +

~ns/indep-utils/cmu-scen-gen/setdest h) ) 3 2 L M.

-. dev/random C
0 RNG h 0 F 1#
4 R 0 )1e

456

;: )

setdest
;' 0

CMU @Q) ;\ Makefile setdest{.cc,.h} 4


) initstate()

1C 6 6 5# '^ +

. ! 4$ \!D

.>" 0 \

'

% '

-.

;'! 0 L > )
)0

ns @ 0 build )

"1$Y )1r .>" '! 0 % ) configure )1.


;' 0

4 6 H )0
bR

+ ) setdest

;'! 0 % ) make

: 0 '3 1V -. Tclcl

1 " '0 +

3 V .0

makefile H ) 0

V .0 F 1#

6 5# '^ +
( 2 setdest.cc @ 0
.6) ns \ (6
;: 1

.6) indep-utils/cmu-scen-gen/setdest
( # Q) ' '3 1C + ~ns/rng.cc

4M.]

; 0 '3 1C + ) % 4 setdest
;'! 0 %

( $2)

) setdest 4 6

./setdest [-n num_of_nodes] [-p pausetime] [-s maxspeed] [-t simtime] \


[-x maxx] [-y maxy] > [outdir/movement-file]

136

! !

10.0m/s \^

0 \0

Q1"1R#

'7

I! 0

f0 '

1. ! o <PP

0 2 <P

'Y )

3 2 L M.

!>

1)

;\ 2s

0 I ) '5

; 1 '3 1C

H I!3 1C

+ )1.

Z M.

L
! b*

500*500

b= ;'

./setdest -n 20 -p 2.0 -s 10.0 -t 200 -x 500 -y 500 > scen-20-test


;\

unix

' c

($.]! ) 0 ' '3 1C L )

3 2 !" \!Y 1

6 [!= )1r ) ' .

% C

)1.

; 0 I!3 1C \ '3 scen-20test 4 6

4 6

;' 0

% C
) % C

Y# ) 3 2 Z M.

$ns_ at 2.000000000000 "$node_(0) setdest 90.441179033457 44.896095544010


1.373556960010"
(90.44, '5M
L M. \^

node_(0) 2

'1

-.

!> < ! o ) 0 ' 0

!] !!d#

' 1#

)1.

O1rC

scen-20-test ) Z TC

kV

1.37m/s \^

;' 0 \0

; 1 kV
(General

;'

*> q T!7 \"

'

~)

) ) '

>

2 L M. 4 6 ) GOD

)1r 3

'3

D0

;\]! ' .C
3 2
;\

2 H @'! )
@

0 )

^ X

&

# #10
!>

TC 4f ' 1

n 7.

3 2

:Directives 3 $ 3 )

Operations Director)GOD

!> ) ' 0 \0

! )1 hop '^
'0 $ > 7

!Cg

44.89)

) H

(.$]

$#

!Cg

TM6 god

3 ) ) '^

god

\
;1

3 2
@1 0 I3
-.

) `2 ) god

L -.

) BG <? 2

! !] ) hop 'Y# Z )

1 "4 6

^ X

$ns_ at 899.642 "$god_ set-dist 23 46 2"


;' 0

'!= !!d# < JNNyGB< @

137

! u$3 (D 6

;' 0

6 5# waypoint I. )1 "

;' .]3 8 5.C @


' 1.! \ h .
)

\ 6 +

+ god

^ X

' > W

\ 6 2 0 )

2 ;\ c1

@ ! ) @

;' '3 1C + calcdest


12 3

!]

I!.-2 > 0 )1X @ $3 )

ad-

0' 0

> 7

\ ) \ 6

1 )

) L M.

@ 1#

0\

1%1 L M.

e6 8 Y
1 "

) 4 6 calcdest 2 ; 1

) @

) `2)

.( W

) 4 6 hockey

) $

# #10 setdest calcdest

!> ) ) . L' X C
) I3 1 )

'7

monarch Q = T 1# '

god

0 !] L1X

^ X

L M.

'3

% C)
)

1 "H )

-.

!]

-. ad-hoc *>

'

CMU 0 # ^ X

-. @ *

Q1"1R# !!d# x @ 0 kV
( V

(*# = : ^ X

'

b 06
">

(
1

!!d# q 3 !] c1$+ q h .

) I3

&4

^ X q'0

(D 6 !]-#

(D 6 '

; ' a CMUTrace \0 = 3

]$3 ) ) 4 0 god

1 "

1 " 4 6 O1rC \ 6 ) ) S 6
1 "

) 3 2 ! hop 'Y#

1C b= ; '

Z 5#

X1rC

Z M.

% C 4 6 ' 1V . )

'

!( !] L1X 4!D7#

-.

ad-hockey

!Cg bR

)18 )

(*# = )

) `2 )

0 1

' 0 ) ) L M.

;'! 0 L $^ !
!!d#

^ X god

8 5.C

(D 6 setdest

) 2 L M.

4 ~ns/indep-utils/cmu-scen-gen/setdest ) 0calcdest Y

' a calcdest ;' '


'3 1V ) 0

-.

4
;1

!,

3'5M

2 3

-. ' 1#

'Y# ' $3 # ^ X 2 L M.

) '( n 7.

(D 6

^ X n 7.

(. )
3 2

138

139

I3

I!
! [!=
. H I!
.]= @

'

*> )

; 0 I!3 1C ) 0 ns )

("'

2 3 * !6 Z ) ; 1
\6 )

456

1 ) ) . L'
'
-.

!=

("'

456

.6 ) L ! :power @ 1#

'! ) \0 = 3

\6 ) L ! @ 1# 2 1
;1

1 )

\6 ) \0 = 0 .

z` MAC Z T 1#
&)

1%

'

1% \6 )

) rC .] @

1 ) ) . L'

ns )

free space model


two ray reflectionmodel
shadowing model
~ns/propagation.{cc,h},

~ns/tworayground.{cc,h}

("'

;\

!=

1%1 ~ns/shadowing

&Free Space Model


(Line Of
> 7

I!M.]

!] H

" Y ; )' )
;1

) )
(

! Y
-.

.( ) L 0 ) . T
!3 0 )1r )

' . 6 d D8 6 ) :

1% ' . 6

0 '0

S 6 L'

' !2 ! Sight)LOS
6 L' )

.6 ) L ! @ 1#

140

! u$3 ;\

. 6 ' !2 . gain

3 ) 'M 3

!> ) Z1$Y ;\ '

\
_1 L1X

.6 ) L !

4C ) ' !2 2 ;'3

-. node-config )1.

Lf H ; 0

@ 1#

) . L' @

) 'M

L=1 9

)15 ) ' . 6 L 5#

; 0 '3 1C I2 ) (.0 = ' > @ 4C

@ 1#

) I.]! loss ) 'M

0 r

;'
'

)'

) gain

'7

) '. 6

) h.

;'

6 L'
(.0 = $# ' 1#
OTcl T ) )

) @

-.

$ns_ node-config -propType Propagation/FreeSpace


;\

)1.

-.

s)

set prop [new Propagation/FreeSpace]


$ns_ node-config -propInstance $prop

&Two Ray Ground h *Y L'


n 7.
)1C

2
o

! F @ ' ! I!M.] !]
I!M.] !, I!M.] !]
;1

d D8 6

-.

\ ) ) !] F
3

0 1

.6 ) L ! @ 1# > 7

.6 ) L ! @ 1# ; ) free space L'

1 @'

S 6 ^Y

6H .

4> L' ' $3

) . L' ) ;'.6

:;' 1

'! # b*Y"

. ! \

Z1X 48 16 ) L'
;1

} -# .$0

3L !
0 ' \ o

> 7

L1 6

141

L ) 'M ! %

) ;'

( . :height c -#)

' !2

'D

+ )

; ' .6 2 e ) 9 L Y Free Space L'


;'3
U C

Free Space L' " Y

.C

L' L1 6

>!0 # X C
'

FX M. D8 6 ) ; 1

.Y

@ 1# @' I2 ' 0

'3

;1

-.

13'

I0 d ) 'M 0 . S1^ ) ;\ c Y
:cross over FX M. D8 6 H L'

> 7

-. Two Ray L' L1 6


;I! 0

;I! 0

> 7

Z " Y

1C . #10 48 16 ) Two Ray L' ) 0 ' a 3

@ 4!"

2 ;1

'!= [ 6 D8 6 0

)18

!, )

-. ) . L'

Y#

!!d#

-. Free Space

' '3 1C

L1 6 ) FX M. D8 6 b= ;

@ 0

'3 1C ) +!. H

)1.

4> L' ' $3

)1.

@ 1#

$ns_ node-config -propType Propagation/TwoRayGround


; 0

-.

! u$3

set prop [new Propagation/TwoRayGround]


$ns_ node-config -propInstance $prop

&:Shadowing Model

*6

) . L'

142

0' 0

S6

W . 0\

3 ' 0

> 7 \6 ) D8 6

6 5# !!d. H !Y D8 6 H )

2 ' 17

!o #

.]2

' 0

0 "'

!o #

H L 5#

;:multipath propagation effects \

!] ' a ) .

> 7 ) L ! @ 1# D8 6

-. F% @ 1 ^

D> L'

!2

' T 1# D>

@' H

) ;\
)

6) . ) <
1%

L1 6 @ 1#

0\

! +#

) 'M ;' 0 '!= [ 6 . ! '

!o #
-.

" ;\ \$]

L'

! [!=
' > 7

*6

L1 6

) d D8 6 )

\>]

@ 1#

Free Space L' L1 6 >X ; 1

3) 'M 0 '

'7

fading effects

) ;' 12

) Z1$Y 0 \ path loss

) 'M 0 I!

;\ shadowing L' 1
D8 6 @ ;' 0

'3

.2)

2 ' I2 L'
;\

) '! ) L ! @ 1# D> L'

.6 ) @ 1# \M!M ) ;\ )

.6 ) @ 1# 0 \ (path loss model) !]


) 'M ;' 0

Y #H

^1

'! ) @ 1# ! ! 1

3 ) 'M

) 'M
!!Y#

L '%

. ! D8 6 0 .

; 0 > 7 Free Space L'

D.V 3 T!7 ) .
& 0 I !2

+!. 4> L1 6 b= ; 1

3) 'M

!2 ' dB 4>!

Z1$Y !]

2 ' I2

143

H F
.6) I3

) @ ;)

) b= ;\ '

!Y D8 6 H ) '! ) L ! @ 1# )
'

!2 ' 4>!

) 2L

F 1# >X 0 \

;1

;\
C

) !Y z 7
L '% ) ;'

: -8

\ '

I0

@ ) 'M
;I

1$
Y 1#

# 1

A ) L' H

(r!7 )

*6

) 1 )

'7

;' 1 45. I3 ' ) ) O >#)

'7 >" ) 0 .

*6
D.V

3) 'M

z 7

)
) 'M

3 ) 'M

1) )

Y D> L ' L' shadowing


" $.

6 5# !!d.

6 5# !!d. H

' 12

0@! $

z 7

$. ) " L

\$]

D0 L1 6 shadowing L'

*6 -8 Y

! +#

shadowing L'

!!d#

)15 ' 1#

*6

TM6 3 2 @ ) 0

L'

144

: !]

2 ' I2 > 7 ) @ 1#
;I! 0

U V. '

L f TM6 3 . ) =

!> ) I!3 1C

3) 'M 0 \

) 0 shadowing L' @ 0

3 ) 'M '

1$ H

0
)1.

!> T7!

>X )

-.

*6

;\ node-config )1.

z 7

1 3 OTcl T )
;' .]3

# first set values of shadowing model


Propagation/Shadowing set pathlossExp_ 2.0 ;# path loss exponent
Propagation/Shadowing set std_db_ 4.0 ;# shadowing deviation (dB)
Propagation/Shadowing set dist0_ 1.0 ;# reference distance (m)
Propagation/Shadowing set seed_ 0 ;# seed for RNG
$ns_ node-config -propType Propagation/Shadowing
) RNG

;' 0

+ (Random Number Generator)RNG


\ : 6 5# '^ +

pre-

D>

-. ' 1#

3 '0 ;' $ Y%
)1.

I! 0

ns F %
-.

$3) '
s)

) <Ey9 [V

0 I!3 1V

H shadowing L'
=

! seed c1

raw seed
pre-defined seed
huristic seed
'! 1#
. ! ^

2 0' 0

-. defined seed
;' 1

set prop [new Propagation/Shadowing]


$prop set pathlossExp_ 2.0
$prop set std_db_ 4.0
$prop set dist0_ 1.0
$prop seed <seed-type> 0 ;# user can specify seeding method
$ns_ node-config -propInstance $prop

145

; 0 !!Y# <seed-type> ) heuristic raw,predef @ 1#

) Z ) seed c1

&O >#) :
I!

3 2 !

1 )
;'

D8 6 0 '

*> T ) ) \6 )

'5 \
.

'7

) 0 H 3 Application

*$

8 5.C ) 'M I!e # ' 1#

)0

C )

;' 0 kV

Phy/WirelessPhy set RXThresh_ <value>


~ ) \6 )
)4 6
.

.
'0 $

> 7

~ns/indep-utils/propagation/threshold.cc ) '%

> 7
2 ; 0
@

-. 456

'! 1#

) '

$ :1

~) ) .

("'

$#

% 4 6 4 R 0 'Y '
;'! 0

@ 1#

;'

threshold @

-.

H ! u$3

'! 0 4 R 0
1 )

'7

threshold -m <propagation-model> [other-options] distance


FreeSpace,
;'

Y 4> )

kV

* <propagation-model> %

.-2 L'
X >#) distance

. W]

) . L' ) ; 1

'

-.

6 [!=

D8 6 @ ) ) !78 \6 ) x

D8 6 \ TwoRayGround or Shadowing

3 . )=

@ 0 kV

[other-option]

H shadowing ) . L' ) O >#)

NE 0 '!3 1V $

-. \6 )

;'! 0 > 7

;1
)1.

) . '

threshold -m Shadowing -r 0.95 50

0 \ r <receive-rate> Z . ) = H shadowing

' 0 h1*Y F # H \]! 4 0 L '


2 Lf

$ ' 1 \6 )

> 7

* 4!"

;' 0

(inverse Q-function) Q

. EP D8 6 )

. )

(.0 = '8)

146

;\

' ) [ $

) [other-option]

3 ) 'M

-pl <path-loss-exponent> -std <shadowing-deviation> -Pt <transmit-power>


-fr <frequency> -Gt <transmit-antenna-gain> -Gr <receive-antenna-gain>
-L <system-loss> -ht <transmit-antenna-height> -hr <receive-antenna-height>
-d0 <reference-distance>

&
&I

H ) ' .-2

0 \]!" 8 C )1r ) ) .

("'

)1.

)1.

$ns_ node-config -propType <propagation-model>


;I!3 )

3) 'M <propagation-model> ) I! 1#

0 \ ) . L' U V.

)1.

Propagation/FreeSpace, Propagation/TwoRayGround or Propagation/Shadowing


) <propagation-model>

3) 'M @ $3 $prop ;\ ) . L'

!!Y#

s)

)1.

; !2

1C

$ns_ node-config -propInstance $prop


shadowing ) . L'

1$ H $prop_ ;\ RNG ) seed @ 0 kV

'Y )1.
;\

$sprop_ seed <seed-type> <value>

147

> 7

) )

~ns/indep-utils/propagation/threshold.cc ) 0 threshold
;1

-. l C X >#)

threshold -m <propagation-model> [other-options] distance

'7 ) \6 )

148

I3 ! 456

ns )
6 5#

3 !!d.

F$.+

A )

+
;\
;' 1

>!. =
*a10 ^1$+ 1C ) ns

!=

) A ) F 1#

!!d#

C L ) @1 0 I3 )

\6

( ) ) ' 1#

456

!> ; )

) '

# F 1#

!>
1C )
(" )

~ns/tools/rng.{cc, h}, ~ns/tools/random.{cc, h}, ~ns/tools/ranvar.{cc, h},


ns/tools/pareto.{cc, h}, ~ns/tools/expoo.{cc, h}, ~ns/tools/integrator.{cc, h}, and
ns/tcl/lib/nsrandom.tcl
& 6 5# '^ +
C++

3'0 ;\ MRG32k3a >!0 # 2' a . 2

' 0 '!"1# )
'Y# )
' H

6 5# '^ 4M.]
)

4* ;'

H @ %

' 0 '!"1#

( % ' 1#
3 ;'

@ %

' 0 '!"1# 4 0 )
;'3

) ( %

!= H 4

RNG h 0

0 ' .]3 MRG32k3a ' 0 '!"1#


4

' 1#

;: '^ @ .6 I3

( %

r
'0 3

[!= 6 5# '^

( %W # 2 1 a ) )

*!6 2

149

6 5#

3 !!d. 2 ; 1

. ;' 0

.C

'% RNG

-.

'Y 6 5# '^
@ 1.! ) 0

!>

!"

\]

6 5# !!d. 3 '

Y @' seeded '

;' 1 4M.]

) :defaultRNG

3 ) 'M @
M

'

-.

# :; 1
;\

b*!6 M! @ $ 3 . ) =
@ !$X
f0 '

'% @ %
# # '3

3 ) 1$ *
H ) *# 3
) @*

!=

;1

'

H 0
f0 '

3) # I )

6 5# '^

2' a

= '^ H

2' a 4M.]

!> 3)
-.

-.
6 5# !!d.

!> H

;:I! 0 ) *# )

!> H )
+ RNG ' '%

H!# 1# )1r 1

6 5# '^ q 'Y @ % c

6 [!= RNG H

3) *# WD,
! '1

( % @ 1 4M.]

150

'^

Z ' 1#

) *# H )

6 5# !!d. 3 ;I!

) 4M.]

3 ) *#

;' 0 '!"1# @ 0 '!=


. !

^ X

;I

!A1# + ) ) L1$Y F 1#
;'! 0 Y%

0\

1R$3 [!= ) 6 5#
e ) +

.*

tools/rng.h and tools/rng.cc ) F>

3'0

&:seeding the RNG RNG '


$

2 ;\ (9<?BE 6 [!= ) 'M I ) ' seed H I!e #

) defaultRNG ) seed ) 'M ' ) 0


;\ MAXINT # 9 ! seed H
10@
. )2; 1

= seed ) 'M ) 0
-. 4M.]

;'!3 !!d# P
. '
!= ) ( %

1R$3 I3
\!D

!= RNG \Y!>X >X

"@
1%1 F 1#

>.Y ) 'M ;' 0

\!Y>X @

) defaultRNG ) 'M

3 ) *# ) 3 seed I!e #

) H TM6 ; ) ' 1% '


;\ RNG

$ ;'

' > s)
'% seed
-. I3 @

C ' ) @ !!d#

!!Y

T 1# '
1%

'5

3 RNG !M 0 '! 0 I!e #


6 5# '^ " >

; 1 '3 1C ' ) $ H
)

)#

( % 0
1R$3 '^ @ 0 . ) 2
&L f

# Usage: ns rng-test.tcl [replication number]


if {$argc > 1} {
puts "Usage: ns rng-test.tcl \[replication number\]"
exit
}
set run 1
if {$argc == 1} {
set run [lindex $argv 0]
}
if {$run < 1} {
set run 1
}
# seed the default RNG

151

global defaultRNG
$defaultRNG seed 9999
# create the RNGs and set them to the correct substream
set arrivalRNG [new RNG]
set sizeRNG [new RNG]
for {set j 1} {$j < $run} {incr j} {
$arrivalRNG next-substream
$sizeRNG next-substream
}
# arrival_ is a exponential random variable describing the time
# between consecutive packet arrivals
set arrival_ [new RandomVariable/Exponential]
$arrival_ set avg_ 5
$arrival_ use-rng $arrivalRNG
# size_ is a uniform random variable describing packet sizes
set size_ [new RandomVariable/Uniform]
$size_ set min_ 100
$size_ set max_ 5000
$size_ use-rng $sizeRNG
# print the first 5 arrival times and sizes
for {set j 0} {$j < 5} {incr j} {
puts [format "%-8.3f %-4d" [$arrival_ value] \
[expr round([$size_ value])]]
}
& 1 '3 1C
% ns rng-test.tcl 1
6.358 4783
5.828 1732
1.469 2188
0.732 3076
4.002 626
% ns rng-test.tcl 5
0.691 1187
0.204 4924
8.849 857

4*

% C

152

2.111 4505
3.200 1143

&OTcl ) 3
;' 1

\6 tools/rng.cc 4C )

'3 1C ' ) $ H

'1

>X seed I!e # '

10@

OTcl ) RNG h 0

' 1#

>!. =
)1.

n=0 2 n RNG seed I!e # & seed n


;1
) 'Y 6 5# '^ & next-random

;' 2

;' 2
; 'Y @ %
; 10@ %
) ' . ) !Y z 7

! ! H

'.

@ %

F 1# H

) 1 0 seed ) 'M & seed


# Y 1# & next-substream

' ) & reset-start-substream

0 '^ H @' 2 & normal avg std


;'

z 7

! ! H

I. ) " L

0 '^ H @' 2 & lognormal avg std

F 1# H

;'
\6 tcl/lib/ns-random.tcl )

'

-. OTcl 4C ) ' 1#

+ ) ' . ) !Y

RNG h 0 )

)1.
;' 1

;'
max min

+ mu

) \C 1 * F 1# H

$ F 1# H

0 '^ H @' 2 & & exponential mu

0 !78 '^ H @' 2 & uniform min max


;\

;\ k-1 P ! \C 1 * F 1# H

0 !78 '^ H @' 2 & integer k

153

&L f

# Usage: ns rng-test2.tcl [replication number]


if {$argc > 1} {
puts "Usage: ns rng-test2.tcl \[replication number\]"
exit
}
set run 1
if {$argc == 1} {
set run [lindex $argv 0]
}
if {$run < 1} {
set run 1
}
# the default RNG is seeded with 12345
# create the RNGs and set them to the correct substream
set arrivalRNG [new RNG]
set sizeRNG [new RNG]
for {set j 1} {$j < $run} {incr j} {
$arrivalRNG next-substream
$sizeRNG next-substream
}
# print the first 5 arrival times and sizes
for {set j 0} {$j < 5} {incr j} {
puts [format "%-8.3f %-4d" [$arrivalRNG lognormal 5 0.1] \
[expr round([$sizeRNG normal 5000 100])]]
}
& 1 ' 3 1C
% ns rng-test2.tcl 1
142.776 5038
174.365 5024
147.160 4984
169.693 4981

4*

3 % C

154

187.972 4982
% ns rng-test2.tcl 5
160.993 4907
119.895 4956
149.468 5131
137.678 4985
158.936 4871

&C++ ) 3
Y# tools/rng.h )

;'
@ %

'

!= RNG h 0 T 1# 6 5# '^ ' 0 '!"1#

# $ ' T ) H tools/random.h ) random h 0 * \

e ) + ) '
;\

&' 3
seed I!e # '

seed=0 2

seed

~) )

;' 2
;\ MAXINT P ! !78 '^ 0 ' 2
; 10@ %

) 'Y 6 5# '^ @'


'.

@ %

; 'Y @ %
;\ k-1 P ! \C 1 * F 1# H
.\ r P ! \C 1 * F 1# H
b a ! \C 1 * F 1# H

)' .

L $^ h 0 1 ^ F 1#

10@

>X

) 1 0 seed ) 'M & Long seed(viod)


) 'Y 6 5# '^ & long next(void)
2 & double next_double(viod)

' ) & void reset_start_substream(void)


# Y 1# & & void reset_next_substream(void)
0 !78 '^ H @'

2 & int uniform(int k)

0 '^ H @' 2 & double uniform(double r)

0 '^ H @' 2 & double uniform(double a,double b)


;\

;'

+ 9yP

$ F 1# H

.*

6 5# '^

RNG seed I!e # & void set_seed(long seed)


; 1 '3 1C ' ) $ H

;[0,1]

>!. =

0 '^ H @' 2 & double exponential(void)

155

+ k

$ F 1# H

0 '^ H @' 2 & double exponential(double k)


;'

F 1# H

0 '^ H @' 2 & double normal(double avg, double std)


;'

F 1# H

+ ) ' . ) !Y z 7

! !

0 '^ H @' 2 & double longnormal(double avg, double std)


;'

+ ) ' . ) !Y z 7

! ! H

I. ) "
&L f

/* create new RNGs */


RNG arrival (23456);
RNG size;
/* set the RNGs to the appropriate substream */
for (int i = 1; i < 3; i++) {
arrival.reset_next_substream();
size.reset_next_substream();
}
/* print the first 5 arrival times and sizes */
for (int j = 0; j < 5; j++) {
printf ("%-8.3f %-4d\n", arrival.lognormal(5, 0.1),
int(size.normal(500, 10)));
}
& 1 '3 1C

3 % C

6 5#

3 !!d.

161.826 506
160.591 503
157.145 509
137.715 507
118.573 496

156

'^ @ %

'3

= 6 5# '^ ' 0 '!"1#

(.!D

H>

Z H RandomVariable h 0

Y# ~ns/ranvar.h ) h 0

&'

; ) ) 6 [!= 6 5#

class RandomVariable : public TclObject {


public:
virtual double value() = 0;
int command(int argc, const char*const* argv);
RandomVariable();
protected:
RNG* rng_;
};
3 . )=

8C

3) 'M

F 1# 3 ;' '
;1

&' $

.6 2 l C

-. F 1# H
'3

) 'M @'

3 F 1#

!=

h 0

value '. ;' '

) ) ( l15V . ) = ' .]3 '

Y# @1 0 I3 0

0
. )=

3 F 1#

class UniformRandomVariable min_, max_


class ExponentialRandomVariable avg_
class ParetoRandomVariable avg_, shape_
class ParetoIIRandomVariable avg_, shape_
class ConstantRandomVariable val_
class HyperExponentialRandomVariable avg_, cov_
class NormalRandomVariable avg_, std_
class LogNormalRandomVariable avg_, std_

)1r 0 6 5# !!d. H

+ )1e

1$

;\ h .
&'! 0 ) )

set u [new RandomVariable/Uniform]

4 OTcl ) RandomVariable h 0
)1.

+ [10,20] ! \C 1 *

157

$u set min_ 10
$u set max_ 20
$u value
-. 4> \$] ) '
&

.-2 6 [!= 6 5# '^ ' 0 '!"1# RandomVariable !

k!5V# 6 [!= !,RNG ) 'M

RandomVariable

H @ 1#

6 [!= )1r

use-rng '.

;' 0

set rng [new RNG]


$rng seed 0
set e [new RandomVariable/Exponential]
$e use-rng $rng
3L
)

0 '0

>!. = : .]]2 @ 0 F$%

: . 1!= L

(> M#
&'

Integrator

h 0

Y# ~ns/integrator
& integrator.h 4 6

class Integrator : public TclObject {


public:
Integrator();
void set(double x, double y);
void newPoint(double x, double y);
int command(int argc, const char*const* argv);
protected:
double lastx_;
double lasty_;
double sum_;
};
& integrator.cc 4 6

158

Integrator::Integrator() : lastx_(0.), lasty_(0.), sum_(0.)


{
bind("lastx_", &lastx_);
bind("lasty_", &lasty_);
bind("sum_", &sum_);
}
void Integrator::set(double x, double y)
{
lastx_ = x;
lasty_ = y;
sum_ = 0.;
}
void Integrator::newPoint(double x, double y)
{
sum_ += (x - lastx_) * lasty_;
lastx_ = x;
lasty_ = y;
}
int Integrator::command(int argc, const char*const* argv)
{
if (argc == 4) {
if (strcmp(argv[1], "newpoint") == 0) {
double x = atof(argv[2]);
double y = atof(argv[3]);
newPoint(x, y);
return (TCL_OK);
}
}
return (TclObject::command(argc, argv));
}

Anda mungkin juga menyukai