blib/lib/UR/Service/RPC/Executer.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 123 CC 14 SCAR 39.7
CC
14
Cov
0%
execute
110.0
create
30.0
authenticate
2.0
CRAP 52.8
1 package UR::Service::RPC::Executer;
2  
3 use UR;
4  
5 use strict;
6 use warnings;
7 our $VERSION = "0.47"; # UR $VERSION;
8  
9 class UR::Service::RPC::Executer {
10 has => [
11 fh => { is => 'IO::Handle', doc => 'handle we will send and receive messages across' },
12 ],
13 has_optional => [
14 use_sigio => { is => 'Boolean', default_value => 0 },
15 ],
16 is_transactional => 0,
17 };
18  
19  
20 sub create {
21 my $class = shift;
22  
23 my $obj = $class->SUPER::create(@_);
24 return unless $obj;
25  
26 if ($obj->use_sigio) {
27 UR::Service::RPC->enable_sigio_processing($obj);
28 }
29  
30 $obj->create_subscription(method => 'use_sigio',
31 callback => sub {
32 my ($changed_object, $changed_property, $old_value, $new_value) = @_;
33 return 1 if ($old_value == $new_value);
34  
35 if ($new_value) {
36 UR::Service::RPC->enable_sigio_processing($obj);
37 } else {
38 UR::Service::RPC->disable_sigio_processing($obj);
39 }
40 });
41 return $obj;
42 }
43  
44  
45 # sub classes can override this
46 # If they're going to reject the request, $msg should be modified in place
47 # with a return value and exception, because we're going to return it right back
48 # to the requester
49 sub authenticate {
50 # my($self,$msg) = @_;
51 return 1;
52 }
53  
54 # Process one message off of the file handle
55 sub execute {
56 my $self = shift;
57
58 my $msg = UR::Service::RPC::Message->recv($self->fh);
59 unless ($msg) {
60 # The other end probably closed the socket
61 $self->close_connection();
62 return 1;
63 }
64  
65 my $response;
66  
67 if ($self->authenticate($msg)) {
68  
69 my $target_class = $msg->target_class || ref($self);
70 my $method = $msg->method_name;
71 my @arglist = $msg->param_list;
72 my $wantarray = $msg->wantarray;
73 my %resp_msg_args = ( target_class => $target_class,
74 method_name => $method,
75 params => \@arglist,
76 'wantarray' => $wantarray,
77 fh => $self->fh );
78  
79  
80 my $method_name = join('::',$target_class, $method);
81 if (! $target_class->can($method)) {
82 $resp_msg_args{exception} =
83 qq(Can't locate object method "$method" via package "$target_class" (perhaps you forgot to load "$target_class"?));
84  
85 } else {
86 local $@;
87 if ($wantarray) {
88 my @retval;
89 eval { no strict 'refs'; @retval = &{$method_name}(@arglist); };
90 $resp_msg_args{return_values} = \@retval unless ($@);
91 } elsif (defined $wantarray) {
92 my $retval;
93 eval { no strict 'refs'; no warnings; $retval = &{$method_name}(@arglist); };
94 $resp_msg_args{return_values} = [$retval] unless ($@);
95 } else {
96 eval { no strict 'refs'; &{$method_name}(@arglist); };
97 }
98 $resp_msg_args{exception} = $@ if $@;
99 }
100 $response = UR::Service::RPC::Message->create(%resp_msg_args);
101  
102 } else {
103 # didn't authenticate.
104 $response = $msg;
105 }
106  
107 unless ($response->send()) {
108 $self->fh->close();
109 }
110  
111 return 1;
112 }
113  
114  
115  
116 sub close_connection {
117 my $self = shift;
118
119 $self->use_sigio(0);
120  
121 $self->fh->close();
122 }
123  
124
125 1;
126  
127  
128 =pod
129  
130 =head1 NAME
131  
132 UR::Service::RPC::Executer - Base class for modules implementing RPC executers
133  
134 =head1 DESCRIPTION
135  
136 This class is an abstract base class used to implement RPC executers. That
137 is, modules meant to have their methods called from another process, and have
138 the results passed back to the original caller. The communication happens
139 over a read-write filehandle such as a socket by passing L<UR::Service::RPC::Message>
140 objects back and forth.
141  
142 Executors are subordinate to a L<UR::Service::RPC::Server> object which
143 handles decoding the message passed over the socket, calling the method
144 on the correct executor in the right context, and returning the result
145 back through the file handle.
146  
147 =head1 PROPERTIES
148  
149 =over 4
150  
151 =item fh => IO::Handle
152  
153 File handle messages are received on and responses are sent to
154  
155 =item use_sigio => Boolean
156  
157 If true, the Server will set up a callback on the IO signal to handle
158 execution, so the Server does not need to block in loop().
159  
160 =back
161  
162 =head1 METHODS
163  
164 =over 4
165  
166 =item authenticate
167  
168 $bool = $exec->authenticate($msg);
169  
170 This is called by execute() after the message object is deserialized from the
171 filehandle. The default implementation just returns true. Subclasses can
172 override this to examine the UR::Service::RPC::Message object and return
173 true or fale whether it should allow or disallow execution. If authentication
174 fails, the Executor should modify the Message object in-place with a proper
175 return value and exception.
176  
177 =item execute
178  
179 $exec->execute();
180  
181 Called when the Server detects data is available to read on its file handle.
182 It deserializes the message and calls authenticate. If authentication fails,
183 it immediately passes the message object back to the caller.
184  
185 If authentication succeeds, it calls the appropriate method in the Executor
186 package, and creates a new Message object with the return value to pass back
187 to the caller.
188  
189 =item close_connection
190  
191 $exec->close_connection();
192  
193 Called by execute() when it detects that the file handle has closed.
194  
195 =back
196  
197 Derived classes should define additional methods that then become callable
198 by execute().
199  
200 =head1 SEE ALSO
201  
202 UR::Service::RPC::Server, UR::Service::RPC::Message
203  
204 =cut
205