| 1 | package RPC::Async::Server; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = '1.05'; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | RPC::Async::Server - server side of asynchronous RPC framework | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use RPC::Async::Server; | ||
| 14 | use IO::EventMux; | ||
| 15 | |||
| 16 | my $mux = IO::EventMux->new; | ||
| 17 | my $rpc = RPC::Async::Server->new($mux); | ||
| 18 | init_clients($rpc); | ||
| 19 | |||
| 20 | while ($rpc->has_clients()) { | ||
| 21 | my $event = $rpc->io($mux->mux) or next; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub rpc_add_numbers { | ||
| 25 | my ($caller, %args) = @_; | ||
| 26 | my $sum = $args{n1} + $args{n2}; | ||
| 27 | $rpc->return($caller, sum => $sum); | ||
| 28 | } | ||
| 29 | |||
| 30 | 1; | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | This module provides the magic that hides the details of doing asynchronous RPC | ||
| 35 | on the server side. It does not dictate how to implement initialisation or main | ||
| 36 | loop, although it requires the application to use IO::EventMux. | ||
| 37 | |||
| 38 | When creating a new C<RPC::Async::Server> object with the C<new> method, you | ||
| 39 | are also telling it what package it should invoke callback functions in. If the | ||
| 40 | package argument is omitted, it will use the caller's package. | ||
| 41 | |||
| 42 | Users of this module are written as a kind of hybrid between a perl executable | ||
| 43 | program and a library module. They need a small wrapper program around them to | ||
| 44 | initialise communication with their clients somehow. The convention of | ||
| 45 | L<RPC::Async::Client> dictates that they call a method named init_clients as | ||
| 46 | described in L</SYNOPSIS>. | ||
| 47 | |||
| 48 | =head1 METHODS | ||
| 49 | |||
| 50 | =head2 C<rpc_*($caller, @args)> (callbacks) | ||
| 51 | |||
| 52 | The method named rpc_PROCEDURE will be called back from C<io> when the client | ||
| 53 | calls the method PROCEDURE. The first argument is an opaque handle to be used | ||
| 54 | when calling C<return>, and the remaining arguments are the ones given by the | ||
| 55 | client. | ||
| 56 | |||
| 57 | The return values from these methods are ignored. Return a value by calling | ||
| 58 | C<return> on the RPC server object. It is not necessary to call C<return> | ||
| 59 | before returning from this method, but it should be called eventually. If the | ||
| 60 | client sends invalid data, throw an exception to disconnect him. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | 1 | ▶ | use IO::EventMux; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 65 | 0 | ▶ | use RPC::Async::Util qw(make_packet append_data read_packet expand); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 66 | use RPC::Async::Coderef; | ||
| 67 | |||
| 68 | =head2 C<new($mux [, $package])> | ||
| 69 | |||
| 70 | Instantiate a new RPC server object that will call back methods in C<$package>. | ||
| 71 | If C<$package> is omitted, the caller's package will be used. | ||
| 72 | |||
| 73 | The C<$mux> object must be an instance of C<IO::EventMux> or something | ||
| 74 | compatible, which will be used for I/O. It is the responsibility of the caller | ||
| 75 | to poll this object and call C<io>, as detailed below. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub new { | ||
| 80 | my ($class, $mux, $package) = @_; | ||
| 81 | |||
| 82 | if (not $package) { | ||
| 83 | ($package) = caller; | ||
| 84 | } | ||
| 85 | #print __PACKAGE__, ": called from '$package'\n"; | ||
| 86 | |||
| 87 | my %self = ( | ||
| 88 | mux => $mux, | ||
| 89 | package => $package, | ||
| 90 | buf => undef, | ||
| 91 | clients => {}, | ||
| 92 | ); | ||
| 93 | |||
| 94 | return bless \%self, (ref $class || $class); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _decode_args { | ||
| 98 | my ($self, $fh, @args) = @_; | ||
| 99 | |||
| 100 | foreach my $arg (@args) { | ||
| 101 | if (not ref $arg) { | ||
| 102 | # do nothing | ||
| 103 | |||
| 104 | } elsif (ref $arg eq "ARRAY") { | ||
| 105 | $self->_decode_args($fh, @$arg); | ||
| 106 | |||
| 107 | } elsif (ref $arg eq "HASH") { | ||
| 108 | $self->_decode_args($fh, values %$arg); | ||
| 109 | |||
| 110 | } elsif (ref $arg eq "REF") { | ||
| 111 | $self->_decode_args($fh, $$arg); | ||
| 112 | |||
| 113 | } elsif (ref $arg eq "CODE") { | ||
| 114 | die __PACKAGE__.": coderef?"; | ||
| 115 | |||
| 116 | } elsif (UNIVERSAL::isa($arg, "RPC::Async::Coderef")) { | ||
| 117 | my $id = $arg->id; | ||
| 118 | $arg->set_call(sub { | ||
| 119 | $self->{mux}->send($fh, make_packet([ $id, "call", @_ ])); | ||
| 120 | }); | ||
| 121 | $arg->set_destroy(sub { | ||
| 122 | return if !$self->{mux}; | ||
| 123 | $self->{mux}->send($fh, make_packet([ $id, "destroy" ])); | ||
| 124 | }); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | sub _handle_read { | ||
| 130 | my ($self, $fh, $data) = @_; | ||
| 131 | |||
| 132 | append_data \$self->{buf}, $data; | ||
| 133 | |||
| 134 | while (my $thawed = read_packet(\$self->{buf})) { | ||
| 135 | if (ref $thawed eq "ARRAY" and @$thawed >= 2) { | ||
| 136 | my ($id, $method, @args) = @$thawed; | ||
| 137 | |||
| 138 | my $caller = [ $fh, $id, $method ]; | ||
| 139 | $self->_decode_args($fh, @args); | ||
| 140 | |||
| 141 | # Set main ref and package ref to package if not main. | ||
| 142 | my $main = \%main::; | ||
| 143 | my $package = $self->{package} eq 'main' | ||
| 144 | ? $main : $main->{$self->{package}}; | ||
| 145 | |||
| 146 | # Check if the method exists and call it | ||
| 147 | if(exists $package->{"rpc_$method"}) { | ||
| 148 | # Get code refrence back | ||
| 149 | my $sub = *{$package->{"rpc_$method"}}{CODE}; | ||
| 150 | if($sub) { | ||
| 151 | $sub->($caller, @args); | ||
| 152 | } | ||
| 153 | |||
| 154 | } elsif($method eq 'methods') { | ||
| 155 | my %methods = map { /^rpc_(.+)/; $1 => {} } | ||
| 156 | grep {$_ =~ /^rpc_/} keys %{$package}; | ||
| 157 | my %opt = @args; | ||
| 158 | if($opt{defs}) { | ||
| 159 | foreach my $method (keys %methods) { | ||
| 160 | if(exists $package->{"def_$method"}) { | ||
| 161 | my $sub = *{$package->{"def_$method"}}{CODE}; | ||
| 162 | if($sub) { | ||
| 163 | $methods{$method}{in} | ||
| 164 | = expand($sub->($caller, 1), 1); | ||
| 165 | $methods{$method}{out} | ||
| 166 | = expand($sub->($caller, 0)); | ||
| 167 | |||
| 168 | } else { | ||
| 169 | print "Could not find sub def_$1\n"; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
| 173 | } | ||
| 174 | $self->return($caller, methods => \%methods); | ||
| 175 | |||
| 176 | } else { | ||
| 177 | $self->return($caller, errors => [ | ||
| 178 | "No sub '$method' in package '$self->{package}'" | ||
| 179 | ]); | ||
| 180 | } | ||
| 181 | |||
| 182 | } else { | ||
| 183 | die "bad data in thawed packet\n"; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | =head2 C<add_client($socket)> | ||
| 189 | |||
| 190 | Add a client to the internal list of clients. This method is not usually called | ||
| 191 | directly. | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | sub add_client { | ||
| 196 | my ($self, $sock) = @_; | ||
| 197 | $self->{mux}->add($sock); | ||
| 198 | $self->{clients}{$sock} = 1; | ||
| 199 | } | ||
| 200 | |||
| 201 | =head2 C<add_listener($socket)> | ||
| 202 | |||
| 203 | Add a listening socket. Connections to this socket will be automatically added | ||
| 204 | to the internal list of clients. This method is not usually called directly. | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | sub add_listener { | ||
| 209 | my ($self, $sock) = @_; | ||
| 210 | $self->{mux}->add($sock, Listen => 1); | ||
| 211 | $self->{clients}{$sock} = 1; | ||
| 212 | } | ||
| 213 | |||
| 214 | =head2 C<return($caller, @args)> | ||
| 215 | |||
| 216 | Must be called exactly once for each callback to one of the C<rpc_*> methods. | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | sub return { | ||
| 221 | my ($self, $caller, @args) = @_; | ||
| 222 | my ($sock, $id, $procedure) = @$caller; | ||
| 223 | |||
| 224 | if (!$self->{check_response} | ||
| 225 | or $self->{check_response}->($procedure, @args)) { | ||
| 226 | #print "return: sending: $id @args\n"; | ||
| 227 | $self->{mux}->send($sock, make_packet([ $id, @args ])); | ||
| 228 | } else { | ||
| 229 | die "return: check_response failed\n"; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | =head2 C<io($event)> | ||
| 234 | |||
| 235 | This method is called in the program's main loop every time an event is | ||
| 236 | received from IO::EventMux::mux. If C<io> processed this event and determined | ||
| 237 | that it is not relevant to the caller, it returns C<undef>. Otherwise, the | ||
| 238 | event is returned. This leads to the calling style of | ||
| 239 | |||
| 240 | my $event = $rpc->io($mux->mux) or next; | ||
| 241 | |||
| 242 | in the main loop. If more than one RPC server is in use, chain the calls like | ||
| 243 | |||
| 244 | my $event = $mux->mux; | ||
| 245 | $event = $rpc1->io($event) or next; | ||
| 246 | $event = $rpc2->io($event) or next; | ||
| 247 | |||
| 248 | This method will invoke the C<rpc_*> callbacks as needed. | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | sub io { | ||
| 253 | my ($self, $event) = @_; | ||
| 254 | my $fh = $event->{fh}; | ||
| 255 | |||
| 256 | if ($fh && exists $self->{clients}{$fh} or | ||
| 257 | $event->{type} eq "accepted" && $self->{clients}{$event->{parent_fh}}) { | ||
| 258 | my $type = $event->{type}; | ||
| 259 | |||
| 260 | if ($type eq "read") { | ||
| 261 | #print __PACKAGE__, ": got ", length $event->{data}, " bytes\n"; | ||
| 262 | |||
| 263 | eval { $self->_handle_read($fh, $event->{data}) }; | ||
| 264 | if ($@) { | ||
| 265 | warn __PACKAGE__.": Disconnecting client for error: $@\n"; | ||
| 266 | $self->{mux}->kill($fh); | ||
| 267 | } | ||
| 268 | |||
| 269 | } elsif ($type eq "closed") { | ||
| 270 | delete $self->{clients}{$fh}; | ||
| 271 | #print "Client $fh disconnected; ", int(keys %{$self->{clients}}), | ||
| 272 | # " clients left.\n" | ||
| 273 | |||
| 274 | } elsif ($type eq "accepted") { | ||
| 275 | $self->add_client($fh); | ||
| 276 | } | ||
| 277 | |||
| 278 | return; | ||
| 279 | |||
| 280 | } else { | ||
| 281 | return $event; | ||
| 282 | } | ||
| 283 | |||
| 284 | } | ||
| 285 | |||
| 286 | =head2 C<has_clients()> | ||
| 287 | |||
| 288 | Returns true if and only if at least one client is still connected to this | ||
| 289 | server. | ||
| 290 | |||
| 291 | =cut | ||
| 292 | |||
| 293 | sub has_clients { | ||
| 294 | my ($self) = @_; | ||
| 295 | return scalar %{$self->{clients}}; | ||
| 296 | } | ||
| 297 | |||
| 298 | 1; | ||
| 299 | |||
| 300 | =head1 AUTHOR | ||
| 301 | |||
| 302 | Troels Liebe Bentsen <tlb@rapanden.dk>, Jonas Jensen <jbj@knef.dk> | ||
| 303 | |||
| 304 | =head1 COPYRIGHT | ||
| 305 | |||
| 306 | Copyright(C) 2005-2007 Troels Liebe Bentsen | ||
| 307 | Copyright(C) 2005-2007 Jonas Jensen | ||
| 308 | |||
| 309 | This library is free software; you can redistribute it and/or modify | ||
| 310 | it under the same terms as Perl itself. | ||
| 311 | |||
| 312 | =cut | ||
| 313 | |||
| 314 | # vim: et sw=4 sts=4 tw=80 | ||