ServerDoc App Client.

 

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

to the system menu of the console windows as following:

 

 

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

' Author: SY

' Copyright 2011

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

' . Connect, query for SQL data flow from within ServerDoc App

' . Threadpool 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: Client connecting to ServerDoc and posting queries for Perf Counters

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

 

const SCRIPTEZ_API    = "ScriptEZ.API" ' Component name

const PROBING_DELAY   = 10000          ' Delay in ms before launching a query

 

Dim SELECTED_FIELDS

Dim WshShell

Dim ScriptEZMain

Dim ScriptEZConnect

Dim hThreadPool

Dim onExit

Dim InputString

Dim HostAndPort

Dim typeEnc

 

' start the script

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

On Error Resume Next

Main()

 

' Main Subroutine

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

Sub Main()

 

  ' selecting known fields for a SQL table

  SELECTED_FIELDS   = "DATE,SERVER_TIME,IN_USE_MEMORY,OWN_MEMORY,PENDING_TASKS," + _

                      "SEND,RECV,RECV_MAX,RECV_MIN,QUERY_COUNT,QUERY_FAILURE,UPDATE_COUNT," + _

                      "INCOMING_REQUEST,IP_LIMIT,WHITE_IP,BLOCKED_IP,COM_APARTMENT"

  onExit       = 0

  Set WshShell = CreateObject("WScript.Shell")

 

  ' instantiate component object with callback interface

  Set ScriptEZMain = WScript.CreateObject(SCRIPTEZ_API,"ScriptEZMain_")

  'ScriptEZMain.SetCPUCoreAffinity "0"

 

  ' creating thread pool with 1 thread for posting SQL query

  hThreadPool = ScriptEZMain.CreateTaskQueueObject(1)

 

  ' connect to server

  hStringTable = ScriptEZMain.MallocStringTable(2)

  r = ScriptEZMain.AddStringTableItem(hStringTable,"localhost:8088")

  r = ScriptEZMain.AddStringTableItem(hStringTable,"88.173.237.11:8088")

  InputString = ScriptEZMain.InputComboBox(0, _

                                          "Enter hostname and port to reach ServerDoc App, such like localhost:8088", _

                                          hStringTable)

  ScriptEZMain.FreeStringTable hStringTable

 

  If Len(InputString) <= 0 Then

    Set ScriptEZMain = Nothing

    Set WshShell = Nothing  

    Exit Sub

  End If

 

  HostAndPort = Split(InputString,":")

  Set ScriptEZConnect = WScript.CreateObject(SCRIPTEZ_API,"ScriptEZConnect_")

 

  hStringTable = ScriptEZMain.MallocStringTable(3)

  r = ScriptEZMain.AddStringTableItem(hStringTable,".FULL ENCRYPTION (RECOMMENDED)")

  r = ScriptEZMain.AddStringTableItem(hStringTable,".CUSTOM ENCRYPTION WITH 64 BYTES")

  r = ScriptEZMain.AddStringTableItem(hStringTable,".NO ENCRYPTION (UNSAFE & INCOMPATIBLE)")

  encryptionType = ScriptEZMain.InputComboBox(0,"SELECT A TYPE OF DATA ENCRYPTION FOR DIALING WITH SERVERDOC",_

                                              -hStringTable)

  ScriptEZMain.FreeStringTable hStringTable

 

  If Len(encryptionType) Then

      ' The third parameter can be chosen among 3 values:

      '  0 (no encryption),

      ' -1 (encrypting all data, by default),

      '  n bytes encryption

      Select Case encryptionType

        Case ".NO ENCRYPTION (UNSAFE & INCOMPATIBLE)"

             typeEnc = 0

        Case ".FULL ENCRYPTION (RECOMMENDED)"

             typeEnc = -1

        Case ".CUSTOM ENCRYPTION WITH 64 BYTES"

             typeEnc = 64  ' along with 64 bytes

      End Select

      r  = ScriptEZConnect.ServerDocConnect(HostAndPort(0), HostAndPort(1),typeEnc)

      ScriptEZMain.FreeBSTR encryptionType

   Else

      Set ScriptEZConnect = Nothing

      Set ScriptEZMain = Nothing

      Set WshShell = Nothing  

      Exit Sub

   End If

 

  ' Restore previously saved console window's positions

  r = ScriptEZConnect.RestoreWindowLayout(-1,WScript.ScriptName+ "_" + HostAndPort(0)+":"+HostAndPort(1))

 

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

  ScriptEZConnect.AppRegister

 

  ' block until socket break or exit event

  Do Until onExit = 3

      WScript.Sleep 100

  Loop

 

  ' Save current console window's positions

  r = ScriptEZConnect.SaveWindowLayout(-1,WScript.ScriptName+ "_" + HostAndPort(0)+":"+HostAndPort(1))

 

  ' App unregister before ending

  ScriptEZConnect.AppUnregister

  WScript.Sleep 3000

  Set ScriptEZConnect = Nothing

  Set ScriptEZMain = Nothing

  Set WshShell = Nothing

 

End Sub

 

' Callbacks for App Events notification and fetching help text

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

