Thực hiện mô phỏng

Một phần của tài liệu Tìm hiểu lớp liên kết dữ liệu trong mô hình 7 lớp osi và mô phỏng mạng token buslan (Trang 42 - 48)

V. MÔ PHỎNG MẠNG TOKENBUS LAN

5.5.Thực hiện mô phỏng

Để mô phỏng hoạt động của token bus, nhóm em đã sử dụng công cụ mô phỏng là Ns2 là một công cụ khá phổ biến và hiệu quả cho việc mô phỏng mạng. Giải

thuật cho mô phỏng Token bus bằng Ns2 của nhóm em là:

Bước 2: Tạo 7 nút để thiết lập hình dạng mạng ,các nút được đánh số từ 0 đến 6, trong đó riêng các nút 0,2,4,6 được dán nhãn lần lượt là TramA, TramB,

TramC, TramD tượng chưng cho các trạm trong vòng lặp logic.

Bước 3: Mở file bám vết nam(nam trace file) và định nghĩa thủ tục finish sau khi đóng file bám vết và chạy nam trên file bám vết.

Bước 4: Tạo một liên kết 2 hướng giữa các nút và khởi tạo vị trí của các nút cho việc thiết lập sơđồ mạng LAN.

Bước 5: Thiết lập liên kết TCP giữa một vài nút và áp dụng các ứng dụng FTP (FTP Application) trên UDP.

Bước 6: Thiết lập các sự kiện cho việc mô phỏng mạng Token bus chia làm 3 trường hợp:

§ TH1: Vòng lặp logic ban đầu gồm 3 trạm là TramA,TramC,TramD

tại các nút 0, 4, 6 dữ liệu sẽ đi theo các thẻ bài di chuyển trong vòng lặp logic bắt đầu từ TramC => TramD => TramA.

§ TH2: Vòng lặp logic được bổ sung thêm TramB(nút 2), khi đó dữ liệu sẽ đi theo các thẻ bài di chuyển trong vòng lặp logic đi theo chu trình TramA => TramB => TramC =>TramD.

§ TH3: Vòng lặp logic bớt đi TramC không tham gia chu trình, khi đó

dữ liệu sẽđi theo các thẻ bài di chuyển trong vòng lặp logic bắt đầu từ

TramA đi qua TramB và đến thẳng TramD.

Kết quả của việc mô phỏng:

PHỤ LỤC: MÃ NGUỒN

#--- --- ---

#---Tên file : Tokenbus.tcl #---Mô phỏng mạng Tokenbus

#---Tạo đối tượng mô phỏng mới---- --- ---

set ns [new Simulator]

#---Thiết lập băng thông là 0.5Mb set Bandwidth "0.5Mb"

#---Thiết lập kích thước gói là 800

set packetsize 800

#---Thiết lập khoảng thời gian là 0.004

set intval 0.004

#---Khai báo màu sắc cần dùng khi mô phỏng $ns color 1 magenta $ns color 2 seagreen $ns color 3 deepskyblue $ns color 4 brown $ns color 5 salmon

#---Khai báo các nút cần dùng khi mô phỏng

foreach i "0 1 2 3 4 5 6" { set n$i [$ns node]

}

#---Thiết lập màu sắc cần dùng cho các nút $n0 color "navy" $n1 color "salmon" $n2 color "steelblue" $n3 color "deepskyblue" $n4 color "steelblue" $n5 color "seagreen" $n6 color "steelblue" #---Gán nhãn cho các trạm $ns at 0.0 "$n0 label TramA" $ns at 0.0 "$n2 label TramB" $ns at 0.0 "$n4 label TramC" $ns at 0.0 "$n6 label TramD" #---Tạo file phục vụ chức năng bám vết của Nam(network simulation) set nf [open tokenbus.nam w] $ns namtrace-all $nf (adsbygoogle = window.adsbygoogle || []).push({});

#---Mở file bám vết

