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

Annotation of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (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 : parrello 1.4 use DocUtils;
11 : parrello 1.1 use Time::HiRes;
12 :     use ErrorDocument;
13 :     use CGI;
14 :    
15 :     =head1 General Server Helper
16 :    
17 :     This package provides a method-- I<RunServer>-- that can be called from a CGI
18 : parrello 1.5 script to perform the duties of a FIG server. RunServer is called with three
19 :     parameters: the name of the server package (e.g. C<SAP> for B<SAP.pm>),
20 :     the first command-line parameter, and the URL prefix to use for the documentation.
21 :     The command-line parameter (if defined) will be used as the tracing key, and is
22 :     used to indicate that the script is being invoked from the command line rather
23 :     than over the web.
24 : parrello 1.1
25 :     =cut
26 :    
27 :     sub RunServer {
28 :     # Get the parameters.
29 : parrello 1.5 my ($serverName, $key, $docURL) = @_;
30 : parrello 1.1 # Get the CGI parameters.
31 :     my $cgi;
32 :     if (! $key) {
33 :     # No tracing key, so presume we're a web service.
34 :     $cgi = CGI->new();
35 : parrello 1.3 # Check for a source parameter. This gets used as the tracing key.
36 :     $key = $cgi->param('source');
37 :     if (! $key) {
38 :     # No source parameter, so do normal setup.
39 :     ETracing($cgi);
40 :     } else {
41 :     # Set up tracing using the specified key.
42 :     ETracing($key);
43 :     # Trace the CGI parameters.
44 :     Tracer::TraceParms($cgi);
45 :     }
46 : parrello 1.1 } else {
47 :     # We're being invoked from the command line. Use the tracing
48 :     # key to find the parm file and create the CGI object from that.
49 :     my $ih = Open(undef, "<$FIG_Config::temp/$key.parms");
50 :     $cgi = CGI->new($ih);
51 :     # Set up tracing using the specified key.
52 :     ETracing($key);
53 :     # Trace the CGI parameters.
54 :     Tracer::TraceParms($cgi);
55 :     }
56 :     Trace("Running $serverName server request.") if T(3);
57 : parrello 1.4 # Is this a documentation request?
58 :     my $module = $cgi->param('pod');
59 :     if ($module) {
60 :     # Here we have a documentation request. In this case, we produce POD HTML.
61 :     # Start the output page.
62 :     print CGI::header();
63 :     print CGI::start_html(-title => 'Documentation Page',
64 :     -style => { src => "$FIG_Config::cgi_url/Html/css/ERDB.css" });
65 :     # Protect from errors.
66 :     eval {
67 :     # We'll format the HTML text in here.
68 : parrello 1.5 my $html = DocUtils::ShowPod($module, $docURL);
69 : parrello 1.4 # Output the POD HTML.
70 :     print $html;
71 :     };
72 :     # Process any error.
73 :     if ($@) {
74 :     print CGI::blockquote({ class => 'error' }, $@);
75 :     }
76 :     # Close off the page.
77 :     print CGI::end_html();
78 : parrello 1.1 } else {
79 : parrello 1.4 # Here we have a function request. Get the function name.
80 :     my $function = $cgi->param('function') || "";
81 :     Trace("Server function is $function.") if T(3);
82 :     # Insure the function name is valid.
83 :     Die("Invalid function name.")
84 :     if $function =~ /\W/;
85 :     # The parameter structure will go in here.
86 :     my $args;
87 :     # Start the timer.
88 :     my $start = time();
89 :     # The output document goes in here.
90 :     my $document;
91 :     # The sapling database goes in here.
92 :     my $sapling;
93 :     # Protect from errors.
94 : parrello 1.1 eval {
95 : parrello 1.4 # Parse the arguments.
96 :     $args = YAML::Load($cgi->param('args'));
97 : parrello 1.1 };
98 : parrello 1.4 # Check to make sure we got everything.
99 : parrello 1.1 if ($@) {
100 : parrello 1.4 $document = ErrorDocument->new('<initialization>', $@);
101 :     } elsif (! $function) {
102 :     $document = ErrorDocument->new('<missing>', "No function specified.");
103 : parrello 1.1 } else {
104 : parrello 1.4 # We're okay, so load the server function object.
105 :     Trace("Requiring $serverName") if T(3);
106 :     eval {
107 :     require "$serverName.pm";
108 :     };
109 : parrello 1.1 # If we have an error, create an error document.
110 :     if ($@) {
111 :     $document = ErrorDocument->new($function, $@);
112 : parrello 1.4 Trace("Error loadin server module: $@") if T(2);
113 : parrello 1.1 } else {
114 : parrello 1.4 # Having successfully loaded the server code, we create the object.
115 :     my $serverThing = eval("$serverName" . '->new()');
116 : parrello 1.1 # If we have an error, create an error document.
117 :     if ($@) {
118 :     $document = ErrorDocument->new($function, $@);
119 : parrello 1.4 Trace("Error creating server function object: $@") if T(2);
120 :     } else {
121 :     # No error, so execute the server method.
122 :     Trace("Executing $function.") if T(2);
123 :     $document = eval("\$serverThing->$function(\$args)");
124 :     # If we have an error, create an error document.
125 :     if ($@) {
126 :     $document = ErrorDocument->new($function, $@);
127 :     Trace("Error encountered by service: $@") if T(2);
128 :     }
129 : parrello 1.1 }
130 :     }
131 :     }
132 : parrello 1.4 # Stop the timer.
133 :     my $duration = int(time() - $start + 0.5);
134 :     Trace("Function executed in $duration seconds.") if T(2);
135 :     # Output the YAML.
136 :     print $cgi->header(-type => 'text/plain');
137 :     print YAML::Dump($document);
138 : parrello 1.1 }
139 :     }
140 :    
141 :     =head2 Utility Methods
142 :    
143 :     The methods in this section are utilities of general use to the various
144 :     server modules.
145 :    
146 :     =head3 GetIdList
147 :    
148 :     my $ids = ServerThing::GetIdList($name => $args);
149 :    
150 :     Get a named list of IDs from an argument structure. If the IDs are
151 :     missing, or are not a list, an error will occur.
152 :    
153 :     =over 4
154 :    
155 :     =item name
156 :    
157 :     Name of the argument structure member that should contain the ID list.
158 :    
159 :     =item args
160 :    
161 :     Argument structure from which the ID list is to be extracted.
162 :    
163 :     =item RETURN
164 :    
165 :     Returns a reference to a list of IDs taken from the argument structure.
166 :    
167 :     =back
168 :    
169 :     =cut
170 :    
171 :     sub GetIdList {
172 :     # Get the parameters.
173 :     my ($name, $args) = @_;
174 :     # Try to get the IDs from the argument structure.
175 :     my $retVal = $args->{$name};
176 :     # Throw an error if no member was found.
177 :     Confess("No '$name' parameter found.") if ! defined $retVal;
178 :     # Get the parameter type. We was a list reference. If it's a scalar, we'll
179 :     # convert it to a singleton list. If it's anything else, it's an error.
180 :     my $type = ref $retVal;
181 :     if (! $type) {
182 :     $retVal = [$retVal];
183 :     } elsif ($type ne 'ARRAY') {
184 :     Confess("The '$name' parameter must be a list.");
185 :     }
186 :     # Return the result.
187 :     return $retVal;
188 :     }
189 :    
190 :    
191 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3