| 1 | package RPC::Async::Client; | ||
| 2 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = '1.05'; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | RPC::Async::Client - client side of asynchronous RPC framework | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use RPC::Async::Client; | ||
| 14 | use IO::EventMux; | ||
| 15 | |||
| 16 | my $mux = IO::EventMux->new; | ||
| 17 | my $rpc = RPC::Async::Client->new($mux, "perl://add-server.pl"); | ||
| 18 | # or # my $rpc = RPC::Async::Client->new($mux, "tcp://127.0.0.1:1234"); | ||
| 19 | |||
| 20 | $rpc->add_numbers(n1 => 2, n2 => 3, | ||
| 21 | sub { | ||
| 22 | my %reply = @_; | ||
| 23 | print "2 + 3 = $reply{sum}\n"; | ||
| 24 | }); | ||
| 25 | |||
| 26 | while ($rpc->has_requests || $rpc->has_coderefs) { | ||
| 27 | my $event = $rpc->io($mux->mux) or next; | ||
| 28 | } | ||
| 29 | |||
| 30 | $rpc->disconnect; | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | This module provides the magic that hides the details of doing asynchronous RPC | ||
| 35 | on the client side. It does not dictate how to implement initialisation or main | ||
| 36 | loop, although it requires the application to use L<IO::EventMux>. | ||
| 37 | |||
| 38 | The procedures made available by the remote server can be called directly on the | ||
| 39 | L<RPC::Async::Client> instance or via the C<call()> method where they are | ||
| 40 | further documented. | ||
| 41 | |||
| 42 | =head1 METHODS | ||
| 43 | |||
| 44 | =over | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | 3 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 49 | 3 | ▶ | use Socket; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 50 | 0 | ▶ | use RPC::Async::Util qw(make_packet append_data read_packet); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 51 | use RPC::Async::Coderef; | ||
| 52 | use RPC::Async::URL; | ||
| 53 | |||
| 54 | =item new($mux, $url, @urlargs) | ||
| 55 | |||
| 56 | Connects to an RPC server via the URL given in $url. Such URLs can be of the | ||
| 57 | forms specified in L<RPC::Async::URL>, although it must connect to a | ||
| 58 | bi-directional stream socket. Alternatively, pass an open file descriptor. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub new { | ||
| 63 | my ($class, $mux, $url, @args) = @_; | ||
| 64 | |||
| 65 | my %self = ( | ||
| 66 | mux => $mux, | ||
| 67 | requests => {}, | ||
| 68 | serial => -1, | ||
| 69 | buf => undef, | ||
| 70 | coderefs => {}, | ||
| 71 | ); | ||
| 72 | my ($fh, @urlargs) = url_connect($url, @args); | ||
| 73 | $mux->add($fh); | ||
| 74 | $self{fh} = $fh; | ||
| 75 | $self{urlargs} = \@urlargs; | ||
| 76 | |||
| 77 | return bless \%self, (ref $class || $class); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub AUTOLOAD { | ||
| 81 | my $self = shift; | ||
| 82 | if (@_ < 1) { return; } | ||
| 83 | |||
| 84 | our $AUTOLOAD; | ||
| 85 | my $procedure = $AUTOLOAD; | ||
| 86 | $procedure =~ s/.*:://; | ||
| 87 | |||
| 88 | return $self->call($procedure, @_); | ||
| 89 | } | ||
| 90 | |||
| 91 | =item call($procedure, @args, $subref) | ||
| 92 | |||
| 93 | Performs a remote procedure call. Rather than using this directly, this package | ||
| 94 | enables some AUTOLOAD magic that allows calls to remote procedures directly on | ||
| 95 | the L<RPC::Async::Client> instance. | ||
| 96 | |||
| 97 | Arguments are passed in key/value style by convention, although any arguments | ||
| 98 | may be given. The last argument a remote procedure call is a subroutine | ||
| 99 | reference to be executed upon completion of the call. This framework makes no | ||
| 100 | guarantees as to when, if ever, this sub will be called. Specifically, remote | ||
| 101 | procedures may return in a different order than they were called in. | ||
| 102 | |||
| 103 | Fairly complex data structures may be given as arguments, except for circular | ||
| 104 | ones. In particular, subroutine references are allowed. | ||
| 105 | |||
| 106 | The call itself is given a uniq id that is returned and can later be used | ||
| 107 | with other subs. | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub call { | ||
| 112 | my ($self, $procedure, @args) = @_; | ||
| 113 | my $callback = pop @args; | ||
| 114 | |||
| 115 | @args = $self->_encode_args(@args); | ||
| 116 | |||
| 117 | my $id = $self->_unique_id; | ||
| 118 | $self->{requests}{$id} = $callback; | ||
| 119 | |||
| 120 | #print "RPC::Async::Client sending: $id $procedure @args\n"; | ||
| 121 | $self->{mux}->send($self->{fh}, make_packet([ $id, $procedure, @args ])); | ||
| 122 | |||
| 123 | return $id; | ||
| 124 | } | ||
| 125 | |||
| 126 | =item disconnect | ||
| 127 | |||
| 128 | Call this to gracefully disconnect from the server without leaving zombie | ||
| 129 | processes or such. | ||
| 130 | |||
| 131 | =cut | ||
| 132 | |||
| 133 | sub disconnect { | ||
| 134 | my ($self) = @_; | ||
| 135 | $self->{mux}->kill($self->{fh}); | ||
| 136 | url_disconnect($self->{fh}, @{$self->{urlargs}}); | ||
| 137 | } | ||
| 138 | |||
| 139 | =item has_requests | ||
| 140 | |||
| 141 | Returns true iff there is at least one request pending. Usually, this means that | ||
| 142 | we should not terminate yet. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub has_requests { | ||
| 147 | my ($self, $event) = @_; | ||
| 148 | return scalar %{$self->{requests}}; | ||
| 149 | } | ||
| 150 | |||
| 151 | =item has_coderefs | ||
| 152 | |||
| 153 | Returns true if the remote side holds a reference to a subroutine given to it | ||
| 154 | in an earlier call. Depending on the application, this may be taken as a hint | ||
| 155 | that we should not terminate yet. This information is obtained via interaction | ||
| 156 | with Perl's garbage collector on the server side. | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | sub has_coderefs { | ||
| 161 | my ($self, $event) = @_; | ||
| 162 | return scalar %{$self->{coderefs}}; | ||
| 163 | } | ||
| 164 | |||
| 165 | =item io($event) | ||
| 166 | |||
| 167 | Inspect an event from EventMux. All such events must be passed through here in | ||
| 168 | order to handle asynchronous replies. If the event was handled, C<undef> is | ||
| 169 | returned. Otherwise, the event is returned for processing by other | ||
| 170 | RPC::Async::Client handlers or the main program itself. | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | sub io { | ||
| 175 | my ($self, $event) = @_; | ||
| 176 | |||
| 177 | if ($event->{fh} and $event->{fh} == $self->{fh}) { | ||
| 178 | my $type = $event->{type}; | ||
| 179 | if ($type eq "read") { | ||
| 180 | #print "RPC::Async::Client got ", length $event->{data}, " bytes\n"; | ||
| 181 | $self->_handle_read($event->{data}); | ||
| 182 | |||
| 183 | } elsif ($type eq "closed") { | ||
| 184 | die __PACKAGE__ .": server disconnected\n"; | ||
| 185 | } | ||
| 186 | |||
| 187 | return; | ||
| 188 | |||
| 189 | } else { | ||
| 190 | return $event; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | =item dump_requests | ||
| 195 | |||
| 196 | Returns a string documenting what requests are pending. For debugging only. | ||
| 197 | |||
| 198 | =cut | ||
| 199 | |||
| 200 | sub dump_requests { | ||
| 201 | my ($self) = @_; | ||
| 202 | use Data::Dumper; | ||
| 203 | return Dumper($self->{requests}); | ||
| 204 | } | ||
| 205 | |||
| 206 | =item wait($timeout, [@ids]) | ||
| 207 | |||
| 208 | Block until we have enough events from the RPC server to matches all the | ||
| 209 | id's listed or we get a timeout. If no id's are given then any id will be | ||
| 210 | used. This function can be useful when we want to make sure the server is | ||
| 211 | started in the other end, fx. if we need to connect to it on another socket. | ||
| 212 | |||
| 213 | If all events was received in time, 1 is returned, C<undef> is returned on | ||
| 214 | timeout. | ||
| 215 | |||
| 216 | Any unrelated events will be buffered and pushed back on the stack when the | ||
| 217 | wait call has finished. | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | sub wait { | ||
| 222 | my ($self, $timeout, @ids) = @_; | ||
| 223 | my %match = map { $_ => 1 } @ids; | ||
| 224 | my $count = (int @ids or 1); | ||
| 225 | my $mux = $self->{mux}; | ||
| 226 | |||
| 227 | # Set timeout only on our own fh. | ||
| 228 | $mux->timeout($self->{fh}, $timeout); | ||
| 229 | my @events; | ||
| 230 | while($count and my $event = $mux->mux()) { | ||
| 231 | if ($event->{fh} and $event->{fh} == $self->{fh}) { | ||
| 232 | if ($event->{type} eq "read") { | ||
| 233 | foreach my $id ($self->_handle_read($event->{data})) { | ||
| 234 | if(exists $match{$id} or @ids == 0) { | ||
| 235 | $count--; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | } elsif($event->{type} eq 'timeout') { | ||
| 240 | last; | ||
| 241 | |||
| 242 | } elsif ($event->{type} eq "closed") { | ||
| 243 | die __PACKAGE__ .": server disconnected\n"; | ||
| 244 | } | ||
| 245 | |||
| 246 | } else { | ||
| 247 | push @events, $event; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | $mux->push_event(@events); | ||
| 252 | return $count > 0?undef:1; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub _handle_read { | ||
| 256 | my ($self, $data) = @_; | ||
| 257 | my @ids; | ||
| 258 | |||
| 259 | # TODO: Use buffering code in EventMux and remove functions from Util.pm | ||
| 260 | append_data(\$self->{buf}, $data); | ||
| 261 | while (my $thawed = read_packet(\$self->{buf})) { | ||
| 262 | if (ref $thawed eq "ARRAY" and @$thawed >= 1) { | ||
| 263 | my ($id, @args) = @$thawed; | ||
| 264 | my $callback = delete $self->{requests}{$id}; | ||
| 265 | |||
| 266 | push(@ids, $id); | ||
| 267 | |||
| 268 | if (defined $callback) { | ||
| 269 | #print __PACKAGE__, ": callback(@args)\n"; | ||
| 270 | $callback->(@args); | ||
| 271 | |||
| 272 | } elsif (exists $self->{coderefs}{$id}) { | ||
| 273 | my ($command, @cb_args) = @args; | ||
| 274 | if ($command eq "destroy") { | ||
| 275 | delete $self->{coderefs}{$id}; | ||
| 276 | } elsif ($command eq "call") { | ||
| 277 | $self->{coderefs}{$id}->(@cb_args); | ||
| 278 | } else { | ||
| 279 | warn __PACKAGE__.": Unknown command for callback"; | ||
| 280 | } | ||
| 281 | |||
| 282 | } else { | ||
| 283 | warn __PACKAGE__.": Spurious reply to id $id\n"; | ||
| 284 | } | ||
| 285 | } else { | ||
| 286 | warn __PACKAGE__.": Bad data in thawed packet"; | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | return @ids; | ||
| 291 | } | ||
| 292 | |||
| 293 | sub _unique_id { | ||
| 294 | my ($self) = @_; | ||
| 295 | |||
| 296 | $self->{serial}++; | ||
| 297 | return $self->{serial} &= 0x7FffFFff; | ||
| 298 | } | ||
| 299 | |||
| 300 | sub _encode_args { | ||
| 301 | my ($self, @args) = @_; | ||
| 302 | |||
| 303 | return map { | ||
| 304 | my $arg = $_; | ||
| 305 | if (not ref $arg) { | ||
| 306 | $arg; | ||
| 307 | } elsif (ref $arg eq "ARRAY") { | ||
| 308 | [ $self->_encode_args(@$arg) ]; | ||
| 309 | } elsif (ref $arg eq "HASH") { | ||
| 310 | my %h; | ||
| 311 | keys %h = scalar keys %$arg; # preallocate buckets | ||
| 312 | foreach my $key (keys %$arg) { | ||
| 313 | my ($v) = $self->_encode_args($arg->{$key}); | ||
| 314 | $h{$key} = $v; | ||
| 315 | } | ||
| 316 | \%h; | ||
| 317 | } elsif (ref $arg eq "Regexp") { | ||
| 318 | #TODO: Copy over regexp options /ig etc. | ||
| 319 | $arg =~ /:(.*)\)$/; | ||
| 320 | $1; | ||
| 321 | } elsif (ref $arg eq "REF") { | ||
| 322 | my ($v) = $self->_encode_args($$arg); | ||
| 323 | \$v; | ||
| 324 | } elsif (ref $arg eq "CODE") { | ||
| 325 | my $id = $self->_unique_id; | ||
| 326 | $self->{coderefs}{$id} = $arg; | ||
| 327 | RPC::Async::Coderef->new($id); | ||
| 328 | } else { | ||
| 329 | $arg; | ||
| 330 | } | ||
| 331 | } @args; | ||
| 332 | } | ||
| 333 | |||
| 334 | =back | ||
| 335 | |||
| 336 | =head1 AUTHOR | ||
| 337 | |||
| 338 | Troels Liebe Bentsen <tlb@rapanden.dk>, Jonas Jensen <jbj@knef.dk> | ||
| 339 | |||
| 340 | =head1 COPYRIGHT | ||
| 341 | |||
| 342 | Copyright(C) 2005-2007 Troels Liebe Bentsen | ||
| 343 | Copyright(C) 2005-2007 Jonas Jensen | ||
| 344 | |||
| 345 | This library is free software; you can redistribute it and/or modify | ||
| 346 | it under the same terms as Perl itself. | ||
| 347 | |||
| 348 | =cut | ||
| 349 | |||
| 350 | 1; | ||
| 351 | |||
| 352 | # vim: et sw=4 sts=4 tw=80 | ||