TCP/IP Server and SQLite usage.

 

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:

' . TCP/IP usage

' . 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

' . SQlite API usage

' . String and BSTR usage

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

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

'

' Purpose: Simple TCP/IP Server broadcasting random data packet 2.5 times/second

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

 

const SCRIPTEZ_API = "ScriptEZ.API" ' Component name

 

Dim InboundDataDico ' Scripting.Dictionary object

Dim ScriptEZ

Dim ScriptEZConsole

Dim hSocketSvr

Dim StartTime

Dim TotalRecv

Dim hThreadPool

Dim hThreadPoolConsole

Dim onExit

Dim showRecvData

Dim enumerateClient

Dim hSQLite

' start the script

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

'On Error Resume Next

Main()

 

' Main Subroutine

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

Sub Main()

  hSQLite      = 0

  enumerateClient= 0

  showRecvData = 0

  onExit       = 0

  TotalRecv    = 0

  StartTime    = Now()

 

  Set InboundDataDico = CreateObject("Scripting.Dictionary")

 

  ' instantiate component object with callback interface

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

  Set ScriptEZConsole = WScript.CreateObject(SCRIPTEZ_API,"ScriptEZConsole_")

  'ScriptEZ.SetCPUCoreAffinity "0"

 

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

  ScriptEZ.AppRegister

  ScriptEZConsole.AppRegister

 

   ' Hide console window if 0

  ScriptEZ.ShowConsoleWindow 1

 

  ' create server socket object +  extra  encryption

  hSocketSvr = ScriptEZ.CreatePoolSocketServerObject(12348,vbNullString,100,8,0,0)

 

  If hSocketSvr > 0 Then

    ScriptEZ.EnableSocketObjectDataEncryption hSocketSvr,true,-1

 

    hSQLite = ScriptEZ.CreateSQLiteDBObject("c:\dummy.ldb")

    n=ScriptEZ.SQLite_execDML(hSQLite,"CREATE TABLE IF NOT EXISTS TConnection(" + _

                                      "[DATE] Text," + _

                                      "[CONNECTED_CLIENT] Text," + _

                                      "[BEGIN_TIME] Text," + _

                                      "[END_TIME] Text," + _

                                      "[UPDATE_TIME] Text," + _

                                      "[INBOUND_KBYTES] Text," + _

                                      "[OUTBOUND_KBYTES] Text);")

    ' creating thread pool with 4 threads

    hThreadPool = ScriptEZ.CreateTaskQueueObject(4)

   

    hThreadPoolConsole = ScriptEZConsole.CreateTaskQueueObject(1)

     r = ScriptEZConsole.AddTask( hThreadPoolConsole,0)

 

    ' block until AppEventID = 3 is received

    Do Until onExit = 3

       WScript.Sleep 1000

    Loop

  End If

 

  ScriptEZ.DestroySQLiteDBObject hSQLite

  ScriptEZ.AppUnregister

  ScriptEZConsole.AppUnregister

  WScript.Sleep 4000

  Set ScriptEZ = Nothing

  Set ScriptEZConsole = Nothing

  Set InboundDataDico = Nothing

End Sub

 

' Callbacks for App Events notification and fetching help text

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

Sub ScriptEZ_onAppEvent(EventID)

 

  If EventID = 3 Then onExit = EventID

  

  If EventID = 0 Then showRecvData = EventID

  If EventID = 1 Then showRecvData = EventID

  If EventID = 4 Then enumerateClient = 1

  If EventID = 5 Then enumerateClient = 0

 

End Sub

 

Function ScriptEZ_AppEventHelp()

 

 ScriptEZ_AppEventHelp = _

  CStr("  **** Supported AppEvents ****" + vbLF + _

       "AppEventID = 0" + vbTab + "to hide received data (default)" + vbLF + _

       "AppEventID = 1" + vbTab + "to display received data" + vbLF + _

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

       "AppEventID = 4" + vbTab + "to enumerate connected clients" + vbLF + _

       "AppEventID = 5" + vbTab + "not to enumerate connected clients (default)" + vbLF _

       )

End Function

 

 

' Callbacks for socket object

' ( server side )

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

