| 1 | # -*- perl -*- | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # PlRPC - Perl RPC, package for writing simple, RPC like clients and | ||||||||||||||||||||||||||||
| 5 | # servers | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # RPC::PlClient.pm is the module for writing the PlRPC client. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # Copyright (c) 1997, 1998 Jochen Wiedmann | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # You may distribute under the terms of either the GNU General Public | ||||||||||||||||||||||||||||
| 13 | # License or the Artistic License, as specified in the Perl README file. | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # Author: Jochen Wiedmann | ||||||||||||||||||||||||||||
| 16 | # Email: jochen.wiedmann at freenet.de | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use RPC::PlClient::Comm (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 4 | ▶ | use Net::Daemon::Log (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use IO::Socket (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | package RPC::PlClient; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 4 | $RPC::PlClient::VERSION = '0.2020'; | |||||||||||||||||||||||||||
| 29 | 4 | @RPC::PlClient::ISA = qw(Net::Daemon::Log); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | ############################################################################ | ||||||||||||||||||||||||||||
| 33 | # | ||||||||||||||||||||||||||||
| 34 | # Name: new | ||||||||||||||||||||||||||||
| 35 | # | ||||||||||||||||||||||||||||
| 36 | # Purpose: Constructor of the PlRPC::Client module | ||||||||||||||||||||||||||||
| 37 | # | ||||||||||||||||||||||||||||
| 38 | # Inputs: $self - Class name | ||||||||||||||||||||||||||||
| 39 | # @attr - Attribute list | ||||||||||||||||||||||||||||
| 40 | # | ||||||||||||||||||||||||||||
| 41 | # Returns: Client object; dies in case of errors. | ||||||||||||||||||||||||||||
| 42 | # | ||||||||||||||||||||||||||||
| 43 | ############################################################################ | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 5 | ▶ | sub new ($@) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=16 SCAR=30.2
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 5 | my $proto = shift; | |||||||||||||||||||||||||||
| 47 | 5 | my $self = {@_}; | |||||||||||||||||||||||||||
| 48 | 5 | ▶ | bless($self, (ref($proto) || $proto)); | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 5 | my $comm = $self->{'comm'} = RPC::PlClient::Comm->new($self); | |||||||||||||||||||||||||||
| 51 | 5 | ▶ | my $app = $self->{'application'} or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | $self->Fatal("Missing application name"); | ||||||||||||||||||||||||||||
| 53 | 5 | ▶ | my $version = $self->{'version'} or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | $self->Fatal("Missing version number"); | ||||||||||||||||||||||||||||
| 55 | 5 | ▶ | my $user = $self->{'user'} || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'user'} || ''
MC/DC100%1/1
$self->{'user'} || ''
$self->{'user'}
Truth table100%2/2
A
A= $self->{'user'}
| |||||||||||||||||||||||||||||
| 56 | 5 | ▶ | my $password = $self->{'password'} || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'password'} || ''
MC/DC0%0/1
$self->{'password'} || ''
$self->{'password'}
Truth table50%1/2
A
A= $self->{'password'}
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 5 | my $socket; | |||||||||||||||||||||||||||
| 59 | 5 | ▶ | if (!($socket = $self->{'socket'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 5 | ▶ | $self->Fatal("Missing peer address") unless $self->{'peeraddr'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | $self->Fatal("Missing peer port") | ||||||||||||||||||||||||||||
| 62 | unless ($self->{'peerport'} || | ||||||||||||||||||||||||||||
| 63 | 5 | ▶ | index($self->{'peeraddr'}, ':') != -1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"peerport"} or index($self->{"peeraddr"}, ":") != -1
MC/DC0%0/2
$self->{"peerport"} or index($self->{"peeraddr"}, ":") != -1
$self->{"peerport"}
index($self->{"peeraddr"}, ":") != -1
Truth table33%1/3
A or B
A= $self->{"peerport"}B= index($self->{"peeraddr"}, ":") != -1
| |||||||||||||||||||||||||||||
| 64 | $socket = $self->{'socket'} = IO::Socket::INET->new | ||||||||||||||||||||||||||||
| 65 | ('PeerAddr' => $self->{'peeraddr'}, | ||||||||||||||||||||||||||||
| 66 | 'PeerPort' => $self->{'peerport'}, | ||||||||||||||||||||||||||||
| 67 | 'Proto' => $self->{'socket_proto'}, | ||||||||||||||||||||||||||||
| 68 | 'Type' => $self->{'socket_type'}, | ||||||||||||||||||||||||||||
| 69 | 5 | 'Timeout' => $self->{'timeout'}); | |||||||||||||||||||||||||||
| 70 | 5 | ▶ | $self->Fatal("Cannot connect: $!") unless $socket; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 5 | $self->Debug("Connected to %s, port %s", | |||||||||||||||||||||||||||
| 73 | $socket->peerhost(), $socket->peerport()); | ||||||||||||||||||||||||||||
| 74 | 5 | $self->Debug("Sending login message: %s, %s, %s, %s", | |||||||||||||||||||||||||||
| 75 | $app, $version, $user, "x" x length($password)); | ||||||||||||||||||||||||||||
| 76 | 5 | $comm->Write($socket, [$app, $version, $user, $password]); | |||||||||||||||||||||||||||
| 77 | 5 | $self->Debug("Waiting for server's response ..."); | |||||||||||||||||||||||||||
| 78 | 5 | my $reply = $comm->Read($socket); | |||||||||||||||||||||||||||
| 79 | 5 | ▶ | die "Unexpected EOF from server" unless defined($reply); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 5 | ▶ | die "Expected server to return an array ref" unless ref($reply) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 5 | ▶ | my $msg = defined($reply->[1]) ? $reply->[1] : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 5 | ▶ | die "Refused by server: $msg" unless $reply->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 5 | $self->Debug("Logged in, server replies: $msg"); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 5 | ▶ | return ($self, $msg) if wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 4 | $self; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | ############################################################################ | ||||||||||||||||||||||||||||
| 91 | # | ||||||||||||||||||||||||||||
| 92 | # Name: Call | ||||||||||||||||||||||||||||
| 93 | # | ||||||||||||||||||||||||||||
| 94 | # Purpose: Coerce method located on the server | ||||||||||||||||||||||||||||
| 95 | # | ||||||||||||||||||||||||||||
| 96 | # Inputs: $self - client instance | ||||||||||||||||||||||||||||
| 97 | # $method - method name | ||||||||||||||||||||||||||||
| 98 | # @args - method attributes | ||||||||||||||||||||||||||||
| 99 | # | ||||||||||||||||||||||||||||
| 100 | # Returns: method results; dies in case of errors. | ||||||||||||||||||||||||||||
| 101 | # | ||||||||||||||||||||||||||||
| 102 | ############################################################################ | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 37 | ▶ | sub Call ($@) { | ||||||||||||||||||||||||||
|
Subroutine
Call: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 37 | my $self = shift; | |||||||||||||||||||||||||||
| 106 | 37 | my $socket = $self->{'socket'}; | |||||||||||||||||||||||||||
| 107 | 37 | my $comm = $self->{'comm'}; | |||||||||||||||||||||||||||
| 108 | 37 | $comm->Write($socket, [@_]); | |||||||||||||||||||||||||||
| 109 | 37 | my $msg = $comm->Read($socket); | |||||||||||||||||||||||||||
| 110 | 37 | ▶ | die "Unexpected EOF while waiting for server reply" unless defined($msg); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 37 | ▶ | die "Server returned error: $$msg" if ref($msg) eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 23 | ▶ | die "Expected server to return an array ref" unless ref($msg) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 23 | @$msg; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 4 | ▶ | sub ClientObject { | ||||||||||||||||||||||||||
|
Subroutine
ClientObject: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 4 | my $client = shift; my $class = shift; my $method = shift; | |||||||||||||||||||||||||||
| 118 | 4 | my($object) = $client->Call('NewHandle', $class, $method, @_); | |||||||||||||||||||||||||||
| 119 | 4 | ▶ | die "Constructor didn't return a TRUE value" unless $object; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 4 | ▶ | die "Constructor didn't return an object" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | unless $object =~ /^((?:\w+|\:\:)+)=(\w+)/; | ||||||||||||||||||||||||||||
| 122 | 4 | RPC::PlClient::Object->new($1, $client, $object); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub Disconnect { | ||||||||||||||||||||||||||
|
Subroutine
Disconnect: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 126 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 127 | 0 | $self->{'socket'} = undef; | |||||||||||||||||||||||||||
| 128 | 0 | 1; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | package RPC::PlClient::Object; | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 4 | ▶ | use vars qw($AUTOLOAD); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 13 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 137 | 13 | my $method = $AUTOLOAD; | |||||||||||||||||||||||||||
| 138 | 13 | my $index; | |||||||||||||||||||||||||||
| 139 | 13 | ▶ | die "Cannot parse method: $method" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | unless ($index = rindex($method, '::')) != -1; | ||||||||||||||||||||||||||||
| 141 | 13 | my $class = substr($method, 0, $index); | |||||||||||||||||||||||||||
| 142 | 13 | $method = substr($method, $index+2); | |||||||||||||||||||||||||||
| 143 | 13 | ▶ | eval <<"EOM"; | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=7.0
Subroutine
divide: called CC=2 SCAR=7.0
Subroutine
multiply: called CC=2 SCAR=7.0
Subroutine
subtract: called CC=2 SCAR=7.0
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 144 | package $class; | ||||||||||||||||||||||||||||
| 145 | sub $method { | ||||||||||||||||||||||||||||
| 146 | my \$self = shift; | ||||||||||||||||||||||||||||
| 147 | my \$client = \$self->{'client'}; my \$object = \$self->{'object'}; | ||||||||||||||||||||||||||||
| 148 | my \@result = \$client->Call('CallMethod', \$object, '$method', | ||||||||||||||||||||||||||||
| 149 | \@_); | ||||||||||||||||||||||||||||
| 150 | return \@result if wantarray; | ||||||||||||||||||||||||||||
| 151 | return \$result[0]; | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | EOM | ||||||||||||||||||||||||||||
| 154 | 13 | goto &$AUTOLOAD; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 158 | 4 | my($class, $cl, $client, $object) = @_; | |||||||||||||||||||||||||||
| 159 | 4 | ▶ | $class = ref($class) if ref($class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 161 | 4 | my $ocl = "${class}::$cl"; | |||||||||||||||||||||||||||
| 162 | 4 | ▶ | @{"${ocl}::ISA"} = $class unless @{"${ocl}::ISA"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 4 | my $self = { 'client' => $client, 'object' => $object }; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 4 | bless($self, $ocl); | |||||||||||||||||||||||||||
| 166 | 4 | $self; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 4 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 171 | 4 | my $saved_error = $@; # Save $@ | |||||||||||||||||||||||||||
| 172 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 173 | 4 | ▶ | if (my $client = delete $self->{'client'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 4 | eval { $client->Call('DestroyHandle', $self->{'object'}) }; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 4 | $@ = $saved_error; # Restore $@ | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 4 | 1; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | __END__ | ||||||||||||||||||||||||||||