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

Diff of /FigKernelPackages/SSserver.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.10, Thu Aug 27 19:46:36 2009 UTC revision 1.13, Fri Nov 6 22:54:13 2009 UTC
# Line 6  Line 6 
6    
7  use LWP::UserAgent;  use LWP::UserAgent;
8  use YAML;  use YAML;
9    use ErrorMessage;
10    no warnings qw(once);
11    
12  use strict;  use strict;
13    
# Line 46  Line 48 
48    
49      my $ss = SSserver->new(%options);      my $ss = SSserver->new(%options);
50    
51  Construct a new SSserver object. The following options are supported.  Construct a new server object. The
52    following options are supported.
53    
54  =over 4  =over 4
55    
56  =item url  =item url
57    
58  URL for the subsystem server. This option may be used to redirect requests to a  URL for the server. This option is required.
 test version of the server, or to an older server script.  
59    
60  =item singleton  =item singleton (optional)
61    
62  If TRUE, results from methods will be returned in singleton mode. In singleton  If TRUE, results from methods will be returned in singleton mode. In singleton
63  mode, if a single result comes back, it will come back as a scalar rather than  mode, if a single result comes back, it will come back as a scalar rather than
# Line 68  Line 70 
70  sub new {  sub new {
71      # Get the parameters.      # Get the parameters.
72      my ($class, %options) = @_;      my ($class, %options) = @_;
73        # Turn off YAML compression, which causes problems with our hash keys.
74        $YAML::CompressSeries = 0;
75      # Get the options.      # Get the options.
76      my $url = $options{url} || "http://servers.nmpdr.org/subsystem/server.cgi";      my $url = $options{url} || "http://servers.nmpdr.org/subsystem/server.cgi";
77      my $singleton = $options{singleton} || 0;      my $singleton = $options{singleton} || 0;
78      # Create the fields of the object.      # Create the fields of the object. Note that if we're in localhost mode,
79        # the user agent is actually a SAP object.
80      my $server_url = $url;      my $server_url = $url;
81      my $ua = LWP::UserAgent->new();      my $ua;
82      # Create the SAPserver object.      if ($server_url ne 'localhost') {
83            require LWP::UserAgent;
84            $ua = LWP::UserAgent->new();
85            $ua->timeout(20 * 60);
86        } else {
87            require "SS.pm";
88            $ua = SS->new();
89        }
90        # Create the server object.
91      my $retVal = {      my $retVal = {
92                      server_url => $server_url,                      server_url => $server_url,
93                      ua => $ua,                      ua => $ua,
94                      singleton => $singleton,                      singleton => $singleton,
95                   };                   };
96      # Bless and return it.      # Bless it.
97      bless $retVal, $class;      bless $retVal, $class;
98        # Get the list of permitted methods from the server.
99        my $methodList = $retVal->_call_method(methods => []);
100        # Convert it to a hash and store it in this object.
101        $retVal->{methodHash} = { map { $_ => 1 } @$methodList };
102        # Return the object.
103      return $retVal;      return $retVal;
104  }  }
105    
 =head2 Public Methods  
   
 All L<SS/Primary Methods> are also methods of this object.  
   
