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

Annotation of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (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 :     while (($cgi = new CGI::Fast()) &&
65 :     (MAX_REQUESTS == 0 || ++$requests < MAX_REQUESTS)) {
66 :     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 :     =head3 GetIdList
107 :    
108 : parrello 1.19 my $ids = ServerThing::GetIdList($name => $args, $optional);
109 : parrello 1.9
110 :     Get a named list of IDs from an argument structure. If the IDs are
111 :     missing, or are not a list, an error will occur.
112 :    
113 :     =over 4
114 :    
115 :     =item name
116 :    
117 :     Name of the argument structure member that should contain the ID list.
118 :    
119 :     =item args
120 :    
121 :     Argument structure from which the ID list is to be extracted.
122 :    
123 : parrello 1.19 =item optional (optional)
124 :    
125 :     If TRUE, then a missing value will not generate an error. Instead, an empty list
126 :     will be returned. The default is FALSE.
127 :    
128 : parrello 1.9 =item RETURN
129 :    
130 :     Returns a reference to a list of IDs taken from the argument structure.
131 :    
132 :     =back
133 :    
134 :     =cut
135 :    
136 :     sub GetIdList {
137 :     # Get the parameters.
138 : parrello 1.19 my ($name, $args, $optional) = @_;
139 : parrello 1.9 # Try to get the IDs from the argument structure.
140 :     my $retVal = $args->{$name};
141 : parrello 1.19 # Was a member found?
142 :     if (! defined $retVal) {
143 :     # No. If we're optional, return an empty list; otherwise throw an error.
144 :     if ($optional) {
145 :     $retVal = [];
146 :     } else {
147 :     Confess("No '$name' parameter found.");
148 :     }
149 :     } else {
150 :     # Here we found something. Get the parameter type. We was a list reference.
151 :     # If it's a scalar, we'll convert it to a singleton list. If it's anything
152 :     # else, it's an error.
153 :     my $type = ref $retVal;
154 :     if (! $type) {
155 :     $retVal = [$retVal];
156 :     } elsif ($type ne 'ARRAY') {
157 :     Confess("The '$name' parameter must be a list.");
158 :     }
159 : parrello 1.9 }
160 :     # Return the result.
161 :     return $retVal;
162 :     }
163 :    
164 :    
165 :     =head3 RunTool
166 :    
167 :     ServerThing::RunTool($name => $cmd);
168 :    
169 :     Run a command-line tool. A non-zero return value from the tool will cause
170 :     a fatal error, and the tool's error log will be traced.
171 :    
172 :     =over 4
173 :    
174 :     =item name
175 :    
176 :     Name to give to the tool in the error output.
177 :    
178 :     =item cmd
179 :    
180 :     Command to use for running the tool. This should be the complete command line.
181 :     The command should not contain any fancy piping, though it may redirect the
182 :     standard input and output. The command will be modified by this method to
183 :     redirect the error output to a temporary file.
184 :    
185 :     =back
186 :    
187 :     =cut
188 :    
189 :     sub RunTool {
190 :     # Get the parameters.
191 :     my ($name, $cmd) = @_;
192 :     # Compute the log file name.
193 :     my $errorLog = "$FIG_Config::temp/errors$$.log";
194 :     # Execute the command.
195 :     Trace("Executing command: $cmd") if T(3);
196 :     my $res = system("$cmd 2> $errorLog");
197 :     Trace("Return from $name tool is $res.") if T(3);
198 :     # Check the result code.
199 :     if ($res != 0) {
200 :     # We have an error. If tracing is on, trace it.
201 :     if (T(1)) {
202 :     TraceErrorLog($name, $errorLog);
203 :     }
204 :     # Delete the error log.
205 :     unlink $errorLog;
206 :     # Confess the error.
207 : parrello 1.10 Confess("$name command failed with error code $res.");
208 : parrello 1.9 } else {
209 :     # Everything worked. Trace the error log if necessary.
210 :     if (T(3) && -s $errorLog) {
211 :     TraceErrorLog($name, $errorLog);
212 :     }
213 :     # Delete the error log if there is one.
214 :     unlink $errorLog;
215 :     }
216 :     }
217 :    
218 :    
219 :     =head2 Internal Utility Methods
220 :    
221 :     The methods in this section are used internally by this package.
222 :    
223 : parrello 1.6 =head3 RunRequest
224 :    
225 : parrello 1.9 ServerThing::RunRequest($cgi, $serverName);
226 : parrello 1.6
227 :     Run a request from the specified server using the incoming CGI parameter
228 :     object for the parameters.
229 :    
230 :     =over 4
231 :    
232 :     =item cgi
233 :    
234 :     CGI query object containing the parameters from the web service request.
235 :    
236 : parrello 1.13 =item serverThing
237 : parrello 1.6
238 : parrello 1.13 Server object against which to run the request.
239 : parrello 1.6
240 :     =back
241 :    
242 :     =cut
243 :    
244 :     sub RunRequest {
245 :     # Get the parameters.
246 : parrello 1.13 my ($cgi, $serverThing, $docURL) = @_;
247 : parrello 1.9 # Determine the request type.
248 :     if ($cgi->param('pod')) {
249 : parrello 1.4 # Here we have a documentation request. In this case, we produce POD HTML.
250 : parrello 1.9 ProducePod($cgi->param('pod'));
251 :     } elsif ($cgi->param('file')) {
252 :     # Here we have a file request. Process according to the type.
253 :     my $type = $cgi->param('file');
254 :     if ($type eq 'open') {
255 :     OpenFile($cgi->param('name'));
256 :     } elsif ($type eq 'create') {
257 :     CreateFile();
258 :     } elsif ($type eq 'read') {
259 :     ReadChunk($cgi->param('name'), $cgi->param('location'), $cgi->param('size'));
260 :     } elsif ($type eq 'write') {
261 :     WriteChunk($cgi->param('name'), $cgi->param('data'));
262 :     } else {
263 :     Die("Invalid file function \"$type\".");
264 : parrello 1.4 }
265 : parrello 1.1 } else {
266 : parrello 1.9 # The default is a function request. Get the function name.
267 : parrello 1.4 my $function = $cgi->param('function') || "";
268 : parrello 1.15 Trace("Server function for task $$ is $function.") if T(3);
269 : parrello 1.4 # Insure the function name is valid.
270 :     Die("Invalid function name.")
271 :     if $function =~ /\W/;
272 :     # The parameter structure will go in here.
273 :     my $args;
274 :     # Start the timer.
275 :     my $start = time();
276 :     # The output document goes in here.
277 :     my $document;
278 :     # The sapling database goes in here.
279 :     my $sapling;
280 :     # Protect from errors.
281 : parrello 1.1 eval {
282 : parrello 1.4 # Parse the arguments.
283 :     $args = YAML::Load($cgi->param('args'));
284 : parrello 1.1 };
285 : parrello 1.4 # Check to make sure we got everything.
286 : parrello 1.1 if ($@) {
287 : parrello 1.10 SendError($@, "Error formatting parameters.");
288 : parrello 1.4 } elsif (! $function) {
289 : parrello 1.10 SendError("No function specified.", "No function specified.");
290 : parrello 1.1 } else {
291 : parrello 1.13 $document = eval("\$serverThing->$function(\$args)");
292 : parrello 1.1 # If we have an error, create an error document.
293 :     if ($@) {
294 : parrello 1.13 SendError($@, "Error detected by service.");
295 :     Trace("Error encountered by service: $@") if T(2);
296 : parrello 1.1 } else {
297 : parrello 1.13 # No error, so we output the result.
298 :     print $cgi->header(-type => 'text/plain');
299 : parrello 1.18 my $string = YAML::Dump($document);
300 :     print $string;
301 :     MemTrace(length($string) . " bytes returned from $function by task $$.") if T(Memory => 3);
302 : parrello 1.1 }
303 :     }
304 : parrello 1.4 # Stop the timer.
305 :     my $duration = int(time() - $start + 0.5);
306 : parrello 1.15 Trace("Function executed in $duration seconds by task $$.") if T(2);
307 : parrello 1.1 }
308 :     }
309 :    
310 : parrello 1.9 =head3 CreateFile
311 :    
312 :     ServerThing::CreateFile();
313 :    
314 :     Create a new, empty temporary file and send its name back to the client.
315 :    
316 :     =cut
317 :    
318 :     sub CreateFile {
319 :     ##TODO: Code
320 :     }
321 :    
322 :     =head3 OpenFile
323 : parrello 1.6
324 : parrello 1.9 ServerThing::OpenFile($name);
325 : parrello 1.1
326 : parrello 1.9 Send the length of the named file back to the client.
327 :    
328 :     =over 4
329 :    
330 :     =item name
331 :    
332 :     ##TODO: name description
333 :    
334 :     =back
335 :    
336 :     =cut
337 :    
338 :     sub OpenFile {
339 :     # Get the parameters.
340 :     my ($name) = @_;
341 :     ##TODO: Code
342 :     }
343 : parrello 1.1
344 : parrello 1.9 =head3 ReadChunk
345 : parrello 1.1
346 : parrello 1.9 ServerThing::ReadChunk($name, $location, $size);
347 : parrello 1.1
348 : parrello 1.9 Read the indicated number of bytes from the specified location of the
349 :     named file and send them back to the client.
350 : parrello 1.1
351 :     =over 4
352 :    
353 :     =item name
354 :    
355 : parrello 1.9 ##TODO: name description
356 : parrello 1.1
357 : parrello 1.9 =item location
358 : parrello 1.1
359 : parrello 1.9 ##TODO: location description
360 : parrello 1.1
361 : parrello 1.9 =item size
362 : parrello 1.1
363 : parrello 1.9 ##TODO: size description
364 : parrello 1.1
365 :     =back
366 :    
367 :     =cut
368 :    
369 : parrello 1.9 sub ReadChunk {
370 : parrello 1.1 # Get the parameters.
371 : parrello 1.9 my ($name, $location, $size) = @_;
372 :     ##TODO: Code
373 : parrello 1.1 }
374 :    
375 : parrello 1.9 =head3 WriteChunk
376 : parrello 1.1
377 : parrello 1.9 ServerThing::WriteChunk($name, $data);
378 : parrello 1.8
379 : parrello 1.9 Write the specified data to the named file.
380 : parrello 1.8
381 :     =over 4
382 :    
383 :     =item name
384 :    
385 : parrello 1.9 ##TODO: name description
386 :    
387 :     =item data
388 :    
389 :     ##TODO: data description
390 :    
391 :     =back
392 :    
393 :     =cut
394 :    
395 :     sub WriteChunk {
396 :     # Get the parameters.
397 :     my ($name, $data) = @_;
398 :     ##TODO: Code
399 :     }
400 :    
401 :    
402 :     =head3 ProducePod
403 :    
404 :     ServerThing::ProducePod($module);
405 :    
406 :     Output the POD documentation for the specified module.
407 :    
408 :     =over 4
409 : parrello 1.8
410 : parrello 1.9 =item module
411 : parrello 1.8
412 : parrello 1.9 Name of the module whose POD document is to be displayed.
413 : parrello 1.8
414 :     =back
415 :    
416 :     =cut
417 :    
418 : parrello 1.9 sub ProducePod {
419 : parrello 1.8 # Get the parameters.
420 : parrello 1.9 my ($module) = @_;
421 :     # Start the output page.
422 :     print CGI::header();
423 :     print CGI::start_html(-title => 'Documentation Page',
424 :     -style => { src => "http://servers.nmpdr.org/sapling/Html/css/ERDB.css" });
425 :     # Protect from errors.
426 :     eval {
427 :     # We'll format the HTML text in here.
428 :     require DocUtils;
429 :     my $html = DocUtils::ShowPod($module, "http://servers.nmpdr.org/sapling/servers.cgi?pod=");
430 :     # Output the POD HTML.
431 :     print $html;
432 :     };
433 :     # Process any error.
434 :     if ($@) {
435 :     print CGI::blockquote({ class => 'error' }, $@);
436 : parrello 1.8 }
437 : parrello 1.9 # Close off the page.
438 :     print CGI::end_html();
439 :    
440 : parrello 1.8 }
441 :    
442 :     =head3 TraceErrorLog
443 :    
444 :     ServerThing::TraceErrorLog($name, $errorLog);
445 :    
446 :     Trace the specified error log file. This is a very dinky routine that
447 :     performs a task required by L</RunTool> in multiple places.
448 :    
449 :     =over 4
450 :    
451 :     =item name
452 :    
453 :     Name of the tool relevant to the log file.
454 :    
455 :     =item errorLog
456 :    
457 :     Name of the log file.
458 :    
459 :     =back
460 :    
461 :     =cut
462 :    
463 :     sub TraceErrorLog {
464 :     my ($name, $errorLog) = @_;
465 :     my $errorData = Tracer::GetFile($errorLog);
466 :     Trace("$name error log:\n$errorData");
467 :     }
468 :    
469 : parrello 1.10 =head3 SendError
470 :    
471 :     ServerThing::SendError($message, $status);
472 :    
473 :     Fail an HTTP request with the specified error message and the specified
474 :     status message.
475 :    
476 :     =over 4
477 :    
478 :     =item message
479 :    
480 :     Detailed error message. This is sent as the page content.
481 :    
482 :     =item status
483 :    
484 :     Status message. This is sent as part of the status code.
485 :    
486 :     =back
487 :    
488 :     =cut
489 :    
490 :     sub SendError {
491 :     # Get the parameters.
492 :     my ($message, $status) = @_;
493 :     Trace("Error \"$status\" $message") if T(2);
494 :     # Print the header and the status message.
495 :     print CGI::header(-type => 'text/plain',
496 :     -status => "500 $status");
497 :     # Print the detailed message.
498 :     print $message;
499 :     }
500 :    
501 :    
502 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3