Index: trunk/pstamp/web/request.php
===================================================================
--- trunk/pstamp/web/request.php	(revision 16283)
+++ trunk/pstamp/web/request.php	(revision 16358)
@@ -1,23 +1,23 @@
-<html>
- <head>
-  <title>Postage Stamp Request Form</title>
- </head>
-<form method="post">
-
-<body>
-
-<H1 align=center>
-Postage Stamp Request Form
-</h1>
-
-<!--- for dumping output from the following code for debug TODO: remove this--->
-<pre>
 <?php 
 
+// A php program that generates a postage stamp request form
+
+// There are two modes on the form List Images simply lists the input images that match
+// the image selection critera
+
+// When make stamps is selected when the page is posted, a postage stamp request is queued.
+// 
+// Then we respond to http posts by listing the status of the jobs.
+// Once all of the jobs complete we set the page back to submission mode again
+// The mode for the page is given by $request_id != 0
+
 $COMMAND_DIR = "/export/data0/bills/src/ipp/pstamp/scripts";
-$STAMPS_DIR = "stamps";
+$STAMPS_DIR = "/export/data1/bills/pstamp/work/stamps";
 
 // Initialize variables
 $output_array = array();
+
+$request_id = 0;
+$last_request_id = 0;
 
 $raw_selected = "";
@@ -61,4 +61,6 @@
 
 $rvar_cmd_mode = "";
+$rvar_request_id = 0;
+$rvar_last_request_id = 0;
 
 // now get the values from this post
@@ -110,5 +112,12 @@
 }
 
