Demo WebApp with PHP/ScriptEZ.API.

 

<?php

 

// PHP 5.3.x for Windows can be downloaded here: http://windows.php.net

// WAMPServer can be downloaded here: http://www.wampserver.com/en/

//////////////////////////////////////////////////////////////////////////////////

// Author: SYO

// Demo ScriptEZ.API based Web application which uses:

// . Thread pool,

// . ServerDoc API(client/server),

// . SQLite usage (direct file access).

//

// Purpose: Sends to a WebBrowser HTML info which is fetched from the WebServer

//          and ServerDoc Performance Counters (Both are running in the same box)

//////////////////////////////////////////////////////////////////////////////////

 

// Global constantes & variables

const SERVERDOC_HOST = "localhost";

const SERVERDOC_PORT = "8088";

 

 

// selecting known fields for a SQLite 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";

 

$ScriptEZ      = null;

$sink          = null;

$hThreadPool   = 0;

 

class ScriptEZ_EventHandlers

{

  var $onExit   = 0;

  function onRun( $task_param )

  {

    Global $ScriptEZ,

           $SELECTED_FIELDS,

           $sink;

 

    // Step 1: posting field names

    $r = $ScriptEZ->ServerDocPostSQLFields($SELECTED_FIELDS);

 

    com_message_pump(1000);

       

    // Step 2: posting SQL query

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

    $QueryCommandString =  "SELECT * FROM PERF_COUNT WHERE DATE='" . date("d/m/Y") . "';";

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

    $r = $ScriptEZ->ServerDocPostSQLQuery($QueryCommandString,":memory:");

    com_message_pump(1000);

    if($r != 0 )

    {

      $sink->onExit = 3;

    }

    return 0;

  }

 

  function onServerDocStatus( $error_number )

  {

    Global $ScriptEZ,

           $SELECTED_FIELDS,

           $hThreadPool,

           $sink;

    if($error_number == 0 )

    {

      $r = $ScriptEZ->AddTask($hThreadPool,0); // Thread for posting SQL Query

    }

    else

    {

      $sink->onExit = 3;

    }

    return 0;

  }

 

  function onServerDocData( $DataRows, $RowCount, $DataSize )

  {

    Global $ScriptEZ,

           $SELECTED_FIELDS,

           $hThreadPool,

           $sink;

    if($DataSize > 0)

    {

      $htmArray = "<span style='color:blue'><a href='ServerDocOnRun.pdf'>SERVERDOC</a> PERFORMANCE VIEWER</span>”.

                  ”<br><table border=0>";       

      $bstrData  = $ScriptEZ->ConvertStringToBSTR($DataRows);

      $DataArray = explode("\t",$bstrData);

      $LabelArray= explode(",",$SELECTED_FIELDS);

      for($i=0; $i < 17; $i++)

      {

       $htmArray .= "<tr><td style='background:#333333;padding:.75pt' ><span style='color:yellow' />".

                    $LabelArray[$i] .

                    "<td style='background:#CCFFFF;padding:.75pt'>" . $DataArray[$i] ."</td></tr>";

      }

      $ScriptEZ->FreeBSTR($bstrData);

      $htmArray .= "</table>";

      echo $htmArray;

    }

    $sink->onExit = 3;

    return -1; // let component to free memory chunk

 }

 

}

 

// ---------------------------------------------------------------

// client/server SQLite with ScriptEZ.API: remote database file

// ---------------------------------------------------------------

function ClientServerMain()

