MongoDB Tutorial 1: Buttons 2

From The Uniform Server Wiki
Jump to navigation Jump to search

 

MongoDB Standalone
UniServer 6-Carbo.

Introduction

This page covers adding functionality to buttons Start MongoDB and Start Mongo Client

Top button

A button’s text is dynamic allowing it to be written and read. This capability allows a button to have more that one state. Current state is determined by reading its text.

Top button of our application has two states Start MongoDB and Stop MongoDB. following shows how to create a toggle button:

Toggle action:

  • Read button text
  • Use an if statement to compare text to START_DB_BTXT
  • If match (start) change button text to stop
  • Else change button text to start
$text =  wb_get_text(wb_get_control($window, ID_DB_BUTTON));

if($text == START_DB_BTXT ){ // Start server
  wb_set_text(wb_get_control($window, ID_DB_BUTTON),STOP_DB_BTXT); 
else{                         // Stop   server
 wb_set_text(wb_get_control($window,    ID_DB_BUTTON),START_DB_BTXT); 
}

Top

Start

If the button is displaying Start MongoDB when clicked we want to perform the following actions:

  • Start MongoDB server
  • Set button text to stop
  • Enable Mongo-client button

Gives the following code:

     if($text == START_DB_BTXT ){ // Start server

       //== Start MongoDB
       start_mongo_no_auth();

       //== Set buttons
       wb_set_text(wb_get_control($window,    ID_DB_BUTTON),STOP_DB_BTXT);  // set button text
       wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), TRUE);     // Enable Client button 
     }

Top

Stop

If the button is displaying Stop MongoDB when clicked we want to perform the following actions:

  • If Mongo-client running
  1. Prompt user to close it.
  • Mongo-client not running
  1. Stop MongoDB server
  2. Set button text to start
  3. Disable Mongo-client button

Gives the following code:

     else{                         // Stop   server
       if(mongo_client_running()){ // Client must be stopped before Mongo Server
         $str  = "Before you can stop MongoDB \n";
         $str .= "Please close Mongo Client \n\n";

         $str .= "Type EXIT to close client\n";
         $str .= "Data from RAM needs to be written to disk.\n";

         wb_message_box($window, $str, "CLIENT RUNNING", WBC_INFO);          
         break;
       }
       else{                   // Client not running

         //== Stop MongoDB
         stop_mongo_no_auth();

         //==Set button text
         wb_set_text(wb_get_control($window,    ID_DB_BUTTON),START_DB_BTXT);  // set button text
         wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), FALSE);  // DISABLE Client button 
      }
     }

Top

Complete button code

   //=== Start-Stop Servers ===================================================
   case ID_DB_BUTTON:          // Button toggles
     $text =  wb_get_text(wb_get_control($window, ID_DB_BUTTON));  // get button text

     if($text == START_DB_BTXT ){ // Start server

       //== Start MongoDB
       start_mongo_no_auth();

       //== Set buttons
       wb_set_text(wb_get_control($window,    ID_DB_BUTTON),STOP_DB_BTXT);  // set button text
       wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), TRUE);     // Enable Client button 
     }

     else{                         // Stop   server
       if(mongo_client_running()){ // Client must be stopped before Mongo Server
         $str  = "Before you can stop MongoDB \n";
         $str .= "Please close Mongo Client \n\n";

         $str .= "Type EXIT to close client\n";
         $str .= "Data from RAM needs to be written to disk.\n";

         wb_message_box($win_mongo, $str, "CLIENT RUNNING", WBC_INFO);          
         break;
       }
       else{                   // Client not running

         //== Stop MongoDB
         stop_mongo_no_auth();

         //==Set button text
         wb_set_text(wb_get_control($window,    ID_DB_BUTTON),START_DB_BTXT);  // set button text
         wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), FALSE);     // DISABLE Client button 
      }
     }

   break; 
   //================================================ END Start-Stop Servers ==

Top


Start Client

Starting the client requires running its corresponding function. If already running inform user and exit case statement. Code snippets as shown below:

   //=== Start Client =========================================================
   case ID_CLIENT_BUTTON:          // Button 
    if(mongo_client_running()){    // Already running  
      wb_message_box($window, " No action taken client already running", "CLIENT RUNNING", WBC_INFO); 
      break; 
    }
    else{                         // Not running
       client_no_auth();          // Start a cmd window and run mohgo command line interface
    }
   break; 
   //===================================================== END Start Client ===


Top

Complete handler function

Adding all the snippets of code gives the following

//=== 4) Handler Function =====================================================
function process_mongo($window, $id){

   switch($id) {

   //=== Start-Stop Servers ===================================================
   case ID_DB_BUTTON:          // Button toggles
     $text =  wb_get_text(wb_get_control($window, ID_DB_BUTTON));  // get button text

     if($text == START_DB_BTXT ){ // Start server

       //== Start MongoDB
       start_mongo_no_auth();

       //== Set buttons
       wb_set_text(wb_get_control($window,    ID_DB_BUTTON),STOP_DB_BTXT);  // set button text
       wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), TRUE);  // Enable Client button 
     }

     else{                         // Stop   server
       if(mongo_client_running()){ // Client must be stopped before Mongo Server
         $str  = "Before you can stop MongoDB \n";
         $str .= "Please close Mongo Client \n\n";

         $str .= "Type EXIT to close client\n";
         $str .= "Data from RAM needs to be written to disk.\n";

         wb_message_box($window, $str, "CLIENT RUNNING", WBC_INFO);          
         break;
       }
       else{                   // Client not running

         //== Stop MongoDB
         stop_mongo_no_auth();

         //==Set button text
         wb_set_text(wb_get_control($window,    ID_DB_BUTTON),START_DB_BTXT);  // set button text
         wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), FALSE);  // DISABLE Client button 
      }
     }

   break; 
   //================================================ END Start-Stop Servers ==

   //=== Start Client =========================================================
   case ID_CLIENT_BUTTON:          // Button 
    if(mongo_client_running()){    // Already running  
      wb_message_box($window, " No action taken client already running", "CLIENT RUNNING", WBC_INFO); 
      break; 
    }
    else{                         // Not running
       client_no_auth();          // Start a cmd window and run mohgo command line interface
    }
   break; 
   //===================================================== END Start Client ===

   //=== Open command window ==================================================
   // Open a command window in folder bin
   case ID_OPEN_CMD_BUTTON:          // Button 
    open_cmd_window();
   break;
   //============================================== END Open command window ===

   //=== Display help info ====================================================
   case ID_HELP_BUTTON:          // Button 

    wb_exec('Notepad',INFO_TXT);

   break; 
   //================================================ END Display help info ===

   case IDCLOSE:                             // Constant IDCLOSE (8) predefined
      wb_destroy_window($window);            // Destroy the window
      break;
   }
}
//================================================ END 4) Handler Function ===

Test

Run mongo_tutorial.bat

  • Check server functions as expected
  • Check client has access to the server
  • Perform other tests as required
  • Finally test Stop server works

Top

Finishing touches

Create a folder mongodb_1\top_level

It contains a single file Run_MongoDB.bat with the following content:

cd php
php.exe -c mongo_tutorial_cli.ini ..\control\mongo_tutorial.phpw
EXIT

To run server copy this file to folder mongodb_1. Alternatively convert the file to an exe using bat2exe

Finally delete folder z_temp no longer required.

Top

Summary

Top