ExternalStreamMonitor.st
author Claus Gittinger <cg@exept.de>
Fri, 15 Jun 2018 04:00:37 +0200
changeset 18220 d1ebaddf1100
parent 18138 e374fbbe6c55
child 18447 7fd51d712f8a
permissions -rw-r--r--
#UI_ENHANCEMENT by cg class: Tools::CheckinInfoDialog class changed: #windowSpec

"{ Encoding: utf8 }"

"
 COPYRIGHT (c) 1997 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libtool' }"

"{ NameSpace: Smalltalk }"

SystemStatusMonitor subclass:#ExternalStreamMonitor
	instanceVariableNames:'semaphores streams filterOpenStreamsOnlyHolder
		filterSocketsOnlyHolder filterConnectedSocketsOnlyHolder'
	classVariableNames:''
	poolDictionaries:''
	category:'Monitors-ST/X'
!

!ExternalStreamMonitor class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1997 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"


!

documentation
"
    This view shows smalltalk's external stream instances - a debugging tool.

    [disclaimer:]
        this is based on one of the oldest tools in the system, written in the early 90's.
        It does in no way reflect the way GUIs are designed/written these days.
        
    [see also:]
        Stream ExternalStream
        WindowGroup
        ProcessMonitor
        SemaphoreMonitor

    [author:]
        Claus Gittinger

    [start with:]
        ExternalStreamMonitor open
"
! !

!ExternalStreamMonitor class methodsFor:'defaults'!

defaultLabel
    ^ 'External Streams Monitor'

    "Created: 23.1.1997 / 02:52:53 / cg"
! !

!ExternalStreamMonitor class methodsFor:'menu specs'!

filterMenu
    "This resource specification was automatically generated
     by the MenuEditor of ST/X."

    "Do not manually edit this!! If it is corrupted,
     the MenuEditor may not be able to read the specification."


    "
     MenuEditor new openOnClass:ExternalStreamMonitor andSelector:#filterMenu
     (Menu new fromLiteralArrayEncoding:(ExternalStreamMonitor filterMenu)) startUp
    "

    <resource: #menu>

    ^ 
     #(Menu
        (
         (MenuItem
            label: 'Open Streams Only'
            hideMenuOnActivated: false
            indication: filterOpenStreamsOnlyHolder
          )
         (MenuItem
            label: '-'
          )
         (MenuItem
            label: 'Sockets Only'
            hideMenuOnActivated: false
            indication: filterSocketsOnlyHolder
          )
         (MenuItem
            label: 'Connected Sockets Only'
            hideMenuOnActivated: false
            indication: filterConnectedSocketsOnlyHolder
          )
         )
        nil
        nil
      )
!

mainMenu
    "This resource specification was automatically generated
     by the MenuEditor of ST/X."

    "Do not manually edit this!! If it is corrupted,
     the MenuEditor may not be able to read the specification."


    "
     MenuEditor new openOnClass:ExternalStreamMonitor andSelector:#mainMenu
     (Menu new fromLiteralArrayEncoding:(ExternalStreamMonitor mainMenu)) startUp
    "

    <resource: #menu>

    ^ 
     #(Menu
        (
         (MenuItem
            label: 'File'
            submenu: 
           (Menu
              (
               (MenuItem
                  label: 'Exit'
                  itemValue: closeRequestToTopView
                )
               )
              nil
              nil
            )
          )
         (MenuItem
            label: 'Filter'
            submenuChannel: filterMenu
          )
         (MenuItem
            label: 'Operations'
            submenuChannel: operationsMenu
          )
         )
        nil
        nil
      )

    "Modified: / 07-04-2017 / 14:21:44 / cg"
!

operationsMenu
    "This resource specification was automatically generated
     by the MenuEditor of ST/X."

    "Do not manually edit this!! If it is corrupted,
     the MenuEditor may not be able to read the specification."


    "
     MenuEditor new openOnClass:ExternalStreamMonitor andSelector:#filterMenu
     (Menu new fromLiteralArrayEncoding:(ExternalStreamMonitor filterMenu)) startUp
    "

    <resource: #menu>

    ^ 
     #(Menu
        (
         (MenuItem
            label: 'Close'
            itemValue: menuCloseSelected
          )
         (MenuItem
            label: '-'
          )
         (MenuItem
            label: 'Inspect'
            itemValue: inspectStream
          )
         )
        nil
        nil
      )

    "Created: / 07-04-2017 / 14:22:18 / cg"
! !

!ExternalStreamMonitor methodsFor:'aspects'!

filterConnectedSocketsOnlyHolder
    filterConnectedSocketsOnlyHolder isNil ifTrue:[
        filterConnectedSocketsOnlyHolder := false asValue.
        filterConnectedSocketsOnlyHolder onChangeSend:#filterConnectedSocketsOnlyHolderChanged to:self.
    ].    
    ^ filterConnectedSocketsOnlyHolder
!