106  =head3 AUTOLOAD  =head3 AUTOLOAD
107    
108      my $result = $ss->method(%args);      my $result = $server->method(%args);
109    
110  Call a function on the server. Any method call on this object (other than  Call a function on the server. Any method call on this object (other than
111  the constructor) is translated into a request against the server. This  the constructor) is translated into a request against the server. This
112  enables us to add new server functions without requiring an update to this  enables us to add new server functions without requiring an update to this
113  module. The parameters are specified as a hash, and the result is a scalar  object or its parent. The parameters are usually specified as a hash, and the
114  or object reference. If an error occurred, we will throw an exception.  result is a scalar or object reference. In some cases the parameters are a list.
115    To deistinguish between the two cases, all hash keys must begin with hyphens.
116    
117    If an error occurs, we will throw an exception.
118    
119  =cut  =cut
120    
# Line 106  Line 123 
123    
124  sub AUTOLOAD {  sub AUTOLOAD {
125      # Get the parameters. We do some fancy dancing to allow the user to pass      # Get the parameters. We do some fancy dancing to allow the user to pass
126      # in a hash, a list, or a hash reference.      # in a hash, a list, a list reference, or a hash reference.
127      my $self = shift @_;      my $self = shift @_;
128      my $args = $_[0];      my $args = $_[0];
129      if (defined $args) {      if (defined $args) {
# Line 136  Line 153 
153      my $function = $AUTOLOAD;      my $function = $AUTOLOAD;
154      # Strip off the stuff before the method name.      # Strip off the stuff before the method name.
155      $function =~ s/.+:://;      $function =~ s/.+:://;
156      # Compute the argument document.      # Validate the method name.
157      my $argString = YAML::Dump($args);      if (! $self->{methodHash}{$function}) {
158      # Get our user agent.          die "Method \"$function\" not supported.";
     my $ua = $self->{ua};  
     # Request the function from the server.  
     my $response = $ua->post($self->{server_url},  
                              [function => $function, args => $argString,  
                               source => __PACKAGE__ ]);  
     # Get the response content.  
     my $content = $response->content;  
     if (! $response->is_success) {  
         die "Server error " . $response->status_line . "\n$content";  
159      } else {      } else {
160          $retVal = YAML::Load($content);          # Call the method.
161          # Figure out what we got back.          $retVal = $self->_call_method($function, $args);
162          my $returnType = ref $retVal;          # We have our result. Adjust for singleton mode.
163          if ($returnType) {          if ($self->{singleton} && ref $retVal eq 'HASH' && scalar(keys %$retVal) <= 1) {
             if ($returnType eq 'ErrorDocument') {  
                 # Here an error occurred, so we throw an exception using the  
                 # error message.  
                 die $retVal->{message};  
             } elsif ($self->{singleton} && $returnType eq 'HASH' &&  
                      scalar(keys %$retVal) <= 1) {  
164                  # Here we're in singleton mode and we got a single result,                  # Here we're in singleton mode and we got a single result,
165                  # so we dereference a bit to make it easier for the user                  # so we dereference a bit to make it easier for the user
166                  # to access it.                  # to access it.
167                  ($retVal) = values %$retVal;                  ($retVal) = values %$retVal;
168              }              }
169          }          }
     }  
170      # Return the result.      # Return the result.
171      return $retVal;      return $retVal;
172  }  }
# Line 181  Line 182 
182    
183  sub DESTROY { }  sub DESTROY { }
184    
185    =head2 Utility Methods
186    
187    =head3 _call_method
188    
189        my $result = $server->_call_method($method, $args);
190    
191    Call the specified method on the server with the specified arguments and
192    return the result. The arguments must already be packaged as a hash or
193    list reference. This method is the heart of the AUTOLOAD method, and is
194    provided as a utility for specialized methods that can't use the AUTOLOAD
195    facility.
196    
197    =over 4
198    
199    =item method
200    
201    Name of the server function being invoked.
202    
203    =item args
204    
205    Argument object to pass to the function.
206    
207    =item RETURN
208    
209    Returns a hash or list reference with the function results.
210    
211    =back
212    
213    =cut
214    
215    sub _call_method {
216        # Get the parameters.
217        my ($self, $method, $args) = @_;
218        # Declare the return variable.
219        my $retVal;
220        # Get our user agent.
221        my $ua = $self->{ua};
222        # Determine the type.
223        if (ref $ua eq 'LWP::UserAgent') {
224            # Here we're going to a server. Compute the argument document.
225            my $argString = YAML::Dump($args);
226            # Request the function from the server.
227            my $content = $self->_send_request(function => $method, args => $argString,
228                                               source => __PACKAGE__);
229            $retVal = YAML::Load($content);
230        } else {
231            # Here we're calling a local method.
232            $retVal = eval("\$ua->$method(\$args)");
233            # Check for an error.
234            if ($@) {
235                die "Package error: $@";
236            }
237        }
238        # Return the result.
239        return $retVal;
240    }
241    
242    =head3 _send_request
243    
244        my $result = $server->_send_request(%parms);
245    
246    Send a request to the server. This method must not be called in localhost
247    mode. If an error occurs, this method will die; otherwise, the content of
248    the response will be passed back as the result.
249    
250    =over 4
251    
252    =item parms
253    
254    Hash of CGI parameters to send to the server.
255    
256    =item RETURN
257    
258    Returns the string returned by the server in response to the request.
259    
260    =back
261    
262    =cut
263    
264    sub _send_request {
265        # Get the parameters.
266        my ($self, %parms) = @_;
267        # Get the user agent.
268        my $ua = $self->{ua};
269        # Request the function from the server. Note that the hash is actually passed
270        # as a list reference.
271        my $response = $ua->post($self->{server_url}, [ %parms ]);
272        # Get the response content.
273        my $retVal = $response->content;
274        # Fail if there was an error.
275        if (! $response->is_success) {
276            die ErrorMessage->new($retVal, $response->status_line);
277        }
278        # Return the result.
279        return $retVal;
280    }
281    
282    
283  1;  1;

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.13

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3