wake-up from stand-by (and reconnect to library)

VA Smalltalk is a "100% VisualAge compatible" IDE that includes the original VisualAge technology and the popular VA Assist and WidgetKit add-ons.

Moderators: Eric Clayberg, wembley, tc, Diane Engles, solveig

wake-up from stand-by (and reconnect to library)

Postby jkoepp » Wed Dec 03, 2008 2:39 am

If you don't close your development image when setting the computer into stand-by modus then after wakeing up under Windows you often have to >Reconnect to Server< or you get this kind of error when using the browsers:

Error accessing ...\manager.dat: Error reading reply header from server

The simple Solution is to execute EmLibrary startUp.

More elegant would be to have Smalltalk catching the Operating System wake-up event and informing each application in the manner of restart, startUp or exiting / shutDown methods in SubApplication.

The following code defines an application for doing this under Windows. When going to standby each subapplication receives #fallAsleep. When waking up #wakeUp is send. As an example is #reconnectToDefaultLibrary given, which should be moved to another (development-) application. This is tested with Emsrv, not with file connections.

Code: Select all
Application create: #WindowsStandbyWakeUpApp with:
    (#( PlatformWidgets)
        collect: [:each | Smalltalk at: each ifAbsent: [
            Application errorPrerequisite: #WindowsStandbyWakeUpApp missing: each]])!

WindowsStandbyWakeUpApp becomeDefault!
Application subclass: #WindowsStandbyWakeUpApp
    instanceVariableNames: ''
    classVariableNames: 'LastPowerMessage OldWmPowerbroadcastSelector '
    poolDictionaries: 'PlatformConstants '!


WindowsStandbyWakeUpApp becomeDefault!

!OSWidget publicMethods !

wmPowerbroadcast: wParam with: lParam
   "Notifies every OSShell that a power-management event has occurred.
   
   wParam
      0 = Query Suspend - PBT_APMQUERYSUSPEND Return TRUE to grant the request to suspend. To deny the request, return BROADCAST_QUERY_DENY
      4 = Suspend = Standy-by Modus (PBT_APMSUSPEND)
      7 = Wake up by user (PBT_APMRESUMESUSPEND)
      18 = Wake up general (PBT_APMRESUMEAUTOMATIC)
      
   see http://msdn.microsoft.com/en-us/library/aa373247(VS.85).aspx"

   ^WindowsStandbyWakeUpApp wmPowerbroadcast: wParam with: lParam! !

OSWidget categoriesFor: #'wmPowerbroadcast:with:' are: #('AbtRun-Internal')!

!SubApplication class publicMethods !

fallAsleep
   "The default behavior for the receiver before the computer
   goes to stand-by (sleep) modus is to do nothing.
   
   This is called each time before the computer goes to stand-by (sleep) modus"!

wakeUp
   "The default behavior for the receiver after the computer is woken up
   from stand-by is to do nothing.
   
   This is called each time the computer is woken up from stand-by modus" ! !

SubApplication class categoriesFor: #'fallAsleep' are: #('EM-API')!
SubApplication class categoriesFor: #'wakeUp' are: #('EM-API')!

!WindowsStandbyWakeUpApp class publicMethods !

loaded
   "install messageselector for handling the power-management event"
   
   | selector |
   "keep a possible previous message-selector for WmPowerbroadcast for uninstall"
   selector := OSWidget eventTableAt: WmPowerbroadcast.
   selector ~= #wmPowerbroadcast:with: ifTrue: [OldWmPowerbroadcastSelector := selector].
   
   OSWidget eventTableAt: WmPowerbroadcast put: #wmPowerbroadcast:with:
!

reconnectToDefaultLibrary
   "After the computer is woken up from stand-by
   reconnect to the default libary if the connection is broken during stand-by time"
   
   | libraryClass emLibrary |
   
   libraryClass := Smalltalk at: #EmLibrary ifAbsent: [^self].
   emLibrary := libraryClass default.
   (emLibrary isNil or: [emLibrary isActuallyOpen not]) ifTrue: [^Transcript cr; show: 'Wake-up from standby. Library not connected.'].
   
   "Check broken emsrv connection"
   emLibrary primitiveQueryServerType: emLibrary fileId.
   emLibrary errorCode = 0 ifTrue: [^self "still connected"]. "242 seems to be the errorCode for lost emsrv-connections after wake-up"
   
   "reconnect"
   emLibrary := libraryClass startUp.
   
   Transcript cr; show: 'Library reconnected after wake-up to ', emLibrary fullName!

removing
   "reset EventTable to previous state"
      
   OSWidget eventTableAt: WmPowerbroadcast put: OldWmPowerbroadcastSelector!

wakeUp
   "example: after the computer is woken up from stand-by
   reconnect to the default Libary when in development"
   
   "System isRuntime ifTrue: [^self].
   
   self reconnectToDefaultLibrary"!

wmPowerbroadcast: wParam with: lParam
   "Notifies every OSShell that a power-management event has occurred.
   
   wParam
      0 = Query Suspend - PBT_APMQUERYSUSPEND Return TRUE to grant the request to suspend. To deny the request, return BROADCAST_QUERY_DENY
      4 = Suspend = Standy-by Modus (PBT_APMSUSPEND)
      7 = Wake up by user (PBT_APMRESUMESUSPEND)
      18 = Wake up general (PBT_APMRESUMEAUTOMATIC)
      
   see http://msdn.microsoft.com/en-us/library/aa373247(VS.85).aspx"

   "execute the message only once (while MSWindows is sending the message to every OSShell in the image)"
   wParam = LastPowerMessage ifTrue: [^1].
   LastPowerMessage := wParam.
   
   wParam = 18 ifTrue: [
      "PBT_APMRESUMEAUTOMATIC"
      System loadedSubApplications do: [:app | app wakeUp] ].

   wParam = 4 ifTrue: [
      "PBT_APMSUSPEND"
      System loadedSubApplications do: [:app | app fallAsleep] ].
   
   ^1! !

WindowsStandbyWakeUpApp class categoriesFor: #'loaded' are: #('initialize - release')!
WindowsStandbyWakeUpApp class categoriesFor: #'reconnectToDefaultLibrary' are: #('events')!
WindowsStandbyWakeUpApp class categoriesFor: #'removing' are: #('initialize - release')!
WindowsStandbyWakeUpApp class categoriesFor: #'wakeUp' are: #('events')!
WindowsStandbyWakeUpApp class categoriesFor: #'wmPowerbroadcast:with:' are: #('events')!

WindowsStandbyWakeUpApp initializeAfterLoad!

WindowsStandbyWakeUpApp loaded!


Installation: Copy the code to an empty workspace, select all and execute fileIn.

The code is not prepared for packaging and not tested in Runtime. WakeUp and reconnectToDefaultLibrary works fine in my 7.0 development image. It should work with all Versions, even 8.0.

Juergen
jkoepp
 
Posts: 9
Joined: Mon Mar 31, 2008 4:40 am

Re: wake-up from stand-by (and reconnect to library)

Postby marten » Wed Dec 03, 2008 5:54 am

jkoepp wrote:If you don't close your development image when setting the computer into stand-by modus then after wakeing up under Windows you often have to >Reconnect to Server< or you get this kind of error when using the browsers:

Error accessing ...\manager.dat: Error reading reply header from server



I also had these problems ! Thanks ! Cool stuff ...

Marten
Marten Feldtmann, Principal Smalltalk User, Private
SkypeMe callto://marten.feldtmann
marten
[|]
 
Posts: 641
Joined: Sat Oct 14, 2006 7:10 am
Location: Hamburg - Germany

Re: wake-up from stand-by (and reconnect to library)

Postby wembley » Wed Dec 03, 2008 10:19 am

Juergen -

This looks very interesting. We will very definitely evaluate it for inclusion in V8 -- case 38220.
John O'Keefe [|], Principal Smalltalk Architect, Instantiations Inc.
wembley
Moderator
 
Posts: 405
Joined: Mon Oct 16, 2006 3:01 am
Location: Durham, NC


Return to VA Smalltalk 7.0, 7.5 & 8.0

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest