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

Annotation of /FigKernelPackages/ServerThing.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (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 : parrello 1.6 if (! defined $key) {
33 :     # No tracing key, so presume we're a web service. Check for Fast CGI.
34 :     if ($ENV{REQUEST_METHOD} eq '') {
35 :     # Here we're doing Fast CGI. In this case, the tracing key is the
36 :     # server name.
37 :     ETracing($serverName);
38 :     # Loop through the fast CGI requests.
39 :     require CGI::Fast;
40 :     while ($cgi = new CGI::Fast()) {
41 :     RunRequest($cgi, $serverName, $docURL);
42 :     }
43 : parrello 1.3 } else {
44 : parrello 1.6 # Here we have a normal web service (non-Fast).
45 :     my $cgi = CGI->new();
46 :     # Check for a source parameter. This gets used as the tracing key.
47 :     $key = $cgi->param('source');
48 :     if (! $key) {
49 :     # No source parameter, so do normal setup. Note we turn off
50 :     # CGI parameter tracing.
51 :     ETracing($cgi, 'noParms');
52 :     } else {
53 :     # Set up tracing using the specified key.
54 :     ETracing($key);
55 :     }
56 :     # Run this request.
57 :     RunRequest($cgi, $serverName, $docURL);
58 : parrello 1.3 }
59 : parrello 1.1 } else {
60 :     # We're being invoked from the command line. Use the tracing
61 :     # key to find the parm file and create the CGI object from that.
62 :     my $ih = Open(undef, "<$FIG_Config::temp/$key.parms");
63 :     $cgi = CGI->new($ih);
64 :     # Set up tracing using the specified key.
65 :     ETracing($key);
66 : parrello 1.6 # Run this request.
67 :     RunRequest($cgi, $serverName, $docURL);
68 : parrello 1.1 }
69 : parrello 1.6 }
70 :    
71 :    
72 :     =head3 RunRequest
73 :    
74 :     ServerThing::RunRequest($cgi, $serverName, $docURL);
75 :    
76 :     Run a request from the specified server using the incoming CGI parameter
77 :     object for the parameters.
78 :    
79 :     =over 4
80 :    
81 :     =item cgi
82 :    
83 :     CGI query object containing the parameters from the web service request.
84 :    
85 :     =item serverName
86 :    
87 :     Name of the server to be used for running the request.
88 :    
89 :     =item docURL
90 :    
91 :     URL to be used for a documentation request.
92 :    
93 :     =back
94 :    
95 :     =cut
96 :    
97 :     sub RunRequest {
98 :     # Get the parameters.
99 :     my ($cgi, $serverName, $docURL) = @_;
100 : parrello 1.1 Trace("Running $serverName server request.") if T(3);
101 : parrello 1.4 # Is this a documentation request?
102 :     my $module = $cgi->param('pod');
103 :     if ($module) {
104 :     # Here we have a documentation request. In this case, we produce POD HTML.
105 :     # Start the output page.
106 :     print CGI::header();
107 :     print CGI::start_html(-title => 'Documentation Page',
108 : parrello 1.7 -style => { src => "http://servers.nmpdr.org/sapling/Html/css/ERDB.css" });
109 : parrello 1.4 # Protect from errors.
110 :     eval {
111 :     # We'll format the HTML text in here.
112 : parrello 1.5 my $html = DocUtils::ShowPod($module, $docURL);
113 : parrello 1.4 # Output the POD HTML.
114 :     print $html;
115 :     };
116 :     # Process any error.
117 :     if ($@) {
118 :     print CGI::blockquote({ class => 'error' }, $@);
119 :     }
120 :     # Close off the page.
121 :     print CGI::end_html();
122 : parrello 1.1 } else {
123 : parrello 1.4 # Here we have a function request. Get the function name.
124 :     my $function = $cgi->param('function') || "";
125 :     Trace("Server function is $function.") if T(3);
126 :     # Insure the function name is valid.
127 :     Die("Invalid function name.")
128 :     if $function =~ /\W/;
129 :     # The parameter structure will go in here.
130 :     my $args;
131 :     # Start the timer.
132 :     my $start = time();
133 :     # The output document goes in here.
134 :     my $document;
135 :     # The sapling database goes in here.
136 :     my $sapling;
137 :     # Protect from errors.
138 : parrello 1.1 eval {
139 : parrello 1.4 # Parse the arguments.
140 :     $args = YAML::Load($cgi->param('args'));
141 : parrello 1.1 };
142 : parrello 1.4 # Check to make sure we got everything.
143 : parrello 1.1 if ($@) {
144 : parrello 1.4 $document = ErrorDocument->new('<initialization>', $@);
145 :     } elsif (! $function) {
146 :     $document = ErrorDocument->new('<missing>', "No function specified.");
147 : parrello 1.1 } else {
148 : parrello 1.4 # We're okay, so load the server function object.
149 :     Trace("Requiring $serverName") if T(3);
150 :     eval {
151 :     require "$serverName.pm";
152 :     };
153 : parrello 1.1 # If we have an error, create an error document.
154 :     if ($@) {
155 :     $document = ErrorDocument->new($function, $@);
156 : parrello 1.6 Trace("Error loading server module: $@") if T(2);
157 : parrello 1.1 } else {
158 : parrello 1.4 # Having successfully loaded the server code, we create the object.
159 :     my $serverThing = eval("$serverName" . '->new()');
160 : parrello 1.1 # If we have an error, create an error document.
161 :     if ($@) {
162 :     $document = ErrorDocument->new($function, $@);
163 : parrello 1.4 Trace("Error creating server function object: $@") if T(2);
164 :     } else {
165 :     # No error, so execute the server method.
166 :     Trace("Executing $function.") if T(2);
167 :     $document = eval("\$serverThing->$function(\$args)");
168 :     # If we have an error, create an error document.
169 :     if ($@) {
170 :     $document = ErrorDocument->new($function, $@);
171 :     Trace("Error encountered by service: $@") if T(2);
172 :     }
173 : parrello 1.1 }
174 :     }
175 :     }
176 : parrello 1.4 # Stop the timer.
177 :     my $duration = int(time() - $start + 0.5);
178 :     Trace("Function executed in $duration seconds.") if T(2);
179 :     # Output the YAML.
180 :     print $cgi->header(-type => 'text/plain');
181 :     print YAML::Dump($document);
182 : parrello 1.1 }
183 :     }
184 :    
185 : parrello 1.6
186 : parrello 1.1 =head2 Utility Methods
187 :    
188 :     The methods in this section are utilities of general use to the various
189 :     server modules.
190 :    
191 :     =head3 GetIdList
192 :    
193 :     my $ids = ServerThing::GetIdList($name => $args);
194 :    
195 :     Get a named list of IDs from an argument structure. If the IDs are
196 :     missing, or are not a list, an error will occur.
197 :    
198 :     =over 4
199 :    
200 :     =item name
201 :    
202 :     Name of the argument structure member that should contain the ID list.
203 :    
204 :     =item args
205 :    
206 :     Argument structure from which the ID list is to be extracted.
207 :    
208 :     =item RETURN
209 :    
210 :     Returns a reference to a list of IDs taken from the argument structure.
211 :    
212 :     =back
213 :    
214 :     =cut
215 :    
216 :     sub GetIdList {
217 :     # Get the parameters.
218 :     my ($name, $args) = @_;
219 :     # Try to get the IDs from the argument structure.
220 :     my $retVal = $args->{$name};
221 :     # Throw an error if no member was found.
222 :     Confess("No '$name' parameter found.") if ! defined $retVal;
223 :     # Get the parameter type. We was a list reference. If it's a scalar, we'll
224 :     # convert it to a singleton list. If it's anything else, it's an error.
225 :     my $type = ref $retVal;
226 :     if (! $type) {
227 :     $retVal = [$retVal];
228 :     } elsif ($type ne 'ARRAY') {
229 :     Confess("The '$name' parameter must be a list.");
230 :     }
231 :     # Return the result.
232 :     return $retVal;
233 :     }
234 :    
235 :    
236 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3