| [ Team LiB ] |
|
Communicating ProcessesChapter 24 presented two examples: a browser for the examples in this book, and a simple shell in which to try out Tcl commands. In that chapter they are put into the same application. The two examples shown below hook these two applications together using the send command. Example 43-2 changes the Run and Reset procedures of the browser to send EvalEcho commands to the shell. Example 43-2 Hooking the browser to an eval server
# Replace the Run and Reset procedures of the browser in
# Example 24–3 on page 384 with these procedures
# Start up the evalsrv.tcl script.
proc StartEvalServer {} {
global browse
# Start the shell and pass it our name.
exec evalsrv.tcl [tk appname] &
# Wait for evalsrv.tcl to send us its name
tkwait variable browse(evalInterp)
}
proc Run {} {
global browse
set apps [winfo interps]
set ix [lsearch -glob $apps evalsrv.tcl*]
if {$ix < 0} {
# No evalsrv.tcl application running
StartEvalServer
}
if {![info exists browse(evalInterp)]} {
# Hook up to already running eval server
set browse(evalInterp) [lindex $apps $ix]
}
if [catch {send $browse(evalInterp) {info vars}} err] {
# It probably died - restart it.
StartEvalServer
}
# Send the command asynchronously. The two
# list commands foil the concat done by send and
# the uplevel in EvalEcho
send -async $browse(evalInterp) \
[list EvalEcho [list source $browse(current)]]
}
# Reset the shell interpreter in the eval server
proc Reset {} {
global browse
send $browse(evalInterp) {EvalEcho reset}
}
The number of lists created before the send command may seem excessive, but they are all necessary. The send command concatenates its arguments, so instead of letting it do that, we pass it a single list. Similarly, EvalEcho expects a single argument that is a valid command, so list is used to construct that. The StartEvalServer procedure starts up the shell. Command-line arguments are used to pass the application name of the browser to the shell. The shell completes the connection by sending its own application name back to the browser. The browser stores the name of the shell application in browser(evalInterp). The code that the shell uses is shown in Example 43-3: Example 43-3 Making the shell into an eval server
# Add this to the shell application shown
# in Example 24–4 on page 389
if {$argc > 0} {
# Send our application name to the browser
send [lindex $argv 0] \
[list set browse(evalInterp) [tk appname]]
}
|
| [ Team LiB ] |
|