809
|
1 |
package TestConnection; |
|
2 |
|
|
3 |
use strict; |
|
4 |
use warnings; |
|
5 |
use parent 'AnyEvent::XMPP::IM::Connection'; |
|
6 |
|
|
7 |
use 5.010; |
|
8 |
|
|
9 |
our $HOST = 'localhost'; |
|
10 |
our $TIMEOUT = 5; |
|
11 |
our %PASSWORD_FOR = ( |
|
12 |
one => '12345', |
|
13 |
two => '23451', |
|
14 |
three => '34512', |
|
15 |
four => '45123', |
|
16 |
five => '51234', |
866
|
17 |
six => '123456', |
809
|
18 |
); |
|
19 |
|
|
20 |
sub new { |
|
21 |
my ( $class, $username, %options ) = @_; |
|
22 |
|
|
23 |
my $cond = AnyEvent->condvar; |
|
24 |
my $timer = AnyEvent->timer( |
|
25 |
after => $TIMEOUT, |
|
26 |
cb => sub { |
|
27 |
$cond->send('timeout'); |
|
28 |
}, |
|
29 |
); |
|
30 |
|
|
31 |
my $self = $class->SUPER::new( |
|
32 |
username => $username, |
|
33 |
domain => $HOST, |
|
34 |
password => $options{'password'} // $PASSWORD_FOR{$username}, |
|
35 |
); |
|
36 |
|
|
37 |
$self->reg_cb(error => sub { |
|
38 |
my ( undef, $error ) = @_; |
|
39 |
|
|
40 |
$cond->send($error->string); |
|
41 |
}); |
|
42 |
|
|
43 |
bless $self, $class; |
|
44 |
|
|
45 |
$self->{'condvar'} = $cond; |
|
46 |
$self->{'timeout_timer'} = $timer; |
|
47 |
|
|
48 |
$self->connect; |
|
49 |
|
|
50 |
return $self; |
|
51 |
} |
|
52 |
|
|
53 |
sub cond { |
|
54 |
my ( $self ) = @_; |
|
55 |
|
|
56 |
return $self->{'condvar'}; |
|
57 |
} |
|
58 |
|
|
59 |
1; |