XPC/Agent.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 112 CC 14 SCAR 39.0
CC
14
Cov
0%
_call_
110.0
debug
12.0
AUTOLOAD
12.0
CRAP 49.5
1 #!/usr/bin/perl -w
2 #
3 # Agent.pm - XPC Agent
4 #
5 # Copyright (C) 2001 Gregor N. Purdy.
6 # All rights reserved.
7 #
8 # This is free software; you can redistribute it and/or modify it under
9 # the same terms as Perl itself.
10 #
11  
12  
13 use strict;
14  
15 package XPC::Agent;
16  
17 use URI;
18 use LWP::UserAgent;
19 use HTTP::Request;
20 use XPC;
21  
22  
23 #
24 # new()
25 #
26  
27 sub new
28 {
29 my $class = shift;
30 my $uri_input = shift;
31  
32 my $self = bless { }, $class;
33  
34 $self->{URI} = URI->new($uri_input);
35 $self->{DEBUG} = 0;
36  
37 return $self;
38 }
39  
40  
41 #
42 # debug()
43 #
44  
45 sub debug
46 {
47 my $self = shift;
48  
49 $self->{DEBUG} = ($_[0] ? 1 : 0) if @_;
50  
51 return $self->{DEBUG};
52 }
53  
54  
55 #
56 # uri()
57 #
58  
59 sub uri
60 {
61 my $self = shift;
62 return $self->{URI};
63 }
64  
65  
66 #
67 # _call_()
68 #
69  
70 sub _call_
71 {
72 my ($self, $procedure, @args) = @_;
73  
74 #
75 # Request Building:
76 #
77  
78 print "$0: Building XPC request...\n" if $self->debug();
79  
80 my $uri_string = $self->uri->as_string;
81  
82 my $xpc_req = XPC->new_call($procedure);
83 my $xpc_string = $xpc_req->as_string;
84  
85 if ($self->debug()) {
86 print "XPC REQUEST:\n";
87 print $xpc_string;
88 print "\n\n";
89 }
90  
91 print "$0: Building HTTP request 'POST => $uri_string'...\n" if $self->debug();
92  
93 my $http_req = HTTP::Request->new('POST', $uri_string);
94  
95 print "$0: Adding XPC content...\n" if $self->debug();
96  
97 $http_req->content($xpc_string);
98  
99 if ($self->debug()) {
100 print "HTTP REQUEST:\n";
101 print $http_req->as_string;
102 print "\n\n";
103 }
104  
105 #
106 # Sending Request:
107 #
108  
109 my $ua = LWP::UserAgent->new;
110 my $http_res = $ua->request($http_req);
111  
112 #
113 # Response Processing:
114 #
115  
116 if ($self->debug()) {
117 print "HTTP RESPONSE:\n";
118 print $http_res->as_string;
119 print "\n\n";
120 }
121  
122 if ($http_res->code != 200) {
123 die "XPC::Agent::_call_(): Server did not return status 200!\n";
124 }
125  
126 my $xpc_res = XPC->new($http_res->content);
127 $xpc_res = $xpc_res->[0];
128  
129 if ($xpc_res->faults) {
130 my $fault = $xpc_res->fault(0);
131 die "XPC::Agent::_call_(): Detected fault presence but can't find it!\n" unless defined $fault;
132 my $code = $fault->code;
133 my $message = $fault->message;
134 die "Fault [$code]: $message\n";
135 } else {
136 my $result = $xpc_res->result(0);
137 return $result->value;
138 }
139 }
140  
141  
142 #
143 # AUTOLOAD()
144 #
145  
146 use vars qw($AUTOLOAD);
147  
148 sub AUTOLOAD
149 {
150 my ($self, @args) = @_;
151  
152 return if $AUTOLOAD eq 'XPC::Agent::DESTROY';
153  
154 my $procedure = $AUTOLOAD;
155 $procedure =~ s/^XPC::Agent:://;
156  
157 print "Calling procedure '$procedure'...\n" if $self->debug();
158  
159 return $self->_call_($procedure, @args);
160 }
161  
162  
163 1;
164  
165  
166 =head1 NAME
167  
168 XPC::Agent - XML Procedure Call client
169  
170  
171 =head1 SYNOPSIS
172  
173 use XPC::Agent;
174 my $agent = XPC::Agent->new($server_url);
175 printf "localtime() --> %s\n", $agent->localtime();
176  
177  
178 =head1 DESCRIPTION
179  
180 Uses Perl's AUTOLOAD mechanism to intercept calls to undefined subroutines
181 and forward them via XPC over HTTP to a server.
182  
183  
184 =head1 AUTHOR
185  
186 Gregor N. Purdy E<lt>gregor@focusresearch.comE<gt>
187  
188  
189 =head1 COPYRIGHT
190  
191 Copyright (C) 2001 Gregor N. Purdy.
192 All rights reserved.
193  
194 This is free software; you can redistribute it and/or modify it under
195 the same terms as Perl itself.
196