daigai

Well-Known Member
Đây là bài tập lớn môn cơ sở mạng.Toàn bộ code đây:(gôm 4 file)

Nguồn : Trang website chuyên về điện tử viễn thông.
Code:
File thứ nhất là mm1k.tcl với code là
#———-Tao doi tuong mo phong————————————————————

#–Tao doi tuong mo phong
set ns [new Simulator]
#–Dinh nghia mau cho goi du lieu
$ns color 1 Black

#———-Mo file bam vet va file bam vet nam de ghi lai tat ca so lieu mo phong————

#–Mo file bam vet out.tr
set tf [open out.tr w]
$ns trace-all $tf

#–Mo file bam vet NAM
set nf [open out.nam w]
$ns namtrace-all $nf

#———-Mo file de chua kich thuoc hang doi,ti le mat goi  toc do mat goi—————–

#–Mo file Pe.tr chua ti le mat goi du lieu
set pe [open Pe.tr w]
set f0 [open Nq.tr w]

#–Mo file Kichthuochangdoi.tr chua kich thuoc hang doi
set f1 [open Kichthuochangdoi.tr w]

#–Mo file Tocdomatgoi.tr chua toc do mat goi theo thoi gian
set pktslost [open Tocdomatgoi.tr w]

#———-Tham so cho tien trinh toi va tien trinh phuc vu———————————-

#–Lamda, toc do trung binh mot yeu cau toi hang doi
set lamda 120.0
#–Thiet laptop bien ro
set ro 0.9
#–Kich thuoc hang doi
set K 6
#–Thoi gian chay mo phong
set t_mophong 200

#———-Tao 2 nut,mau sac,hinh dang, nhan————————————————-

#–Tao nut 1—
set n1 [$ns node]
#–Thiet laptop mau cho nut 1
$n1 color red
#–Gan nhan cho nut 1
$n1 label “Nguon”
#–Thiet laptop vi tri cua nhan
$n1 label-at up

#–Tao nut 2—
set n2 [$ns node]
#–Thiet laptop mau cho nut 1
$n2 color blue
#–Gan nhan cho nut 1
$n2 label “Dich”
#–Thiet laptop vi tri cua nhan
$n2 label-at up

#———-Tao link giua 2 nut va kich thuoc hang doi—————————————

#–Tao lien ket nut 1 va nut 2 co dung luong 100kbs va tre truyen 100 ms lien ket
#–chieu tu nut 1 den nut 2 tuy chon loai bo (DropTail)
set link [$ns simplex-link $n1 $n2 100kb 100ms DropTail]
#–Thiet laptop khich thuoc hang doi
$ns queue-limit $n1 $n2 $K

#———-Vi tri,mau sac lien ket  hai nut tren giao dien NAM——————————

#–Thiet laptop vi tri cua 2 nut
$ns simplex-link-op $n1 $n2 orient right
#–Hien thi mau lien ket giua hai nut 1 va 2
$ns simplex-link-op $n1 $n2 color “green”
#–Giam sat kich thuoc hang doi
$ns simplex-link-op $n1 $n2 queuePos 0.5

#———-Khai bao Tien trinh toi co toc do tuan theo phan bo mu————————–

set InterArrivalTime [new RandomVariable/Exponential]
$InterArrivalTime set avg_ [expr 1/$lamda]

#———-Khai bao Tien trinh phuc vu co toc do tuan theo phan bo mu———————-

set pktSize [new RandomVariable/Exponential]
$pktSize set avg_ [expr 100000.0/(8*($lamda/$ro))]

#———-Tao nguon src gui cac goi va dinh no vao nut 1———————————-

#–Thiet laptop phien ket noi UDP
set src [new Agent/UDP]
#–Gia su goi can gui co kich thuoc 100000
$src set packetSize_ 100000
#–Tao nguon src gui cac goi va dinh no vao nut 1
$ns attach-agent $n1 $src

#———-Tao sink lien ket giua 2 nut—————————————————–

