Subject: | SRV-Support from Net::XMPP2::Connection missed |
This patch adds SRV-Support from Net::XMPP2::Connection (0.14) to
AnyEvent::XMPP::Connection
Subject: | Connection.pm.patch |
--- /usr/lib/perl5/vendor_perl/5.8.8/AnyEvent/XMPP/Connection.pm 2009-01-26 16:59:30.647720000 +0100
+++ Connection.pm 2009-01-26 16:55:14.539813905 +0100
@@ -2,6 +2,7 @@
use strict;
use AnyEvent;
use IO::Socket::INET;
+use Net::DNS::Resolver;
use AnyEvent::XMPP::Parser;
use AnyEvent::XMPP::Writer;
use AnyEvent::XMPP::Util qw/split_jid join_jid simxml/;
@@ -285,10 +286,18 @@
return $self;
}
-=item B<connect ()>
+=item B<connect ($no_srv_rr)>
Try to connect (non blocking) to the domain and port passed in C<new>.
+A SRV RR lookup will be performed on the domain to discover
+the host and port to use. If you don't want this set C<$no_srv_rr>
+to a true value. C<$no_srv_rr> is false by default.
+
+As the SRV RR lookup might return multiple host and you fail to
+connect to one you might just call this function again to try a
+different host.
+
The connection is performed non blocking, so this method will just
trigger the connection process. The event C<connect> will be emitted
when the connection was successfully established.
@@ -302,9 +311,30 @@
=cut
sub connect {
- my ($self) = @_;
+ my ($self, $no_srv_rr) = @_;
my ($host, $port) = ($self->{domain}, defined $self->{port} ? $self->{port} : 5222);
+ if ($self->{override_host}) {
+ $host = $self->{override_host};
+
+ } else {
+ unless ($no_srv_rr) {
+ my $res = Net::DNS::Resolver->new;
+ my $p = $res->query ('_xmpp-client._tcp.'.$host, 'SRV');
+ if ($p) {
+ my @srvs = grep { $_->type eq 'SRV' } $p->answer;
+ if (@srvs) {
+ @srvs = sort { $a->priority <=> $b->priority } @srvs;
+ @srvs = sort { $b->weight <=> $a->weight } @srvs; # TODO
+ $port = $srvs[0]->port;
+ $host = $srvs[0]->target;
+ }
+ }
+ }
+ }
+
+ $port = $self->{override_port} if defined $self->{override_port};
+
$self->SUPER::connect ($host, $port, $self->{connect_timeout});
}