Function ScriptEZ_onConnectionRequest(hSocketClientObjectProxy)

 

 If hSQLite = 0 Then

   ScriptEZ_onConnectionRequest = CLng(0) 

   Exit Function

 End If

 

 Dim zDate, zMonth, zDay, zClient

 zDate = Date()

 

 Dim hStringValueTable

 hStringValueTable = ScriptEZ.MallocStringTable(7)

 zMonth = CStr(Month(zDate))

 If Len(zMonth) < 2 Then zMonth = "0" +zMonth

 zDay = CStr(Day(zDate))

 If Len(zDay) < 2 Then zDay = "0" +zDay

 zClient = ScriptEZ.GetSocketConnectedClientName(hSocketClientObjectProxy)

 HostAndPort = Split(zClient,":")

 ScriptEZ.FreeBSTR zClient

 

 r = ScriptEZ.AddStringTableItem(hStringValueTable, CStr(Year(zDate))+zMonth+zDay )

 r = ScriptEZ.AddStringTableItem(hStringValueTable, HostAndPort(0)+":"+HostAndPort(1) )

 r = ScriptEZ.AddStringTableItem(hStringValueTable, CStr(Time()) )

 r = ScriptEZ.AddStringTableItem(hStringValueTable, "0" )

 r = ScriptEZ.AddStringTableItem(hStringValueTable, "0" )

 r = ScriptEZ.AddStringTableItem(hStringValueTable, "0" )

 r = ScriptEZ.AddStringTableItem(hStringValueTable, "0" )

 

 Dim sqlInsertCmd

 sqlInsertCmd = ScriptEZ.SQLite_buildInsertCommand("TConnection",hStringValueTable)

 'ScriptEZ.Echo sqlInsertCmd

 n = ScriptEZ.SQLite_execDML(hSQLite,sqlInsertCmd)

 

 ScriptEZ.FreeBSTR sqlInsertCmd

 ScriptEZ.FreeStringTable hStringValueTable

 

 ScriptEZ_onConnectionRequest = CLng(ScriptEZ.AddSocketClientObjectEventHandlers(hSocketClientObjectProxy))

 ' delegating to the thread pool....

 r = ScriptEZ.AddTask( hThreadPool,hSocketClientObjectProxy )

 

 r = ScriptEZ.SetSocketObjectLocalStorage(hSocketClientObjectProxy,0)

 InboundDataDico.Add HostAndPort(0)+":"+HostAndPort(1),0

 

End Function

 

' ( client/proxy side)

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

Function ScriptEZ_onConnectionState(hOwnerSocket,state_desc,error_number)

 

 Dim zClient, HostAndPort, hStringFieldTable, hStringValueTable, zWhere

 Dim r, sqlUpdateCmd

 

 ScriptEZ.Echo state_desc

 zClient = ScriptEZ.GetSocketConnectedClientName(hOwnerSocket)

 HostAndPort = Split(zClient,":")

 ScriptEZ.FreeBSTR zClient

 zWhere = "WHERE CONNECTED_CLIENT='" + HostAndPort(0) + ":" + HostAndPort(1) + "'"

 hStringFieldTable = ScriptEZ.MallocStringTable(1)

 r = ScriptEZ.AddStringTableItem(hStringFieldTable, "END_TIME" )

 

 hStringValueTable = ScriptEZ.MallocStringTable(1)

 r = ScriptEZ.AddStringTableItem(hStringValueTable, CStr(Time()) )

 

 sqlUpdateCmd = ScriptEZ.SQLite_buildUpdateCommand("TConnection",hStringFieldTable,hStringValueTable,zWhere)

 r = ScriptEZ.SQLite_execDML(hSQLite,sqlUpdateCmd)

 

 ScriptEZ.FreeBSTR sqlUpdateCmd

 ScriptEZ.FreeStringTable hStringFieldTable

 ScriptEZ.FreeStringTable hStringValueTable

 ScriptEZ_onConnectionState = CLng(0)

 

End Function

 

' ( client/proxy side)

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

Function ScriptEZ_onData(hOwnerSocket,raw_data,data_size)

 

 Dim bstrData,inBoundSize, sqlUpdateCmd

 Dim r, zClient, HostAndPort

 Dim zWhere, hStringFieldTable, hStringValueTable

 

 TotalRecv = TotalRecv + data_size

 If showRecvData = 1 Then

   bstrData = ScriptEZ.ConvertStringToBSTR(raw_data)

   ScriptEZ.Echo CStr(Now)  + " size(" + CStr(data_size) + "): " + CStr(bstrData)

   ScriptEZ.FreeBSTR bstrData

 End If

 ScriptEZ.FreeString raw_data

 

 

 zClient = ScriptEZ.GetSocketConnectedClientName(hOwnerSocket)

 HostAndPort = Split(zClient,":")

 ScriptEZ.FreeBSTR zClient

 inBoundSize = InboundDataDico.Item(HostAndPort(0)+":"+HostAndPort(1)) + data_size

 InboundDataDico.Remove( HostAndPort(0)+":"+HostAndPort(1) )

 InboundDataDico.Add HostAndPort(0)+":"+HostAndPort(1),inBoundSize

 

 zWhere = "WHERE CONNECTED_CLIENT='" + CStr(HostAndPort(0)) +":" + CStr(HostAndPort(1)) + "'"

 hStringFieldTable = ScriptEZ.MallocStringTable(2)

 r = ScriptEZ.AddStringTableItem(hStringFieldTable, "INBOUND_KBYTES" )

 r = ScriptEZ.AddStringTableItem(hStringFieldTable, "UPDATE_TIME" )

 

 hStringValueTable = ScriptEZ.MallocStringTable(2)

 r = ScriptEZ.AddStringTableItem(hStringValueTable, CStr(Round(inBoundSize/1024.0,2)) )

 r = ScriptEZ.AddStringTableItem(hStringValueTable, CStr(Time()) )

 

 sqlUpdateCmd = ScriptEZ.SQLite_buildUpdateCommand("TConnection",hStringFieldTable,hStringValueTable,zWhere)

 r = ScriptEZ.SQLite_execDML(hSQLite,sqlUpdateCmd)

 ScriptEZ.FreeBSTR sqlUpdateCmd

 ScriptEZ.FreeStringTable hStringFieldTable

 ScriptEZ.FreeStringTable hStringValueTable

 ScriptEZ_onData = CLng(0)

 

