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

Annotation of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.31 - (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 :     ETracing($key || $serverName);
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.9 # Try to get the IDs from the argument structure.
203 :     my $retVal = $args->{$name};
204 : parrello 1.19 # Was a member found?
205 :     if (! defined $retVal) {
206 :     # No. If we're optional, return an empty list; otherwise throw an error.
207 :     if ($optional) {
208 :     $retVal = [];
209 :     } else {
210 :     Confess("No '$name' parameter found.");
211 :     }
212 :     } else {
213 :     # Here we found something. Get the parameter type. We was a list reference.
214 :     # If it's a scalar, we'll convert it to a singleton list. If it's anything
215 :     # else, it's an error.
216 :     my $type = ref $retVal;
217 :     if (! $type) {
218 :     $retVal = [$retVal];
219 :     } elsif ($type ne 'ARRAY') {
220 :     Confess("The '$name' parameter must be a list.");
221 :     }
222 : parrello 1.9 }
223 :     # Return the result.
224 :     return $retVal;
225 :     }
226 :    
227 :    
228 :     =head3 RunTool
229 :    
230 :     ServerThing::RunTool($name => $cmd);
231 :    
232 :     Run a command-line tool. A non-zero return value from the tool will cause
233 :     a fatal error, and the tool's error log will be traced.
234 :    
235 :     =over 4
236 :    
237 :     =item name
238 :    
239 :     Name to give to the tool in the error output.
240 :    
241 :     =item cmd
242 :    
243 :     Command to use for running the tool. This should be the complete command line.
244 :     The command should not contain any fancy piping, though it may redirect the
245 :     standard input and output. The command will be modified by this method to
246 :     redirect the error output to a temporary file.
247 :    
248 :     =back
249 :    
250 :     =cut
251 :    
252 :     sub RunTool {
253 :     # Get the parameters.
254 :     my ($name, $cmd) = @_;
255 :     # Compute the log file name.
256 :     my $errorLog = "$FIG_Config::temp/errors$$.log";
257 :     # Execute the command.
258 : parrello 1.26 Trace("Executing command: $cmd") if T(ServerUtilities => 3);
259 : parrello 1.9 my $res = system("$cmd 2> $errorLog");
260 : parrello 1.26 Trace("Return from $name tool is $res.") if T(ServerUtilities => 3);
261 : parrello 1.9 # Check the result code.
262 :     if ($res != 0) {
263 :     # We have an error. If tracing is on, trace it.
264 : parrello 1.26 if (T(ServerUtilities => 1)) {
265 : parrello 1.9 TraceErrorLog($name, $errorLog);
266 :     }
267 :     # Delete the error log.
268 :     unlink $errorLog;
269 :     # Confess the error.
270 : parrello 1.10 Confess("$name command failed with error code $res.");
271 : parrello 1.9 } else {
272 :     # Everything worked. Trace the error log if necessary.
273 : parrello 1.26 if (T(ServerUtilities => 3) && -s $errorLog) {
274 : parrello 1.9 TraceErrorLog($name, $errorLog);
275 :     }
276 :     # Delete the error log if there is one.
277 :     unlink $errorLog;
278 :     }
279 :     }
280 :    
281 :    
282 :     =head2 Internal Utility Methods
283 :    
284 :     The methods in this section are used internally by this package.
285 :    
286 : parrello 1.6 =head3 RunRequest
287 :    
288 : parrello 1.9 ServerThing::RunRequest($cgi, $serverName);
289 : parrello 1.6
290 :     Run a request from the specified server using the incoming CGI parameter
291 :     object for the parameters.
292 :    
293 :     =over 4
294 :    
295 :     =item cgi
296 :    
297 :     CGI query object containing the parameters from the web service request.
298 :    
299 : parrello 1.13 =item serverThing
300 : parrello 1.6
301 : parrello 1.13 Server object against which to run the request.
302 : parrello 1.6
303 :     =back
304 :    
305 :     =cut
306 :    
307 :     sub RunRequest {
308 :     # Get the parameters.
309 : parrello 1.13 my ($cgi, $serverThing, $docURL) = @_;
310 : parrello 1.9 # Determine the request type.
311 :     if ($cgi->param('pod')) {
312 : parrello 1.4 # Here we have a documentation request. In this case, we produce POD HTML.
313 : parrello 1.9 ProducePod($cgi->param('pod'));
314 : disz 1.31 } elsif ($cgi->param('code')) {
315 :     LineNumberize($cgi->param('code'));
316 : parrello 1.9 } elsif ($cgi->param('file')) {
317 :     # Here we have a file request. Process according to the type.
318 :     my $type = $cgi->param('file');
319 :     if ($type eq 'open') {
320 :     OpenFile($cgi->param('name'));
321 :     } elsif ($type eq 'create') {
322 :     CreateFile();
323 :     } elsif ($type eq 'read') {
324 :     ReadChunk($cgi->param('name'), $cgi->param('location'), $cgi->param('size'));
325 :     } elsif ($type eq 'write') {
326 :     WriteChunk($cgi->param('name'), $cgi->param('data'));
327 :     } else {
328 :     Die("Invalid file function \"$type\".");
329 : parrello 1.4 }
330 : parrello 1.1 } else {
331 : parrello 1.9 # The default is a function request. Get the function name.
332 : parrello 1.4 my $function = $cgi->param('function') || "";
333 : parrello 1.15 Trace("Server function for task $$ is $function.") if T(3);
334 : parrello 1.4 # Insure the function name is valid.
335 :     Die("Invalid function name.")
336 :     if $function =~ /\W/;
337 :     # The parameter structure will go in here.
338 :     my $args;
339 :     # Start the timer.
340 :     my $start = time();
341 :     # The output document goes in here.
342 :     my $document;
343 :     # The sapling database goes in here.
344 :     my $sapling;
345 :     # Protect from errors.
346 : parrello 1.1 eval {
347 : parrello 1.4 # Parse the arguments.
348 :     $args = YAML::Load($cgi->param('args'));
349 : parrello 1.1 };
350 : parrello 1.4 # Check to make sure we got everything.
351 : parrello 1.1 if ($@) {
352 : parrello 1.10 SendError($@, "Error formatting parameters.");
353 : parrello 1.4 } elsif (! $function) {
354 : parrello 1.10 SendError("No function specified.", "No function specified.");
355 : parrello 1.1 } else {
356 : parrello 1.30 $document = eval { $serverThing->$function($args) };
357 : parrello 1.1 # If we have an error, create an error document.
358 :     if ($@) {
359 : parrello 1.13 SendError($@, "Error detected by service.");
360 :     Trace("Error encountered by service: $@") if T(2);
361 : parrello 1.1 } else {
362 : parrello 1.13 # No error, so we output the result.
363 :     print $cgi->header(-type => 'text/plain');
364 : parrello 1.18 my $string = YAML::Dump($document);
365 :     print $string;
366 :     MemTrace(length($string) . " bytes returned from $function by task $$.") if T(Memory => 3);
367 : parrello 1.1 }
368 :     }
369 : parrello 1.4 # Stop the timer.
370 :     my $duration = int(time() - $start + 0.5);
371 : parrello 1.29 Trace("Function $function executed in $duration seconds by task $$.") if T(2);
372 : parrello 1.1 }
373 :     }
374 :    
375 : parrello 1.9 =head3 CreateFile
376 :    
377 :     ServerThing::CreateFile();
378 :    
379 :     Create a new, empty temporary file and send its name back to the client.
380 :    
381 :     =cut
382 :    
383 :     sub CreateFile {
384 :     ##TODO: Code
385 :     }
386 :    
387 :     =head3 OpenFile
388 : parrello 1.6
389 : parrello 1.9 ServerThing::OpenFile($name);
390 : parrello 1.1
391 : parrello 1.9 Send the length of the named file back to the client.
392 :    
393 :     =over 4
394 :    
395 :     =item name
396 :    
397 :     ##TODO: name description
398 :    
399 :     =back
400 :    
401 :     =cut
402 :    
403 :     sub OpenFile {
404 :     # Get the parameters.
405 :     my ($name) = @_;
406 :     ##TODO: Code
407 :     }
408 : parrello 1.1
409 : parrello 1.9 =head3 ReadChunk
410 : parrello 1.1
411 : parrello 1.9 ServerThing::ReadChunk($name, $location, $size);
412 : parrello 1.1
413 : parrello 1.9 Read the indicated number of bytes from the specified location of the
414 :     named file and send them back to the client.
415 : parrello 1.1
416 :     =over 4
417 :    
418 :     =item name
419 :    
420 : parrello 1.9 ##TODO: name description
421 : parrello 1.1
422 : parrello 1.9 =item location
423 : parrello 1.1
424 : parrello 1.9 ##TODO: location description
425 : parrello 1.1
426 : parrello 1.9 =item size
427 : parrello 1.1
428 : parrello 1.9 ##TODO: size description
429 : parrello 1.1
430 :     =back
431 :    
432 :     =cut
433 :    
434 : parrello 1.9 sub ReadChunk {
435 : parrello 1.1 # Get the parameters.
436 : parrello 1.9 my ($name, $location, $size) = @_;
437 :     ##TODO: Code
438 : parrello 1.1 }
439 :    
440 : parrello 1.9 =head3 WriteChunk
441 : parrello 1.1
442 : parrello 1.9 ServerThing::WriteChunk($name, $data);
443 : parrello 1.8
444 : parrello 1.9 Write the specified data to the named file.
445 : parrello 1.8
446 :     =over 4
447 :    
448 :     =item name
449 :    
450 : parrello 1.9 ##TODO: name description
451 :    
452 :     =item data
453 :    
454 :     ##TODO: data description
455 :    
456 :     =back
457 :    
458 :     =cut
459 :    
460 :     sub WriteChunk {
461 :     # Get the parameters.
462 :     my ($name, $data) = @_;
463 :     ##TODO: Code
464 :     }
465 :    
466 :    
467 : disz 1.31 =head3 LineNumberize
468 :    
469 :     ServerThing::LineNumberize($module);
470 :    
471 :     Output the module line by line with line numbers
472 :    
473 :     =over 4
474 :    
475 :     =item module
476 :    
477 :     Name of the module to line numberized
478 :    
479 :     =back
480 :    
481 :     =cut
482 :    
483 :     sub LineNumberize {
484 :     # Get the parameters.
485 :     my ($module) = @_;
486 :     my $fks_path = "$FIG_Config::fig_disk/dist/releases/current/FigKernelScripts/$module";
487 :     # Start the output page.
488 :     print CGI::header();
489 :     print CGI::start_html(-title => 'Documentation Page',
490 :     -style => { src => "http://servers.nmpdr.org/sapling/Html/css/ERDB.css" });
491 :     # Protect from errors.
492 :     eval {
493 :     if (-e $fks_path) {
494 :     print "<pre>\n";
495 :     my $i = 1;
496 :     foreach my $line (`cat $fks_path`) {
497 :     print "$i.\t$line";
498 :     $i++;
499 :     }
500 :     print "</pre>\n";
501 :     } else {
502 :     print "File $fks_path not found";
503 :     }
504 :    
505 :     };
506 :     # Process any error.
507 :     if ($@) {
508 :     print CGI::blockquote({ class => 'error' }, $@);
509 :     }
510 :     # Close off the page.
511 :     print CGI::end_html();
512 :    
513 :     }
514 :    
515 : parrello 1.9 =head3 ProducePod
516 :    
517 :     ServerThing::ProducePod($module);
518 :    
519 :     Output the POD documentation for the specified module.
520 :    
521 :     =over 4
522 : parrello 1.8
523 : parrello 1.9 =item module
524 : parrello 1.8
525 : parrello 1.9 Name of the module whose POD document is to be displayed.
526 : parrello 1.8
527 :     =back
528 :    
529 :     =cut
530 :    
531 : parrello 1.9 sub ProducePod {
532 : parrello 1.8 # Get the parameters.
533 : parrello 1.9 my ($module) = @_;
534 :     # Start the output page.
535 :     print CGI::header();
536 :     print CGI::start_html(-title => 'Documentation Page',
537 :     -style => { src => "http://servers.nmpdr.org/sapling/Html/css/ERDB.css" });
538 :     # Protect from errors.
539 :     eval {
540 :     # We'll format the HTML text in here.
541 :     require DocUtils;
542 : parrello 1.20 my $html = DocUtils::ShowPod($module, "http://servers.nmpdr.org/sapling/server.cgi?pod=");
543 : parrello 1.9 # Output the POD HTML.
544 :     print $html;
545 :     };
546 :     # Process any error.
547 :     if ($@) {
548 :     print CGI::blockquote({ class => 'error' }, $@);
549 : parrello 1.8 }
550 : parrello 1.9 # Close off the page.
551 :     print CGI::end_html();
552 :    
553 : parrello 1.8 }
554 :    
555 :     =head3 TraceErrorLog
556 :    
557 :     ServerThing::TraceErrorLog($name, $errorLog);
558 :    
559 :     Trace the specified error log file. This is a very dinky routine that
560 :     performs a task required by L</RunTool> in multiple places.
561 :    
562 :     =over 4
563 :    
564 :     =item name
565 :    
566 :     Name of the tool relevant to the log file.
567 :    
568 :     =item errorLog
569 :    
570 :     Name of the log file.
571 :    
572 :     =back
573 :    
574 :     =cut
575 :    
576 :     sub TraceErrorLog {
577 :     my ($name, $errorLog) = @_;
578 :     my $errorData = Tracer::GetFile($errorLog);
579 :     Trace("$name error log:\n$errorData");
580 :     }
581 :    
582 : parrello 1.10 =head3 SendError
583 :    
584 :     ServerThing::SendError($message, $status);
585 :    
586 :     Fail an HTTP request with the specified error message and the specified
587 :     status message.
588 :    
589 :     =over 4
590 :    
591 :     =item message
592 :    
593 :     Detailed error message. This is sent as the page content.
594 :    
595 :     =item status
596 :    
597 :     Status message. This is sent as part of the status code.
598 :    
599 :     =back
600 :    
601 :     =cut
602 :    
603 :     sub SendError {
604 :     # Get the parameters.
605 :     my ($message, $status) = @_;
606 :     Trace("Error \"$status\" $message") if T(2);
607 : parrello 1.30 # Check for a DBserver error. These can be retried and get a special status
608 :     # code.
609 :     my $realStatus;
610 :     if ($message =~ /DBServer Error:\s+/) {
611 :     $realStatus = "503 $status";
612 :     } else {
613 :     $realStatus = "500 $status";
614 :     }
615 : parrello 1.10 # Print the header and the status message.
616 :     print CGI::header(-type => 'text/plain',
617 : parrello 1.30 -status => $realStatus);
618 : parrello 1.10 # Print the detailed message.
619 :     print $message;
620 :     }
621 :    
622 :    
623 : disz 1.31 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3