XMPP/Ext/Ping.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 141 CC 15 SCAR 25.0
CC
15
Cov
0%
ping
30.0
_start_cust_timeout
20.0
init
12.0
CRAP 12.2
1 package AnyEvent::XMPP::Ext::Ping;
2 use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
3 use AnyEvent::XMPP::Util qw/simxml/;
4 use AnyEvent::XMPP::Ext;
5 use strict;
6  
7 our @ISA = qw/AnyEvent::XMPP::Ext/;
8  
9 =head1 NAME
10  
11 AnyEvent::XMPP::Ext::Ping - Implementation of XMPP Ping XEP-0199
12  
13 =head1 SYNOPSIS
14  
15 use AnyEvent::XMPP::Ext::Ping;
16  
17 my $con = AnyEvent::XMPP::IM::Connection->new (...);
18 $con->add_extension (my $ping = AnyEvent::XMPP::Ext::Ping->new);
19  
20 # this enables auto-timeout of a connection if it didn't answer
21 # within 120 seconds to a ping with a reply
22 $ping->enable_timeout ($con, 120);
23  
24 my $cl = AnyEvent::XMPP::Client->new (...);
25 $cl->add_extension (my $ping = AnyEvent::XMPP::Ext::Ping->new);
26  
27 # this enables auto-timeout of newly created connections
28 $ping->auto_timeout (120);
29  
30 $ping->ping ($con, 'ping_dest@server.tld', sub {
31 my ($time, $error) = @_;
32 if ($error) {
33 # we got an error
34 }
35 # $time is a float (seconds) of the rtt if you got Time::HiRes
36 });
37  
38 =head1 DESCRIPTION
39  
40 This extension implements XEP-0199: XMPP Ping.
41 It allows you to define a automatic ping timeouter that will disconnect
42 dead connections (which didn't reply to a ping after N seconds). See also
43 the documentation of the C<enable_timeout> method below.
44  
45 It also allows you to send pings to any XMPP entity you like and
46 will measure the time it took if you got L<Time::HiRes>.
47  
48 =head1 METHODS
49  
50 =over 4
51  
52 =item B<new (%args)>
53  
54 Creates a new ping handle.
55  
56 =cut
57  
58 sub new {
59 my $this = shift;
60 my $class = ref($this) || $this;
61 my $self = bless { @_ }, $class;
62 $self->init;
63 $self
64 }
65  
66 =item B<auto_timeout ($timeout)>
67  
68 This method enables automatic connection timeout of
69 new connections. It calls C<enable_timeout> (see below)
70 for every new connection that was connected and emitted
71 a C<stream_ready> event.
72  
73 This is useful if you want connections that have this extension
74 automatically timeouted. In particular this is useful with modules
75 like L<AnyEvent::XMPP::Client> (see also L<SYNOPSIS> above).
76  
77 =cut
78  
79 sub auto_timeout {
80 my ($self, $timeout) = @_;
81  
82 $self->{autotimeout} = $timeout;
83  
84 return if defined $self->{cb_id2};
85  
86 $self->{cb_id2} =
87 $self->reg_cb (
88 stream_ready => sub {
89 my ($self, $con) = @_;
90 $self->enable_timeout ($con, \$self->{autotimeout});
91 },
92 disconnect => sub {
93 my ($self, $con) = @_;
94 $self->disable_timeout ($con);
95 }
96 );
97 }
98  
99 =item B<enable_timeout ($con, $timeout)>
100  
101 This enables a periodical ping on the connection C<$con>.
102 C<$timeout> must be the seconds that the ping intervals last.
103  
104 If the server which is connected via C<$con> didn't respond within C<$timeout>
105 seconds the connection C<$con> will be disconnected.
106  
107 Please note that there already is a basic timeout mechanism
108 for dead TCP connections in L<AnyEvent::XMPP::Connection>, see also
109 the C<whitespace_ping_interval> configuration variable for a connection
110 there. It then will depend on TCP timeouts to disconnect the connection.
111  
112 Use C<enable_timeout> and C<auto_timeout> only if you really feel
113 like you need an explicit timeout for your connections.
114  
115 =cut
116  
117 sub enable_timeout {
118 my ($self, $con, $timeout) = @_;
119 my $rt = $timeout;
120 unless (ref $timeout) {
121 $rt = \$timeout;
122 }
123 $self->_start_cust_timeout ($con, $rt);
124 }
125  
126 sub disable_timeout {
127 my ($self, $con) = @_;
128 delete $self->{cust_timeouts}->{$con};
129 }
130  
131 sub _start_cust_timeout {
132 my ($self, $con, $rtimeout) = @_;
133 return unless $con->is_connected;
134  
135 $self->{cust_timeouts}->{$con} =
136 AnyEvent->timer (after => $$rtimeout, cb => sub {
137 delete $self->{cust_timeouts}->{$con};
138 return unless $con->is_connected;
139  
140 $self->ping ($con, undef, sub {
141 my ($t, $e) = @_;
142  
143 if (defined ($e) && $e->condition eq 'client-timeout') {
144 $con->disconnect ("exceeded ping timeout of $$rtimeout seconds");
145 } else {
146 $self->_start_cust_timeout ($con, $rtimeout)
147 }
148 }, $$rtimeout);
149 });
150 }
151  
152 sub init {
153 my ($self) = @_;
154  
155 if (eval "require Time::HiRes") {
156 $self->{has_time_hires} = 1;
157 }
158  
159 $self->{cb_id} = $self->reg_cb (
160 iq_get_request_xml => sub {
161 my ($self, $con, $node, $handled) = @_;
162  
163 if ($self->handle_ping ($con, $node)) {
164 $$handled = 1;
165 }
166 }
167 );
168 }
169  
170 sub disco_feature { xmpp_ns ('ping') }
171  
172 sub DESTROY {
173 my ($self) = @_;
174 $self->unreg_cb ($self->{cb_id});
175 $self->unreg_cb ($self->{cb_id2}) if defined $self->{cb_id2};
176 }
177  
178 sub handle_ping {
179 my ($self, $con, $node) = @_;
180  
181 if (my ($q) = $node->find_all ([qw/ping ping/])) {
182 unless ($self->{ignore_pings}) {
183 $con->reply_iq_result ($node);
184 }
185 return 1;
186 }
187  
188 0;
189 }
190  
191 =item B<ping ($con, $dest, $cb, $timeout)>
192  
193 This method sends a ping request to C<$dest> via the L<AnyEvent::XMPP::Connection>
194 in C<$con>. If C<$dest> is undefined the ping will be sent to the connected
195 server. C<$cb> will be called when either the ping timeouts, an error occurs
196 or the ping result was received. C<$timeout> is an optional timeout for the
197 ping request, if C<$timeout> is not given the default IQ timeout for the
198 connection is the relevant timeout.
199  
200 The first argument to C<$cb> will be the seconds of the round trip time for
201 that request (If you have L<Time::HiRes>). If you don't have L<Time::HiRes>
202 installed the first argument will be undef.
203  
204 The second argument to C<$cb> will be either undef if no error occured or
205 a L<AnyEvent::XMPP::Error::IQ> error object.
206  
207 =cut
208  
209 sub ping {
210 my ($self, $con, $jid, $cb, $timeout) = @_;
211  
212 my $time = 0;
213 if ($self->{has_time_hires}) {
214 $time = [Time::HiRes::gettimeofday ()];
215 }
216  
217 $con->send_iq (
218 get => { defns => ping => node => { name => 'ping' } },
219 sub {
220 my ($n, $e) = @_;
221  
222 my $elap = 0;
223 if ($self->{has_time_hires}) {
224 $elap = Time::HiRes::tv_interval ($time, [Time::HiRes::gettimeofday ()]);
225 }
226  
227 $cb->($elap, $e);
228 },
229 (defined $jid ? (to => $jid) : ()),
230 (defined $timeout ? (timeout => $timeout) : ()),
231 );
232 }
233  
234 =item B<ignore_pings ($bool)>
235  
236 This method is mostly for testing, it tells this extension
237 to ignore all ping requests and will prevent any response from
238 being sent.
239  
240 =cut
241  
242 sub ignore_pings {
243 my ($self, $enable) = @_;
244 $self->{ignore_pings} = $enable;
245 }
246  
247 =back
248  
249 =head1 AUTHOR
250  
251 Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
252  
253 =head1 COPYRIGHT & LICENSE
254  
255 Copyright 2007, 2008 Robin Redeker, all rights reserved.
256  
257 This program is free software; you can redistribute it and/or modify it
258 under the same terms as Perl itself.
259  
260 =cut
261  
262 1;