filterOpenStreamsOnlyHolder 
    filterOpenStreamsOnlyHolder isNil ifTrue:[
        filterOpenStreamsOnlyHolder := false asValue.
        filterSocketsOnlyHolder onChangeSend:#filterOpenStreamsOnlyHolder to:self.
    ].    
    ^ filterOpenStreamsOnlyHolder
!

filterSocketsOnlyHolder
    filterSocketsOnlyHolder isNil ifTrue:[
        filterSocketsOnlyHolder := false asValue.
        filterSocketsOnlyHolder onChangeSend:#filterSocketsOnlyHolderChanged to:self.
    ].    
    ^ filterSocketsOnlyHolder
! !

!ExternalStreamMonitor methodsFor:'drawing'!

titleLine
    OperatingSystem isMSWINDOWSlike ifTrue:[
        ^ 'Type                 Global                     Handle Detail'.
    ] ifFalse:[    
        ^ 'Type                 Global                     FD Detail'.
    ].
    
    "
     ExternalStreamMonitor open
    "

    "Modified: / 17.6.1998 / 14:52:48 / cg"
!

updateList
    "update list of semaphores"

    |newList|

    shown ifTrue:[
        newList := ExternalStream allSubInstances.
        newList := newList reject:#isExecutor.
        
        self filterSocketsOnlyHolder value ifTrue:[
            newList := newList select:[:eachStream | eachStream isSocket].
        ].
        self filterOpenStreamsOnlyHolder value ifTrue:[
            newList := newList select:[:eachStream | eachStream isOpen].
        ].
        self filterConnectedSocketsOnlyHolder value ifTrue:[
            newList := newList select:[:eachStream | eachStream isSocket and:[eachStream isConnected]].
        ].
        
        "sort by hashKey - will not always generate unique numbers,
         but most of the time, this works ... for now"

        newList sort:[:s1 :s2 | 
                    s1 className < s2 className
                    or:[ s1 className = s2 className
                         and:[false]]
                ].

        newList ~= streams ifTrue:[
            self updateStatus:newList
        ].
    ].
    self installDelayedUpdate.
!

updateStatus:newStreamsList
    "update status display of semaphores"

    |oldList list oldSelection newSelection|

    shown ifTrue:[
        oldList := listView list.
        oldSelection := listView selection.
        oldSelection notNil ifTrue:[
            oldSelection := oldSelection collect:[:lineNr | streams at:(lineNr - self numberOfHeadlines) ].
            newSelection := OrderedCollection new.
        ].

        newStreamsList notNil ifTrue:[
            streams := WeakArray withAll:newStreamsList.
        ].
        streams notNil ifTrue:[
            list := OrderedCollection new:(streams size + self numberOfHeadlines).
            list add:self titleLine.
            list add:(String new:self titleLine size withAll:$-).

            streams validElementsDo:[:aStream |
                |waiters waitersNames type globalNameOrNil handleString
                 isOpen handle detail color line|

"/                "/ need a copy - it may change while being enumerated
"/                [
"/                    count := aSemaphore count.
"/                    waiters := aSemaphore waitingProcesses copy.
"/                ] valueUninterruptably.

"/                str := '' writeStream.
"/                [
"/                    waiters notNil ifTrue:[
"/                        waiters do:[:aProcess |
"/                            str nextPut:$[.
"/                            aProcess id printOn:str.
"/                            str nextPutAll:' '''.
"/                            str nextPutAll:(aProcess name contractTo:40).
"/                            str nextPutAll:'''<'.
"/                            aProcess priority printOn:str.
"/                            str nextPutAll:'>]'.
"/                            str space.
"/                        ].
"/                    ]
"/                ] valueUninterruptably.
"/                waitersNames := str contents.

"/                (aSemaphore respondsTo:#name) ifTrue:[
"/                    nm := aSemaphore name.
"/                    nm isNil ifTrue:[
"/                        nm := ''
"/                    ]
"/                ] ifFalse:[
"/                    nm := ''
"/                ].
                type := aStream className.

                globalNameOrNil := Smalltalk keyAtValue:aStream.
                
                aStream isFileStream ifTrue:[
                    detail := aStream pathName.
                ] ifFalse:[
                    aStream isSocket ifTrue:[
                        detail := ''.
                        aStream getPeer notNil ifTrue:[
                            detail := detail,(aStream getPeerName printString),(aStream getPeer printString).
                        ].
                        aStream port notNil ifTrue:[
                            detail := detail,':',aStream port printString
                        ].
                    ] ifFalse:[
                        aStream isPipeStream ifTrue:[
                            detail := aStream commandString.
                        ].    
                    ].
                ].
                
                OperatingSystem isMSWINDOWSlike ifTrue:[
                    aStream isOpen ifTrue:[
                        handle := aStream fileHandle.
                        handle isInteger ifTrue:[
                            handleString := handle printString
                        ] ifFalse:[
                            handleString := (handle address ? 0) hexPrintString
                        ].
                    ].
                    handleString := (handleString ? '') leftPaddedTo:7
                ] ifFalse:[
                    aStream isOpen ifTrue:[
                        [
                        ] on:StreamError do:[
                            handleString := aStream fileDescriptor printString
                        ].    
                    ].
                    handleString := (handleString ? '') leftPaddedTo:3
                ].

                line := (type contractTo:20) paddedTo:20.
                line := line , ' ' , (((globalNameOrNil ? '') contractTo:25) paddedTo:25).
                line := line , ' ' , handleString.
                line := line , ' ' , (detail ? '').

                isOpen := aStream isOpen.
                
"/                line := line
"/                          , ' '
"/                          , ((nm contractTo:25) paddedTo:25)
"/                          , ' '
"/                          , (count printStringLeftPaddedTo:3)
"/                          , ' '
"/                          , owner printString
"/                          , ' '
"/                          , (waiters size printStringLeftPaddedTo:3)
"/                          , ' '
"/                          , waitersNames.

                isOpen ifFalse:[
                    color := Color red.
                ] ifTrue:[
                    color := Color blue.
                ].
                line := line colorizeAllWith:color.

                list add:line.
                oldSelection notNil ifTrue:[
                    (oldSelection includesIdentical:aStream) ifTrue:[
                        newSelection add:list size.
                    ]
                ]
            ].
        ].
        "avoid flicker"
        (oldList notNil and:[oldList size == list size]) ifTrue:[
            list keysAndValuesDo:[:idx :entry |
                (oldList at:idx) ~= entry ifTrue:[
                    listView at:idx put:entry
                ]
            ]
        ] ifFalse:[
            listView setList:list.
            "the first two entries cannot be selected"
            listView attributeAt:1 put:#disabled.
            listView attributeAt:2 put:#disabled.
        ].
        newSelection notNil ifTrue:[
            listView selectWithoutScroll:newSelection
        ].
        listView flush
    ].
    self installDelayedUpdate.

    "Modified: / 11-10-2017 / 13:56:14 / cg"
