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

Annotation of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3