Sub ScriptEZConnect_onAppEvent(EventID)

 

 ScriptEZMain.Echo " -> EventID:" + CStr(EventID)

 Select Case EventID

   Case 3  onExit= 3

 End Select

 

End Sub

 

Function ScriptEZConnect_AppEventHelp()

 

 ScriptEZConnect_AppEventHelp = vbLF +_

  CStr("Filename: " + Chr(34) + CStr(WScript.ScriptFullName) + Chr(34) + vbLF +_

       "  **** Supported AppEvents ****" + vbLF +_

       "AppEventID = 3" + vbTab + "to exit" + vbLF _

       )

End Function

 

' Callbacks for ServerDoc Comm

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

Function ScriptEZConnect_onServerDocStatus(error_number)

 

  Dim code_err

  code_err = CStr(error_number)

 

  If error_number = 0 Then

    r = ScriptEZConnect.ServerDocPostSQLFields(SELECTED_FIELDS )

    r = ScriptEZMain.AddTask(hThreadPool,0) ' Thread for posting SQL Query

    ScriptEZMain.SetConsoleTitle "ServerDoc@" + InputString + " connected"

  Else

    ScriptEZMain.Echo code_err

  End If

  ScriptEZConnect_onServerDocStatus = CLng(0)

 

End Function

 

' Callbacks for ServerDoc Comm

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

Function ScriptEZConnect_onServerDocData(DataRows, RowCount, DataSize)

 

  If RowCount > 0 Then

    Dim bstrData,encryptLabel

    ' converting raw C-String to BSTR

    bstrData  = ScriptEZMain.ConvertStringToBSTR(DataRows)

    bstrArray  = Split(bstrData,vbTab)

    ScriptEZMain.FreeBSTR bstrData

 

    Select Case typeEnc

           Case 0

                encryptLabel = "none (unsafe & incompatible)"

           Case -1

                encryptLabel = "full (recommended)"

           Case 64

                encryptLabel = "along with 64 bytes (custom)"

    End Select

 

    bstrData = vbCRLF + _

              " Refresh delay____: " + CStr(PROBING_DELAY/1000) + " seconds" + vbCRLF + _

              " ." + vbCRLF + _

              " DATE_____________: " + bstrArray(0) + vbCRLF + _

              " SERVER_TIME______: " + bstrArray(1) + vbCRLF + _

              " IN_USE_MEMORY____: " + bstrArray(2) + vbCRLF + _

              " OWN_MEMORY_______: " + bstrArray(3) + vbCRLF + _

              " PENDING_TASKS____: " + bstrArray(4) + vbCRLF + _

              " SEND_____________: " + bstrArray(5) + vbCRLF + _

              " RECV_____________: " + bstrArray(6) + vbCRLF + _

              " RECV_MAX_________: " + bstrArray(7) + vbCRLF + _

              " RECV_MIN_________: " + bstrArray(8) + vbCRLF + _

              " QUERY_COUNT______: " + bstrArray(9) + vbCRLF + _

              " QUERY_FAILURE____: " + bstrArray(10) + vbCRLF + _

              " UPDATE_COUNT_____: " + bstrArray(11) + vbCRLF + _

              " INCOMING_REQUEST_: " + bstrArray(12) + vbCRLF + _

              " IP_LIMIT_________: " + bstrArray(13) + vbCRLF + _

              " WHITE_IP_________: " + bstrArray(14) + vbCRLF + _

              " BLOCKED_IP_______: " + bstrArray(15) + vbCRLF + _

              " COM_APARTMENT____: " + bstrArray(16) + vbCRLF + _

              " ." + vbCRLF + _

              " Encryption type__: " + encryptLabel

 

    ScriptEZMain.ClearConsole

    ScriptEZMain.Echo CStr(bstrData) + vbLF

    ScriptEZMain.FreeString raw_data

  End If

  ScriptEZConnect_onServerDocData = CLng(0)

 

End Function

 

' Callback for thread pool object (TaskQueueObject)

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

Function ScriptEZMain_onRun( localStorage )

 

    ' Yielding a while

    WScript.Sleep PROBING_DELAY

   

    ' Submitting a simple SQL query to ServerDoc about its Performance counters

    QueryCommandString =  "SELECT * FROM PERF_COUNT WHERE DATE='"+ CStr(Date)+"';"

    ' Instead of a full path filename, ":memory:" is a special name for Perf counters

    r = ScriptEZConnect.ServerDocPostSQLQuery(QueryCommandString,":memory:" )

   

    If onExit = 3 Then

     ScriptEZMain_onRun = CLng(0) ' stop this task

     ScriptEZMain.Echo "Exit thread pool..."

     r = ScriptEZConnect.ServerDocDisconnect

     Exit Function

    ElseIf r <> 0 Then

     ScriptEZMain.Echo "Terminate task..."

     ScriptEZMain_onRun = CLng(0) ' stop this task

     ScriptEZMain.SetConsoleTitle "ServerDoc@" + InputString + " disconnected"

    Else

     ScriptEZMain_onRun = CLng(1) ' continue this task again

    End If   

 

End Function