End Function

 

' Callbacks for thread pool object (TaskQueueObject)

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

Function ScriptEZ_onRun( hSocketClientProxy )

 

  ' Send random data 2.5 times per second

  WScript.Sleep 400

  r = Rnd()

  if( r = 0 ) then r = 1

  TestData = "BoM" + String(r*1536,"x") + "EoM" + vbLF + Chr(0)

  data_size =  Len(TestData)

  r = ScriptEZ.SendSocketObjectData( hSocketClientProxy,TestData, data_size )

  If r <> 0 Then

    ScriptEZ_onRun = CLng(0)

    Exit Function

  End If

 

  Dim HostAndPort, zClient, zWhere, r

  Dim hStringFieldTable, hStringValueTable

  Dim sqlUpdateCmd, total_size

  

  zClient = ScriptEZ.GetSocketConnectedClientName(hSocketClientProxy)

  HostAndPort = Split(zClient,":")

  ScriptEZ.FreeBSTR zClient

  zWhere = "WHERE CONNECTED_CLIENT='" + CStr(HostAndPort(0)) +":" + CStr(HostAndPort(1)) + "'"

  hStringFieldTable = ScriptEZ.MallocStringTable(2)

  r = ScriptEZ.AddStringTableItem(hStringFieldTable, "OUTBOUND_KBYTES" )

  r = ScriptEZ.AddStringTableItem(hStringFieldTable, "UPDATE_TIME" )

 

  hStringValueTable = ScriptEZ.MallocStringTable(2)

  total_size = ScriptEZ.GetSocketObjectLocalStorage(hSocketClientProxy)

  total_size = total_size + data_size

  r = ScriptEZ.SetSocketObjectLocalStorage(hSocketClientProxy,total_size)

  r = ScriptEZ.AddStringTableItem(hStringValueTable, CStr(Round(total_size/1024.0,2)) )

  r = ScriptEZ.AddStringTableItem(hStringValueTable, CStr(Time()) )

 

  sqlUpdateCmd = ScriptEZ.SQLite_buildUpdateCommand("TConnection",hStringFieldTable,hStringValueTable,zWhere)

  r = ScriptEZ.SQLite_execDML(hSQLite,sqlUpdateCmd)

  ScriptEZ.FreeBSTR sqlUpdateCmd

  ScriptEZ.FreeStringTable hStringFieldTable

  ScriptEZ.FreeStringTable hStringValueTable

 

  If onExit = 3 Then

    ScriptEZ_onRun = CLng(0)

  Else

    ScriptEZ_onRun = CLng(1)

  End If

 

End Function

 

' Notification on Task aborting

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

Sub ScriptEZ_onAbort( hSocketClientProxy )

End Sub

 

Function ScriptEZConsole_onRun( localStorage )

 

 Dim  zClient

 ScriptEZ.SetConsoleTitle CStr(StartTime) + " (ScriptEZ.API) - TCP/IP Server@12348"

 

 WScript.Sleep 1000

 ScriptEZ.ClearConsole

 If enumerateClient = 0 Then ScriptEZ.Echo  vbLF + " Received: " + _

                                            CStr(Round(TotalRecv/1024.0,2)) + " KBytes"

 If enumerateClient = 1 Then

    ScriptEZ.Echo  vbLF + " Clients:" + _

                  CStr(ScriptEZ.GetActiveSocketClientObjectNumber(hSocketSvr))+" Received: " + _

                  CStr(Round(TotalRecv/1024.0,2))+" KBytes"

    Do While 1

     hSocketClientProxy = ScriptEZ.EnumActiveSocketClientObject(hSocketSvr)

     If hSocketClientProxy <= 0 Then

        Exit Do

     End If

     zClient = ScriptEZ.GetSocketConnectedClientName(hSocketClientProxy)

     ScriptEZ.Echo  _

            CStr(zClient) + _

            " Inbound:" + CStr(ScriptEZ.SocketObjectPendingInboundMessageCount(hSocketClientProxy)) + _

            " Outbound:" + CStr(ScriptEZ.SocketObjectPendingOutboundMessageCount(hSocketClientProxy))

     ScriptEZ.FreeBSTR zClient

    Loop

    If ScriptEZ.GetActiveSocketClientObjectNumber(hSocketSvr) = 0 Then TotalRecv = 0

 End If

 If onExit = 3 Then

   ScriptEZConsole_onRun = CLng(0)

 Else

   ScriptEZConsole_onRun = CLng(1)

 End If

 

End Function