[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.6, Wed Jun 24 16:24:24 2009 UTC revision 1.12, Thu Nov 5 20:59:01 2009 UTC
# Line 1  Line 1 
   
1  package SSserver;  package SSserver;
2    
3  #  #
# Line 6  Line 5 
5  #  #
6    
7  use LWP::UserAgent;  use LWP::UserAgent;
 use Data::Dumper;  
8  use YAML;  use YAML;
9    use ErrorMessage;
10    no warnings qw(once);
11    
12  use strict;  use strict;
13    
14  sub new  =head1 Subsystem Server Helper Object
15  {  
16      my($class, $server_url) = @_;  =head2 Description
17    
18    This module is used to call the Subsystem Server, which is a special-purpose
19    server for manipulating subsystem data from the Sapling database. Each Subsystem
20    Server function corresponds to a method of this object.
21    
22    This package deliberately uses no internal SEED packages or scripts, only common
23    PERL modules.
24    
25    The fields in this object are as follows.
26    
27    =over 4
28    
29    =item server_url
30    
31    The URL used to request data from the subsystem server.
32    
33    =item ua
34    
35    The user agent for communication with the server.
36    
37    =item singleton
38    
39    Indicates whether or not results are to be returned in singleton mode. In
40    singleton mode, if the return document is a hash reference with only one
41    entry, the entry value is returned rather than the hash.
42    
43    =back
44    
45    =cut
46    
47    =head3 new
48    
49      $server_url = "http://servers.nmpdr.org/subsystem/server.cgi" unless $server_url;      my $ss = SSserver->new(%options);
 #    $server_url = "http://bio-macpro-2.mcs.anl.gov/~disz/FIG/subsystem_server_sapling.cgi" unless $server_url;  
 #    $server_url = "http://bio-big.mcs.anl.gov/server/FIG/subsystem_server_sapling.cgi" unless $server_url;  
50    
51      #$server_url = "http://bio-macpro-1.mcs.anl.gov/ross-proj/FIG/co_occurs_server.cgi" unless $server_url;  Construct a new server object. The
52    following options are supported.
53    
54      my $self = {  =over 4
55    
56    =item url
57    
58    URL for the server. This option is required.
59    
60    =item singleton (optional)
61    
62    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
64    as a hash value accessible via an incoming ID.
65    
66    =back
67    
68    =cut
69    
70    sub new {
71        # Get the parameters.
72        my ($class, %options) = @_;
73        # Turn off YAML compression, which causes problems with our hash keys.
74        $YAML::CompressSeries = 0;
75        # Get the options.
76        my $url = $options{url} || "http://servers.nmpdr.org/subsystem/server.cgi";
77        my $singleton = $options{singleton} || 0;
78        # 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;
81        my $ua;
82        if ($server_url ne 'localhost') {
83            require LWP::UserAgent;
84            $ua = LWP::UserAgent->new();
85        } else {
86            require "SS.pm";
87            $ua = SS->new();
88        }
89        # Create the server object.
90        my $retVal = {
91          server_url => $server_url,          server_url => $server_url,
92          ua => LWP::UserAgent->new(),                      ua => $ua,
93                        singleton => $singleton,
94      };      };
95      $self->{ua}->timeout(30*60);      # Bless it.
96      return bless $self, $class;      bless $retVal, $class;
97        # Get the list of permitted methods from the server.
98        my $methodList = $retVal->_call_method(methods => []);
99        # Convert it to a hash and store it in this object.
100        $retVal->{methodHash} = { map { $_ => 1 } @$methodList };
101        # Return the object.
102        return $retVal;
103  }  }
104    
105  sub is_in_subsystem {  =head3 AUTOLOAD
106          my ($self, @args) = @_;  
107          return $self->run_query("is_in_subsystem", @args);      my $result = $server->method(%args);
108    
109    Call a function on the server. Any method call on this object (other than
110    the constructor) is translated into a request against the server. This
111    enables us to add new server functions without requiring an update to this
112    object or its parent. The parameters are usually specified as a hash, and the
113    result is a scalar or object reference. In some cases the parameters are a list.
114    To deistinguish between the two cases, all hash keys must begin with hyphens.
115    
116    If an error occurs, we will throw an exception.
117    
118    =cut
119    
120    # This variable will contain the method name.
121    our $AUTOLOAD;
122    
123    sub AUTOLOAD {
124        # Get the parameters. We do some fancy dancing to allow the user to pass
125        # in a hash, a list, a list reference, or a hash reference.
126        my $self = shift @_;
127        my $args = $_[0];
128        if (defined $args) {
129            if (scalar @_ gt 1) {
130                # Here we have multiple arguments. We check the first one for a
131                # leading hyphen.
132                if ($args =~ /^-/) {
133                    # This means we have hash-form parameters.
134                    my %args = @_;
135                    $args = \%args;
136                } else {
137                    # This means we have list-form parameters.
138                    my @args = @_;
139                    $args = \@args;
140  }  }
141  sub is_in_subsystem_with {          } else {
142          my ($self, @args) = @_;              # Here we have a single argument. If it's a scalar, we convert it
143          return $self->run_query("is_in_subsystem_with", @args);              # to a singleton list.
144                if (! ref $args) {
145                    $args = [$args];
146  }  }
   
 sub all_subsystems {  
         my ($self, @args) = @_;  
         return $self->run_query("all_subsystems", @args);  
147  }  }
148        }
149  sub subsystem_spreadsheet {      # Declare the return variable.
150          my ($self, @args) = @_;      my $retVal;
151          return $self->run_query("subsystem_spreadsheet", @args);      # Get the method name.
152        my $function = $AUTOLOAD;
153        # Strip off the stuff before the method name.
154        $function =~ s/.+:://;
155        # Validate the method name.
156        if (! $self->{methodHash}{$function}) {
157            die "Method \"$function\" not supported.";
158        } else {
159            # Call the method.
160            $retVal = $self->_call_method($function, $args);
161            # We have our result. Adjust for singleton mode.
162            if ($self->{singleton} && ref $retVal eq 'HASH' && scalar(keys %$retVal) <= 1) {
163                # Here we're in singleton mode and we got a single result,
164                # so we dereference a bit to make it easier for the user
165                # to access it.
166                ($retVal) = values %$retVal;
167            }
168        }
169        # Return the result.
170        return $retVal;
171  }  }
172    
173    =head3 DESTROY
174    
175  sub metabolic_reconstruction {      $ss->DESTROY();
176          my ($self, @args) = @_;  
177          return $self->run_query("metabolic_reconstruction", @args);  This method has no function. It's purpose is to keep the destructor from
178  }  being caught by the autoload processing.
179    
180    =cut
181    
182    sub DESTROY { }
183    
184    =head2 Utility Methods
185    
186    =head3 _call_method
187    
188        my $result = $server->_call_method($method, $args);
189    
190    Call the specified method on the server with the specified arguments and
191    return the result. The arguments must already be packaged as a hash or
192    list reference. This method is the heart of the AUTOLOAD method, and is
193    provided as a utility for specialized methods that can't use the AUTOLOAD
194    facility.
195    
196    =over 4
197    
198    =item method
199    
200    Name of the server function being invoked.
201    
202    =item args
203    
204    Argument object to pass to the function.
205    
206  sub run_query  =item RETURN
 {  
     my($self, $function, @args ) = @_;  
207    
208              my $arg_string = Dump(@args);  Returns a hash or list reference with the function results.
             my $form = [function => $function,  
                         args => "$arg_string"];  
209    
210              my $res = $self->{ua}->post($self->{server_url}, $form);  =back
211              if ($res->is_success)  
212              {  =cut
213                  return Load($res->content);  
214    sub _call_method {
215        # Get the parameters.
216        my ($self, $method, $args) = @_;
217        # Declare the return variable.
218        my $retVal;
219        # Get our user agent.
220        my $ua = $self->{ua};
221        # Determine the type.
222        if (ref $ua eq 'LWP::UserAgent') {
223            # Here we're going to a server. Compute the argument document.
224            my $argString = YAML::Dump($args);
225            # Request the function from the server.
226            my $content = $self->_send_request(function => $method, args => $argString,
227                                               source => __PACKAGE__);
228            $retVal = YAML::Load($content);
229        } else {
230            # Here we're calling a local method.
231            $retVal = eval("\$ua->$method(\$args)");
232            # Check for an error.
233            if ($@) {
234                die "Package error: $@";
235              }              }
             else  
             {  
                 die "error on post " . $res->content;  
236              }              }
237        # Return the result.
238        return $retVal;
239  }  }
240    
241    =head3 _send_request
242    
243  1;      my $result = $server->_send_request(%parms);
244    
245    Send a request to the server. This method must not be called in localhost
246    mode. If an error occurs, this method will die; otherwise, the content of
247    the response will be passed back as the result.
248    
249    =over 4
250    
251    =item parms
252    
253    Hash of CGI parameters to send to the server.
254    
255    =item RETURN
256    
257    Returns the string returned by the server in response to the request.
258    
259    =back
260    
261    =cut
262    
263    sub _send_request {
264        # Get the parameters.
265        my ($self, %parms) = @_;
266        # Get the user agent.
267        my $ua = $self->{ua};
268        # Request the function from the server. Note that the hash is actually passed
269        # as a list reference.
270        my $response = $ua->post($self->{server_url}, [ %parms ]);
271        # Get the response content.
272        my $retVal = $response->content;
273        # Fail if there was an error.
274        if (! $response->is_success) {
275            die ErrorMessage->new($retVal, $response->status_line);
276        }
277        # Return the result.
278        return $retVal;
279    }
280    
281    
282    1;

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.12

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3