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

Annotation of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (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.27 use constant MAX_REQUESTS => 10;
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 :     } elsif ($cgi->param('file')) {
315 :     # Here we have a file request. Process according to the type.
316 :     my $type = $cgi->param('file');
317 :     if ($type eq 'open') {
318 :     OpenFile($cgi->param('name'));
319 :     } elsif ($type eq 'create') {
320 :     CreateFile();
321 :     } elsif ($type eq 'read') {
322 :     ReadChunk($cgi->param('name'), $cgi->param('location'), $cgi->param('size'));
323 :     } elsif ($type eq 'write') {
324 :     WriteChunk($cgi->param('name'), $cgi->param('data'));
325 :     } else {
326 :     Die("Invalid file function \"$type\".");
327 : parrello 1.4 }
328 : parrello 1.1 } else {
329 : parrello 1.9 # The default is a function request. Get the function name.
330 : parrello 1.4 my $function = $cgi->param('function') || "";
331 : parrello 1.15 Trace("Server function for task $$ is $function.") if T(3);
332 : parrello 1.4 # Insure the function name is valid.
333 :     Die("Invalid function name.")
334 :     if $function =~ /\W/;
335 :     # The parameter structure will go in here.
336 :     my $args;
337 :     # Start the timer.
338 :     my $start = time();
339 :     # The output document goes in here.
340 :     my $document;
341 :     # The sapling database goes in here.
342 :     my $sapling;
343 :     # Protect from errors.
344 : parrello 1.1 eval {
345 : parrello 1.4 # Parse the arguments.
346 :     $args = YAML::Load($cgi->param('args'));
347 : parrello 1.1 };
348 : parrello 1.4 # Check to make sure we got everything.
349 : parrello 1.1 if ($@) {
350 : parrello 1.10 SendError($@, "Error formatting parameters.");
351 : parrello 1.4 } elsif (! $function) {
352 : parrello 1.10 SendError("No function specified.", "No function specified.");
353 : parrello 1.1 } else {
354 : parrello 1.13 $document = eval("\$serverThing->$function(\$args)");
355 : parrello 1.1 # If we have an error, create an error document.
356 :     if ($@) {
357 : parrello 1.13 SendError($@, "Error detected by service.");
358 :     Trace("Error encountered by service: $@") if T(2);
359 : parrello 1.1 } else {
360 : parrello 1.13 # No error, so we output the result.
361 :     print $cgi->header(-type => 'text/plain');
362 : parrello 1.18 my $string = YAML::Dump($document);
363 :     print $string;
364 :     MemTrace(length($string) . " bytes returned from $function by task $$.") if T(Memory => 3);
365 : parrello 1.1 }
366 :     }
367 : parrello 1.4 # Stop the timer.
368 :     my $duration = int(time() - $start + 0.5);
369 : parrello 1.15 Trace("Function executed in $duration seconds by task $$.") if T(2);
370 : parrello 1.1 }
371 :     }
372 :    
373 : parrello 1.9 =head3 CreateFile
374 :    
375 :     ServerThing::CreateFile();
376 :    
377 :     Create a new, empty temporary file and send its name back to the client.
378 :    
379 :     =cut
380 :    
381 :     sub CreateFile {
382 :     ##TODO: Code
383 :     }
384 :    
385 :     =head3 OpenFile
386 : parrello 1.6
387 : parrello 1.9 ServerThing::OpenFile($name);
388 : parrello 1.1
389 : parrello 1.9 Send the length of the named file back to the client.
390 :    
391 :     =over 4
392 :    
393 :     =item name
394 :    
395 :     ##TODO: name description
396 :    
397 :     =back
398 :    
399 :     =cut
400 :    
401 :     sub OpenFile {
402 :     # Get the parameters.
403 :     my ($name) = @_;
404 :     ##TODO: Code
405 :     }
406 : parrello 1.1
407 : parrello 1.9 =head3 ReadChunk
408 : parrello 1.1
409 : parrello 1.9 ServerThing::ReadChunk($name, $location, $size);
410 : parrello 1.1
411 : parrello 1.9 Read the indicated number of bytes from the specified location of the
412 :     named file and send them back to the client.
413 : parrello 1.1
414 :     =over 4
415 :    
416 :     =item name
417 :    
418 : parrello 1.9 ##TODO: name description
419 : parrello 1.1
420 : parrello 1.9 =item location
421 : parrello 1.1
422 : parrello 1.9 ##TODO: location description
423 : parrello 1.1
424 : parrello 1.9 =item size
425 : parrello 1.1
426 : parrello 1.9 ##TODO: size description
427 : parrello 1.1
428 :     =back
429 :    
430 :     =cut
431 :    
432 : parrello 1.9 sub ReadChunk {
433 : parrello 1.1 # Get the parameters.
434 : parrello 1.9 my ($name, $location, $size) = @_;
435 :     ##TODO: Code
436 : parrello 1.1 }
437 :    
438 : parrello 1.9 =head3 WriteChunk
439 : parrello 1.1
440 : parrello 1.9 ServerThing::WriteChunk($name, $data);
441 : parrello 1.8
442 : parrello 1.9 Write the specified data to the named file.
443 : parrello 1.8
444 :     =over 4
445 :    
446 :     =item name
447 :    
448 : parrello 1.9 ##TODO: name description
449 :    
450 :     =item data
451 :    
452 :     ##TODO: data description
453 :    
454 :     =back
455 :    
456 :     =cut
457 :    
458 :     sub WriteChunk {
459 :     # Get the parameters.
460 :     my ($name, $data) = @_;
461 :     ##TODO: Code
462 :     }
463 :    
464 :    
465 :     =head3 ProducePod
466 :    
467 :     ServerThing::ProducePod($module);
468 :    
469 :     Output the POD documentation for the specified module.
470 :    
471 :     =over 4
472 : parrello 1.8
473 : parrello 1.9 =item module
474 : parrello 1.8
475 : parrello 1.9 Name of the module whose POD document is to be displayed.
476 : parrello 1.8
477 :     =back
478 :    
479 :     =cut
480 :    
481 : parrello 1.9 sub ProducePod {
482 : parrello 1.8 # Get the parameters.
483 : parrello 1.9 my ($module) = @_;
484 :     # Start the output page.
485 :     print CGI::header();
486 :     print CGI::start_html(-title => 'Documentation Page',
487 :     -style => { src => "http://servers.nmpdr.org/sapling/Html/css/ERDB.css" });
488 :     # Protect from errors.
489 :     eval {
490 :     # We'll format the HTML text in here.
491 :     require DocUtils;
492 : parrello 1.20 my $html = DocUtils::ShowPod($module, "http://servers.nmpdr.org/sapling/server.cgi?pod=");
493 : parrello 1.9 # Output the POD HTML.
494 :     print $html;
495 :     };
496 :     # Process any error.
497 :     if ($@) {
498 :     print CGI::blockquote({ class => 'error' }, $@);
499 : parrello 1.8 }
500 : parrello 1.9 # Close off the page.
501 :     print CGI::end_html();
502 :    
503 : parrello 1.8 }
504 :    
505 :     =head3 TraceErrorLog
506 :    
507 :     ServerThing::TraceErrorLog($name, $errorLog);
508 :    
509 :     Trace the specified error log file. This is a very dinky routine that
510 :     performs a task required by L</RunTool> in multiple places.
511 :    
512 :     =over 4
513 :    
514 :     =item name
515 :    
516 :     Name of the tool relevant to the log file.
517 :    
518 :     =item errorLog
519 :    
520 :     Name of the log file.
521 :    
522 :     =back
523 :    
524 :     =cut
525 :    
526 :     sub TraceErrorLog {
527 :     my ($name, $errorLog) = @_;
528 :     my $errorData = Tracer::GetFile($errorLog);
529 :     Trace("$name error log:\n$errorData");
530 :     }
531 :    
532 : parrello 1.10 =head3 SendError
533 :    
534 :     ServerThing::SendError($message, $status);
535 :    
536 :     Fail an HTTP request with the specified error message and the specified
537 :     status message.
538 :    
539 :     =over 4
540 :    
541 :     =item message
542 :    
543 :     Detailed error message. This is sent as the page content.
544 :    
545 :     =item status
546 :    
547 :     Status message. This is sent as part of the status code.
548 :    
549 :     =back
550 :    
551 :     =cut
552 :    
553 :     sub SendError {
554 :     # Get the parameters.
555 :     my ($message, $status) = @_;
556 :     Trace("Error \"$status\" $message") if T(2);
557 :     # Print the header and the status message.
558 :     print CGI::header(-type => 'text/plain',
559 :     -status => "500 $status");
560 :     # Print the detailed message.
561 :     print $message;
562 :     }
563 :    
564 :    
565 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3