set f [open tokenbus.tr w] $ns trace-all $f #---Tạo kết nối giữa các nút $ns duplex-link $n0 $n1 $Bandwidth 30ms DropTail $ns duplex-link $n1 $n2 $Bandwidth 30ms DropTail $ns duplex-link $n1 $n3 $Bandwidth 30ms DropTail $ns duplex-link $n3 $n4 $Bandwidth 30ms DropTail $ns duplex-link $n3 $n5 $Bandwidth 30ms DropTail $ns duplex-link $n5 $n6 $Bandwidth 30ms DropTail #---Thiết lập vị trí giữa các nút $ns duplex-link-op $n0 $n1 orient right $ns duplex-link-op $n2 $n1 orient down $ns duplex-link-op $n1 $n3 orient right $ns duplex-link-op $n3 $n4 orient down $ns duplex-link-op $n3 $n5 orient right $ns duplex-link-op $n5 $n6 orient up #---Thiết lập các phiên kết nối TCP--- --- #---tạo con trỏ “tcp1” cho tác nhân TCP(một đối tượng trong NS) set tcp1 [new Agent/TCP]

#---Định nghĩa nút nguồn cho kết nối Tcp(nút 0)

$ns attach-agent $n0 $tcp1 $tcp1 set fid_ 1 ;

#---Tạo con trỏ và nút nguồn tương tự cho các phiên kết nối kế tiếp set tcp2 [new Agent/TCP]

$ns attach-agent $n2 $tcp2 $tcp2 set fid_ 1 ;

set tcp3 [new Agent/TCP] $ns attach-agent $n4 $tcp3 $tcp3 set fid_ 1 ;

set tcp4 [new Agent/TCP] $ns attach-agent $n6 $tcp4 $tcp4 set fid_ 1 ;

set tcp5 [new Agent/TCP] $ns attach-agent $n0 $tcp5 $tcp5 set fid_ 1 ;

set tcp6 [new Agent/TCP] $ns attach-agent $n2 $tcp6 $tcp6 set fid_ 1 ;

set tcp7 [new Agent/TCP] $ns attach-agent $n4 $tcp7 $tcp7 set fid_ 1 ;

set tcp8 [new Agent/TCP] $ns attach-agent $n6 $tcp8 $tcp8 set fid_ 1 ;

set tcp9 [new Agent/TCP] $ns attach-agent $n0 $tcp9 $tcp9 set fid_ 1 ;

$ns attach-agent $n2 $tcp10 $tcp10 set fid_ 1 ;

set tcp11 [new Agent/TCP] $ns attach-agent $n4 $tcp11 $tcp11 set fid_ 1 ;

set tcp12 [new Agent/TCP] $ns attach-agent $n6 $tcp12 $tcp12 set fid_ 1 ;

#---Định nghĩa hoạt động Tcp đích và gán nó tới các con trỏ sink1 set sink1 [new Agent/TCPSink] #---Định nghĩa nút đích $ns attach-agent $n2 $sink1

#---Làm tương tự đối với các Tcp đích khác

set sink2 [new Agent/TCPSink] $ns attach-agent $n4 $sink2 set sink3 [new Agent/TCPSink] $ns attach-agent $n6 $sink3 set sink4 [new Agent/TCPSink] $ns attach-agent $n0 $sink4 set sink5 [new Agent/TCPSink] $ns attach-agent $n2 $sink5 set sink6 [new Agent/TCPSink] $ns attach-agent $n4 $sink6 set sink7 [new Agent/TCPSink] $ns attach-agent $n6 $sink7 set sink8 [new Agent/TCPSink] $ns attach-agent $n0 $sink8 set sink9 [new Agent/TCPSink] $ns attach-agent $n2 $sink9 set sink10 [new Agent/TCPSink] $ns attach-agent $n4 $sink10 set sink11 [new Agent/TCPSink] $ns attach-agent $n6 $sink11 set sink12 [new Agent/TCPSink] $ns attach-agent $n0 $sink12 #---Tạo các kết nối giữa nút nguồn và nút đích $ns connect $tcp1 $sink1 $ns connect $tcp2 $sink2 $ns connect $tcp3 $sink3 $ns connect $tcp4 $sink4 $ns connect $tcp5 $sink5 $ns connect $tcp6 $sink6 $ns connect $tcp7 $sink7 $ns connect $tcp8 $sink8 $ns connect $tcp9 $sink9 $ns connect $tcp10 $sink11 $ns connect $tcp12 $sink12 #---Thiết lập phiên kết nối Tcp trên Tc

set ftp1 [new Application/FTP] $ftp1 attach-agent $tcp1

set ftp2 [new Application/FTP] $ftp2 attach-agent $tcp2