! !

!ExternalStreamMonitor methodsFor:'menu'!

selectedStreamsDo:aBlock
    "evaluate aBlock on all selected streams"

    self selectionIndicesDo:[:n |
        |nr stream|
        
        nr := n - self numberOfHeadlines.
        nr notNil ifTrue:[
            nr > 0 ifTrue:[
                stream := streams at:nr.
                (stream notNil and:[stream ~~ 0]) ifTrue:[
                   aBlock value:stream
                ]
            ]
        ]
    ].
!

statusMenu
    "return a popUpMenu"

    <resource: #programMenu>

    |labels selectors m|

    labels := resources array:#(
                         'Close'
                         '-'
                         'Inspect'
                         'References'
                        ).
    selectors := #(
                         menuCloseSelected
                         nil
                         inspectStream
                         menuReferences
                        ).

    updateProcess isNil ifTrue:[
        labels := labels,(resources array:#('-' 'Update' )).
        selectors := selectors , #(nil updateView). 
    ].

    m := PopUpMenu labels:labels
                   selectors:selectors.


    listView hasSelection ifFalse:[
        m disableAll:#(
                         menuCloseSelected
                         inspectStream
                      )
    ].
    ^ m

    "Modified: / 18-08-2017 / 14:34:12 / cg"
! !

!ExternalStreamMonitor methodsFor:'queries'!

numberOfHeadlines
    ^ 2
! !

!ExternalStreamMonitor methodsFor:'user actions'!

doubleClicked
    "open an inspector on the selected stream"

    self inspectStream

    "Created: / 23-01-1997 / 03:22:04 / cg"
    "Modified: / 31-01-1997 / 22:33:27 / cg"
    "Modified (comment): / 07-04-2017 / 14:28:01 / cg"
!

filterConnectedSocketsOnlyHolderChanged
    self sensor pushUserEvent:#updateList for:self
!

filterOpenStreamsOnlyHolderChanged
    self sensor pushUserEvent:#updateList for:self
!

filterSocketsOnlyHolderChanged
    self sensor pushUserEvent:#updateList for:self
!

inspectStream
    "open an inspector on the selected stream(s)"

    self selectedStreamsDo:[:eachStream | eachStream inspect]

    "Modified (format): / 07-04-2017 / 14:27:45 / cg"
!

menuCloseSelected
    "close the selected stream(s)"

    self selectedStreamsDo:[:eachStream | eachStream close].
    self updateList.

    "Created: / 07-04-2017 / 14:26:04 / cg"
!

menuReferences
    "show references to the selected stream"

    self selectedStreamsDo:[:eachStream |
        (ReferenceFinder findAllPathsTo:eachStream) inspect
        "/ eachStream allReferences inspect
    ]

    "Modified (format): / 07-04-2017 / 14:27:45 / cg"
! !

!ExternalStreamMonitor class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !