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

Annotation of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3