| 1 | package Horris::Connection::Plugin::Retry; | ||
| 2 | # ABSTRACT: Auto Reconnect Plugin on Horris | ||
| 3 | |||
| 4 | |||
| 5 | use Moose; | ||
| 6 | use AnyEvent::RetryTimer; | ||
| 7 | extends 'Horris::Connection::Plugin'; | ||
| 8 | with 'MooseX::Role::Pluggable::Plugin'; | ||
| 9 | |||
| 10 | my $timer; | ||
| 11 | sub on_connect { | ||
| 12 | if( $timer ) { | ||
| 13 | $timer->success; | ||
| 14 | undef $timer; | ||
| 15 | } | ||
| 16 | } | ||
| 17 | sub on_disconnect { | ||
| 18 | my ($self) = @_; | ||
| 19 | $timer ||= AnyEvent::RetryTimer->new ( | ||
| 20 | on_retry => sub { | ||
| 21 | my ($timer) = @_; | ||
| 22 | $self->connection->irc->connect($self->connection->server, $self->connection->port, { | ||
| 23 | nick => $self->connection->nickname, | ||
| 24 | user => $self->connection->username, | ||
| 25 | password => $self->connection->password, | ||
| 26 | timeout => 1, | ||
| 27 | }); | ||
| 28 | |||
| 29 | $timer->retry; | ||
| 30 | }, | ||
| 31 | ); | ||
| 32 | } | ||
| 33 | |||
| 34 | __PACKAGE__->meta->make_immutable; | ||
| 35 | |||
| 36 | 1; | ||
| 37 | |||
| 38 | __END__ |