{

  Global $ScriptEZ,

         $sink,

         $hThreadPool;

   

  // Instantiate COM object

  $ScriptEZ = new COM("ScriptEZ.API");

  $beginTime = $ScriptEZ->GetTimeStampEx();

  // creating thread pool with 1 thread for posting SQL query

  $hThreadPool = $ScriptEZ->CreateTaskQueueObject(1);

 

   

  // Instantiate COM Callback object

  $sink     = new ScriptEZ_EventHandlers();

  // install event sink class

  com_event_sink($ScriptEZ, $sink, "_IAPIEvents");

 

  date_default_timezone_set("Europe/Madrid");

  echo "<br><br>".

       "Welcome and have a nice day !<br>" . "In France, today is " . date("F j, Y, G:i:s a").

       "<br><br>";  $OSVersion = $ScriptEZ->GetWindowsVersion();

  echo "<table border=1>" .

       "<tr>" .

        "<td style='background:#333333;padding:.75pt' ><span style='color:#FF6600' />Global Used Memory %</td>" .

        "<td style='background:#333333;padding:.75pt' ><span style='color:#FF6600' />WebServer Used Memory </td>" .

        ($OSVersion != 90 && $OSVersion != 2000 && $OSVersion != 40 ?

         "<td style='background:#333333;padding:.75pt' ><span style='color:#FF6600' />WebServer's Handles & Threads </td>"

         :

         "<td style='background:#333333;padding:.75pt' ><span style='color:#FF6600' />WebServer's Threads </td>"

        ).

       "</tr>" .

       "<tr>"  .

       "<td>" . $ScriptEZ->ProbeMemoryStatus(0) . "% over " .

                number_format($ScriptEZ->ProbeMemoryStatus(1), 0, ',', ' ') . " KBytes <br></td>" .

       "<td>" . number_format($ScriptEZ->ProbeMemoryStatus(7), 0, ',', ' ') . " KBytes (" .

                number_format($ScriptEZ->ProbeMemoryStatus(7)/$ScriptEZ->ProbeMemoryStatus(1)*100.0,2, '.', ' ') . "%)</td>" .

       ($OSVersion != 90 && $OSVersion != 2000 && $OSVersion != 40 ?

         "<td>".$ScriptEZ->ProbeMemoryStatus(-7)." | ".$ScriptEZ->ProbeMemoryStatus(-4)."</td>"

         :

         "<td>".$ScriptEZ->ProbeMemoryStatus(-4)."</td>"

       ).

       "</tr>" .

       "</table><br>";

 

 

 $r  = $ScriptEZ->ServerDocConnect(SERVERDOC_HOST,SERVERDOC_PORT,-1);

 if( $r == 0 )

 {

   com_message_pump(4000);

   // endless loop

   while($sink->onExit != 3)

   {

     com_message_pump(0);// dispatch COM events

   }

 }

 $endTime = $ScriptEZ->GetTimeStampEx();

 $takenTime = $ScriptEZ->GetTimeDiff($endTime,$beginTime);

 echo  "<br>".

       "<span style='color:blue;font-size:10.0pt'>" .

       "© Powered by <a href='http://sites.google.com/site/scriptezapi/'>ScriptEZ.API</a>" .

       " version: ". $ScriptEZ->ProbeMemoryStatus(-2) .

       " (Client/Server Database@" . SERVERDOC_HOST . "),</span><span style='color:red;font-size:10.0pt'> execution time: ".

       (strlen($takenTime) <= 0 ?"< 1 sec":$takenTime) .

       "<br>&nbsp;&nbsp;&nbsp;&nbsp;<a href='WebScriptEZ_PHP.htm' title='Click to see source code'>Show me !</a>" .

       "</span><br><br>";

 

 $ScriptEZ->FreeBSTR($beginTime);

 $ScriptEZ->FreeBSTR($endTime);

 if(strlen($takenTime)>0)

 {

   $ScriptEZ->FreeBSTR($takenTime);

 }

 $ScriptEZ = null;

 $sink     = null;

}

 

// ---------------------------------------------------------------

// Direct access to SQLite database file: ServerDoc resides in the

// same host as does the WebServer

// ---------------------------------------------------------------

function LocalFileMain()