set ftp3 [new Application/FTP]

$ftp4 attach-agent $tcp4

set ftp5 [new Application/FTP] $ftp5 attach-agent $tcp5

set ftp6 [new Application/FTP] $ftp6 attach-agent $tcp6

set ftp7 [new Application/FTP] $ftp7 attach-agent $tcp7

set ftp8 [new Application/FTP] $ftp8 attach-agent $tcp8

set ftp9 [new Application/FTP] $ftp9 attach-agent $tcp9

set ftp10 [new Application/FTP] $ftp10 attach-agent $tcp10 set ftp11 [new Application/FTP] $ftp11 attach-agent $tcp11 set ftp12 [new Application/FTP] $ftp12 attach-agent $tcp12

#---Thiết lập kết nối Cbr dựa trên Udp (adsbygoogle = window.adsbygoogle || []).push({});

set udp0 [new Agent/UDP] $ns attach-agent $n3 $udp0

set cbr0 [new

Application/Traffic/CBR] $cbr0 attach-agent $udp0 set null0 [new Agent/Null] $ns attach-agent $n4 $null0 $ns connect $udp0 $null0

#---Thiết lập kích thước gói của Cbr0

$cbr0 set packetSize_ $packetsize #---Thiết lập khoảng thời gian của Cbr0

$cbr0 set interval_ $intval

#---Thiết lập các kết nối Cbr tương tự như trên

set udp1 [new Agent/UDP] $ns attach-agent $n4 $udp1

set cbr1 [new

Application/Traffic/CBR] $cbr1 attach-agent $udp1 set null1 [new Agent/Null] $ns attach-agent $n6 $null1 $ns connect $udp1 $null1

$cbr1 set packetSize_ $packetsize $cbr1 set interval_ $intval

set udp2 [new Agent/UDP] $ns attach-agent $n6 $udp2

set cbr2 [new

Application/Traffic/CBR] $cbr2 attach-agent $udp2 set null2 [new Agent/Null] $ns attach-agent $n0 $null2 $ns connect $udp2 $null2

$cbr2 set packetSize_ $packetsize $cbr2 set interval_ $intval

set udp3 [new Agent/UDP] $ns attach-agent $n0 $udp3

