RPC/Server.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 83 CC 3 SCAR 16.1
CC
3
Cov
0%
new
12.0
start
2.0
_debug
2.0
CRAP 5.0
1 # Jabber-RPC: Server Functions
2 # (c) DJ Adams 2001
3  
4 # $Id: Server.pm,v 1.1.1.1 2001/09/24 11:23:02 dj Exp $
5  
6 =head1 NAME
7  
8 Jabber::RPC::Server - Jabber-RPC Server
9  
10 =head1 SYNOPSIS
11  
12 use Jabber::RPC::Server;
13  
14 sub function1 {
15 ...
16 }
17  
18 sub function2 {
19 ...
20 }
21  
22 # Server as Jabber component
23 my $server = new Jabber::RPC::Server(
24 server => 'myserver.org:5702',
25 identauth => 'jrpc.myserver.org:secret',
26 connectiontype => 'component',
27 methods => {
28 'rpc.function1' => \&function1,
29 'rpc.function2' => \&function2,
30 }
31 );
32  
33 # or ...
34  
35 # Server as Jabber client
36 my $server = new Jabber::RPC::Server(
37 server => 'myserver.org',
38 identauth => 'id:secret',
39 resource => 'jrpc-server',
40 methods => {
41 'rpc.function1' => \&function1,
42 'rpc.function2' => \&function2,
43 }
44 );
45  
46  
47 $server->start;
48  
49
50 =head1 DESCRIPTION
51  
52 Jabber::RPC::Server is an implementation of a Jabber-RPC server.
53 A Jabber-RPC server receives XML-RPC-encoded calls carried over
54 Jabber and responds to them.
55  
56 You can have your Jabber::RPC::Server connect as a Jabber component
57 or as a Jabber client - see the differences in the SYNOPSIS example
58 above. (One difference is that you need to specify connectiontype =>
59 'component' for a Jabber component connection - the default assumption
60 is that you want to connect as a Jabber client.)
61  
62 If you don't specify a value for the 'resource' argument (in the
63 SYNOPSIS example, a value is specified for the Jabber client 'flavour'
64 but not for the Jabber component 'flavour') then the default of
65 'jrpc-server' will be used.
66  
67 =head1 VERSION
68  
69 early
70  
71 =head1 AUTHOR
72  
73 DJ Adams
74  
75 =head1 SEE ALSO
76  
77 Jabber::RPC::Client, Jabber::Connection
78  
79 =cut
80  
81 package Jabber::RPC::Server;
82  
83 use Frontier::RPC2;
84 use Jabber::Connection;
85 use Jabber::NS qw(:all);
86  
87 use constant RESOURCE => 'jrpc-server';
88  
89 use strict;
90  
91 sub new {
92 my $class = shift; my %args = @_;
93 my $self = {};
94  
95 # to supply:
96 # server: jabber server/port
97 # identauth: jabber identity/password
98 # connectiontype: component (optional, defaults to client)
99 # methods: hashref of methods and handlers
100
101 # Host/port and Identity
102 $self->{server} = $args{server};
103 ($self->{id}, $self->{pass}) = split(':', $args{identauth});
104 $self->{methods} = $args{methods};
105 $self->{connectiontype} = lc $args{connectiontype} || 'client';
106  
107 # Connect as Component ?
108 if ($self->{connectiontype} eq 'component') {
109  
110 _debug("Component connection requested");
111  
112 $self->{connection} = new Jabber::Connection(
113 server => $self->{server},
114 localname => $self->{id},
115 ns => NS_ACCEPT,
116 log => 1,
117 debug => 1,
118 );
119  
120 $self->{connection}->connect
121 or die "oops: ".$self->{connection}->lastError;
122  
123 $self->{connection}->auth($self->{pass});
124  
125 }
126  
127 # Default: Connect as Client
128 else {
129  
130 _debug("Client connection requested");
131  
132 $self->{connection} = new Jabber::Connection(
133 server => $self->{server},
134 log => 1,
135 debug => 1,
136 );
137  
138 $self->{connection}->connect
139 or die "oops: ".$self->{connection}->lastError;
140  
141 $self->{connection}->auth($self->{id}, $self->{pass}, $args{resource} || RESOURCE);
142  
143 }
144  
145 # XMLRPC server
146 $self->{RPC2} = new Frontier::RPC2;
147  
148 # Set up request handler
149 $self->{connection}->register_handler(
150  
151 'iq' => sub {
152  
153 my $node = shift;
154 _debug("rpc_handler received: ", $node->toStr);
155  
156 my $request = $node->getTag('query', NS_RPC)->getTag('methodCall');
157 my $response = $self->{RPC2}->serve($request->toStr, $self->{methods});
158 $response =~ s/^<\?[^>]+>\n//;
159 $node->attr('type', IQ_RESULT);
160  
161 my $from = $node->attr('from');
162  
163 # Must set 'from' ourselves if we're a component
164 if ($self->{connectiontype} eq 'component') {
165 $node->attr('from', $node->attr('to'));
166 }
167 else {
168 $node->attr('from', '');
169 }
170  
171 $node->attr('to', $from);
172 $request->hide;
173 $node->getTag('query')->rawdata($response);
174 $self->{connection}->send($node);
175  
176 }
177  
178 );
179 _debug("Request handler set up");
180  
181 bless $self, $class;
182 return $self;
183
184 }
185  
186 # Start the service
187 sub start {
188  
189 my $self = shift;
190  
191 _debug("Starting process...");
192  
193 # Go!
194 $self->{connection}->start;
195  
196 }
197  
198  
199 sub _debug {
200  
201 print STDERR "DEBUG: @_\n";
202  
203 }
204  
205 1;
206