#–Thiet laptop phien ket noi LossMonitor
set sink [new Agent/LossMonitor]
#–Tao ra sink1 gan voi nut n2 (day la noi thu nhan traffic)
$ns attach-agent $n2 $sink
#–Thiet laptop ket noi luu luong tu nguon va luu luong tu sink
$ns connect $src $sink

#——– Thuc tuc ket thuc se chay file out.nam de mo phong ——————————-

proc finish {} {
#–Tu khoa global de khai bao cac bien ngoai thu tuc
global ns tf nf pktslost pe
#–Phuong thuc “flush-trace” se ket xuat cac vet vao cac file tuong ung
$ns flush-trace
#Dong cac file bam viet duoi day lai
close $tf
close $nf
close $pktslost
close $pe
#Tinh T,T_q,N
#–Tinh do dai he thong tu file qm.out dua vao cong thuc tinh trong N.awk
exec awk -f N.awk qm.out
#–Chay mo phong NAM
exec nam out.nam &
#–Tinh thoi gian trong he thong tu file out.tr dua vao cong thuc tinh trong T.awk
exec awk -f T.awk out.tr
#–Tinh thoi gian trong hang doi tu file out.tr dua vao cong thuc tinh trong Tq.awk
exec awk -f Tq.awk out.tr
#–Thuc hien ve do thi kich thuoc hang doi voi xgraph voi ca thiet laptop ve mau sac va
#–kich thuoc cua so ve ,cac tieu de
exec xgraph  Nq.tr  -geometry 800×400  -t “Kich thuoc hang doi” -x “Thoi gian(s) ” -y “Kich thuoc hang doi” &
exec xgraph Tocdomatgoi.tr -geometry 800×400 -y ”  Mat goi tren theo thoi gian” -x “Thoi gian(s)” -t “Toc do mat goi ” &
#–Ket thuc va tra ve 0 giong nhu trang thai cua he thong
exit 0
}

#—— Thu tuc dinh thoi gian va kich thuoc goi gui cua nguon $src————————-

proc sendpacket {} {
#–Tu khoa global de khai bao cac bien ngoai thu tuc
global ns src InterArrivalTime pktSize
#–Thiet laptop thoi  thoi gian
set time [$ns now]
#–Thiet laptop  thoi gian cac khoang thoi gian gui goi
$ns at [expr $time + [$InterArrivalTime value]] “sendpacket”
set bytes [expr round ([$pktSize value])]
$src send $bytes
}

#———– Thu tuc xuat tong so packet nhan duoc o sink————————————-

