PHP WinBinder: Alternative control 2: Difference between revisions

From The Uniform Server Wiki
Jump to navigation Jump to search
(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 ...)
 
(Proofreading and grammatical changes; some minor reformatting)
 
Line 2: Line 2:
'''''Alternative control part 2'''''
'''''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.
This page is a continuation of the alternative control. It covers starting and stopping Apache and MySQL servers from a Windows application.


== Initial Problem ==
== Initial Problem ==
Starting or stopping a server, spawns a separate process, this allows the main application to continue executing uninterrupted.
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 is that there is no way to determine when it completes.


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.  


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


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


Add this to create control section  
Add this to create control section  
Line 17: Line 17:
  wb_create_timer($mainwin, ID_APP_TIMER, 100);    // One second interval
  wb_create_timer($mainwin, ID_APP_TIMER, 100);    // One second interval
</pre>
</pre>
The above produces a 100ms tick. Every 100ms calls the handler function
The above produces a 100ms tick. Every 100ms it calls the handler function.


The handler function contains this code which in turn calls our main_timer() function
The handler function contains this code, which in turn calls our main_timer() function.
<pre>
<pre>
  // Main timer tick call other functions
  // Main timer tick call other functions
Line 28: Line 28:
A timer must be specifically killed using function wb_destroy_timer($window, ID);
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  
Add this to the close window section as shown below:
<pre>
<pre>
case IDCLOSE:                                // Constant IDCLOSE (8) predefined  
case IDCLOSE:                                // Constant IDCLOSE (8) predefined  
Line 35: Line 35:
     break;  
     break;  
</pre>
</pre>
'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''
== Timer function main_timer() ==
== Timer function main_timer() ==
The timer function contains four sections corresponding to Apache start/stop and MySQL start/stop
The timer function contains four sections corresponding to Apache start/stop and MySQL start/stop.


Each section has the following format:
Each section has the following format:
Line 49: Line 49:
  }
  }
</pre>
</pre>
Variable $Start_timer_1 is a global allowing its value to be retained between calls to this function.
Variable $Start_timer_1 is a global, allowing its value to be retained between calls to this function.


The complete function code as follows:
The complete function code as follows:
Line 95: Line 95:
//====================================================== Main Timer handler ===
//====================================================== Main Timer handler ===
</pre>
</pre>
'''''[[#top | Top]]'''''
 
=== Global Variables ===
=== Global Variables ===
The global variables need to be defined at the top of the main script
The global variables need to be defined at the top of the main script.


Add the following just above constants
Add the following just above constants
Line 120: Line 120:


'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''
== Server main functions ==
== Server main functions ==
For a Windows application the main server functions stop_servers.php and start_servers.php require some modifications
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  
These were copied from folder UniServer\unicon\main and their names were 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.
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. What's important is to add these with an include staetment.


Add the last two lines as shown
Add the last two lines as shown
Line 136: Line 135:
include "stop_servers.inc.php";
include "stop_servers.inc.php";
</pre>   
</pre>   
With these function added we can add more functionality.
With these functions added, we can continue adding more capability.


'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''
== Push Buttons ==
== 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.
Buttons provide a user interface that can easily be abused. By this I mean that 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.
For some application this may or may not be an issue. Attempting to start or stop servers in quick succession can produce undesirable effects. Similarly if a server is not running, the associated buttons, if clicked, will again produce undesirable results.
 
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.
A neat solution is to disable any buttons until their functionality can be guaranteed.


'''''[[#top | Top]]'''''
=== Server All Start/Stop ===
=== Server All Start/Stop ===
{|
{|
|-
|-
|
|
Code on the right is added to the Handler Function
The code on the right is added to the Handler Function. It is self-explanatory.


It is self-explanatory.
Depending on the button caption text, the servers are either started or stopped. Appropriate buttons are disabled.
 
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
A timer is started and the server is 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.
The timer function checks a server’s status. On detecting a state change, it calls the init functions. This sets the appropriate indicator and button states.
|
|
<pre>
<pre>
Line 188: Line 180:
</pre>
</pre>
|}
|}
'''''[[#top | Top]]'''''
 
=== Apache Start/Stop ===
=== Apache Start/Stop ===
{|
{|
|-
|-
|
|
Code on the right is added to the Handler Function
The code on the right is added to the Handler Function. It is self-explanatory.


It is self-explanatory.
Depending on the button caption text, the servers are either started or stopped.  Appropriate buttons are disabled.


Depending on the button caption text the servers are either started or stopped.
A timer is started and the server is either started or stopped. The parameter passed is binary coded: 1=Apache, 2=MySQL 3=Both


Appropriate buttons are disabled.
The timer function checks a server’s status. On detecting a state change, it calls the init functions. This sets the appropriate indicator and button states.
 
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.
|
|
<pre>
<pre>
Line 223: Line 211:
</pre>
</pre>
|}
|}
'''''[[#top | Top]]'''''
=== MySQL Start/Stop ===
=== MySQL Start/Stop ===
{|
{|
|-
|-
|
|
Code on the right is added to the Handler Function
Code on the right is added to the Handler Function. It is self-explanatory.
 
It is self-explanatory.
 
Depending on the button caption text the servers are either started or stopped.


Appropriate buttons are disabled.
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
A timer is started and the server is 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.
The timer function checks a server’s status. On detecting a state change, it calls the init functions. This sets the appropriate indicator and button states.
|
|
<pre>
<pre>
Line 258: Line 241:
</pre>
</pre>
|}
|}
'''''[[#top | Top]]'''''
=== Apanel ===
=== Apanel ===
{|
{|
|-
|-
|
|
Code on the right is added to the Handler Function
Code on the right is added to the Handler Function.


Note: Code is placed above the  switch($id) statement
Note: Code is placed above the  switch($id) statement.
|
|
<pre>
<pre>
Line 281: Line 263:
</pre>
</pre>
|}
|}
'''''[[#top | Top]]'''''


=== phpMyAdmin ===
=== phpMyAdmin ===
Line 287: Line 268:
|-
|-
|
|
Code on the right is added to the Handler Function
Code on the right is added to the Handler Function.


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


|
|
Line 306: Line 287:
</pre>
</pre>
|}
|}
'''''[[#top | Top]]'''''
 


