PHP WinBinder: Alternative control 2

From The Uniform Server Wiki
Revision as of 07:26, 28 January 2010 by Ric (talk | contribs) (New page: {{Nav PHP WinBinder}} '''''Alternative control part 2''''' This page is a continuation of the alternative control. It covers starting and stopping Apache and MySQL servers from a windows ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

 

UniServer 5-Nano
PHP WinBinder.

Alternative control part 2

This page is a continuation of the alternative control. It covers starting and stopping Apache and MySQL servers from a windows application.

Initial Problem

Starting or stopping a server, spawns a separate process, this allows the main application to continue executing uninterrupted.

An implication of this, other than monitoring a server’s status there is no way to determine the time it completes.

What is required is a timer to periodically check a server’s status. This can be handled by using a timer control for example

Add this to the constants define("ID_APP_TIMER", 9001);

Add this to create control section

 wb_create_timer($mainwin, ID_APP_TIMER, 100);     // One second interval

The above produces a 100ms tick. Every 100ms calls the handler function

The handler function contains this code which in turn calls our main_timer() function

 // Main timer tick call other functions
 if($id ==ID_APP_TIMER){  // 100 ms tick
  main_timer();           // Call main timer function
 }

A timer must be specifically killed using function wb_destroy_timer($window, ID);

Add the above line to the close window section as shown below

case IDCLOSE:                                // Constant IDCLOSE (8) predefined 
    wb_destroy_timer($window, ID_APP_TIMER); // Kill timer
    wb_destroy_window($window);              // Destroy the window
    break; 

Top

Timer function main_timer()

The timer function contains four sections corresponding to Apache start/stop and MySQL start/stop

Each section has the following format:

 if($Start_timer_1){             // Run code if timer_1 started
     if(apache_running()){       // Check server
        $Start_timer_1 = FALSE;  // Disable timer
        main_init();             // Update buttons and indicators
     }
 }

Variable $Start_timer_1 is a global allowing its value to be retained between calls to this function.

The complete function code as follows:

//=== Main Timer handler ======================================================
function  main_timer(){

 global $Start_timer_Apache_run;
 global $Start_timer_Apache_stop;
 global $Start_timer_MySQL_run;
 global $Start_timer_MySQL_stop;

 // Apache run
 if($Start_timer_Apache_run){             // Run code if timer started
     if(apache_running()){                // Check server
        $Start_timer_Apache_run = FALSE;  // Disable timer
        main_init();                      // Update buttons and indicators
     }
 }

 //Apache stop
 if($Start_timer_Apache_stop){             // Run code if timer started
     if(!apache_running()){                // Check server
        $Start_timer_Apache_stop = FALSE;  // Disable timer
        main_init();                       // Update buttons and indicators
     }
 }

 //MySQL run
 if($Start_timer_MySQL_run){             // Run code if timer_1 started
     if(MySQL_running()){                // Check server
        $Start_timer_MySQL_run = FALSE;  // Disable timer
        main_init();                     // Update buttons and indicators
     }
 }

 //MySQL stop
 if($Start_timer_MySQL_stop){             // Run code if timer_1 started
     if(!MySQL_running()){                // Check server
        $Start_timer_MySQL_stop = FALSE;  // Disable timer
        main_init();                      // Update buttons and indicators
     }
 }
}
//====================================================== Main Timer handler ===

Top

Global Variables

The global variables need to be defined at the top of the main script

Add the following just above constants

// Common variables
$Start_timer_Apache_run  = FALSE; //pseudo timer control
$Start_timer_Apache_stop = FALSE; //pseudo timer control
$Start_timer_MySQL_run   = FALSE; //pseudo timer control
$Start_timer_MySQL_stop  = FALSE; //pseudo timer control

They are also used in the handler function add the lines as shown at the top of this function.

function process_main($window, $id){

 global $Start_timer_Apache_run;
 global $Start_timer_Apache_stop;
 global $Start_timer_MySQL_run;
 global $Start_timer_MySQL_stop;


Top

Server main functions

For a Windows application the main server functions stop_servers.php and start_servers.php require some modifications

These were copied from folder UniServer\unicon\main their names changed to stop_servers.inc.php and start_servers.inc.php

They had a quick clean up and a wait function added wb_wait($window,2000) to allow Windows to start processing. Probably the time is excessive and can be reduced. You can twiddle with the value whats importent is to add these with an include statment.

Add the last two lines as shown

include "../../../unicon/main/includes/config.inc.php"; // US Core
include "../../../unicon/main/includes/functions.php";  // US Core 

include "start_servers.inc.php";
include "stop_servers.inc.php";

With these function added we can add more functionality.

Top

Push Buttons

Button provide a user interface that can easily be abused by this I mean a user is not restricted they can click buttons in any order and even double click.

For some application this may or may not be an issue however attempting to start or stop servers in quick succession can produce undesirable effects.

Similarly if a server is not running associated buttons if clicked will again produce undesirable results.

A neat solution is to disable any buttons until their functionality can be guaranteed.

Top

Server All Start/Stop

Code on the right is added to the Handler Function

It is self-explanatory.

Depending on the button caption text the servers are either started or stopped.

Appropriate buttons are disabled.

A timer started and server either started or stopped. The parameter passed is binary coded 1=Apache, 2=MySQL 3=Both

Timer function checks a server’s status, on detecting a state change calls the init functions this sets appropriate indicator and button states.

// All button
case ID_PB_ALL:  // All servers
 $text = wb_get_text(wb_get_control($window, ID_PB_ALL));        // get button txt
 if( $text == 'Start All'){                                      // Start All servers
   wb_set_enabled(wb_get_control($window, ID_PB_ALL), FALSE);    // Disable All button
   wb_set_enabled(wb_get_control($window, ID_PB_APACHE), FALSE); // Disable Apache button
   wb_set_enabled(wb_get_control($window, ID_PB_MYSQL), FALSE);  // Disable MySQL button
   $Start_timer_Apache_run = TRUE;                               // start timer
   $Start_timer_MySQL_run  = TRUE;                               // start timer
   main_server_start(3);                                         // start servers
 }
 if( $text == 'Stop All'){                                       // Stop servers
  wb_set_enabled(wb_get_control($window, ID_PB_ALL), FALSE);     // Disable All button
  wb_set_enabled(wb_get_control($window, ID_PB_APACHE), FALSE);  // Disable Apache button
  wb_set_enabled(wb_get_control($window, ID_PB_MYSQL), FALSE);   // Disable MySQL button
  $Start_timer_Apache_stop = TRUE;                               // start timer
  $Start_timer_MySQL_stop  = TRUE;                               // start timer
  main_server_stop(3);                                           // stop servers
 }
break;

Top

Apache Start/Stop

Code on the right is added to the Handler Function

It is self-explanatory.

Depending on the button caption text the servers are either started or stopped.

Appropriate buttons are disabled.

A timer started and server either started or stopped. The parameter passed is binary coded 1=Apache, 2=MySQL 3=Both

Timer function checks a server’s status, on detecting a state change calls the init functions this sets appropriate indicator and button states.

// Apache button
case ID_PB_APACHE:  // Apache server
 $text = wb_get_text(wb_get_control($window, ID_PB_APACHE));     // get button txt
 if( $text == 'Start Apache'){                                   // Start Apache server
  wb_set_enabled(wb_get_control($window, ID_PB_APACHE), FALSE);  // Disable Apache button
  wb_set_enabled(wb_get_control($window, ID_PB_ALL), FALSE);     // Disable All button 
  $Start_timer_Apache_run = TRUE;                                // start timer
  main_server_start(1);                                          // start Apache server
 }
 if( $text == 'Stop Apache'){                                    // Stop Apache server
  wb_set_enabled(wb_get_control($window, ID_PB_APACHE), FALSE);  // Disable Apache button
  $Start_timer_Apache_stop = TRUE;                               // start timer
  main_server_stop(1);                                           // stop Apache server
 }
break;

Top

MySQL Start/Stop

Code on the right is added to the Handler Function

It is self-explanatory.

Depending on the button caption text the servers are either started or stopped.

Appropriate buttons are disabled.

A timer started and server either started or stopped. The parameter passed is binary coded 1=Apache, 2=MySQL 3=Both

Timer function checks a server’s status, on detecting a state change calls the init functions this sets appropriate indicator and button states.

// MySQL button
case ID_PB_MYSQL:  // Apache server
 $text = wb_get_text(wb_get_control($window, ID_PB_MYSQL));     // get button txt
 if( $text == 'Start MySQL'){                                   // Start MySQL server
  wb_set_enabled(wb_get_control($window,  ID_PB_MYSQL), FALSE); // Disabe MySQL button
  wb_set_enabled(wb_get_control($window, ID_PB_ALL), FALSE);    // Disable All button
  $Start_timer_MySQL_run = TRUE;                                // start timer
  main_server_start(2);                                         // start MySQL server 
 }
 if( $text == 'Stop MySQL'){                                     // Stop MySQL server
  wb_set_enabled(wb_get_control($window,  ID_PB_MYSQL), FALSE);  // Disabe MySQL button
  $Start_timer_MySQL_stop = TRUE;                                // start timer
  main_server_stop(2);                                           // stop MySQL server
 }
break;

Top

Apanel

Code on the right is added to the Handler Function

Note: Code is placed above the switch($id) statement

//Apanel Button
 if($id==ID_PB_APANEL){                                 // Apanel button
  $apache_port = get_apache_port();                     // Get actual port      
  if($apache_port == 80){                               // Standard port no change
    $text='http://localhost/apanel/';                   // Use as is
  }
  else{                                                 // Non standard hence
    $text='http://:'.$apache_port.'/localhost/apanel/'; // hence add port
  }
  wb_exec($text);                                       // Open page in browser
} 

Top

phpMyAdmin

Code on the right is added to the Handler Function

Note: Code is placed above the switch($id) statement

//phpMyAdmin Button
if($id==ID_PB_PHPMYADMIN){                                 // phpMyADmin button
  $apache_port = get_apache_port();                        // Get actual port      
  if($apache_port == 80){                                  // Standard port no change
    $text='http://localhost/apanel/phpMyAdmin/';           // Use as is
  }
  else{                                                    // Non standard hence
   $text='http://:'.$apache_port.'/localhost/apanel/phpMyAdmin/'; // hence add port
  }
  wb_exec($text);                                          // Open page in browser
}

Top

My CMD

Code on the right is added to the Handler Function

It is self-explanatory.

// MySQL CMD button
case ID_PB_MYCMD:  // My CMD server
   main_server_start(8);                                     // start MySQL CMD
break;

Top

Test

To see this code in action run test_5.bat (code contained in test_5.phpw)

Run Uniform Server, start and stop each server observe button and indicator states.

Start and stop both servers again observe button and indicator states.

After testing close both Servers and application script.

Top

Summary

At this stage you have fully functional servers.

Next step is to add functionality to remaining buttons covered on the next page.

Top