line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package RMI; |
2
|
|
|
|
|
|
|
|
3
|
24
|
|
|
24
|
|
144
|
use strict; |
|
24
|
|
|
|
|
1127
|
|
|
24
|
|
|
|
|
959
|
|
4
|
24
|
|
|
24
|
|
3083
|
use warnings; |
|
24
|
|
|
|
|
1170
|
|
|
24
|
|
|
|
|
1164
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# the whole base set of classes which make general RMI work |
8
|
|
|
|
|
|
|
# (sub-classes of RMI Server & Client provide specific implementations such as sockets, etc.) |
9
|
24
|
|
|
24
|
|
139
|
use RMI::Node; |
|
24
|
|
|
|
|
46
|
|
|
24
|
|
|
|
|
576
|
|
10
|
24
|
|
|
24
|
|
8310
|
use RMI::Client; |
|
24
|
|
|
|
|
52
|
|
|
24
|
|
|
|
|
584
|
|
11
|
24
|
|
|
24
|
|
16088
|
use RMI::Server; |
|
24
|
|
|
|
|
67
|
|
|
24
|
|
|
|
|
605
|
|
12
|
24
|
|
|
24
|
|
18988
|
use RMI::ProxyObject; |
|
24
|
|
|
|
|
69
|
|
|
24
|
|
|
|
|
750
|
|
13
|
24
|
|
|
24
|
|
16589
|
use RMI::ProxyReference; |
|
24
|
|
|
|
|
64
|
|
|
24
|
|
|
|
|
1475
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our @executing_nodes; # required for some methods on the remote side to find the RMI node acting upon them |
16
|
|
|
|
|
|
|
our %proxied_classes; # tracks classes which have been fully proxied into this process by some client |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# turn on debug messages if an environment variable is set |
19
|
|
|
|
|
|
|
our $DEBUG; |
20
|
24
|
|
|
24
|
|
2390
|
BEGIN { $RMI::DEBUG = $ENV{RMI_DEBUG}; }; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# this is used at the beginning of each debug message |
23
|
|
|
|
|
|
|
# setting it to a single space for a server makes server/client distinction |
24
|
|
|
|
|
|
|
# more readable in combined output. |
25
|
|
|
|
|
|
|
our $DEBUG_MSG_PREFIX = ''; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=pod |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 NAME |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
RMI - Remote Method Invocation with transparent proxies |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 VERSION |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
This document describes RMI v0.10. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head1 SYNOPSIS |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
#process 1: an example server on host "myserver" |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
use RMI::Server::Tcp; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $s = RMI::Server::Tcp->new(port => 1234); |
44
|
|
|
|
|
|
|
$s->run; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
#process 2: an example client |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
use RMI::Client::Tcp; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $c = RMI::Client::Tcp->new( |
52
|
|
|
|
|
|
|
host => 'myserver', |
53
|
|
|
|
|
|
|
port => 1234, |
54
|
|
|
|
|
|
|
); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$c->call_use('IO::File'); |
57
|
|
|
|
|
|
|
$r = $c->call_class_method('IO::File','new','/etc/passwd'); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
$line1 = $r->getline; # works as an object |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
$line2 = <$r>; # works as a file handle |
62
|
|
|
|
|
|
|
@rest = <$r>; # detects scalar/list context correctly |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
$r->isa('IO::File'); # transparent in standard ways |
65
|
|
|
|
|
|
|
$r->can('getline'); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
ref($r) eq 'RMI::ProxyObject'; # the only sign this isn't a real IO::File... |
68
|
|
|
|
|
|
|
# (see RMI::Client's use_remote() to fix this) |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 DESCRIPTION |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
RMI stands for Remote Method Invocation. The RMI modules allow one process |
73
|
|
|
|
|
|
|
to have virtual object "stubs" which are proxies for real objects |
74
|
|
|
|
|
|
|
in another process. When methods are invoked on the proxy, the method |
75
|
|
|
|
|
|
|
actually runs in the other process. When results are returned, those |
76
|
|
|
|
|
|
|
values may also be proxies for the real items in the other process. Parameters |
77
|
|
|
|
|
|
|
from the client are also automatically proxied on the server side during method |
78
|
|
|
|
|
|
|
execution. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
In addition to invoking methods on proxy objects trasparently, an RMI::Client |
81
|
|
|
|
|
|
|
can invoke class methods, regular function calls, and other Perl functionaity |
82
|
|
|
|
|
|
|
on the remote server. Calls like these are typically the first step to obtain |
83
|
|
|
|
|
|
|
a remote object in the first place. This is different than implementations |
84
|
|
|
|
|
|
|
in other languages, which typically require that a server have limited and |
85
|
|
|
|
|
|
|
specific objects it returns, with all further proxying happening through them. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
The procedure typically goes as follows: |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
1. a server is started which has access to some objects or data which is of value |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
2. a client connects to that server, and asks that it execute code on its behalf |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
3. the results returned may contain objects or other references, |
94
|
|
|
|
|
|
|
which the client recieves as proxies which "seem like" the real thing |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
4. further interaction with the returned proxy objects/refs automatically |
97
|
|
|
|
|
|
|
make calls through the client to the server internally |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 METHODS |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
The RMI module has no public methods of its own. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
See B and B for detailed API information and examples. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
See B and B for details on behavior of |
106
|
|
|
|
|
|
|
proxies. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
See B for internals and details on the wire protocol. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 PROXY OBJECTS AND REFERENCES |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
A proxy object is an object on one "side" of an RMI connection which represents |
113
|
|
|
|
|
|
|
an object which really exists on the other side. When an RMI::Client calls a |
114
|
|
|
|
|
|
|
method on its associated RMI::Server, and that method returns a reference of any |
115
|
|
|
|
|
|
|
kind, a proxy is made on the client side, rather than a copy. The proxy object |
116
|
|
|
|
|
|
|
appears to be a reference to the real object, but internally it engages in |
117
|
|
|
|
|
|
|
messaging across the client to the server for all method calls, dereferencing, |
118
|
|
|
|
|
|
|
etc. It contains no actual data, and implements no actual methods calls. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
By the same token, when a client passes objects or other references to the |
121
|
|
|
|
|
|
|
server as parameters to a method call, the server generates a proxy for those |
122
|
|
|
|
|
|
|
objects, so that the remote method call may "call back" the client for detailed |
123
|
|
|
|
|
|
|
access to the objects it passed. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
The choice to proxy by default rather than generate a copy on the remote side by |
126
|
|
|
|
|
|
|
default is distinct from some remoting systems. It is, of course, possible to |
127
|
|
|
|
|
|
|
explicitly ask the server to serialize a given object, but because a serialized |
128
|
|
|
|
|
|
|
object may not behave the same way when it has lost its environment, this is not |
129
|
|
|
|
|
|
|
the default behavior. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Proxied objects are only revealed as such by a call to ref(), which reveals the |
132
|
|
|
|
|
|
|
object is actually an RMI::ProxyObject. Calls to isa() and can() are proxied |
133
|
|
|
|
|
|
|
across the connection to the remote side, and will maintain the correct API. |
134
|
|
|
|
|
|
|
Remote objects which implement AUTOLOAD for their API will still work correctly. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Plain proxied references, and as well as objects, are "tied" so as to |
137
|
|
|
|
|
|
|
operate as the correct type of Perl primitive. SCALAR, ARRAY, HASH, CODE and |
138
|
|
|
|
|
|
|
GLOB/IO references, blessed or otherwise, will be proxied as the same type of |
139
|
|
|
|
|
|
|
reference on the other side. The RMI system uses Perl's "tie" functionality to |
140
|
|
|
|
|
|
|
do this. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 GARBAGE COLLECTION |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Until a proxy is destroyed, the side which sent the item will keep an |
145
|
|
|
|
|
|
|
additional reference to the real item, both to facilitate proxying, and to |
146
|
|
|
|
|
|
|
prevent garbage collection. Upon destruction on the reciever side, a message is |
147
|
|
|
|
|
|
|
sent to the sender to expire its link to the item in question, and allow garbage |
148
|
|
|
|
|
|
|
collection if no other references exist. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 DEBUGGING RMI CODE |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
When the RMI_DEBUG environment variable set to 1, the RMI modules will emit |
153
|
|
|
|
|
|
|
detailed information to STDERR during all "conversations" between itself and the |
154
|
|
|
|
|
|
|
remote side. This works for RMI::Client, RMI::Server, and anything else which |
155
|
|
|
|
|
|
|
inherits from RMI::Node. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
This value is available inside the application as $RMI::DEBUG. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
The package variable $RMI::DEBUG_MSG_PREFIX will be printed at the beginning of |
160
|
|
|
|
|
|
|
each message. Changing this value allows the viewer to separate both halves of |
161
|
|
|
|
|
|
|
a conversation. (The test suite for RMI sets this value to ' ' for the server |
162
|
|
|
|
|
|
|
side, causing server activity to be indented relative to client activity in |
163
|
|
|
|
|
|
|
the debug output.) |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
RMI_DEBUG=1 perl -I lib t/01_*.t |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 SECURITY |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 no inherent security is built-in |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
If you require restrctions on what the server provides, a custom sub-class |
172
|
|
|
|
|
|
|
should be written around the server to restrict the types of calls it will |
173
|
|
|
|
|
|
|
receive. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
This is wise whenever the server is exposed to an untrusted network. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 LIMITS TO TRANSPARENCY |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head2 calls to ref($my_proxy) reveal the true class RMI::ProxyObject |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Proxied objects/references reveal that they are proxies when ref($o) is |
182
|
|
|
|
|
|
|
called on them, unless the entire package is proxied with ->use_remote. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Calls to ->isa() still operate as though the proxy were the object it |
185
|
|
|
|
|
|
|
represents. Code which goes around the isa() override to UNIVERSAL::isa() |
186
|
|
|
|
|
|
|
will circumvent the illusion as well. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 remote objects do not stringify to matche the original object |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Like ref(), this reveals the actual reference (and possibly class) of the proxy, |
191
|
|
|
|
|
|
|
not the object which is proxied. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 calls to use_remote() does not auto-proxy all package variables |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Calls to "use_remote" will proxy subroutine calls, but not package variable |
196
|
|
|
|
|
|
|
access automatically, besides @ISA. If necessary, it must be done explicitly |
197
|
|
|
|
|
|
|
with a call to bind(). |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 the client may not be able to "tie" variables which are proxies |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
The RMI modules use "tie" on every proxy reference to channel access to the |
202
|
|
|
|
|
|
|
other side. The effect of attempting to tie a proxy reference may destroy its |
203
|
|
|
|
|
|
|
ability to proxy. (This is untested.) |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
In most cases, applications do not tie a variable created elsewhere because it |
206
|
|
|
|
|
|
|
destroys its prior value. As such this is unlikely to be a problem, but is |
207
|
|
|
|
|
|
|
still technically a hole in transparency. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head2 change to $_[N] values will not affect the original variable |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Remote calls to subroutines/methods which modify aliases in @_ directly to tamper |
212
|
|
|
|
|
|
|
with the caller's variables will not work as it would with a local method |
213
|
|
|
|
|
|
|
call. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
This is supportable, but adds considerable overhead to support modules which |
216
|
|
|
|
|
|
|
create a side effect which is avoided because it is, mostly, a bad idea. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Perl technically passes an alias to even non-reference values, though the |
219
|
|
|
|
|
|
|
common "my ($v1,$v2) = @_;" makes a copy which safely allows the subroutine to |
220
|
|
|
|
|
|
|
behave as though the values were pass-by-copy. |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
sub foo { |
223
|
|
|
|
|
|
|
$_[0]++; # BAD! |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
my $v = 1; |
226
|
|
|
|
|
|
|
foo($v); |
227
|
|
|
|
|
|
|
$v == 2; # SURPRISE! |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
If foo() were called via RMI, in the current implementation, $v would still |
230
|
|
|
|
|
|
|
have its original value. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Packages which implement this surprise behavior include Compress::Zlib! If |
233
|
|
|
|
|
|
|
this feature were added the overhead to Compress::Zlib would still make you |
234
|
|
|
|
|
|
|
want to wrap the call... |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=head2 code which relies on caller() will probably fail |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
This means that some modules which perform magic during import() may not work |
239
|
|
|
|
|
|
|
as intended. |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
This problem is prevented in one place automatically by the current RMI |
242
|
|
|
|
|
|
|
implementation: there is custom code to handle exporting of methods into the |
243
|
|
|
|
|
|
|
caller's namespace inside "use_remote". |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head1 IMPLEMENTATIONS IN OTHER LANGUAGES |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
The use of transparent proxy objects goes by the term "RMI" in Java, "Drb" |
248
|
|
|
|
|
|
|
in Ruby, "PYRO" in Python, "Remoting" in .NET. |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
It is similar in functionality to architectures such as CORBA, SOAP, RPC and |
251
|
|
|
|
|
|
|
DCOM. |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
None of the above use the same protocols (except Java's RMI has an optional |
254
|
|
|
|
|
|
|
CORBA-related implementation). This module is no exception, sadly. Patches |
255
|
|
|
|
|
|
|
are welcome. |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=head1 SEE ALSO |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
B, B, B, B, |
260
|
|
|
|
|
|
|
B, B, B |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head1 AUTHORS |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
Scott Smith |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=head1 COPYRIGHT |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
Copyright (c) 2008 - 2009 Scott Smith All rights reserved. |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=head1 LICENSE |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under |
273
|
|
|
|
|
|
|
the same terms as Perl itself. |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
The full text of the license can be found in the LICENSE file included with this |
276
|
|
|
|
|
|
|
module. |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=cut |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
1; |