-if ($rvar_cmd_mode == "Make Stamps") {
+// When request_id is non-zero we respond to posts by check the status of that request
+// request_id gets set to zero when the status of all jobs for the request is 'stop'
+$request_id = $rvar_request_id;
+$last_request_id = $rvar_last_request_id;
+
+
+// default to list mode, unless we are waiting for a request to finish
+if (($rvar_cmd_mode == "Make Stamps") || ($request_id != 0)) {
     $pstamp_checked = "checked";
     $list_checked = "";
@@ -118,26 +127,267 @@
 }
 
-echo "\n\n\n";
-
-// If the data is from a Post run the command
-// How do we know? Well if it's the initial get of the web page, rvar_img_type is not set
-// TODO: find a better way to decide whether to proceed
+
+// echo "rvar_request_id: $rvar_request_id\n";
+
+// HERE is the logic for running the various commands
+
+// How do we know? Well if we aren't running the initial get of the web page?
+// Well, in that case rvar_img_type is not set so key off of that
+// TODO: find a better way to decide whether or not to run commands
+
 if ($rvar_img_type) {
 
-    try {
-        $command_line = build_command_line();
+    if ($request_id == 0) {
+        try {
+            $command_line = build_command_line();
+            $error_line = "";
+            run_command();
+            if (! $list_checked) {
+                // The only output from a successful run is the request_id
+                $request_id = Array_pop($output_array);
+                $last_request_id = $request_id;
+                #echo "The request id is $request_id\n";
+                if (count($output_array) != 0) {
+                    throw new Exception("unexpected output returned by pstampwebrequest.");
+                }
+
+                $jobRunning = getRequestStatus();
+                if (!$jobRunning) {
+                    $request_id = 0;
+                }
+
+            }
+        } catch (Exception $e) {
+            $error_line = $e->getMessage();
+        }
+    } else {
+        try {
+            // get the list of jobs for the request
+            // echo "calling getRequestStatus\n";
+            $jobRunning = getRequestStatus();
+
+            if (!$jobRunning) {
+                $request_id = 0;
+            }
+
+        } catch (Exception $e) {
+            $error_line = $e->getMessage();
+        }
+    }
+}
+
+function build_command_line()
+{
+    global $rvar_project, $rvar_user_tag;
+    global $sky_checked, $rsky_checked;
+    global $list_checked;
+    global $rvar_RA, $rvar_DEC;
+    global $rvar_dRA, $rvar_dDEC;
+    global $rvar_X, $rvar_Y;
+    global $rvar_W, $rvar_H;
+    global $exp_checked, $file_checked, $coord_checked;
+    global $rvar_img_type;
+    global $rvar_id, $rvar_class_id;
+    global $command_line;
+    global $STAMPS_DIR;
+
+    $cmd = "pstamp_runcommand.sh pstamp_webrequest.pl";
+    
+    if ($list_checked) {
+        $cmd .= " -list";
+    }
+
+    if (! $rvar_project ) {
+        throw new Exception('project must be specified.');
+    }
+    $cmd .= " -project $rvar_project";
+    if (! $rvar_user_tag ) {
+        throw new Exception('Stamp Name must be specified.');
+    }
+    $cmd .= " -user_tag $STAMPS_DIR/$rvar_user_tag";
+
+    // Set up the ROI parameters
+    if ($sky_checked) {
+        if (! $rvar_RA || ! $rvar_DEC) {
+            throw new Exception('RA and DEC must be specified.');
+        }
+        $cmd .= " -skycenter $rvar_RA $rvar_DEC";
+    } else {
+        if (! $rvar_X || ! $rvar_Y) {
+            throw new Exception('X and Y must be specified.');
+        }
+        $cmd .= " -pixcenter $rvar_X $rvar_Y";
+    }
+
+    if ($rsky_checked) {
+        if (! $rvar_dRA || ! $rvar_dDEC) {
+            throw new Exception('dRA and dDEC must be specified.');
+        }
+        $cmd .= " -arcrange $rvar_dRA $rvar_dDEC";
+    } else {
+        if (! $rvar_W || ! $rvar_H) {
+            throw new Exception('width and height must be specified.');
+        }
+        $cmd .= " -pixrange $rvar_W $rvar_H";
+    }
+
+
+    // Note: in the following block we assume that image type has been
+    // which is true since it comes from the menu
+    if ($exp_checked) {
+        if (! $rvar_id ) {
+            throw new Exception('Must set Exposure ID.');
+        }
+        $cmd .= " -byexp $rvar_img_type $rvar_id";
+    } else if ($file_checked) {
+        if (! $rvar_id ) {
+            throw new Exception('Must set ID specified.');
+        }
+        $cmd .= " -byid $rvar_img_type $rvar_id";
+    } else if ($coord_checked) {
+        // $cmd .= " -bycoord $rvar_img_type";
+        $coord_checked = "";
+        $exp_checked = "checked";
+        throw new Exception("Image selection by coordinate not implemented yet.");
+    }
+
+    if (($rvar_img_type == "raw") || ($rvar_img_type == "chip")) {
+        if (! $rvar_class_id ) {
+            throw new Exception("must specify Class ID with Image Type $rvar_img_type.");
+        }
+        $cmd .= " $rvar_class_id";
+    }
+
+    return escapeshellcmd($cmd);
+}
+
+function run_command()
+{
+    global $command_line;
+    global $output_array;
+    global $error_line;
+    global $command_status;
+    global $COMMAND_DIR;
+
+
+    exec ("$COMMAND_DIR/$command_line", $output_array, $command_status);
+
+    $size = sizeof($output_array);
+    // echo "output_array  contains $size lines\n";
+    if ($command_status == 0) {
+        // On success we just remember the results
+        $dump_results = 0;
+        if ($dump_results) {
+            echo "Output: $size lines\n";
+            for ($i = 0; $i < $size; $i++) {
+                echo "$output_array[$i]\n";
+            }
+        }
+    } else {
+        // copy the output to the error_line
         $error_line = "";
-        run_command();
-    } catch (Exception $e) {
-        $error_line = $e->getMessage();
+        for ($i = 0; $i < $size; $i++) {
+             $error_line .= "$output_array[$i]\n";
+        }
+    }
+
+}
+
+function printURL($line)
+{
+    global $request_id;
+    global $last_request_id;
+
+    echo "<tr><td>";
+    $doURL = 1;
+    if ($doURL) {
+        // echo "output_line: $line\n";
+        $elements = explode(" ", $line);
+        if (count($elements) == 3) {
+            $job_id   = $elements[0];
+            $state    = $elements[1];
+            $path     = $elements[2];
+            $fileName = basename($path);
+            if ($state == "stop") {
+                // this job is done, list the url as a link
+                echo "<a href=\"http:$path\" target=\"_blank\" type=\"image/fits\">";
+                echo $fileName;
+                echo "</a>";
+                echo "&nbsp;&nbsp;&nbsp; request_id: $last_request_id &nbsp;&nbsp;";
+                echo "job_id: $job_id &nbsp;&nbsp;&nbsp; state: $state";
+            } else {
+                // TODO: refine this output
+                echo "$fileName&nbsp;&nbsp;&nbsp; request_id: $request_id &nbsp;&nbsp;&nbsp;";
+                echo "job_id: $job_id &nbsp;&nbsp;&nbsp; state: $state";
+            }
+        }
+    } else {
+        print "$line";
+    }
+
+    echo "</td></tr>";
+}
+
+function countRunningJobs()
+{
+    global $output_array;
+
+    $runningJobs = 0;
+    $size = sizeof($output_array);
+    for ($i = 0; $i < $size; $i++) {
+        $elements = explode(" ", $output_array[$i]);
+        if (count($elements) == 3) {
+            $state    = $elements[1];
+            if ($state != "stop") {
+                $runningJobs++;
+            }
+        } else {
+            throw new Exception ("incorrect data in job status: $output_array[$i]");
+        }
+    }
+    return $runningJobs;
+}
+
+function getRequestStatus()
+{
+    global $request_id;
+    global $command_line;
+    global $command_status;
+
+    $command_line = "pstamp_runcommand.sh pstamp_listjobs.pl $request_id";
+    run_command();
+
+    if ($command_status == 0) {
+        return countRunningJobs();
+    } else {
+        return 0;
     }
 }
 
 ?>
-</pre>
-<!-- end of preformatted area for debug printing TODO: remove this -->
-
+
+<!-- Beginning of the HTML --------------------------------------------- -->
+<html>
+ <head>
+  <title>Postage Stamp Request Form</title>
+    <?php
+        if ($request_id != 0) {
+            // This doesn't do what I want. It does a get not a post
+
+            // echo '<META HTTP-EQUIV="refresh" CONTENT="5">';
+
+        }
+    ?>
+ </head>
+<form method="post">
+
+<body>
+
+<H1 align=center>
+Postage Stamp Request Form
+</h1>
 
 <!-- Whole page is a single column table -->
+
 
 <table width=90% align=center>
@@ -327,4 +577,7 @@
   <table width=40%>
   <tr>
+
+<?php
+  if ($request_id == 0): ?>
     <td><input type=submit value="Submit"></td>
     <td><b>Mode:</b>&nbsp;&nbsp;
@@ -332,4 +585,11 @@
     <input type=radio name="cmd_mode" value="List Images" <?php echo $list_checked; ?> >List Images
     </td>
+<?php 
+  else: ?>
+    <td><input type=submit value="Get Status"></td>
+<?php
+  echo "<td><b>Request Id: $request_id";
+  endif; ?>
+
   </tr>
   </table>
@@ -361,6 +621,7 @@
     </td>
 </tr>
-<!-- a blank row for space-->
-<tr height=20><td></td></tr>
+
+<!-- a blank row for space -->
+<tr height=20><td> </td></tr>
 
 <tr>
@@ -374,8 +635,11 @@
     if ($command_status == 0) {
         if ($list_checked) {
+            // in list mode the output is a list of image files, just list them
+            // later we might add links to cause a stamp to be made from a selected file
             for ($i = 0; $i < $size; $i++)  {
                 echo "<tr><td>$output_array[$i]</td></tr>";
             }
         } else {
+            // output the list of urls
             for ($i = 0; $i < $size; $i++)  {
                 printURL($output_array[$i]);
@@ -387,4 +651,22 @@
 </td>
 </tr>
+<!-- a blank row for our hidden element here-->
+
+<!-- request_id being non-zero causes us to issue status requests instead of new requsts -->
+<!--
+
+need a way to cancel a request There's probably no reason to have the value hidden, but we do
+need to set it as the last thing that we do
+
+-->
+
+<tr height=20>
+<td>
+    <input type="hidden" name="request_id" value=<?php echo $request_id ?> >
+</td>
+<td>
+    <input type="hidden" name="last_request_id" value=<?php echo $last_request_id ?> >
+</td>
+</tr>
 </table>
 
@@ -398,139 +680,7 @@
 
 <?php 
-// dump parameters 
-
-   // phpinfo(32);
-
-?>
-
-<?php
-
-function build_command_line()
-{
-    global $rvar_project, $rvar_user_tag;
-    global $sky_checked, $rsky_checked;
-    global $list_checked;
-    global $rvar_RA, $rvar_DEC;
-    global $rvar_dRA, $rvar_dDEC;
-    global $rvar_X, $rvar_Y;
-    global $rvar_W, $rvar_H;
-    global $exp_checked, $file_checked, $coord_checked;
-    global $rvar_img_type;
-    global $rvar_id, $rvar_class_id;
-    global $command_line;
-    global $STAMPS_DIR;
-
-    $cmd = "pstampwebrequest.sh";
-    
-    if ($list_checked) {
-        $cmd .= " -list";
-    }
-
-    if (! $rvar_project ) {
-        throw new Exception('project must be specified.');
-    }
-    $cmd .= " -project $rvar_project";
-    if (! $rvar_user_tag ) {
-        throw new Exception('Stamp Name must be specified.');
-    }
-    $cmd .= " -user_tag $STAMPS_DIR/$rvar_user_tag";
-
-    // Set up the ROI parameters
-    if ($sky_checked) {
-        if (! $rvar_RA || ! $rvar_DEC) {
-            throw new Exception('RA and DEC must be specified.');
-        }
-        $cmd .= " -skycenter $rvar_RA $rvar_DEC";
-    } else {
-        if (! $rvar_X || ! $rvar_Y) {
-            throw new Exception('X and Y must be specified.');
-        }
-        $cmd .= " -pixcenter $rvar_X $rvar_Y";
-    }
-
-    if ($rsky_checked) {
-        if (! $rvar_dRA || ! $rvar_dDEC) {
-            throw new Exception('dRA and dDEC must be specified.');
-        }
-        $cmd .= " -arcrange $rvar_dRA $rvar_dDEC";
-    } else {
-        if (! $rvar_W || ! $rvar_H) {
-            throw new Exception('width and height must be specified.');
-        }
-        $cmd .= " -pixrange $rvar_W $rvar_H";
-    }
-
-
-    // Note: in the following block we assume that image type has been
-    // which is true since it comes from the menu
-    if ($exp_checked) {
-        if (! $rvar_id ) {
-            throw new Exception('Must set Exposure ID.');
-        }
-        $cmd .= " -byexp $rvar_img_type $rvar_id";
-    } else if ($file_checked) {
-        if (! $rvar_id ) {
-            throw new Exception('Must set ID specified.');
-        }
-        $cmd .= " -byid $rvar_img_type $rvar_id";
-    } else if ($coord_checked) {
-        // $cmd .= " -bycoord $rvar_img_type";
-        $coord_checked = "";
-        $exp_checked = "checked";
-        throw new Exception("Image selection by coordinate not implemented yet.");
-    }
-
-    if (($rvar_img_type == "raw") || ($rvar_img_type == "chip")) {
-        if (! $rvar_class_id ) {
-            throw new Exception("must specify Class ID with Image Type $rvar_img_type.");
-        }
-        $cmd .= " $rvar_class_id";
-    }
-
-    return escapeshellcmd($cmd);
-}
-
-function run_command()
-{
-    global $command_line;
-    global $output_array;
-    global $error_line;
-    global $command_status;
-    global $COMMAND_DIR;
-
-
-    exec ("$COMMAND_DIR/$command_line", $output_array, $command_status);
-
-    $size = sizeof($output_array);
-    // echo "output_array  contains $size lines\n";
-    if ($command_status == 0) {
-        // echo "Output: $size lines\n";
-        for ($i = 0; $i < $size; $i++) {
-          //  echo "$output_array[$i]\n";
-        }
-    } else {
-        // copy the output to the error_line
-        $error_line = "";
-        for ($i = 0; $i < $size; $i++) {
-             $error_line .= "$output_array[$i]\n";
-        }
-    }
-
-}
-
-function printURL($path)
-{
-    echo "<tr><td>";
-    $doURL = 1;
-    if ($doURL) {
-        $fileName = basename($path);
-        echo "<a href=\"http:$path\" target=\"_blank\" type=\"image/fits\">";
-        echo $fileName;
-        echo "</a>";
-    } else {
-        print "$path";
-    }
-    echo "</td></tr>";
-}
+    // dump parameters 
+
+    // phpinfo(32);
 
 ?>
@@ -538,3 +688,4 @@
 </body>
 </form>
+
 </html>
