Ignore:
Timestamp:
Oct 18, 2009, 3:59:29 PM (15 years ago)
Author:
Alex Vandiver <alexmv@mit.edu>
Branches:
master, release-1.10, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
f3678c3
Parents:
99cc3dc
git-author:
Alex Vandiver <alexmv@mit.edu> (03/22/09 02:06:46)
git-committer:
Alex Vandiver <alexmv@mit.edu> (10/18/09 15:59:29)
Message:
Make Jabber try to reconnect when disconnected, at exponential intervals

Make the ConnectionManager store auth information on connect, and use
that auth information to try to reconnect and re-auth.  Use some
simple exponential backoff, capped at 5 minutes, as intervals for
reconnecting.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/modules/Jabber/lib/BarnOwl/Module/Jabber/ConnectionManager.pm

    r8ff511d ra957e92  
    4141      if $self->{$jidStr}->{Client};
    4242    delete $self->{$jidStr};
     43
     44    return 1;
     45}
     46
     47sub scheduleReconnect {
     48    my $self = shift;
     49    my $jidStr = shift;
     50    return 0 unless exists $self->{$jidStr};
     51    BarnOwl::admin_message(Jabber => "Disconnected from jabber account $jidStr");
     52
     53    BarnOwl::remove_dispatch($self->{$jidStr}->{Client}->{fileno}) if $self->{$jidStr}->{Client}->{fileno};
     54    $self->{$jidStr}->{Client}->Disconnect()
     55      if $self->{$jidStr}->{Client};
     56
     57    $self->{$jidStr}->{Status} = "reconnecting";
     58    $self->{$jidStr}->{ReconnectBackoff} = 5;
     59    $self->{$jidStr}->{ReconnectAt} = time + $self->{$jidStr}->{ReconnectBackoff};
     60    return 1;
     61}
     62
     63sub setAuth {
     64    my $self = shift;
     65    my $jidStr = shift;
     66    $self->{$jidStr}->{Auth} = shift;
     67}
     68
     69sub tryReconnect {
     70    my $self = shift;
     71    my $jidStr = shift;
     72    my $force = shift;
     73
     74    return 0 unless exists $self->{$jidStr};
     75    return 0 unless $self->{$jidStr}{Status} eq "reconnecting";
     76    return 0 unless $force or (time > $self->{$jidStr}{ReconnectAt});
     77
     78    $self->{$jidStr}->{ReconnectBackoff} *= 2;
     79    $self->{$jidStr}->{ReconnectBackoff} = 60*5
     80        if $self->{$jidStr}->{ReconnectBackoff} > 60*5;
     81    $self->{$jidStr}->{ReconnectAt} = time + $self->{$jidStr}->{ReconnectBackoff};
     82
     83    my $status = $self->{$jidStr}->{Client}->Connect;
     84    return 0 unless $status;
     85
     86    my @result = $self->{$jidStr}->{Client}->AuthSend( %{ $self->{$jidStr}->{Auth} } );
     87    if ( !@result || $result[0] ne 'ok' ) {
     88        $self->removeConnection($jidStr);
     89        BarnOwl::error( "Error in jabber reconnect: " . join( " ", @result ) );
     90        return 0;
     91    }
     92
     93    BarnOwl::admin_message(Jabber => "Reconnected to jabber as $jidStr");
     94    $self->{$jidStr}{Status} = "available";
    4395
    4496    return 1;
     
    88140}
    89141
     142sub jidActive {
     143    my $self = shift;
     144    my $jidStr = shift;
     145    return(exists $self->{$jidStr} and $self->{$jidStr}{Status} eq "available");
     146}
     147
    90148sub sidExists {
    91149    my $self = shift;
     
    110168    my $jid = shift;
    111169    $jid = $jid->GetJID('full') if UNIVERSAL::isa($jid, 'Net::XMPP::JID');
    112     return $self->{$jid}->{Client} if exists $self->{$jid};
     170    return $self->{$jid}->{Client} if $self->jidActive($jid);
    113171}
    114172
     
    127185    my $jid = shift;
    128186    $jid = $jid->GetJID('full') if UNIVERSAL::isa($jid, 'Net::XMPP::JID');
    129     return $self->{$jid}->{Roster} if exists $self->{$jid};
     187    return $self->{$jid}->{Roster} if $self->jidExists($jid);
    130188}
    131189
Note: See TracChangeset for help on using the changeset viewer.