anim-ctrl.tcl
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:12k
源码类别:

通讯编程

开发平台:

Visual C++

  1. #
  2. # Copyright (C) 1998 by USC/ISI
  3. # All rights reserved.                                            
  4. #                                                                
  5. # Redistribution and use in source and binary forms are permitted
  6. # provided that the above copyright notice and this paragraph are
  7. # duplicated in all such forms and that any documentation, advertising
  8. # materials, and other materials related to such distribution and use
  9. # acknowledge that the software was developed by the University of
  10. # Southern California, Information Sciences Institute.  The name of the
  11. # University may not be used to endorse or promote products derived from
  12. # this software without specific prior written permission.
  13. # THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
  14. # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  15. # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16. # $Header: /cvsroot/nsnam/nam-1/tcl/anim-ctrl.tcl,v 1.37 2002/12/17 01:06:28 buchheim Exp $
  17. Class AnimControl 
  18. # Build main menu
  19. AnimControl instproc init {trace_file args} {
  20. AnimControl instvar PORT_FILE_
  21. catch "array set opts $args"
  22. if [info exists opts(s)] {
  23. AnimControl instvar isSync_
  24. set isSync_ 1
  25. }
  26. if [info exists opts(a)] {
  27. set flag 1
  28. } elseif {[file exists $PORT_FILE_] && [file readable $PORT_FILE_]} {
  29. set flag 0
  30. } else {
  31. set flag 1
  32. }
  33. if [info exists opts(k)] {
  34. AnimControl instvar INIT_PORT_
  35. set INIT_PORT_ $opts(k)
  36. }
  37. if $flag {
  38. # If forced, or we are the first instance, start a 
  39. # new instance of nam
  40. $self local-create-animator $trace_file [join $args]
  41. } else {
  42. # Try to connect to a "main" instance of nam, then terminate
  43. $self remote-create-animator $trace_file [join $args]
  44. }
  45. }
  46. # Stub proc to trigger a AnimControl method
  47. proc ::AnimCtrlOnRemoteRequest args {
  48. eval [AnimControl instance] on-remote-request $args
  49. }
  50. AnimControl instproc on-remote-request { newsock addr port } {
  51. # XXX We don't have any admission control!!!
  52. # Read one line and process the request.
  53. # XXX Require all request commands are within a single line, and 
  54. # every word in the command is separated by a *single* white space
  55. gets $newsock line
  56. set buf [split $line]
  57. switch [lindex $buf 0] {
  58. CA {
  59. # Create a new animator
  60. set trace_file [lindex $buf 1]
  61. # protect any backslashes in the filename
  62. regsub -all {\} $trace_file {\\} trace_file
  63. set args [lrange $buf 2 end]
  64. eval $self create-animator $trace_file $args
  65. }
  66. default {
  67. puts "Unsupported command [lindex $buf 0]"
  68. }
  69. }
  70. close $newsock
  71. }
  72. # Find an appropriate port
  73. AnimControl instproc start-server {} {
  74. $self instvar NAM_PORT_ NAM_SOCK_
  75. AnimControl instvar INIT_PORT_ PORT_FILE_
  76. set NAM_PORT_ $INIT_PORT_
  77. set ret 1
  78. while {$ret} {
  79. incr NAM_PORT_
  80. set ret [catch {set NAM_SOCK_ 
  81. [socket -server ::AnimCtrlOnRemoteRequest $NAM_PORT_]}]
  82. if {$ret} {
  83. # Failed, delete the socket if it's opened
  84. # It seems that on FreeBSD if socket fails no file descriptor is left open
  85. # but this is not the case on solaris. :( 
  86. if [info exists NAM_SOCK_] {
  87. close $NAM_SOCK_
  88. }
  89. if {$NAM_PORT_ - $INIT_PORT_ > 254} {
  90. error "Nam failed to create a socket ranging 
  91. from $INIT_PORT_ to $NAM_PORT_."
  92. }
  93. }
  94. }
  95. # Write that to a lock file, which resides under home directory
  96. # and is read-only by the user
  97. set f [open $PORT_FILE_ w 0600]
  98. puts -nonewline $f $NAM_PORT_
  99. close $f
  100. }
  101. AnimControl instproc remote-create-animator {trace_file args} {
  102. catch "array set opts $args"
  103. AnimControl instvar PORT_FILE_
  104. if [catch {set f [open $PORT_FILE_ RDONLY]}] {
  105. error "Cannot read server port from $PORT_FILE_"
  106. }
  107. set port [read $f]
  108. close $f
  109. if [catch {set sock [socket localhost $port]}] {
  110. puts -nonewline "Cannot connect to existing nam instance. "
  111. puts "Starting a new one..."
  112. $self local-create-animator $trace_file [join $args]
  113. } else {
  114. if {$trace_file == ""} {
  115. puts "A nam instance already exists. Use nam <trace file> to view an animation"
  116. close $sock
  117. } else {
  118. global tcl_platform
  119. if {$tcl_platform(platform) == "windows"} {
  120. if [regexp {^(\\|[A-Za-z]:[/\])} $trace_file] {
  121. set tf $trace_file ;# Absolute pathname
  122. } else {
  123. set tf [pwd]/$trace_file ;# Relative pathname
  124. }
  125. } else {
  126. if [regexp {^[~/]} $trace_file] {
  127. set tf $trace_file ;# Absolute pathname
  128. } else {
  129. set tf [pwd]/$trace_file ;# Relative pathname
  130. }
  131. }
  132. puts $sock "CA $tf [join $args]"
  133. flush $sock
  134. close $sock
  135. }
  136. }
  137. }
  138. AnimControl instproc local-init {} {
  139. $self instvar nam_name wix tagnum animators_
  140. # TK-related initialization
  141. . configure -background [option get . background Nam]
  142. set_app_options [tk appname]
  143. set nam_name "NAM - The Network Animator v[version]"
  144. set wix 0
  145. set tagnum 0
  146. set animators_ {}
  147. $self build-menu 
  148. $self new_webhome 
  149. $self bind-window .
  150. AnimControl instvar instance_ 
  151. if {$instance_ == ""} { 
  152. set instance_ $self 
  153. } else {
  154. error "Couldn't instantiate more than one AnimControl!"
  155. }
  156. # Set window title
  157. #wm title . "[tk appname] console"
  158. wm title . "Nam Console v[version]"
  159. global nam_local_display
  160. set nam_local_display 1
  161. }
  162. AnimControl proc instance {} {
  163. set ac [AnimControl set instance_]
  164. if {$ac != ""} {
  165. return $ac
  166. }
  167. error "Couldn't find instance of AnimControl"
  168. }
  169. AnimControl instproc bind-window { w } {
  170. bind $w <Control-o> "$self on-open"
  171. bind $w <Control-O> "$self on-open"
  172. bind $w <Control-q> "$self done"
  173. bind $w <Control-Q> "$self done"
  174. bind $w <Control-n> "$self on-new"
  175. bind $w <Control-N> "$self on-new"
  176. bind $w <Control-w> "$self on-winlist"
  177. bind $w <Control-W> "$self on-winlist"
  178. bind $w <q> "$self done"
  179. wm protocol $w WM_DELETE_WINDOW "$self done"
  180. }
  181. #----------------------------------------------------------------------
  182. #  Create nam console menubar
  183. #----------------------------------------------------------------------
  184. AnimControl instproc build-menu {} {
  185. $self instvar nam_name
  186. frame .menu -relief groove -bd 2
  187. pack .menu -side top -fill x
  188. set padx 4
  189. set mb .menu.file
  190. set m $mb.m
  191. menubutton $mb -text "File" -menu $m -underline 0 
  192. -borderwidth 1 
  193. menu $m
  194.         # Launch nam editor
  195. $m add command -label "New Nam Editor... Ctrl+N" -command "$self on-new"
  196. $m add command -label "Open... Ctrl+O" -command "$self on-open"
  197. $m add command -label "WinList Ctrl+W" -command "$self on-winlist"
  198. $m add separator
  199. $m add command -label "Quit    Ctrl+Q" -command "$self done"
  200. pack $mb -side left -padx $padx
  201. label .menu.name -text $nam_name -font [smallfont] 
  202. -width 30 -borderwidth 2 -relief groove 
  203. pack .menu.name -side left -fill x -expand true -padx 4 -pady 1
  204. set mb .menu.help
  205. set m $mb.m
  206. menubutton $mb -text "Help" -menu $m -underline 0 
  207. -borderwidth 1 
  208. menu $m
  209. $m add command -label "Help" -command "$self new_web help"
  210. $m add command -label "About nam" -command "$self new_web about"
  211.   
  212. pack $mb -side right -padx $padx
  213. }
  214. AnimControl instproc done {} {
  215. AnimControl instvar PORT_FILE_
  216. $self instvar animators_ NAM_SOCK_ NAM_PORT_
  217. if {[llength $animators_] > 0 && 
  218.     [tk_messageBox -message "Really quit?" -type yesno -icon question 
  219. -title "nam" -default "no"] == "no"} {
  220. return
  221. }
  222. close $NAM_SOCK_
  223. if [catch "set f [open $PORT_FILE_ r]"] {
  224. puts "Nam port file already deleted."
  225. } else {
  226. set buf [gets $f]
  227. close $f
  228. if {$buf == $NAM_PORT_} {
  229. file delete $PORT_FILE_
  230. } else {
  231. puts "Another nam instance is running..."
  232. }
  233. }
  234. foreach a $animators_ {
  235. $a done
  236. }
  237. destroy .
  238. }
  239. AnimControl instproc on-open {} {
  240. $self tkvar openFile_ openPeer_
  241. set openFile_ [tk_getOpenFile 
  242.     -filetypes {{{NAM} {.nam .nam.gz}} {{Nam Editor} {.ns .enam}}} 
  243.     -parent .]
  244. if {$openFile_ == ""} {return}
  245. set w .openFile
  246. set openPeer_ ""
  247. $self do-open-tracefile $w
  248. return
  249. #XXX all cold below is useless
  250. set openPeer_ ""
  251. toplevel .openFile
  252. set w .openFile
  253. wm title $w "Open Trace File"
  254. frame $w.f -borderwidth 1 -relief raised
  255. pack $w.f -side top -fill both -expand true
  256. filesel $w.f.sel $w -variable [$self tkvarname openFile_] 
  257. -command "$self do-open-tracefile $w.f.sel" 
  258. -label "Trace file to open:"
  259. pack $w.f.sel -side top -fill both -expand true
  260. # Peer dialogs
  261. label $w.pl0 -text "Peer:"
  262. pack $w.pl0 -side top -anchor w
  263. entry $w.peerentry -width 50 -relief sunken 
  264. -textvariable [$self tkvar openPeer_]
  265. pack $w.peerentry -side top -fill both -expand true
  266. frame $w.f.btns -borderwidth 0
  267. pack $w.f.btns -side top -fill x
  268. button $w.f.btns.save -text Open 
  269. -command "$self do-open-tracefile $w"
  270. pack $w.f.btns.save -side left -fill x -expand true
  271. button $w.f.btns.cancel -text Cancel -command "destroy $w"
  272. pack $w.f.btns.cancel -side left -fill x -expand true
  273. set openFile_ [pwd]
  274. }
  275. AnimControl instproc do-open-tracefile {w} {
  276. $self tkvar openFile_ openPeer_
  277. checkfile $openFile_ "r" $w.f.sel $w 
  278. "$self create-animator $openFile_ p=$openPeer_"
  279. }
  280. AnimControl instproc local-create-animator { trace_file args } {
  281. $self local-init
  282. $self start-server
  283. if {$trace_file != ""} {
  284. $self create-animator $trace_file [join $args]
  285. }
  286. }
  287. AnimControl instproc create-animator { tracefile args } {
  288. $self instvar animators_
  289. AnimControl instvar ANIMATOR_CLASS_
  290. set fnsets [split $tracefile "."]
  291.         set fnlength [llength $fnsets]
  292.         set ftype [lindex $fnsets [expr $fnlength-1]]
  293.         set ftype [string tolower $ftype]
  294. if {[string compare $ftype "ns"] == 0  || 
  295.     [string compare $ftype "enam"] == 0 } {
  296. # editable nam file
  297.     set ANIMATOR_CLASS_ Editor
  298.     set editor [new $ANIMATOR_CLASS_ $tracefile]
  299.     return
  300. }
  301. set ANIMATOR_CLASS_ Animator
  302. AnimControl instvar isSync_
  303. if {[info exist isSync_]} {
  304.     lappend args {s 1}
  305. }
  306. if ![catch {eval set anim [new $ANIMATOR_CLASS_ $tracefile 
  307. [join $args]]} errMsg] {
  308. lappend animators_ $anim
  309. $self instvar winlist_
  310. if [info exists winlist_] {
  311. $winlist_ insert end [$anim get-name]
  312. }
  313. } else {
  314. global errorInfo
  315. puts "Failed to start animator: "
  316. puts "$errMsg"
  317. puts "$errorInfo"
  318. }
  319. }
  320. #----------------------------------------------------------------------
  321. # Launch nam editor
  322. #----------------------------------------------------------------------
  323. AnimControl instproc on-new {} {
  324. $self instvar animators_
  325. AnimControl instvar ANIMATOR_CLASS_
  326. set ANIMATOR_CLASS_ Editor
  327. set editor [new $ANIMATOR_CLASS_ ""]
  328. }
  329. AnimControl instproc close-animator { animator } {
  330. $self instvar animators_
  331. set pos [lsearch $animators_ $animator]
  332. set animators_ [lreplace $animators_ $pos $pos]
  333. $self instvar winlist_
  334. if [info exists winlist_] {
  335. $winlist_ delete $pos
  336. }
  337. }
  338. # Create a listbox which lists all active windows
  339. AnimControl instproc on-winlist {} {
  340. $self instvar winlist_ 
  341. if [info exists winlist_] {
  342. return
  343. }
  344. set w .winList
  345. toplevel $w
  346. wm title $w "Active Animations"
  347.         frame $w.f -borderwidth 0 -highlightthickness 0
  348. frame $w.f.f
  349. listbox $w.f.f.a -xscrollcommand "$w.f.f.ah set" 
  350. -yscrollcommand "$w.f.f2.av set" -height 10 
  351. -selectmode single 
  352. pack $w.f.f.a -fill both -side top -expand true 
  353. set winlist_ $w.f.f.a
  354. scrollbar $w.f.f.ah -orient horizontal -width 10 -borderwidth 1 
  355. -command "$w.f.f.a xview"
  356. $w.f.f.ah set 0.0 1.0
  357. pack $w.f.f.ah -side bottom -fill x -expand true
  358. frame $w.f.f2
  359. pack $w.f.f2 -side left -fill y
  360. scrollbar $w.f.f2.av -orient vertical -width 10 -borderwidth 1 
  361. -command "$w.f.f.a yview"
  362. $w.f.f2.av set 0.0 1.0
  363. pack $w.f.f2.av -side top -fill y -expand true
  364.         pack $w.f.f -side left -fill both -expand true
  365. pack $w.f -side left -fill both -expand true
  366. bind $w.f.f.a <Double-ButtonPress-1> "$self winlist_show $w.f.f.a"
  367. bind $w.f.f.a <Destroy> "+$self unset winlist_"
  368. bind $w <Control-q> "[AnimControl instance] done"
  369. bind $w <Control-Q> "[AnimControl instance] done"
  370. bind $w <Control-w> "destroy $w"
  371. bind $w <Control-W> "destroy $w"
  372. bind $w <Escape> "destroy $w"
  373. # Fill window list
  374. $self instvar animators_
  375. foreach a $animators_ {
  376. $w.f.f.a insert end [$a get-name]
  377. }
  378. }
  379. AnimControl instproc winlist_show { wlbox } {
  380. set idx [$wlbox index active]
  381. $self instvar animators_
  382. if [llength $animators_] then {
  383.     raise [[lindex $animators_ $idx] get-tkwin]
  384. }
  385. }