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

Annotation of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.35 - (view) (download) (as text)

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     package ServerThing;
4 :    
5 :     use strict;
6 :     use Tracer;
7 :     use YAML;
8 :     use ERDB;
9 :     use TestUtils;
10 :     use Time::HiRes;
11 : parrello 1.9 use File::Temp;
12 : parrello 1.10 use ErrorMessage;
13 : parrello 1.1 use CGI;
14 : parrello 1.9 no warnings qw(once);
15 : parrello 1.1
16 : parrello 1.11 # Maximum number of requests to run per invocation.
17 : parrello 1.29 use constant MAX_REQUESTS => 5000;
18 : parrello 1.11
19 : parrello 1.1 =head1 General Server Helper
20 :    
21 :     This package provides a method-- I<RunServer>-- that can be called from a CGI
22 : parrello 1.9 script to perform the duties of a FIG server. RunServer is called with two
23 :     parameters: the name of the server package (e.g. C<SAP> for B<SAP.pm>) and
24 :     the first command-line parameter. The command-line parameter (if defined) will
25 :     be used as the tracing key, and also indicates that the script is being invoked
26 :     from the command line rather than over the web.
27 : parrello 1.1
28 :     =cut
29 :    
30 :     sub RunServer {
31 :     # Get the parameters.
32 : parrello 1.9 my ($serverName, $key) = @_;
33 : parrello 1.25 # Set up tracing. We never do CGI tracing here; the only question is whether
34 :     # or not the caller passed in a tracing key. If he didn't, we use the server
35 :     # name.
36 : parrello 1.32 ETracing($key || $serverName, destType => 'APPEND', level => '0 ServerThing');
37 : parrello 1.9 # Turn off YAML compression, which causes problems with some of our hash keys.
38 :     $YAML::CompressSeries = 0;
39 : parrello 1.13 # Create the server object.
40 : parrello 1.15 Trace("Requiring $serverName for task $$.") if T(3);
41 : parrello 1.13 eval {
42 :     require "$serverName.pm";
43 :     };
44 :     # If we have an error, create an error document.
45 :     if ($@) {
46 :     SendError($@, "Could not load server module.");
47 :     } else {
48 :     # Having successfully loaded the server code, we create the object.
49 :     my $serverThing = eval("$serverName" . '->new()');
50 : parrello 1.15 Trace("$serverName object created for task $$.") if T(2);
51 : parrello 1.13 # If we have an error, create an error document.
52 :     if ($@) {
53 :     SendError($@, "Could not start server.");
54 : parrello 1.3 } else {
55 : parrello 1.13 # No error, so now we can process the request.
56 :     my $cgi;
57 :     if (! defined $key) {
58 :     # No tracing key, so presume we're a web service. Check for Fast CGI.
59 :     if ($ENV{REQUEST_METHOD} eq '') {
60 :     # Count the number of requests.
61 :     my $requests = 0;
62 : parrello 1.24 Trace("Starting Fast CGI loop.") if T(3);
63 : parrello 1.13 # Loop through the fast CGI requests. If we have request throttling,
64 :     # we exit after a maximum number of requests has been exceeded.
65 :     require CGI::Fast;
66 : parrello 1.23 while ((MAX_REQUESTS == 0 || ++$requests < MAX_REQUESTS) &&
67 :     ($cgi = new CGI::Fast())) {
68 : parrello 1.13 RunRequest($cgi, $serverThing);
69 : parrello 1.16 Trace("Request $requests complete in task $$.") if T(3);
70 : parrello 1.13 }
71 : parrello 1.15 Trace("Terminating FastCGI task $$ after $requests requests.") if T(2);
72 : parrello 1.13 } else {
73 :     # Here we have a normal web service (non-Fast).
74 :     my $cgi = CGI->new();
75 :     # Check for a source parameter. This gets used as the tracing key.
76 :     $key = $cgi->param('source');
77 :     # Run this request.
78 :     RunRequest($cgi, $serverThing);
79 :     }
80 : parrello 1.6 } else {
81 : parrello 1.13 # We're being invoked from the command line. Use the tracing
82 :     # key to find the parm file and create the CGI object from that.
83 :     my $ih = Open(undef, "<$FIG_Config::temp/$key.parms");
84 :     $cgi = CGI->new($ih);
85 :     # Run this request.
86 :     RunRequest($cgi, $serverThing);
87 : parrello 1.6 }
88 : parrello 1.3 }
89 : parrello 1.1 }
90 : parrello 1.6 }
91 :    
92 :    
93 : parrello 1.9 =head2 Server Utility Methods
94 :    
95 :     The methods in this section are utilities of general use to the various
96 :     server modules.
97 :    
98 : parrello 1.21 =head3 AddSubsystemFilter
99 :    
100 :     ServerThing::AddSubsystemFilter(\$filter, $args);
101 :    
102 :     Add subsystem filtering information to the specified query filter clause
103 :     based on data in the argument hash. The argument hash will be checked for
104 : parrello 1.22 the C<-usable> parameter, which includes or excludes unusuable subsystems, and
105 :     the C<-exclude> parameter, which lists types of subsystems that should be
106 : parrello 1.21 excluded.
107 :    
108 :     =over 4
109 :    
110 :     =item filter
111 :    
112 :     Reference to the current filter string. If additional filtering is required,
113 :     this string will be updated.
114 :    
115 :     =item args
116 :    
117 :     Reference to the parameter hash for the current server call. This hash will
118 : parrello 1.22 be examined for the C<-usable> and C<-exclude> parameters.
119 : parrello 1.21
120 :     =back
121 :    
122 :     =cut
123 :    
124 :     use constant SS_TYPE_EXCLUDE_ITEMS => { 'cluster-based' => 1,
125 :     experimental => 1,
126 :     private => 1 };
127 :    
128 :     sub AddSubsystemFilter {
129 :     # Get the parameters.
130 :     my ($filter, $args) = @_;
131 :     # We'll put the new filter stuff in here.
132 :     my @newFilters;
133 :     # Unless unusable subsystems are desired, we must add a clause to the filter.
134 : parrello 1.22 # The default is that only usable subsystems are included.
135 :     my $usable = 1;
136 :     # This default can be overridden by the "-usable" parameter.
137 :     if (exists $args->{-usable}) {
138 :     $usable = $args->{-usable};
139 :     }
140 :     # If we're restricting to usable subsystems, add a filter to that effect.
141 :     if ($usable) {
142 : parrello 1.21 push @newFilters, "Subsystem(usable) = 1";
143 :     }
144 :     # Check for exclusion filters.
145 :     my $exclusions = ServerThing::GetIdList(-exclude => $args, 1);
146 :     for my $exclusion (@$exclusions) {
147 :     if (! SS_TYPE_EXCLUDE_ITEMS->{$exclusion}) {
148 :     Confess("Invalid exclusion type \"$exclusion\".");
149 :     } else {
150 :     # Here we have to exclude subsystems of the specified type.
151 :     push @newFilters, "Subsystem($exclusion) = 0";
152 :     }
153 :     }
154 :     # Do we need to update the incoming filter?
155 :     if (@newFilters) {
156 :     # Yes. If the incoming filter is nonempty, push it onto the list
157 :     # so it gets included in the result.
158 :     if ($$filter) {
159 :     push @newFilters, $$filter;
160 :     }
161 :     # Put all the filters together to form the new filter.
162 :     $$filter = join(" AND ", @newFilters);
163 : parrello 1.26 Trace("Subsystem filter is $$filter.") if T(ServerUtilities => 3);
164 : parrello 1.21 }
165 :     }
166 :    
167 :    
168 :    
169 : parrello 1.9 =head3 GetIdList
170 :    
171 : parrello 1.19 my $ids = ServerThing::GetIdList($name => $args, $optional);
172 : parrello 1.9
173 :     Get a named list of IDs from an argument structure. If the IDs are
174 :     missing, or are not a list, an error will occur.
175 :    
176 :     =over 4
177 :    
178 :     =item name
179 :    
180 :     Name of the argument structure member that should contain the ID list.
181 :    
182 :     =item args
183 :    
184 :     Argument structure from which the ID list is to be extracted.
185 :    
186 : parrello 1.19 =item optional (optional)
187 :    
188 :     If TRUE, then a missing value will not generate an error. Instead, an empty list
189 :     will be returned. The default is FALSE.
190 :    
191 : parrello 1.9 =item RETURN
192 :    
193 :     Returns a reference to a list of IDs taken from the argument structure.
194 :    
195 :     =back
196 :    
197 :     =cut
198 :    
199 :     sub GetIdList {
200 :     # Get the parameters.
201 : parrello 1.19 my ($name, $args, $optional) = @_;
202 : parrello 1.35 # Declare the return variable.
203 :     my $retVal;
204 : parrello 1.32 # Check the argument format.
205 : parrello 1.35 if (! defined $args && $optional) {
206 :     # Here there are no parameters, but the arguments are optional so it's
207 :     # okay.
208 :     $retVal = [];
209 :     } elsif (ref $args ne 'HASH') {
210 :     # Here we have an invalid parameter structure.
211 : parrello 1.32 Confess("No '$name' parameter present.");
212 : parrello 1.35 } else {
213 :     # Here we have a hash with potential parameters in it. Try to get the
214 :     # IDs from the argument structure.
215 :     $retVal = $args->{$name};
216 :     # Was a member found?
217 :     if (! defined $retVal) {
218 :     # No. If we're optional, return an empty list; otherwise throw an error.
219 :     if ($optional) {
220 :     $retVal = [];
221 :     } else {
222 :     Confess("No '$name' parameter found.");
223 :     }
224 : parrello 1.19 } else {
225 : parrello 1.35 # Here we found something. Get the parameter type. We want a list reference.
226 :     # If it's a scalar, we'll convert it to a singleton list. If it's anything
227 :     # else, it's an error.
228 :     my $type = ref $retVal;
229 :     if (! $type) {
230 :     $retVal = [$retVal];
231 :     } elsif ($type ne 'ARRAY') {
232 :     Confess("The '$name' parameter must be a list.");
233 :     }
234 : parrello 1.19 }
235 : parrello 1.9 }
236 :     # Return the result.
237 :     return $retVal;
238 :     }
239 :    
240 :    
241 :     =head3 RunTool
242 :    
243 :     ServerThing::RunTool($name => $cmd);
244 :    
245 :     Run a command-line tool. A non-zero return value from the tool will cause
246 :     a fatal error, and the tool's error log will be traced.
247 :    
248 :     =over 4
249 :    
250 :     =item name
251 :    
252 :     Name to give to the tool in the error output.
253 :    
254 :     =item cmd
255 :    
256 :     Command to use for running the tool. This should be the complete command line.
257 :     The command should not contain any fancy piping, though it may redirect the
258 :     standard input and output. The command will be modified by this method to
259 :     redirect the error output to a temporary file.
260 :    
261 :     =back
262 :    
263 :     =cut
264 :    
265 :     sub RunTool {
266 :     # Get the parameters.
267 :     my ($name, $cmd) = @_;
268 :     # Compute the log file name.
269 :     my $errorLog = "$FIG_Config::temp/errors$$.log";
270 :     # Execute the command.
271 : parrello 1.26 Trace("Executing command: $cmd") if T(ServerUtilities => 3);
272 : parrello 1.9 my $res = system("$cmd 2> $errorLog");
273 : parrello 1.26 Trace("Return from $name tool is $res.") if T(ServerUtilities => 3);
274 : parrello 1.9 # Check the result code.
275 :     if ($res != 0) {
276 :     # We have an error. If tracing is on, trace it.
277 : parrello 1.26 if (T(ServerUtilities => 1)) {
278 : parrello 1.9 TraceErrorLog($name, $errorLog);
279 :     }
280 :     # Delete the error log.
281 :     unlink $errorLog;
282 :     # Confess the error.
283 : parrello 1.10 Confess("$name command failed with error code $res.");
284 : parrello 1.9 } else {
285 :     # Everything worked. Trace the error log if necessary.
286 : parrello 1.26 if (T(ServerUtilities => 3) && -s $errorLog) {
287 : parrello 1.9 TraceErrorLog($name, $errorLog);
288 :     }
289 :     # Delete the error log if there is one.
290 :     unlink $errorLog;
291 :     }
292 :     }
293 :    
294 :    
295 :     =head2 Internal Utility Methods
296 :    
297 :     The methods in this section are used internally by this package.
298 :    
299 : parrello 1.6 =head3 RunRequest
300 :    
301 : parrello 1.9 ServerThing::RunRequest($cgi, $serverName);
302 : parrello 1.6
303 :     Run a request from the specified server using the incoming CGI parameter
304 :     object for the parameters.
305 :    
306 :     =over 4
307 :    
308 :     =item cgi
309 :    
310 :     CGI query object containing the parameters from the web service request.
311 :    
312 : parrello 1.13 =item serverThing
313 : parrello 1.6
314 : parrello 1.13 Server object against which to run the request.
315 : parrello 1.6
316 :     =back
317 :    
318 :     =cut
319 :    
320 :     sub RunRequest {
321 :     # Get the parameters.
322 : parrello 1.13 my ($cgi, $serverThing, $docURL) = @_;
323 : parrello 1.9 # Determine the request type.
324 :     if ($cgi->param('pod')) {
325 : parrello 1.4 # Here we have a documentation request. In this case, we produce POD HTML.
326 : parrello 1.9 ProducePod($cgi->param('pod'));
327 : disz 1.31 } elsif ($cgi->param('code')) {
328 : parrello 1.32 # Here the user wants to see the code for one of our scripts.
329 :     LineNumberize($cgi->param('code'));
330 : parrello 1.9 } elsif ($cgi->param('file')) {
331 :     # Here we have a file request. Process according to the type.
332 :     my $type = $cgi->param('file');
333 :     if ($type eq 'open') {
334 :     OpenFile($cgi->param('name'));
335 :     } elsif ($type eq 'create') {
336 :     CreateFile();
337 :     } elsif ($type eq 'read') {
338 :     ReadChunk($cgi->param('name'), $cgi->param('location'), $cgi->param('size'));
339 :     } elsif ($type eq 'write') {
340 :     WriteChunk($cgi->param('name'), $cgi->param('data'));
341 :     } else {
342 :     Die("Invalid file function \"$type\".");
343 : parrello 1.4 }
344 : parrello 1.1 } else {
345 : parrello 1.9 # The default is a function request. Get the function name.
346 : parrello 1.4 my $function = $cgi->param('function') || "";
347 : parrello 1.15 Trace("Server function for task $$ is $function.") if T(3);
348 : parrello 1.4 # Insure the function name is valid.
349 :     Die("Invalid function name.")
350 :     if $function =~ /\W/;
351 :     # The parameter structure will go in here.
352 :     my $args;
353 :     # Start the timer.
354 :     my $start = time();
355 :     # The output document goes in here.
356 :     my $document;
357 :     # The sapling database goes in here.
358 :     my $sapling;
359 :     # Protect from errors.
360 : parrello 1.1 eval {
361 : parrello 1.4 # Parse the arguments.
362 :     $args = YAML::Load($cgi->param('args'));
363 : parrello 1.1 };
364 : parrello 1.4 # Check to make sure we got everything.
365 : parrello 1.1 if ($@) {
366 : parrello 1.10 SendError($@, "Error formatting parameters.");
367 : parrello 1.4 } elsif (! $function) {
368 : parrello 1.10 SendError("No function specified.", "No function specified.");
369 : parrello 1.1 } else {
370 : parrello 1.30 $document = eval { $serverThing->$function($args) };
371 : parrello 1.1 # If we have an error, create an error document.
372 :     if ($@) {
373 : parrello 1.13 SendError($@, "Error detected by service.");
374 : parrello 1.32 Trace("Error encountered by service: $@") if T(0);
375 : parrello 1.1 } else {
376 : parrello 1.13 # No error, so we output the result.
377 :     print $cgi->header(-type => 'text/plain');
378 : parrello 1.18 my $string = YAML::Dump($document);
379 :     print $string;
380 :     MemTrace(length($string) . " bytes returned from $function by task $$.") if T(Memory => 3);
381 : parrello 1.1 }
382 :     }
383 : parrello 1.4 # Stop the timer.
384 :     my $duration = int(time() - $start + 0.5);
385 : parrello 1.29 Trace("Function $function executed in $duration seconds by task $$.") if T(2);
386 : parrello 1.1 }
387 :     }
388 :    
389 : parrello 1.9 =head3 CreateFile
390 :    
391 :     ServerThing::CreateFile();
392 :    
393 :     Create a new, empty temporary file and send its name back to the client.
394 :    
395 :     =cut
396 :    
397 :     sub CreateFile {
398 :     ##TODO: Code
399 :     }
400 :    
401 :     =head3 OpenFile
402 : parrello 1.6
403 : parrello 1.9 ServerThing::OpenFile($name);
404 : parrello 1.1
405 : parrello 1.9 Send the length of the named file back to the client.
406 :    
407 :     =over 4
408 :    
409 :     =item name
410 :    
411 :     ##TODO: name description
412 :    
413 :     =back
414 :    
415 :     =cut
416 :    
417 :     sub OpenFile {
418 :     # Get the parameters.
419 :     my ($name) = @_;
420 :     ##TODO: Code
421 :     }
422 : parrello 1.1
423 : parrello 1.9 =head3 ReadChunk
424 : parrello 1.1
425 : parrello 1.9 ServerThing::ReadChunk($name, $location, $size);
426 : parrello 1.1
427 : parrello 1.9 Read the indicated number of bytes from the specified location of the
428 :     named file and send them back to the client.
429 : parrello 1.1
430 :     =over 4
431 :    
432 :     =item name
433 :    
434 : parrello 1.9 ##TODO: name description
435 : parrello 1.1
436 : parrello 1.9 =item location
437 : parrello 1.1
438 : parrello 1.9 ##TODO: location description
439 : parrello 1.1
440 : parrello 1.9 =item size
441 : parrello 1.1
442 : parrello 1.9 ##TODO: size description
443 : parrello 1.1
444 :     =back
445 :    
446 :     =cut
447 :    
448 : parrello 1.9 sub ReadChunk {
449 : parrello 1.1 # Get the parameters.
450 : parrello 1.9 my ($name, $location, $size) = @_;
451 :     ##TODO: Code
452 : parrello 1.1 }
453 :    
454 : parrello 1.9 =head3 WriteChunk
455 : parrello 1.1
456 : parrello 1.9 ServerThing::WriteChunk($name, $data);
457 : parrello 1.8
458 : parrello 1.9 Write the specified data to the named file.
459 : parrello 1.8
460 :     =over 4
461 :    
462 :     =item name
463 :    
464 : parrello 1.9 ##TODO: name description
465 :    
466 :     =item data
467 :    
468 :     ##TODO: data description
469 :    
470 :     =back
471 :    
472 :     =cut
473 :    
474 :     sub WriteChunk {
475 :     # Get the parameters.
476 :     my ($name, $data) = @_;
477 :     ##TODO: Code
478 :     }
479 :    
480 :    
481 : disz 1.31 =head3 LineNumberize
482 :    
483 :     ServerThing::LineNumberize($module);
484 :    
485 :     Output the module line by line with line numbers
486 :    
487 :     =over 4
488 :    
489 :     =item module
490 :    
491 :     Name of the module to line numberized
492 :    
493 :     =back
494 :    
495 :     =cut
496 :    
497 :     sub LineNumberize {
498 :     # Get the parameters.
499 :     my ($module) = @_;
500 :     my $fks_path = "$FIG_Config::fig_disk/dist/releases/current/FigKernelScripts/$module";
501 :     # Start the output page.
502 :     print CGI::header();
503 :     print CGI::start_html(-title => 'Documentation Page',
504 :     -style => { src => "http://servers.nmpdr.org/sapling/Html/css/ERDB.css" });
505 :     # Protect from errors.
506 :     eval {
507 : parrello 1.32 if (-e $fks_path) {
508 :     print "<pre>\n";
509 :     my $i = 1;
510 :     foreach my $line (`cat $fks_path`) {
511 :     print "$i.\t$line";
512 :     $i++;
513 :     }
514 :     print "</pre>\n";
515 :     } else {
516 :     print "File $fks_path not found";
517 :     }
518 : disz 1.31 };
519 :     # Process any error.
520 :     if ($@) {
521 :     print CGI::blockquote({ class => 'error' }, $@);
522 :     }
523 :     # Close off the page.
524 :     print CGI::end_html();
525 :    
526 :     }
527 :    
528 : parrello 1.9 =head3 ProducePod
529 :    
530 :     ServerThing::ProducePod($module);
531 :    
532 :     Output the POD documentation for the specified module.
533 :    
534 :     =over 4
535 : parrello 1.8
536 : parrello 1.9 =item module
537 : parrello 1.8
538 : parrello 1.9 Name of the module whose POD document is to be displayed.
539 : parrello 1.8
540 :     =back
541 :    
542 :     =cut
543 :    
544 : parrello 1.9 sub ProducePod {
545 : parrello 1.8 # Get the parameters.
546 : parrello 1.9 my ($module) = @_;
547 :     # Start the output page.
548 :     print CGI::header();
549 :     print CGI::start_html(-title => 'Documentation Page',
550 :     -style => { src => "http://servers.nmpdr.org/sapling/Html/css/ERDB.css" });
551 :     # Protect from errors.
552 :     eval {
553 :     # We'll format the HTML text in here.
554 :     require DocUtils;
555 : parrello 1.20 my $html = DocUtils::ShowPod($module, "http://servers.nmpdr.org/sapling/server.cgi?pod=");
556 : parrello 1.9 # Output the POD HTML.
557 :     print $html;
558 :     };
559 :     # Process any error.
560 :     if ($@) {
561 :     print CGI::blockquote({ class => 'error' }, $@);
562 : parrello 1.8 }
563 : parrello 1.9 # Close off the page.
564 :     print CGI::end_html();
565 :    
566 : parrello 1.8 }
567 :    
568 :     =head3 TraceErrorLog
569 :    
570 :     ServerThing::TraceErrorLog($name, $errorLog);
571 :    
572 :     Trace the specified error log file. This is a very dinky routine that
573 :     performs a task required by L</RunTool> in multiple places.
574 :    
575 :     =over 4
576 :    
577 :     =item name
578 :    
579 :     Name of the tool relevant to the log file.
580 :    
581 :     =item errorLog
582 :    
583 :     Name of the log file.
584 :    
585 :     =back
586 :    
587 :     =cut
588 :    
589 :     sub TraceErrorLog {
590 :     my ($name, $errorLog) = @_;
591 :     my $errorData = Tracer::GetFile($errorLog);
592 :     Trace("$name error log:\n$errorData");
593 :     }
594 :    
595 : parrello 1.10 =head3 SendError
596 :    
597 :     ServerThing::SendError($message, $status);
598 :    
599 :     Fail an HTTP request with the specified error message and the specified
600 :     status message.
601 :    
602 :     =over 4
603 :    
604 :     =item message
605 :    
606 :     Detailed error message. This is sent as the page content.
607 :    
608 :     =item status
609 :    
610 :     Status message. This is sent as part of the status code.
611 :    
612 :     =back
613 :    
614 :     =cut
615 :    
616 :     sub SendError {
617 :     # Get the parameters.
618 :     my ($message, $status) = @_;
619 :     Trace("Error \"$status\" $message") if T(2);
620 : parrello 1.30 # Check for a DBserver error. These can be retried and get a special status
621 :     # code.
622 :     my $realStatus;
623 :     if ($message =~ /DBServer Error:\s+/) {
624 :     $realStatus = "503 $status";
625 :     } else {
626 :     $realStatus = "500 $status";
627 :     }
628 : parrello 1.10 # Print the header and the status message.
629 :     print CGI::header(-type => 'text/plain',
630 : parrello 1.30 -status => $realStatus);
631 : parrello 1.10 # Print the detailed message.
632 :     print $message;
633 :     }
634 :    
635 :    
636 : disz 1.31 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3