set cbr3 [new

$ns attach-agent $n2 $null3 $ns connect $udp3 $null3

$cbr3 set packetSize_ $packetsize $cbr3 set interval_ $intval

set udp4 [new Agent/UDP] $ns attach-agent $n2 $udp4

set cbr4 [new

Application/Traffic/CBR] $cbr4 attach-agent $udp4 set null4 [new Agent/Null] $ns attach-agent $n4 $null4 $ns connect $udp4 $null4

$cbr4 set packetSize_ $packetsize $cbr4 set interval_ $intval

set udp5 [new Agent/UDP] $ns attach-agent $n4 $udp5

set cbr5 [new

Application/Traffic/CBR] $cbr5 attach-agent $udp5 set null5 [new Agent/Null] $ns attach-agent $n6 $null5 $ns connect $udp5 $null5

$cbr5 set packetSize_ $packetsize $cbr5 set interval_ $intval

set udp6 [new Agent/UDP] $ns attach-agent $n6 $udp6

set cbr6 [new

Application/Traffic/CBR] $cbr6 attach-agent $udp6 set null6 [new Agent/Null] $ns attach-agent $n0 $null6 $ns connect $udp6 $null6 (adsbygoogle = window.adsbygoogle || []).push({});

$cbr6 set packetSize_ $packetsize $cbr6 set interval_ $intval

set udp7 [new Agent/UDP] $ns attach-agent $n0 $udp7

set cbr7 [new

Application/Traffic/CBR] $cbr7 attach-agent $udp7 set null7 [new Agent/Null] $ns attach-agent $n2 $null7 $ns connect $udp7 $null7

$cbr7 set packetSize_ $packetsize $cbr7 set interval_ $intval

set udp8 [new Agent/UDP] $ns attach-agent $n2 $udp8

set cbr8 [new

Application/Traffic/CBR] $cbr8 attach-agent $udp8 set null8 [new Agent/Null] $ns attach-agent $n6 $null8 $ns connect $udp8 $null8

$cbr8 set packetSize_ $packetsize $cbr8 set interval_ $intval

#---Thiết lập các sự kiện của thẻ bài #---Các sự kiện của thẻ bài trong $ns at 0.04 "$ftp1 start" $ns at 0.1 "$ns detach-agent $n0 $tcp1;$ns detach-agent $n2 $sink1" $ns at 3.5 "finish" $ns at 0.11 "$ftp2 start" $ns at 0.2 "$ns detach-agent $n2 $tcp2;$ns detach-agent $n4 $sink2" $ns at 3.5 "finish" $ns at 0.21 "$ftp3 start" $ns at 0.30 "$ns detach-agent $n4 $tcp3;$ns detach-agent $n6 $sink3" $ns at 3.5 "finish" $ns at 0.31 "$ftp4 start" $ns at 0.43 "$ns detach-agent $n6 $tcp4;$ns detach-agent $n0 $sink4" $ns at 3.5 "finish" #---Các sự kiện của thẻ bài trong trường hợp 2 $ns at 0.64 "$ftp5 start" $ns at 0.70 "$ns detach-agent $n0 $tcp5;$ns detach-agent $n2 $sink5" $ns at 3.5 "finish" $ns at 0.71 "$ftp6 start" $ns at 0.8 "$ns detach-agent $n2 $tcp6;$ns detach-agent $n4 $sink6" $ns at 3.5 "finish" $ns at 0.81 "$ftp7 start" $ns at 0.9 "$ns detach-agent $n4 $tcp7;$ns detach-agent $n6 $sink7" $ns at 3.5 "finish" $ns at 0.91 "$ftp8 start" $ns at 1.03 "$ns detach-agent $n6 $tcp8;$ns detach-agent $n0 $sink8" $ns at 3.5 "finish" #---Các sự kiện của thẻ bài trong trường hợp 3 $ns at 1.24 "$ftp9 start" $ns at 1.30 "$ns detach-agent $n0 $tcp9;$ns detach-agent $n2 $sink9" $ns at 3.5 "finish" $ns at 1.31 "$ftp10 start" $ns at 1.43 "$ns detach-agent $n2 $tcp10;$ns detach-agent $n6 $sink11" $ns at 3.5 "finish" $ns at 1.44 "$ftp12 start" $ns at 1.56 "$ns detach-agent $n6 $tcp12;$ns detach-agent $n0 $sink12" $ns at 3.5 "finish" #---Các sự kiện của gói dữ liệu trong trường hợp 1 $ns at 0.28 "$cbr1 start" $ns at 0.37 "$cbr1 stop" $ns at 0.39 "$cbr2 start" $ns at 0.43 "$cbr2 stop" #---Các sự kiện của gói dữ liệu trong trường hợp 2

$ns at 0.76 "$cbr3 stop" $ns at 0.81 "$cbr4 start" $ns at 0.82 "$cbr4 stop" $ns at 0.92 "$cbr5 start" $ns at 0.93 "$cbr5 stop" $ns at 1.03 "$cbr6 start" $ns at 1.04 "$cbr6 stop" #---Các sự kiện của gói dữ liệu trong trường hợp 3 $ns at 1.44 "$cbr7 start" $ns at 1.45 "$cbr7 stop" $ns at 1.51 "$cbr8 start" $ns at 1.52 "$cbr8 stop" set nf [open tokenbus.nam w] $ns namtrace-all $nf

set f [open tokenbus.tr w] $ns trace-all $f

#---Định nghĩa thủ tục kết thúc chương trình "finish"--- ---

proc finish {} {

#---Từ khóa global dùng để cho biết chúng ta được dùng các biến khai báo ngoài thủ tục

#---global ns nf

#---đổ trace vào các file tương ứng $ns flush-trace #---đóng file bám vết nf close $nf #---Thực thi hàm awk để tính lưu lượng exec awk { { #---Thực hiện vẽ đồ thị biểu thị kích thước gói tin theo thời gian tại thời điểm ra gói tin ra #---khỏi hàng đợi if(($1=="-" && $5=="tcp") || ($1=="-" && $5=="cbr")) { print $2 "\t" $6 } } } tokenbus.tr > throughput.data exec nam tokenbus.nam &

exec xgraph throughput.data & exit 0

Một phần của tài liệu Tìm hiểu lớp liên kết dữ liệu trong mô hình 7 lớp osi và mô phỏng mạng token buslan (Trang 42 - 48)