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

Annotation of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3