=== My CMD ===
=== My CMD ===
Line 312: Line 293:
|-
|-
|
|
Code on the right is added to the Handler Function
Code on the right is added to the Handler Function.
 
It is self-explanatory.
It is self-explanatory.


Line 327: Line 307:


== Test ==
== Test ==
To see this code in action run '''test_5.bat''' (code contained in test_5.phpw)
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.   
Run Uniform Server, start and stop each server and observe the button and indicator states.
Start and stop both servers, again observing the button and indicator states.   


After testing close both Servers and application script.
After testing, close both Servers and the application script.


'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''
Line 340: Line 319:
At this stage you have fully functional servers.
At this stage you have fully functional servers.


Next step is to add functionality to remaining buttons covered on the [[PHP WinBinder: Alternative control 3 | '''next page''']].
The next step is to add functionality to the remaining buttons.  This is covered on the [[PHP WinBinder: Alternative control 3 | '''next page''']].


'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''

Latest revision as of 14:07, 30 January 2010

 

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 is that there is no way to determine when 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 it 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 this 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 ===

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 and their names were 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. What's important is to add these with an include staetment.

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 functions added, we can continue adding more capability.

Top

Push Buttons

Buttons provide a user interface that can easily be abused. By this I mean that 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. Attempting to start or stop servers in quick succession can produce undesirable effects. Similarly if a server is not running, the associated buttons, if clicked, will again produce undesirable results.

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

Server All Start/Stop

The 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 is started and the server is either started or stopped. The parameter passed is binary coded: 1=Apache, 2=MySQL 3=Both

The timer function checks a server’s status. On detecting a state change, it calls the init functions. This sets the 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;

Apache Start/Stop

The 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 is started and the server is either started or stopped. The parameter passed is binary coded: 1=Apache, 2=MySQL 3=Both

The timer function checks a server’s status. On detecting a state change, it calls the init functions. This sets the 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;

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 is started and the server is either started or stopped. The parameter passed is binary coded: 1=Apache, 2=MySQL 3=Both

The timer function checks a server’s status. On detecting a state change, it calls the init functions. This sets the 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;

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
} 

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
}


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 and observe the button and indicator states. Start and stop both servers, again observing the button and indicator states.

After testing, close both Servers and the application script.

Top

Summary

At this stage you have fully functional servers.

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

Top