Remote and shared lock management (DDE with TCP/IP way).

 

When ScriptEZ.API based script file is running, an identification area is appended

to the system menu of the console windows as following:

 

ScriptEZ

 

'//////////////////////////////////////////////////////////////////////////////

' Author: SY

' Demo ScriptEZ.API based application which uses facilities from RAD API:

' . Dynamic Data Exchange (DDE) usage

' . Thread Pool usage

' . Echo() method usage which displays always messages into a console window

'   instead of WScript.Echo() with 2 behaviours for cscript.exe or wscript.exe

' . ScriptEZ.API Remoting Control Interface usage by NPTelnet.exe program

'   (Connect to ScriptEZ.API process with <processid (PID)> as connection name)

'

' Purpose: Using DDE protocol to ask 10 times per second for lock/unlock onto

'          random numbers between 1224 and 1234 via DDE Server (aka Rldrmon.exe)

'          which is connected to COM/DCOM App (aka RLoader.exe) onto a local or

'          remote PC, otherwise Rldrmon attempts to a ServerDoc app with TCP/IP

'//////////////////////////////////////////////////////////////////////////////

 

const SCRIPTEZ_API = "ScriptEZ.API" ' Component name

 

Dim Hostname

Dim WshShell

Dim ScriptEZ

Dim StartTime

Dim onExit

Dim hDde

Dim hThreadPool

Dim locked

Dim failed

 

' start the script

'////////////////

On Error Resume Next

Main()

 

' Main Subroutine

'////////////////

Sub Main()

 

  locked       = 0

  failed       = 0

  onExit       = 0

  StartTime    = Now()

  Set WshShell = WScript.CreateObject("WScript.Shell")

 

 

 

  ' instantiate component object with callback interface

  Set ScriptEZ = WScript.CreateObject(SCRIPTEZ_API,"ScriptEZ_")

  ScriptEZ.SetCPUCoreAffinity "0" ' CORE1 if multi-core CPU

 

  ' Register App for receiving AppEvent(s) with onAppEvent(EventID)

  ScriptEZ.AppRegister

 

  Dim hStringTable

  hStringTable = ScriptEZ.MallocStringTable(2)

  r = ScriptEZ.AddStringTableItem(hStringTable,"localhost")

  r = ScriptEZ.AddStringTableItem(hStringTable,"127.0.0.1")

  Hostname = ScriptEZ.InputComboBox(0,"Select or enter a host on which Remote Loader app is running: ",hStringTable)

  ScriptEZ.FreeStringTable hStringTable

  if Len(Hostname) <= 0 Then

      ScriptEZ.AppUnregister

      Set ScriptEZ = Nothing

      Exit Sub

  End If

 

  ' case 1 : init conversation with DDE server - connecting to a RLoader app (COM/DCOM way)

  hDde = ScriptEZ.StartConnectionToDDEServer("dde_remotelock@"+Hostname,"lock@"+Hostname)

  ' retry to start DDE server (rldrmon.exe) connecting to a ServerDoc instance, if failed

  If hDde <= 0 Then

   ' case 2: connecting to a ServerDoc app (tcp/ip way)

   WshShell.Run "rldrmon.exe /autoconnect:"+Hostname+":8088",6

 

   ' case 2b: connecting to a ServerDoc for Mobile app (tcp/ip way)

   'WshShell.Run "rldrmon.exe /autoconnect_mobile:"+Hostname+":8088",6

 

   Do Until ScriptEZ.FindWindow("ATL:00424730","Remote Loader Monitor ["+Hostname+":8088]") > 0

     WScript.Sleep 2000

     ScriptEZ.clearConsole

     ScriptEZ.Echo "...Wait till Remote Loader Monitor ["+Hostname+":8088] is up !"

   Loop

   WScript.Sleep 4000 ' to let DDE subsystem to initialize

   ' init conversation with DDE server

   hDde = ScriptEZ.StartConnectionToDDEServer("dde_remotelock@"+Hostname,"lock@"+Hostname)

  End If

  If hDde <= 0 Then

   ScriptEZ.AppUnregister

   Set ScriptEZ = Nothing

   Set WshShell = Nothing

   Exit Sub

  End If

 

  ' Hide console window if 0

  ScriptEZ.ShowConsoleWindow 1

 

  ' creating a pool with 1 thread to request lock/unlock

  hThreadPool = ScriptEZ.CreateTaskQueueObject(1)

  r = ScriptEZ.AddTask( hThreadPool, hDde )   

 

  ' block until AppEventID = 3 is received

  Do Until onExit = 3

       WScript.Sleep 200

  Loop

 

  ScriptEZ.AppUnregister

  If hDde > 0 Then ScriptEZ.ReleaseConnectionFromDDEServer hDde

  Set ScriptEZ = Nothing

  Set WshShell = Nothing

End Sub

 

' Callbacks for App Events notification and fetching help text

'/////////////////////////////////////////////////////////////

Sub ScriptEZ_onAppEvent(EventID)

 

  If EventID = 3 Then onExit = EventID

  

End Sub

 

Function ScriptEZ_AppEventHelp()

 

 ScriptEZ_AppEventHelp = CStr("AppEventID = 3 to exit")

 

End Function

 

' Callback for thread pool object (TaskQueueObject)

'//////////////////////////////////////////////////

Function ScriptEZ_onRun( localStorage )

 

  Dim isLocked

  WScript.Sleep 100 ' repeat this task 10 times per second

  If hDde > 0 Then

 

     ' initialise random number

     Randomize

 

     ' random numbers between 1224 and 1234

     random = Int( (1234-1224+1) * Rnd + 1224.0)

 

     isLocked = ScriptEZ.ExecuteCommandOfDDEServer(hDde,"lockon["+CStr(random)+"]")

     If isLocked > 0 Then

      locked = locked + 1

      isLocked = ScriptEZ.ExecuteCommandOfDDEServer(hDde,"lockoff["+CStr(random)+"]")

     Else

      failed = failed + 1

     End If

  End If

  ScriptEZ.SetConsoleTitle CStr(StartTime) + " - Lock Stats: locked(" + CStr(locked) + "), failed(" + CStr(failed) + ")"

  ScriptEZ.ClearConsole

  ScriptEZ.Echo vbLF + _

                "Lock server: " + Hostname + vbLF + _

                "Locked ....: " + CStr(Fix((locked/(locked+failed))*100)) +"%" + vbLF + _

                "Failed ....: " + CStr(Fix((failed/(locked+failed))*100)) +"%"

  If onExit = 3 Then

   ScriptEZ_onRun = CLng(0) ' stop this task

  Else

   ScriptEZ_onRun = CLng(1) ' run again this task

  End If

 

End Function