proc recv-pkts {} {
#–Tu khoa global de khai bao cac bien ngoai thu tuc
global sink pe
#–Tao mot doi tuong mo phong
set ns [Simulator instance]
#Thiet laptop gia tri cho bien msg
set msg ” Xac suat mat goi Pe= [string range [expr [$sink set nlost_]*100.0/[$sink set expected_]] 0 3] */*”
#–Hien thi mot chu thich
puts $pe “$msg”
$ns trace-annotate $msg
}

#———-Thu tuc xuat tong so packet nhan duoc o sink,so goi mat, ti le mat goi————-

proc recv-pkts1 {} {
#–Tu khoa global de khai bao cac bien ngoai thu tuc
global sink
#–Tao mot doi tuong mo phong
set ns [Simulator instance]
#–Thiet laptop gia tri cho bien msg1
set msg1 ” Nguon 1 da truyen [$sink set expected_] goi, mat [$sink set nlost_] goi, ti le mat goi la [string range [expr [$sink set nlost_]*100.0/[$sink set expected_]] 0 3] ”
#–Hien thi mot chu thich
puts $msg1
$ns trace-annotate $msg1
}

#———–Thietlap thong so de ve do thi Toc do mat goi————————————-

set old_departure 0
proc record {} {
#–Tu khoa global de khai bao cac bien ngoai thu tuc
global ns qmon_size pktslost
#–Cu sau 0.01s lai ghi lai so goi mat
set time 0.01
set now [$ns now]
$qmon_size instvar pdrops_
puts $pktslost “$now [expr $pdrops_/$now] ”
$ns at [expr $now+$time] “record”
}

#———-Lay thong tin ve hang doi———————————————————–
#–Quan ly hang doi
set qmon_size [$ns monitor-queue $n1 $n2 $K]
set monitor [$ns monitor-queue $n1 $n2 stdout_]
set qmon [$ns monitor-queue $n1 $n2 [open qm.out w] 0.3]
$link queue-sample-timeout
#———Thu tuc lay kich thuoc hang doi va ghi vao 2 file $f1——————————–

proc queueLength {sum number} {
global ns monitor f1 f0
#Cu sau 0.01s lai lay chieu dai hang doi
set time 0.01
set len [$monitor set pkts_ ]
set now [$ns now]
set sum [expr $sum+$len]
set number [expr $number+1]
#Ghi chieu dai hang doi tuc thoi vao file Nq.tr
puts $f0 “$now $len”
#Luu do dai hang doi trung binh vao file Kichthuochangdoi.tr-> tinh duoc Nq
puts $f1 “$now [expr 1.0*$sum/$number]”
$ns at [expr $now+$time] “queueLength $sum $number”
}

#———–Thong tin nhom viet bai tap lon—————————————————

#–Xoa man hinh
exec clear >@ stdout
puts “\n                           NHÓM DNP-\n ”
puts “\n                          MÔ PHỎNG HỆ THỐNG HÀNG ĐỢI M/M/1/K \n”
puts “\n                       DANH SÁCH THÀNH VIÊN NHÓM DNP-ET1K53 \n”
puts “\n Nguyễn ………..SHSV ………………Lớp \n”
puts “\n Trần Danh Nam………….SHSV ………………Lớp \n”
puts “\n …………SHSV ……………..Lớp \n”

#———-Dinh cac thoi diem chay thu tuc—————————————————-
$ns at 0 “queueLength 0 0″
$ns at 0.0001 “record”
$ns at 0.0001 “sendpacket”
$ns at $t_mophong “recv-pkts”
$ns at $t_mophong  “recv-pkts1 ”
$ns at $t_mophong “finish”
#———Chay mo phong———————————————————————–
$ns run
#——————————-Duoc viet boi nhom DNP-ET1-K53——————————–
File thứ 2 là T.awk với code là:

#Chuc nang :File nay lay ra thoi gian trong hang doi cua 1 goi
# $1,$2 co nghia la lay gia ti o cot 1,2 trong file .tr

{

#$1 == “+” , 1 goi vao hang doi
if ($1 == “+” ){
vao[i] = $2
i= i +1

#$1 == “-” , 1 goi ra khoi hang doi
}else { if ($1 == “d” ){ i=i-1}
if ($1 == “r”) {

nhan[j]= $2
j= j+1
}
}
}

END{

N = j-1
T[0] =0

# Tinh thoi gian 1 goi trong hang doi , luu vao mang T
for (i = 0;i> “T_avg.tr”
}

File thứ 3 là Tq.awk

#Chuc nang :File nay lay ra thoi gian trong hang doi cua 1 goi
# $1,$2 co nghia la lay gia ti o cot 1,2 trong file .tr (mm1.tr chang han)

{

#$1 == “+” , 1 goi vao hang doi
if ($1 == “+” ){
vao[i] = $2
i= i +1

#$1 == “-” , 1 goi ra khoi hang doi
}else { if ($1 == “d” ){ i=i-1}
if ($1 == “-“) {

ra[j]= $2
j= j+1
}
}
}
END{
N = j-1
T[0] =0
# Tinh thoi gian 1 goi trong hang doi , luu vao mang T
for (i = 0;i> “Tq_avg.tr”
}

File thứ 4 là N.awk với code là:

BEGIN{sum=0;num=0;}{
sum=sum+$6-$7-$8+1;
num++;
print “Do dai he thong ” ,sum/num>>”N_avg.tr”;
}END{}
 

Các chủ đề có liên quan khác

Top