{

  Global $ScriptEZ,

         $SELECTED_FIELDS;

   

  // Instantiate COM object

  $ScriptEZ = new COM("ScriptEZ.API");

   

  $beginTime = $ScriptEZ->GetTimeStampEx();

  date_default_timezone_set("Europe/Madrid");

  echo "Welcome and have a nice day !<br>" . "In France, today is " . date("F j, Y, G:i:s a").

       "<br><br>";  $OSVersion = $ScriptEZ->GetWindowsVersion();

  echo "<table border=1>" .

       "<tr>" .

       "<td style='background:#333333;padding:.75pt' ><span style='color:#FF6600' />Global Used Memory %</td>" .

       "<td style='background:#333333;padding:.75pt' ><span style='color:#FF6600' />WebServer Used Memory </td>" .

        ($OSVersion != 90 && $OSVersion != 2000 && $OSVersion != 40 ?

         "<td style='background:#333333;padding:.75pt' ><span style='color:#FF6600' />WebServer's Handles & Threads </td>"

         :

         "<td style='background:#333333;padding:.75pt' ><span style='color:#FF6600' />WebServer's Threads </td>"

        ).

       "</tr>" .

       "<tr>"  .

       "<td>" . $ScriptEZ->ProbeMemoryStatus(0) . "% over " .

                number_format($ScriptEZ->ProbeMemoryStatus(1), 0, ',', ' ') . " KBytes <br></td>" .

       "<td>" . number_format($ScriptEZ->ProbeMemoryStatus(7), 0, ',', ' ') . " KBytes (" .

                number_format($ScriptEZ->ProbeMemoryStatus(7)/$ScriptEZ->ProbeMemoryStatus(1)*100.0,2,'.',' ')."%)</td>" .

        ($OSVersion != 90 && $OSVersion != 2000 && $OSVersion != 40 ?

         "<td>".$ScriptEZ->ProbeMemoryStatus(-7)." | ".$ScriptEZ->ProbeMemoryStatus(-4)."</td>"

         :

         "<td>".$ScriptEZ->ProbeMemoryStatus(-4)."</td>"

        ).

       "</tr>" .

       "</table><br>";

 

  // open SQLite file which is generated by ServerDoc App

  $hSQlite = $ScriptEZ->CreateSQLiteDBObject("c:\PerfView.ldb");

  if($hSQlite>0)

  {

    // This pragma boost reading operations since no table locking

    $r = $ScriptEZ->SQLite_execQuery($hSQlite,"PRAGMA read_uncommitted=1;");

     

    // Submitting a simple SQL query for ServerDoc Performance Counters

    $QueryCommandString =  "SELECT * FROM PERF_COUNT WHERE DATE='" . date("d/m/Y") . "';";

    $hSQliteRecordSet = $ScriptEZ->SQLite_execQuery($hSQlite,$QueryCommandString);

    if($hSQliteRecordSet>0)

    {

      $LabelArray= explode(",",$SELECTED_FIELDS);

      $htmArray = "<span style='color:blue'>" .

      "<a href='ServerDocOnRun.pdf'>SERVERDOC</a> PERFORMANCE VIEWER</span><br><table border=0>";       

      // iterating the recordset

      do

      {

        for($i=0; $i < 17; $i++)

        { 

         $value = $ScriptEZ->SQLite_fieldValueByName($hSQliteRecordSet,$LabelArray[$i]);

         $htmArray .= "<tr><td style='background:#333333;padding:.75pt' ><span style='color:yellow' />". $LabelArray[$i].

                      "<td style='background:#CCFFFF;padding:.75pt'>" . $value ."</td></tr>";

                           

         $ScriptEZ->FreeBSTR($value);

        }

      }

      while($ScriptEZ->SQLite_nextRow($hSQliteRecordSet) == true);

 

      $htmArray .= "</table>";

      echo $htmArray;

      $ScriptEZ->SQLite_freeRecordSet($hSQliteRecordSet);

    }

    $ScriptEZ->DestroySQLiteDBObject($hSQlite);

  }

 

  $endTime = $ScriptEZ->GetTimeStampEx();

  $takenTime = $ScriptEZ->GetTimeDiff($endTime,$beginTime);

   

  echo  "<br>".

        "<span style='color:blue;font-size:10.0pt'>" .

        "© Powered by <a href='http://sites.google.com/site/scriptezapi/'>ScriptEZ.API</a>" .

        " version: ". $ScriptEZ->ProbeMemoryStatus(-2) .

        " (Direct Database access),</span><span style='color:red;font-size:10.0pt'> execution time= " .

        (strlen($takenTime) <= 0 ?"< 1 sec":$takenTime) .

        "<br>&nbsp;&nbsp;&nbsp;&nbsp;<a href='WebScriptEZ_PHP.htm' title='Click to see source code'>Show me !</a>" .

        "</span><br><br>";

   

  $ScriptEZ->FreeBSTR($beginTime);

  $ScriptEZ->FreeBSTR($endTime);

  if(strlen($takenTime)>0)

  {

    $ScriptEZ->FreeBSTR($takenTime);

  }

  $ScriptEZ = null;

}

 

 

// ---------------------------------------------------------------

// PDO access to SQLite database file. ServerDoc resides in the

// same host as does the WebServer

// ---------------------------------------------------------------

function PDOMain()

