| 1 | # Jabber-RPC: Client Functions | ||
| 2 | # (c) DJ Adams 2001 | ||
| 3 | |||
| 4 | # $Id: Client.pm,v 1.1.1.1 2001/09/24 11:23:02 dj Exp $ | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Jabber::RPC::Client - Jabber-RPC Client | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | use Jabber::RPC::Client; | ||
| 13 | |||
| 14 | # Client connects as Jabber client | ||
| 15 | my $client = new Jabber::RPC::Client( | ||
| 16 | server => 'myserver.org', | ||
| 17 | identauth => 'user:password', | ||
| 18 | endpoint => 'jrpc.myserver.org/rpc-server', | ||
| 19 | ); | ||
| 20 | |||
| 21 | my $result = $client->call('examples.getStateName', 5); | ||
| 22 | print "getStateName: ", $result || $client->lastfault, "\n"; | ||
| 23 | |||
| 24 | # Switch endpoints | ||
| 25 | $client->endpoint('another@endpoint.org/resource'); | ||
| 26 | |||
| 27 | # This time the call will go to the new endpoint | ||
| 28 | my $result = $client->call('examples.getStateList', [12,13,14]); | ||
| 29 | print "getStateList: ", $result || $client->lastfault, "\n"; | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | Jabber::RPC::Client is an implementation of a Jabber-RPC client. | ||
| 34 | A Jabber-RPC client sends XML-RPC-encoded calls carried over | ||
| 35 | Jabber to an endpoint that is a Jabber::RPC::Server. | ||
| 36 | |||
| 37 | It uses the Frontier::RPC2 XML-RPC implementation to make the | ||
| 38 | encoding and decoding calls. | ||
| 39 | |||
| 40 | The endpoint can either be a Jabber component or a Jabber client | ||
| 41 | (see the SYNOPSIS examples for Jabber::RPC::Server) - specify the | ||
| 42 | JID of the endpoint appropriately. | ||
| 43 | |||
| 44 | You can change the endpoint with the endpoint() function. | ||
| 45 | |||
| 46 | If you don't specify any value for a 'resource' argument, a default | ||
| 47 | of 'jrpc-client' will be used. Bear in mind that if you haven't | ||
| 48 | specified a value for the 'resource' argument when starting the | ||
| 49 | Jabber::RPC::Server, a default of 'jrpc-server' will be used there. | ||
| 50 | Hence the endpoint of 'jrpc.myserver.org/rpc-server' in the | ||
| 51 | synopsis example above. | ||
| 52 | |||
| 53 | =head1 VERSION | ||
| 54 | |||
| 55 | early | ||
| 56 | |||
| 57 | =head1 AUTHOR | ||
| 58 | |||
| 59 | DJ Adams | ||
| 60 | |||
| 61 | =head1 SEE ALSO | ||
| 62 | |||
| 63 | Jabber::RPC::Server, Jabber::Connection | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | package Jabber::RPC::Client; | ||
| 68 | |||
| 69 | use Frontier::RPC2; | ||
| 70 | use Jabber::Connection; | ||
| 71 | use Jabber::NodeFactory; | ||
| 72 | use Jabber::NS qw(:all); | ||
| 73 | |||
| 74 | use constant RESOURCE => 'jrpc-client'; | ||
| 75 | |||
| 76 | use vars qw/$AUTOLOAD/; | ||
| 77 | |||
| 78 | use strict; | ||
| 79 | |||
| 80 | sub new { | ||
| 81 | |||
| 82 | # to be supplied | ||
| 83 | # server : jabber server:port e.g. merlix:5222 | ||
| 84 | # identauth : user:password e.g. dj:secret | ||
| 85 | # endpoint : target Jabber-RPC responder | ||
| 86 | # resource: the resource to connect with (optional) | ||
| 87 | |||
| 88 | my $class = shift; my %args = @_; | ||
| 89 | my $self = {}; | ||
| 90 | |||
| 91 | # My (the client's) host/port and Identity | ||
| 92 | $self->{server} = $args{server}; | ||
| 93 | ($self->{id}, $self->{pass}) = split(':', $args{identauth}); | ||
| 94 | |||
| 95 | # Target XMLRPC server | ||
| 96 | $self->{endpoint} = $args{endpoint}; | ||
| 97 | |||
| 98 | # Connect to Jabber | ||
| 99 | $self->{connection} = new Jabber::Connection( | ||
| 100 | server => $self->{server}, | ||
| 101 | # log => 1, | ||
| 102 | # debug => 1, | ||
| 103 | ); | ||
| 104 | |||
| 105 | $self->{connection}->connect | ||
| 106 | or die "oops: ".$self->{connection}->lastError; | ||
| 107 | |||
| 108 | $self->{connection}->auth($self->{id}, $self->{pass}, $args{resource} || RESOURCE); | ||
| 109 | |||
| 110 | # XMLRPC server | ||
| 111 | $self->{RPC2} = new Frontier::RPC2; | ||
| 112 | |||
| 113 | # Node factory | ||
| 114 | $self->{nf} = new Jabber::NodeFactory; | ||
| 115 | |||
| 116 | bless $self, $class; | ||
| 117 | return $self; | ||
| 118 | |||
| 119 | } | ||
| 120 | |||
| 121 | sub call { | ||
| 122 | |||
| 123 | my $self = shift; | ||
| 124 | |||
| 125 | # Clear last fault info, results and results | ||
| 126 | delete $self->{lastfault}; | ||
| 127 | |||
| 128 | # Create XML call payload | ||
| 129 | my $request = $self->{RPC2}->encode_call(@_); | ||
| 130 | $request =~ s/^<\?[^>]+>\n//; # remove XML declaration | ||
| 131 | |||
| 132 | # Create an IQ | ||
| 133 | my $set = $self->{nf}->newNode('iq'); | ||
| 134 | $set->attr('type', IQ_SET); | ||
| 135 | $set->attr('to', $self->{endpoint}); | ||
| 136 | $set->insertTag('query', NS_RPC)->rawdata($request); | ||
| 137 | |||
| 138 | # Send it | ||
| 139 | my $result = $self->{connection}->ask($set); | ||
| 140 | |||
| 141 | # Check for error | ||
| 142 | if ($result->attr('type') eq IQ_ERROR) { | ||
| 143 | my $error = $result->getTag('error'); | ||
| 144 | $self->{lastfault} = $error->data." (".$error->attr('code').")"; | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | # Extract the response | ||
| 149 | my $response = $result->getTag('query', NS_RPC)->getTag('methodResponse'); | ||
| 150 | |||
| 151 | my $struct = $self->{RPC2}->decode($response->toStr); | ||
| 152 | return $struct->{'value'}[0]; | ||
| 153 | |||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | sub endpoint { | ||
| 158 | |||
| 159 | my $self = shift; | ||
| 160 | $self->{endpoint} = $_[0] if $_[0]; | ||
| 161 | return $self->{endpoint}; | ||
| 162 | |||
| 163 | } | ||
| 164 | |||
| 165 | |||
| 166 | sub DESTROY { | ||
| 167 | my $self = shift; | ||
| 168 | $self->{connection}->disconnect; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub AUTOLOAD { | ||
| 172 | # Not sure I really want this... | ||
| 173 | my $self = shift; | ||
| 174 | $AUTOLOAD =~ s/^.*:://; | ||
| 175 | return if $AUTOLOAD eq "DESTROY"; # bzzt! | ||
| 176 | |||
| 177 | my %functions = ( | ||
| 178 | 'base64' => 'Base64', | ||
| 179 | 'boolean' => 'Boolean', | ||
| 180 | 'double' => 'Double', | ||
| 181 | 'int' => 'Integer', | ||
| 182 | 'string' => 'String', | ||
| 183 | 'date_time' => 'DateTime', | ||
| 184 | ); | ||
| 185 | |||
| 186 | return unless defined($functions{$AUTOLOAD}); | ||
| 187 | |||
| 188 | my $retval; | ||
| 189 | eval "\$retval = new Frontier::RPC2::".$functions{$AUTOLOAD}."(\@_)"; | ||
| 190 | |||
| 191 | return $retval; | ||
| 192 | } | ||
| 193 | |||
| 194 | |||
| 195 | sub lastfault { | ||
| 196 | |||
| 197 | my $self = shift; | ||
| 198 | return $self->{'lastfault'}; | ||
| 199 | |||
| 200 | } | ||
| 201 | |||
| 202 | 1; |