⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gui.tcl

📁 计算机网络中的几个简单协议的仿真代码
💻 TCL
字号:
#!/usr/bin/wish# Modify the above line to reflect the correct path for wish in your system## Graphical User Interface for simulating datalink protocols.# # Written by Shivakant Mishra# Set window titlewm title . {Simulator for Datalink Protocols}# Create a frame for help and quit buttons.frame .qframe -borderwidth 10pack .qframe -side top -fill x# Create the quit button.button .qframe.help -text Help -command help_windowbutton .qframe.quit -text Quit -command exitpack .qframe.quit -side rightpack .qframe.help -side rightpack .qframe -side top -fill both -expand true# Create frame for compilationframe .compile -borderwidth 10label .compile.l -text {COMPILE SIMULATOR AND PROTOCOLS:} -padx 0menubutton .compile.makemenu -text Make -menu .compile.makemenu.menuset m [menu .compile.makemenu.menu]$m add command -label make -command make_proc$m add command -label {make protocol2} -command {make_proc protocol2}$m add command -label {make protocol3} -command {make_proc protocol3}$m add command -label {make protocol4} -command {make_proc protocol4}$m add command -label {make protocol5} -command {make_proc protocol5}$m add command -label {make protocol6} -command {make_proc protocol6}$m add command -label {make clean} -command {make_proc clean}pack .compile.l -side leftpack .compile.makemenu -side leftpack .compile -side top -fill both -expand true# Create frames for collecing protocol parametersframe .middle -borderwidth 10label .middle.l -text {PROTOCOL PARAMETERS:} pack .middle.l -side leftpack .middle -side top -fill both -expand trueframe .middle2 -borderwidth 2label .middle2.filler1 -text {} -padx 20label .middle2.e -text Events: -padx 0entry .middle2.ev -width 10 -relief sunken -textvariable eventsset events 100label .middle2.t -text Ticks: -padx 0entry .middle2.tc -width 10 -relief sunken -textvariable ticksset ticks 40label .middle2.pkl -text {Packet Loss:} -padx 0entry .middle2.pkll -width 10 -relief sunken -textvariable pkt_lossset pkt_loss 0pack .middle2.filler1 -side leftpack .middle2.e -side leftpack .middle2.ev -side leftpack .middle2.t -side leftpack .middle2.tc -side left pack .middle2.pkl -side leftpack .middle2.pkll -side left pack .middle2 -side top -fill both -expand trueframe .middle3 -borderwidth 2label .middle3.filler1 -text {} -padx 20label .middle3.ck -text {Garbled Rate:} -padx 0entry .middle3.ckk -width 10 -relief sunken -textvariable cksumset cksum 0label .middle3.d -text Debug?: -padx 0entry .middle3.db -width 10 -relief sunken -textvariable debugset debug 0pack .middle3.filler1 -side leftpack .middle3.ck -side leftpack .middle3.ckk -side leftpack .middle3.d -side leftpack .middle3.db -side leftpack .middle3 -side top -fill both -expand true# Create frames for running various protocolsframe .running -borderwidth 10label .running.l -text {PROTOCOL EXECUTION:}pack .running.l -side leftpack .running -side top -fill both -expand trueframe .running2 -borderwidth 5label .running2.filler1 -text {} -padx 20label .running2.f -text {Output Filename:} -padx 0entry .running2.ofn -width 40 -relief sunken -textvariable fnamemenubutton .running2.protocolmenu -text {Run Protocol} \	-menu .running2.protocolmenu.menuset pm [menu .running2.protocolmenu.menu -tearoff 1]$pm add command -label {Protocol 2} -command {run_protocol protocol2}$pm add command -label {Protocol 3} -command {run_protocol protocol3}$pm add command -label {Protocol 4} -command {run_protocol protocol4}$pm add command -label {Protocol 5} -command {run_protocol protocol5}$pm add command -label {Protocol 6} -command {run_protocol protocol6}pack .running2.filler1 -side leftpack .running2.f -side leftpack .running2.ofn -side leftpack .running2.protocolmenu -side rightpack .running2 -side top -fill both -expand true# Create a text widget to log the outputframe .tset log [text .t.log -width 80 -height 10 \	-borderwidth 2 -relief raised -setgrid true \	-yscrollcommand {.t.scroll set}]scrollbar .t.scroll -command {.t.log yview}pack .t.scroll -side right -fill ypack .t.log -side left -fill both -expand truepack .t -side top -fill both -expand true# Run makeproc make_proc {{w all}} {  global input log  if [catch {open "|make $w |& cat"} input] {    $log insert end $input\n  } else {    fileevent $input readable Log    $log insert end make\n  }}# Run Protocolsproc run_protocol {{w protocol2}} {  global input log events ticks pkt_loss cksum debug fname fileid flag  set flag 0  if {[string compare $fname {}] != 0} {    set flag 1    if [catch {open $fname {WRONLY CREAT}} fileid] {      $log insert end $fileid\n      return    }  }   if [catch {open "|./$w $events $ticks $pkt_loss $cksum \	$debug |& cat"} input] {    $log insert end $input\n    if {$flag == 1} {      catch {close $fileid}    }  } else {    if {$flag == 1} {      fileevent $input readable Log_file    } else {      fileevent $input readable Log    }    $log insert end $w\n  }}# Read and log output from the programproc Log {} {  global input log  if [eof $input] {    Stop    $log insert end DONE\n    $log see end  } else {    gets $input line    $log insert end $line\n    $log see end  }}proc Log_file {} {  global input log fileid  if [eof $input] {    Stop_file    $log insert end DONE\n    $log see end  } else {    gets $input line    puts $fileid $line  }}# Stop the program and close the filesproc Stop {} {  global input but  catch {close $input}}proc Stop_file {} {  global input but fileid  catch {close $input}  catch {close $fileid} }proc help_window {} {  global f  set f [toplevel .helpframe -borderwidth 10]  grab $f  message $f.msg -text {To compile the simulator and protocols:    Click on make menu and choose appropriate option.To run protocols:    1. Enter the values of five simulator parameters    2. Enter a filename if you want to store output in a file    3. Click on Run Protocol menu and choose appropriate protocol to run.To exit:    Click on the Quit button.}  set b [frame $f.buttons -bd 10]  pack $f.msg $f.buttons -side top -fill x  button $b.ok -text OK -underline 0 -command {global f; grab release $f; destroy $f}  pack $b.ok -side top }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -