12.6. Common: The Process Status Display - show_names, toggle_raw, .reassign_prog_name, extra_space

These four functions are responsible for displaying the current process status information in the bottom text box of the Controller. The first function, show_names, does the main work.

   /* Initialize a global variable for the "Raw output" button. */
   RAW = 0;
   
   /*--------------------------------------------------------------------
    * Function:    show_names
    * Returns:     t or nil
    * Description: Displays nsnames output in the Controller.  Uses the
    *              Gamma function nserve_query() to get an array of
    *              TaskInfo instances that contain the needed information.
    *------------------------------------------------------------------*/
   function show_names(widget)
   {
     local namestring;
   
     if (RAW == 0)
       namestring = "Program name        Domain      PID\n";
     else
       namestring = "Process name        Domain      PID\n";
   

The idea here is to build one long string with all the necessary information in it, and display it in the text widget. The beginning of the string is the title row. The global variable, RAW, is changed by the Raw names button calling the toggle_raw function (discussed below).

Next we use Gamma's nserve_query function to extract the information we need from the Cascade NameServer, and using a Gamma with statement, walk through the resulting array making changes and appending the information we need into one long string (namestring). For this we call on the .reassign_prog_name and extra_space functions explained below.

     with q in nserve_query() do
       {
         if (RAW == 0)
           q.reassign_prog_name();
         q.name = string(q.name, extra_space(20 - strlen(q.name)));
         if (q.domain == "toolsdemo")
           namestring = string(namestring, q.name, q.domain, "   ",
                               string(q.pid), "\n");
       }
     if (widget != nil)
       anygui_show_text(widget, namestring, 2);
   }
   

The reassign_prog_name method for the Gamma TaskInfo class lets us give a more user-friendly name to each of the process names in the Demo, including both the Demo programs and the Cogent Software Tools. These are the default names that appear in the Controller text window; the Raw names button toggles between them and the actual process names.

   /*--------------------------------------------------------------------
    * Function:    TaskInfo.reassign_prog_name
    * Returns:     A string
    * Description: Reassigns a few specific .name ivars to the TaskInfo
    *              class, the return value of the task_info() Gamma function.
    *------------------------------------------------------------------*/
   method TaskInfo.reassign_prog_name()
   {
     if (strstr(.name, "dhview") != -1)
         .name = substr(.name, 0, 6);
   
     switch (.name)
       {
       case "/dh/toolsdemo": .name = "Cascade DataHub";
       case "control": .name = "Controller";
       case "emul": .name = "PID Emulator";
       case "monitor": .name = "Monitor";
       case "dhview": .name = "DataHub Viewer";
       case "log": .name = "Log";
       case "tlog": .name = "Cascade TextLogger";
       case "history": .name = "History";
       case "demohistdb": .name = "Cascade Historian";
       }
   }
   

The extra_space function creates a string of spaces, whose length is determined by the number passed in as a parameter. It is used by the show_names function.

   /*--------------------------------------------------------------------
    * Function:    extra_space
    * Returns:     A string
    * Description: Creates a string of empty spaces for a specified length.
    *------------------------------------------------------------------*/
   function extra_space(num)
   {
     local str = "";
     for(i=0; i<num; i++)
       str = string(str, " ");
     str;
   }
   

The toggle_raw function is attached to a callback on the Raw names toggle button. It changes the global variable RAW depending on the state of the button, which is derived using the button's abstracted .switched_on method, then refreshes the display.

   /*--------------------------------------------------------------------
    * Function:    toggle_raw
    * Returns:     t or nil
    * Description: Toggles the nsnames output display.
    *------------------------------------------------------------------*/
   function toggle_raw(button, wgt)
   {
     if (button.switched_on())
         RAW = 1;
     else
         RAW = 0;
     show_names(wgt);
   }