MongoDB Tutorial 1: Basic Window App

From The Uniform Server Wiki
Jump to navigation Jump to search

 

MongoDB Standalone
UniServer 6-Carbo.

MongoDB

Introduction

Our main application is a Windows interface for running MongoDB. Interestingly the real work for this application has already been covered. We have a set of functions that perform all the functionality we need.

A windows application is nothing more than a pretty user interface. One redeeming feature is providing user feedback. If it was not for this I would suggest stick with batch files and scripts.

Is creating a Windows application difficult and complex? Yes if you want to do it the hard way by compiling and using winapi directly. Alternatively WinBinder makes it a snip; all you need to know is a small amount of PHP.

Never reinvent the wheel use one of the many examples as a template. Check out these links for additional information:

This tutorial is provided with a working template see below.

Top

Window template

Template shown below

<?php

chdir(dirname(__FILE__)); // Change wd to this files location

include "winbinder/winbinder.php";             // Location of WinBinder library

//=== Application name
define("APPNAME",   "MongoDB Control" );

//=== Button Text
define("START_DB_BTXT",       "Start MongoDB"); 
define("STOP_DB_BTXT",        "Stop MongoDB"); 
define("START_CLIENT_BTXT",   "Start Mongo Client"); 
define("OPEN_CMD_BTEXT",      "Open cmd Window"); 
define("HELP_INFO_BTXT",      "Help and Information"); 

//-- Constants ----------------------------------------------------------------

define('ID_DB_BUTTON',         1010);
define('ID_CLIENT_BUTTON',     1020);
define('ID_OPEN_CMD_BUTTON',   1030);
define('ID_HELP_BUTTON',       1040);

// Common variables

$win_mongo;             // Window
$statusbar_mongo;       // Status bar

//=== 1) Create main window ---------------------------------------------------
// create main window

define("CAPTION", APPNAME);             // Set caption to match application
if(wb_get_instance(CAPTION, TRUE))      // Is there an existing instance?
    die;                                // Yes: bring it to the front and quit

$win_mongo = wb_create_window(NULL, AppWindow, APPNAME, WBC_CENTER, WBC_CENTER, 207,190, 0);

//=== 2) Create controls for the main window ----------------------------------
wb_create_control($win_mongo, Frame, "Mongo",             5, 5, 190, 147, 0, 0, 0, 0);

wb_create_control($win_mongo, PushButton,      START_DB_BTXT, 15,  25, 170, 25, ID_DB_BUTTON, 0, 0, 0);
wb_create_control($win_mongo, PushButton,  START_CLIENT_BTXT, 15,  55, 170, 25, ID_CLIENT_BUTTON, 0, 0, 0);
wb_create_control($win_mongo, PushButton,     OPEN_CMD_BTEXT, 15,  85, 170, 25, ID_OPEN_CMD_BUTTON, 0, 0, 0);
wb_create_control($win_mongo, PushButton,     HELP_INFO_BTXT, 15, 115, 170, 25, ID_HELP_BUTTON, 0, 0, 0);

//=== 3) Assign handler function to the main window  --------------------------
  wb_set_handler($win_mongo, "process_mongo");
  wb_set_image($win_mongo, getcwd()."/images/utray.ico");  // Add logo   

//=== 5) Enter application loop -----------------------------------------------
wb_main_loop();   

//=== 4) Handler Function -----------------------------------------------------
function process_mongo($window, $id){
  global $win_mongo;
  global $statusbar_mongo;

    switch($id) {

        case 1010:
        case 1020:
            wb_message_box($window, "Button #$id was pressed.");
            break;

        case IDCLOSE:                         // The constant IDCLOSE is predefined
            wb_destroy_window($window);       // Destroy the window
            break;
     }
}

//------------------------------------------------------------------ END OF FILE
?>

Top

Overview

Collectively it looks complex however taking small chunks it really is simple:

  1. First line change-working directory.
  2. Define application name constant
  3. Define several constants to display button text
  4. Each button must have a unique number hence more constants
  5. Define some common variables
  • Section 1
  1. Check for another running instance we only want one active
  2. Create the window
  • Section 2
  1. Add a frame
  2. Add buttons to the window
  • Section 3
  1. Set an handler function to process requests to window.
  2. Add a logo appears top left of window.
  • Section 5
  1. Call main loop function keeps application alive
  • Section 4
  1. This is the handler function it processes all requests to the window.
  2. All button clicks are directed to this function.
  3. Buttons are identified by a unique number and intercepted using a switch statement.

Template produces the following:

Top

Initialisation

When our application is run we have an opportunity to nag a user. We know the server must be shutdown before turning PC off; hence inform a new user of this fact. Allow users to disable nagging, this can be implemented by creating a file.

Add the following line to mongo_db_inc.php

define("NO_NAG_TXT",         "$mongo_base_f/mongodb_1/control/no_nag.txt");  // Dummy file

A user may have intentionally closed the application with server running. On resting the application check for server running and toggle button text accordingly.

Add the following function to end of mongo_tutorial.phpw

//=== Initialise Main =========================================================
function mongo_init(){
  global $win_mongo;

  //=== Nag User
  //== Checkuser disabled nagging
  if(!file_exists(NO_NAG_TXT)){  // File not found 

   $str  = "Before turning your PC off \n";
   $str .= "Ensure you have closed MongoDB \n\n";

   $str .= "Failure to do will result in data loss\n";
   $str .= "Data from RAM needs to be written to disk.\n\n";

   $str .= "Display this alert next time you start MongoDB?";

   if(!wb_message_box($win_mongo, $str, "IMPORTANT", WBC_YESNO)){
     // User wants to disable nagging hence create file 
     //== Create or update a new redirect page 
       $no_nag[] = "To enable nagging\n";
       $no_nag[] = "Delete this file\n";

       $str_no_nag = implode($no_nag);              // Convert array to string
       file_put_contents(NO_NAG_TXT,$str_no_nag); // Save string to file
   } 
  }
  //=== Check mongo runnung
  if(mongodb_running()){  // MongoDB running
    wb_set_text(wb_get_control($win_mongo,    ID_DB_BUTTON),STOP_DB_BTXT);  // set button text
  }
  else{                   // MongoDB not running           
    wb_set_text(wb_get_control($win_mongo,    ID_DB_BUTTON),START_DB_BTXT);   // set button text
    wb_set_enabled(wb_get_control($win_mongo, ID_CLIENT_BUTTON), FALSE);      // Disable Client button 
  }

}//=================================================== END Initialise Main ===

We need to call this function add the following line to end of section 3

  mongo_init();

Currently our script has no access to the functions at top of page add the following line

include_once "mongo_db_inc.php";

Top

Test

  • Run mongo_tutorial.bat (See image on right)
  1. Note top button text start
  2. Client button is greyed out
  • Close window
  • Start MongoDB (alt_control\Run_start_mongo.bat)
  1. Note top button text changes to stop
  2. Client button is enabled
  • Stop server (alt_control\Run_stop_mongo.bat)
  • Close window

Top

Summary

With a working window we need to add functionality to each button.

Net page covers lower two buttons.

Top