blib/lib/Event/RPC/Client.pm untested

filter: Statementstmt 0.0% 0 / 237 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 53 Podpod 0.0% 0 / 43 totaltotal 0.0% 0 / 435 CC 32 SCAR 39.7
CC
32
Cov
0%
load_class
240.0
connect
72.0
send_request
20.0
CRAP 53.0
1 # $Id: Client.pm,v 1.14 2008/06/21 12:47:59 joern Exp $
2  
3 #-----------------------------------------------------------------------
4 # Copyright (C) 2002-2006 Jörn Reder <joern AT zyn.de>.
5 # All Rights Reserved. See file COPYRIGHT for details.
6 #
7 # This module is part of Event::RPC, which is free software; you can
8 # redistribute it and/or modify it under the same terms as Perl itself.
9 #-----------------------------------------------------------------------
10  
11 package Event::RPC::Client;
12  
13 use Event::RPC;
14 use Event::RPC::Message;
15  
16 use Carp;
17 use strict;
18 use IO::Socket::INET;
19  
20 sub get_client_version { $Event::RPC::VERSION }
21 sub get_client_protocol { $Event::RPC::PROTOCOL }
22  
23 sub get_host { shift->{host} }
24 sub get_port { shift->{port} }
25 sub get_sock { shift->{sock} }
26 sub get_timeout { shift->{timeout} }
27 sub get_classes { shift->{classes} }
28 sub get_class_map { shift->{class_map} }
29 sub get_loaded_classes { shift->{loaded_classes} }
30 sub get_error_cb { shift->{error_cb} }
31 sub get_ssl { shift->{ssl} }
32 sub get_auth_user { shift->{auth_user} }
33 sub get_auth_pass { shift->{auth_pass} }
34 sub get_connected { shift->{connected} }
35 sub get_server { shift->{server} }
36 sub get_server_version { shift->{server_version} }
37 sub get_server_protocol { shift->{server_protocol} }
38  
39 sub set_host { shift->{host} = $_[1] }
40 sub set_port { shift->{port} = $_[1] }
41 sub set_sock { shift->{sock} = $_[1] }
42 sub set_timeout { shift->{timeout} = $_[1] }
43 sub set_classes { shift->{classes} = $_[1] }
44 sub set_class_map { shift->{class_map} = $_[1] }
45 sub set_loaded_classes { shift->{loaded_classes} = $_[1] }
46 sub set_error_cb { shift->{error_cb} = $_[1] }
47 sub set_ssl { shift->{ssl} = $_[1] }
48 sub set_auth_user { shift->{auth_user} = $_[1] }
49 sub set_auth_pass { shift->{auth_pass} = $_[1] }
50 sub set_connected { shift->{connected} = $_[1] }
51 sub set_server { shift->{server} = $_[1] }
52 sub set_server_version { shift->{server_version} = $_[1] }
53 sub set_server_protocol { shift->{server_protocol} = $_[1] }
54  
55 sub new {
56 my $class = shift;
57 my %par = @_;
58 my ($server, $host, $port, $classes, $class_map, $error_cb, $timeout) =
59 @par{'server','host','port','classes','class_map','error_cb','timeout'};
60 my ($ssl, $auth_user, $auth_pass) =
61 @par{'ssl','auth_user','auth_pass'};
62  
63 $server ||= '';
64 $host ||= '';
65  
66 if ( $server ne '' and $host eq '' ) {
67 warn "Option 'server' is deprecated. Use 'host' instead.";
68 $host = $server;
69 }
70  
71 my $self = bless {
72 host => $server,
73 server => $host,
74 port => $port,
75 timeout => $timeout,
76 classes => $classes,
77 class_map => $class_map,
78 ssl => $ssl,
79 auth_user => $auth_user,
80 auth_pass => $auth_pass,
81 error_cb => $error_cb,
82 loaded_classes => {},
83 connected => 0,
84 }, $class;
85  
86 return $self;
87 }
88  
89 sub connect {
90 my $self = shift;
91  
92 croak "Client is already connected" if $self->get_connected;
93  
94 my $ssl = $self->get_ssl;
95 my $server = $self->get_server;
96 my $port = $self->get_port;
97 my $timeout = $self->get_timeout;
98  
99 if ($ssl) {
100 eval { require IO::Socket::SSL };
101 croak "SSL requested, but IO::Socket::SSL not installed" if $@;
102 }
103  
104 my $sock;
105 if ($ssl) {
106 $sock = IO::Socket::SSL->new(
107 Proto => 'tcp',
108 PeerPort => $port,
109 PeerAddr => $server,
110 Type => SOCK_STREAM,
111 Timeout => $timeout,
112 )
113 or croak "Can't open SSL connection to $server:$port: $IO::Socket::SSL::ERROR";
114 }
115 else {
116 $sock = IO::Socket::INET->new(
117 Proto => 'tcp',
118 PeerPort => $port,
119 PeerAddr => $server,
120 Type => SOCK_STREAM,
121 Timeout => $timeout,
122 )
123 or croak "Can't open connection to $server:$port - $!";
124 }
125  
126 $sock->autoflush(1);
127  
128 $self->set_sock($sock);
129  
130 $self->check_version;
131  
132 my $auth_user = $self->get_auth_user;
133 my $auth_pass = $self->get_auth_pass;
134  
135 if ($auth_user) {
136 my $rc = $self->send_request(
137 { cmd => 'auth',
138 user => $auth_user,
139 pass => $auth_pass,
140 }
141 );
142 if ( not $rc->{ok} ) {
143 $self->disconnect;
144 croak $rc->{msg};
145 }
146 }
147  
148 if ( not $self->get_classes ) {
149 $self->load_all_classes;
150 }
151 else {
152 $self->load_classes;
153 }
154  
155 $self->set_connected(1);
156  
157 1;
158 }
159  
160 sub log_connect {
161 my $class = shift;
162 my %par = @_;
163 my ( $server, $port ) = @par{ 'server', 'port' };
164  
165 my $sock = IO::Socket::INET->new(
166 Proto => 'tcp',
167 PeerPort => $port,
168 PeerAddr => $server,
169 Type => SOCK_STREAM
170 )
171 or croak "Can't open connection to $server:$port - $!";
172  
173 return $sock;
174 }
175  
176 sub disconnect {
177 my $self = shift;
178  
179 close( $self->get_sock ) if $self->get_sock;
180 $self->set_connected(0);
181  
182 1;
183 }
184  
185 sub DESTROY {
186 shift->disconnect;
187 }
188  
189 sub error {
190 my $self = shift;
191 my ($message) = @_;
192  
193 my $error_cb = $self->get_error_cb;
194  
195 if ($error_cb) {
196 &$error_cb( $self, $message );
197 }
198 else {
199 die "Unhandled error in client/server communication: $message";
200 }
201  
202 1;
203 }
204  
205 sub check_version {
206 my $self = shift;
207  
208 my $rc = $self->send_request( { cmd => 'version', } );
209  
210 $self->set_server_version( $rc->{version} );
211 $self->set_server_protocol( $rc->{protocol} );
212  
213 if ( $rc->{version} ne $self->get_client_version ) {
214 warn "Event::RPC warning: server version $rc->{version} != "
215 . "client version "
216 . $self->get_client_version;
217 }
218  
219 if ( $rc->{protocol} < $self->get_client_protocol ) {
220 die "FATAL: Server protocol version $rc->{protocol} < "
221 . "client protocol version "
222 . $self->get_client_protocol;
223 }
224  
225 1;
226 }
227  
228 sub load_all_classes {
229 my $self = shift;
230  
231 my $rc = $self->send_request( { cmd => 'class_info_all', } );
232  
233 my $class_info_all = $rc->{class_info_all};
234  
235 foreach my $class ( keys %{$class_info_all} ) {
236 $self->load_class( $class, $class_info_all->{$class} );
237 }
238  
239 1;
240 }
241  
242 sub load_classes {
243 my $self = shift;
244  
245 my $classes = $self->get_classes;
246 my %classes;
247 @classes{ @{$classes} } = (1) x @{$classes};
248  
249 my $rc = $self->send_request( { cmd => 'classes_list', } );
250  
251 foreach my $class ( @{ $rc->{classes} } ) {
252 next if not $classes{$class};
253 $classes{$class} = 0;
254  
255 my $rc = $self->send_request(
256 { cmd => 'class_info',
257 class => $class,
258 }
259 );
260  
261 $self->load_class( $class, $rc->{methods} );
262 }
263  
264 foreach my $class ( @{$classes} ) {
265 warn "WARNING: Class '$class' not exported by server"
266 if $classes{$class};
267 }
268  
269 1;
270 }
271  
272 sub load_class {
273 my $self = shift;
274 my ( $class, $methods ) = @_;
275  
276 my $loaded_classes = $self->get_loaded_classes;
277 return 1 if $loaded_classes->{$class};
278 $loaded_classes->{$class} = 1;
279  
280 my $local_method;
281 my $class_map = $self->get_class_map;
282 my $local_class = $class_map->{$class} || $class;
283  
284 # create local destructor for this class
285 {
286 no strict 'refs';
287 my $local_method = $local_class . '::' . "DESTROY";
288 *$local_method = sub {
289 return if not $self->get_connected;
290 my $oid_ref = shift;
291 $self->send_request({
292 cmd => "client_destroy",
293 oid => ${$oid_ref},
294 });
295 };
296 }
297  
298 # create local methods for this class
299 foreach my $method ( keys %{$methods} ) {
300 $local_method = $local_class . '::' . $method;
301  
302 my $method_type = $methods->{$method};
303  
304 if ( $method_type eq '_constructor' ) {
305 # this is a constructor for this class
306 my $request_method = $class . '::' . $method;
307 no strict 'refs';
308 *$local_method = sub {
309 shift;
310 my $rc = $self->send_request({
311 cmd => 'new',
312 method => $request_method,
313 params => \@_,
314 });
315 my $oid = $rc->{oid};
316 return bless \$oid, $local_class;
317 };
318 }
319 elsif ( $method_type eq '1' ) {
320 # this is a simple method
321 my $request_method = $method;
322 no strict 'refs';
323 *$local_method = sub {
324 my $oid_ref = shift;
325 my $rc = $self->send_request({
326 cmd => 'exec',
327 oid => ${$oid_ref},
328 method => $request_method,
329 params => \@_,
330 });
331 return unless $rc;
332 $rc = $rc->{rc};
333 return @{$rc} if wantarray;
334 return $rc->[0];
335 };
336 }
337 else {
338 # this is a object returner
339 my $request_method = $method;
340 no strict 'refs';
341 *$local_method = sub {
342 my $oid_ref = shift;
343 my $rc = $self->send_request({
344 cmd => 'exec',
345 oid => ${$oid_ref},
346 method => $request_method,
347 params => \@_,
348 });
349 return unless $rc;
350 $rc = $rc->{rc};
351  
352 foreach my $val ( @{$rc} ) {
353 if ( ref $val eq 'ARRAY' ) {
354 foreach my $list_elem ( @{$val} ) {
355 my ($class) = split( "=", "$list_elem", 2 );
356 $self->load_class($class)
357 unless $loaded_classes->{$class};
358 my $list_elem_copy = $list_elem;
359 $list_elem = \$list_elem_copy;
360 bless $list_elem,
361 ( $class_map->{$class} || $class );
362 }
363 }
364 elsif ( ref $val eq 'HASH' ) {
365 foreach my $hash_elem ( values %{$val} ) {
366 my ($class) = split( "=", "$hash_elem", 2 );
367 $self->load_class($class)
368 unless $loaded_classes->{$class};
369 my $hash_elem_copy = $hash_elem;
370 $hash_elem = \$hash_elem_copy;
371 bless $hash_elem,
372 ( $class_map->{$class} || $class );
373 }
374 }
375 elsif ( defined $val ) {
376 my ($class) = split( "=", "$val", 2 );
377 $self->load_class($class)
378 unless $loaded_classes->{$class};
379 my $val_copy = $val;
380 $val = \$val_copy;
381 bless $val, ( $class_map->{$class} || $class );
382 }
383 }
384 return @{$rc} if wantarray;
385 return $rc->[0];
386 };
387 }
388 }
389  
390 return $local_class;
391 }
392  
393 sub send_request {
394 my $self = shift;
395 my ($request) = @_;
396  
397 my $message = Event::RPC::Message->new( $self->get_sock );
398  
399 $message->write_blocked($request);
400  
401 my $rc = eval { $message->read_blocked };
402  
403 if ($@) {
404 $self->error($@);
405 return;
406 }
407  
408 if ( not $rc->{ok} ) {
409 $rc->{msg} .= "\n" if not $rc->{msg} =~ /\n$/;
410 croak ("$rc->{msg} -- called via Event::RPC::Client");
411 }
412  
413 return $rc;
414 }
415  
416 1;
417  
418 __END__