{

  Global $ScriptEZ,

         $SELECTED_FIELDS;

   

  // Instantiate COM object

  $ScriptEZ = new COM("ScriptEZ.API");

   

  $beginTime = $ScriptEZ->GetTimeStampEx();

  date_default_timezone_set("Europe/Madrid");

  echo "<br><br>".

       "Welcome and have a nice day !<br>" . "In France, today is " . date("F j, Y, G:i:s a").

       "<br><br>";  $OSVersion = $ScriptEZ->GetWindowsVersion();

  echo "<table border=1>" .

       "<tr>" .

       "<td style='background:#333333;padding:.75pt' ><span style='color:#FF6600' />Global Used Memory %</td>" .

       "<td style='background:#333333;padding:.75pt' ><span style='color:#FF6600' />WebServer Used Memory </td>" .

        ($OSVersion != 90 && $OSVersion != 2000 && $OSVersion != 40 ?

         "<td style='background:#333333;padding:.75pt' ><span style='color:#FF6600' />WebServer's Handles & Threads </td>"

         :

         "<td style='background:#333333;padding:.75pt' ><span style='color:#FF6600' />WebServer's Threads </td>"

        ).

       "</tr>" .

       "<tr>"  .

       "<td>" . $ScriptEZ->ProbeMemoryStatus(0) . "% over " .

                number_format($ScriptEZ->ProbeMemoryStatus(1), 0, ',', ' ') . " KBytes <br></td>" .

       "<td>" . number_format($ScriptEZ->ProbeMemoryStatus(7), 0, ',', ' ') . " KBytes (" .

                number_format($ScriptEZ->ProbeMemoryStatus(7)/$ScriptEZ->ProbeMemoryStatus(1)*100.0,2, '.', ' ') . "%)</td>" .

        ($OSVersion != 90 && $OSVersion != 2000 && $OSVersion != 40 ?

         "<td>".$ScriptEZ->ProbeMemoryStatus(-7)." | ".$ScriptEZ->ProbeMemoryStatus(-4)."</td>"

         :

         "<td>".$ScriptEZ->ProbeMemoryStatus(-4)."</td>"

        ).

       "</tr>" .

       "</table><br>";

 

    // open SQLite file which is generated by ServerDoc App

    $SQlite3 = new PDO("sqlite:c:/PerfView.ldb");

    // This pragma boost reading operations since no table locking

    $r = $SQlite3->exec("PRAGMA read_uncommitted=1;");

     

    // Submitting a simple SQL query for ServerDoc Performance Counters

    $QueryCommandString =  "SELECT * FROM PERF_COUNT WHERE DATE='" . date("d/m/Y") . "';";

    $LabelArray= explode(",",$SELECTED_FIELDS);

   

    $recordSet = $SQlite3->query($QueryCommandString);

    if($recordSet != null)

    {

     $htmArray = "<span style='color:blue'>".

                 "<a href='ServerDocOnRun.pdf'>SERVERDOC</a> PERFORMANCE VIEWER</span><br><table border=0>";       

     // iterating the recordset

     foreach($recordSet as $row)

     {

      if($row != null)

      {

       for($i=0; $i < 17; $i++)

       {

        $value = $row[$LabelArray[$i]];

        $htmArray .= "<tr><td style='background:#333333;padding:.75pt' ><span style='color:yellow' />".$LabelArray[$i].

                     "<td style='background:#CCFFFF;padding:.75pt'>" . $value ."</td></tr>";

       }

      }

     }

     $recordSet = null;

     $htmArray .= "</table>";

     echo $htmArray;

    }

    $SQlite3 = null;

 

  $endTime = $ScriptEZ->GetTimeStampEx();

  $takenTime = $ScriptEZ->GetTimeDiff($endTime,$beginTime);

   

  echo  "<br>".

        "<span style='color:blue;font-size:10.0pt'>" .

        "© Powered by <a href='http://sites.google.com/site/scriptezapi/'>ScriptEZ.API</a>" .

        " version: ". $ScriptEZ->ProbeMemoryStatus(-2) .

        " (PHO Data Object access),</span><span style='color:red;font-size:10.0pt'> execution time= " .

        (strlen($takenTime) <= 0 ?"< 1 sec":$takenTime) .

        "<br>&nbsp;&nbsp;&nbsp;&nbsp;<a href='WebScriptEZ_PHP.htm' title='Click to see source code'>Show me !</a>" .

        "</span><br><br>";

   

  $ScriptEZ->FreeBSTR($beginTime);

  $ScriptEZ->FreeBSTR($endTime);

  if(strlen($takenTime)>0)

  {

    $ScriptEZ->FreeBSTR($takenTime);

  }

  $ScriptEZ = null;

}

 

// ---------------------------------------------------------------

// Three ways to start script here

// ---------------------------------------------------------------

 

// option 1: local database file

LocalFileMain();

 

// option 2: remote database file

ClientServerMain();

 

// option 3: PHP Data Object access

PDOMain();

 

?>