I decided to use the same Msg.pm library that Sriram developed in Advanced Perl Programming. Some of the code even has comments from the original RPC module. When I develop better socket programming skills, I'll go and write my own Msg.pm library with more features and such. I just wanted to know what the monks here think of this module. Is it CPAN worthy? Should I just email Sriram with this newer module and ask him to include it in a reprint of Advanced Perl Programming? Or should I just stick this module in my backpack and pull out as needed? Also, any constructive criticism on how to make this module a little better is appreciated.package RPC_OO; use Msg; use strict; use Carp; use vars qw(@ISA); @ISA = qw(Msg); use FreezeThaw qw(freeze thaw); #----------------------------------------------------------------- # Server side sub new_server { my ($pkg, $my_host, $my_port) = @_; return $pkg->SUPER::new_server($my_host, $my_port, sub {$pkg->_log +in(@_)}); } sub _login { return \&_event_dispatcher; } sub _event_dispatcher { my ($conn, $msg, $err) = @_; return if ($err); # Need better error handling. return unless defined($msg); my ($dir, $id, $gimme, $obj, $method, @params) = thaw ($msg); my ($result, @results); if ($dir eq '>') { # Incoming msg. (outgoing msg from client, that is) eval { no strict 'refs'; # Because we call the subroutine using # a symbolic reference if ($gimme eq 'a') { # Want an array back @results = $obj->$method(@params); } else { $result = $obj->$method(@params); } }; if ($@) { $msg = bless \$@, "RPC::Error"; $msg = freeze('<', $id, $msg); } elsif ($gimme eq 'a') { $msg = freeze('<', $id, @results); } else { $msg = freeze('<', $id, $result); } $conn->send_later($msg); } else { #Someone tried to send a bogus command... $conn->disconnect(); } } #----------------------------------------------------------------- # Client side sub connect { my ($pkg, $host, $port) = @_; my $conn = $pkg->SUPER::connect($host,$port, \&_response); return $conn; } my $send_err = 0; sub handle_send_err { $send_err = $!; } my $g_msg_id = 0; sub rpc { my $conn = shift; my $obj = shift; my $method = shift; my $gimme; $gimme = wantarray ? 'a' : 's'; my $msg_id = ++$g_msg_id; my $serialized_msg = freeze ('>', $msg_id, $gimme, $obj, $method, +@_); # Send and Receive $conn->send_later ($serialized_msg); if ($send_err) { die "RPC Error: $!\n"; } do { Msg->event_loop(1); # Dispatch other messages until we get a r +esponse } until (exists $conn->{rcvd}->{$msg_id} || $send_err); # Dequeue message my $rl_retargs = delete $conn->{rcvd}->{$msg_id}; # ref to list if (ref($rl_retargs->[0]) eq 'RPC::Error') { die ${$rl_retargs->[0]}; } wantarray ? @$rl_retargs : $rl_retargs->[0]; } sub _response { my ($conn, $msg, $err) = @_; return if ($err); # Need better error handling. return unless defined($msg); my ($dir, $id, @data) = thaw ($msg); $conn->{rcvd}->{$id} = [@data]; } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: RFC: RPC_OO.pm
by Juerd (Abbot) on Mar 18, 2002 at 07:40 UTC | |
by Necos (Friar) on Mar 18, 2002 at 11:29 UTC | |
|
Re: RFC: RPC_OO.pm
by perrin (Chancellor) on Mar 18, 2002 at 15:28 UTC | |
by Necos (Friar) on Mar 18, 2002 at 18:34 UTC |