File Coverage

blib/lib/RPC/Simple/Agent.pm
Criterion Covered Total %
statement 51 62 82.2
branch 3 6 50.0
condition n/a
subroutine 10 11 90.9
pod 6 7 85.7
total 70 86 81.4


line stmt bran cond sub pod time code
1             package RPC::Simple::Agent;
2              
3 1     1   5 use strict;
  1         2  
  1         31  
4 1     1   4 use vars qw($VERSION);
  1         1  
  1         31  
5              
6 1     1   528 use RPC::Simple::Factory ;
  1         3  
  1         758  
7              
8             # Items to export into callers namespace by default. Note: do not export
9             # names by default without a very good reason. Use EXPORT_OK instead.
10             # Do not simply export all your public functions/methods/constants.
11              
12             ( $VERSION ) = '$Revision: 1.7 $ ' =~ /\$Revision:\s+([^\s]+)/;
13              
14             # Preloaded methods go here.
15              
16             # connection is opened, ask for a remote object
17             sub new
18             {
19 1     1 1 9 my $type = shift ;
20 1         3 my $factoryObj = shift ; # factory
21 1         63 my $clientRef = shift;
22 1         2 my $index = shift ;
23 1         2 my $remoteClass = shift ; # may be undef
24              
25 1         3 my $self={} ;
26 1         85 $self->{'idx'} = $index ;
27            
28 1         13 $self->{requestId} = 0 ;
29            
30 1         8 $self->{'factory'} = $factoryObj ;
31 1         6 $self->{remoteHostName} = $factoryObj->getRemoteHostName() ;
32              
33 1 50       4 unless (defined $remoteClass)
34             {
35 0         0 $remoteClass = ref($clientRef) ;
36 0         0 $remoteClass =~ s/(\w+)$/Real$1/ ;
37             }
38            
39 1         12 print "Creating $type for $remoteClass\n";
40            
41 1         2 $self->{'clientObj'}= $clientRef ;
42              
43             # store call-back info
44 1     1   20 $self->{callback}{$self->{requestId}} = sub{ $self->remoteCreated(@_)} ;
  1         7  
45 1         3 my $id = $self->{requestId}++ ;
46            
47 1         12 $factoryObj->writeSockBuffer($index, 'new', $id , [ @_ ], $remoteClass ) ;
48            
49 1         3 $self->{remoteClass} = $remoteClass ;
50 1         6 bless $self, $type ;
51             }
52              
53             sub destroy
54             {
55 0     0 0 0 my $self = shift ;
56 0         0 print "RPC::Simple::Agent destroyed\n";
57 0         0 $self->{factory}->destroyRemoteObject($self->{'idx'});
58              
59             # We need to undef the factory and clientObj references
60             # because they create a circular reference and we can not
61             # destroy the factory or the clientObj
62 0         0 undef $self->{factory};
63 0         0 undef $self->{clientObj};
64             }
65              
66             sub delegate
67             {
68             # delegate to remote
69 1     1 1 2 my $self = shift ;
70 1         3 my $method = shift ;
71 1         2 my $id ;
72            
73 1 50       5 if (ref($_[0]) eq 'CODE')
74             {
75             # callback required
76 0         0 $self->{callback}{$self->{requestId}} = shift ; # store call-back info
77 0         0 $id = $self->{requestId}++ ;
78             }
79            
80 1         13 $self->{'factory'}->writeSockBuffer($self->{'idx'},$method, $id,[ @_]) ;
81             }
82              
83             sub remoteCreated
84             {
85 1     1 1 2 my $self = shift ;
86 1         3 my $result = shift ;
87 1         2 my $failStr = shift ;
88              
89 1 50       4 if ($result)
90             {
91 1         303 print "Remote class $self->{remoteClass} created\n";
92 1         5 return ;
93             }
94            
95 0         0 print "Failed to create remote class $self->{remoteClass}\n",$failStr;
96 0         0 undef $self->{clientObj} ;
97             }
98              
99             sub callMethod
100             {
101 1     1 1 2 my $self = shift ;
102 1         3 my $method = shift ;
103 1         2 my $args = shift ;
104 1         7 $self->{clientObj} -> $method (@$args) ;
105             }
106              
107             sub getRemoteHostName
108             {
109 1     1 1 3 my $self = shift ;
110 1         12 return $self->{'remoteHostName'} ;
111             }
112              
113             sub treatCallBack
114             {
115 1     1 1 4 my $self = shift ;
116 1         3 my $reqId = shift ;
117 1         2 my $args = shift ;
118              
119 1         5 my $cbRef = $self->{callback}{$reqId} ;
120              
121 1         5 &$cbRef(@$args);
122            
123 1         9 delete $self->{callback}{$reqId} ;
124             }
125              
126              
127             # Autoload methods go after =cut, and are processed by the autosplit program.
128              
129             1;
130              
131             __END__