[Bio] / FigKernelPackages / ServerThing.pm Repository:
ViewVC logotype

Diff of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.9, Thu Oct 29 18:26:59 2009 UTC revision 1.10, Tue Nov 3 21:20:07 2009 UTC
# Line 9  Line 9 
9      use TestUtils;      use TestUtils;
10      use Time::HiRes;      use Time::HiRes;
11      use File::Temp;      use File::Temp;
12      use ErrorDocument;      use ErrorMessage;
13      use CGI;      use CGI;
14      no warnings qw(once);      no warnings qw(once);
15    
# Line 163  Line 163 
163          # Delete the error log.          # Delete the error log.
164          unlink $errorLog;          unlink $errorLog;
165          # Confess the error.          # Confess the error.
166          Confess("500 $name command failed with error code $res.");          Confess("$name command failed with error code $res.");
167      } else {      } else {
168          # Everything worked. Trace the error log if necessary.          # Everything worked. Trace the error log if necessary.
169          if (T(3) && -s $errorLog) {          if (T(3) && -s $errorLog) {
# Line 244  Line 244 
244          };          };
245          # Check to make sure we got everything.          # Check to make sure we got everything.
246          if ($@) {          if ($@) {
247              $document = ErrorDocument->new('<initialization>', $@);              SendError($@, "Error formatting parameters.");
248          } elsif (! $function) {          } elsif (! $function) {
249              $document = ErrorDocument->new('<missing>', "No function specified.");              SendError("No function specified.", "No function specified.");
250          } else {          } else {
251              # We're okay, so load the server function object.              # We're okay, so load the server function object.
252              Trace("Requiring $serverName") if T(3);              Trace("Requiring $serverName") if T(3);
# Line 255  Line 255 
255              };              };
256              # If we have an error, create an error document.              # If we have an error, create an error document.
257              if ($@) {              if ($@) {
258                  $document = ErrorDocument->new($function, $@);                  SendError($function, $@, "Could not load server module.");
                 Trace("Error loading server module: $@") if T(2);  
259              } else {              } else {
260                  # Having successfully loaded the server code, we create the object.                  # Having successfully loaded the server code, we create the object.
261                  my $serverThing = eval("$serverName" . '->new()');                  my $serverThing = eval("$serverName" . '->new()');
262                  # If we have an error, create an error document.                  # If we have an error, create an error document.
263                  if ($@) {                  if ($@) {
264                      $document = ErrorDocument->new($function, $@);                      SendError($@, "Could not start server.");
                     Trace("Error creating server function object: $@") if T(2);  
265                  } else {                  } else {
266                      # No error, so execute the server method.                      # No error, so execute the server method.
267                      Trace("Executing $function.") if T(2);                      Trace("Executing $function.") if T(2);
268                      $document = eval("\$serverThing->$function(\$args)");                      $document = eval("\$serverThing->$function(\$args)");
269                      # If we have an error, create an error document.                      # If we have an error, create an error document.
270                      if ($@) {                      if ($@) {
271                          $document = ErrorDocument->new($function, $@);                          SendError($@, "Error detected by service.");
272                          Trace("Error encountered by service: $@") if T(2);                          Trace("Error encountered by service: $@") if T(2);
273                        } else {
274                            # No error, so we output the result.
275                            print $cgi->header(-type => 'text/plain');
276                            print YAML::Dump($document);
277                      }                      }
278                  }                  }
279              }              }
# Line 279  Line 281 
281          # Stop the timer.          # Stop the timer.
282          my $duration = int(time() - $start + 0.5);          my $duration = int(time() - $start + 0.5);
283          Trace("Function executed in $duration seconds.") if T(2);          Trace("Function executed in $duration seconds.") if T(2);
         # Output the YAML.  
         print $cgi->header(-type => 'text/plain');  
         print YAML::Dump($document);  
284      }      }
285  }  }
286    
# Line 444  Line 443 
443      Trace("$name error log:\n$errorData");      Trace("$name error log:\n$errorData");
444  }  }
445    
446    =head3 SendError
447    
448        ServerThing::SendError($message, $status);
449    
450    Fail an HTTP request with the specified error message and the specified
451    status message.
452    
453    =over 4
454    
455    =item message
456    
457    Detailed error message. This is sent as the page content.
458    
459    =item status
460    
461    Status message. This is sent as part of the status code.
462    
463    =back
464    
465    =cut
466    
467    sub SendError {
468        # Get the parameters.
469        my ($message, $status) = @_;
470        Trace("Error \"$status\" $message") if T(2);
471        # Print the header and the status message.
472        print CGI::header(-type => 'text/plain',
473                          -status => "500 $status");
474        # Print the detailed message.
475        print $message;
476    }
477    
478    
479    
480  1;  1;

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3