DJabberd/Queue.pm

filter: Statementstmt 24.5% 30 / 122 Branchbran 0.0% 0 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 37.5% 9 / 24 Podpod 0.0% 0 / 12 totaltotal 19.9% 39 / 196 CC 20 SCAR 27.1
CC
20
Cov
19%
new
56.0
on_connection_error
20.0
enqueue
12.0
CRAP 15.1
« DJabberd/Connection/ComponentIn.pm ↑ Summary DJabberd/Stanza/DialbackResult.pm »
1 package DJabberd::Queue;
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 our @EXPORT_OK = qw(NO_CONN RESOLVING CONNECTING CONNECTED);
9  
10 20 ▶use DJabberd::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 20 our $logger = DJabberd::Log->get_logger;
13  
14 20 ▶use fields (
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 'vhost',
16 'endpoints',
17 'to_deliver',
18 'last_connect_fail',
19 'state',
20 'connection',
21 20 );
22  
23 20 ▶use constant NO_CONN => \ "no connection";
Subroutine BEGIN: called CC=1 SCAR=0.0
24 20 ▶use constant RESOLVING => \ "resolving";
Subroutine BEGIN: called CC=1 SCAR=0.0
25 20 ▶use constant CONNECTING => \ "connecting";
Subroutine BEGIN: called CC=1 SCAR=0.0
26 20 ▶use constant CONNECTED => \ "connected";
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod undocumented
29 0 my $self = shift;
30 0 my %opts = @_;
31  
32 0 ▶ $self = fields::new($self) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
33  
34 0 ▶ $self->{vhost} = delete $opts{vhost} or die "vhost required";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"vhost"} = delete $opts{"vhost"} or die "vhost required" 0 0 0
35 0 ▶ Carp::croak("Not a vhost: $self->{vhost}") unless $self->vhost->isa("DJabberd::VHost");
Branch: 1 branch
BranchTrueFalseTotal
unless $self->vhost->isa("DJabberd::VHost") 0 0 0
36  
37 0 ▶ if (my $endpoints = delete $opts{endpoints}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $endpoints = delete $opts{'endpoints'}) { } 0 0 0
38 0 ▶ Carp::croak("endpoints must be an arrayref") unless (ref $endpoints eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $endpoints eq "ARRAY" 0 0 0
39 0 $self->{endpoints} = $endpoints;
40 } else {
41 0 $self->{endpoints} = [];
42 }
43  
44 0 ▶ die "too many opts" if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
45  
46 0 $self->{to_deliver} = []; # DJabberd::QueueItem?
47 0 $self->{last_connect_fail} = 0; # unixtime of last connection failure
48  
49 0 $self->{state} = NO_CONN; # see states above
50  
51 0 return $self;
52 }
53  
54 0 ▶sub endpoints {
Subroutine endpoints: not called CC=2 SCAR=17.9
Pod undocumented
55 0 my $self = shift;
56 0 my $endpoints = $self->{endpoints};
57  
58 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
59 0 @$endpoints = @_;
60 }
61 0 return @$endpoints;
62 }
63  
64 # called by Connection::ServerOut constructor
65 0 ▶sub set_connection {
Subroutine set_connection: not called CC=1 SCAR=6.9
Pod undocumented
66 0 my ($self, $conn) = @_;
67 0 $logger->debug("Set connection for queue to '$self->{domain}' to connection '$conn->{id}'");
68 0 $self->{connection} = $conn;
69 }
70  
71 0 ▶sub vhost {
Subroutine vhost: not called CC=1 SCAR=6.9
Pod undocumented
72 0 my $self = shift;
73 0 return $self->{vhost};
74 }
75  
76 0 ▶sub enqueue {
Subroutine enqueue: not called CC=3 SCAR=24.8
Pod undocumented
77 0 my ($self, $stanza, $cb) = @_;
78  
79 0 $logger->debug("Queuing stanza (" . $stanza . ") for");
80  
81 0 ▶ if ($self->{state} == NO_CONN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} == \"no connection") 0 0 0
82 0 $logger->debug(" .. starting to connect to");
83 0 $self->start_connecting;
84 }
85  
86 0 ▶ if ($self->{state} == CONNECTED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'state'} == \'connected') { } 0 0 0
87 0 $logger->debug(" .. already connected, writing stanza.");
88 0 $self->{connection}->send_stanza($stanza);
89 0 $cb->delivered;
90 } else {
91 0 $logger->debug(" .. pushing queue item.");
92 0 push @{ $self->{to_deliver} }, DJabberd::QueueItem->new($stanza, $cb);
93 }
94 }
95  
96 0 ▶sub failed_to_connect {
Subroutine failed_to_connect: not called CC=2 SCAR=17.9
Pod undocumented
97 0 my $self = shift;
98 0 $self->{state} = NO_CONN;
99 0 $self->{last_connect_fail} = time();
100  
101 0 $logger->debug("Failed to connect queue");
102 0 ▶ while (my $qi = shift @{ $self->{to_deliver} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $qi = shift @{$$self{"to_deliver"};}) 0 0 0
103 0 $qi->callback->error;
104 }
105 }
106  
107 # called by our connection when it's connected
108 0 ▶sub on_connection_connected {
Subroutine on_connection_connected: not called CC=3 SCAR=24.8
Pod undocumented
109 0 my ($self, $conn) = @_;
110 0 $logger->debug("connection $conn->{id} connected! conn=$conn->{id}, selfcon=$self->{connection}->{id}");
111  
112 # TODO why are we this checking here?
113 0 ▶ return unless $conn == $self->{connection};
Branch: 1 branch
BranchTrueFalseTotal
unless $conn == $self->{"connection"} 0 0 0
114  
115 0 $logger->debug(" ... unloading queue items");
116 0 $self->{state} = CONNECTED;
117 0 ▶ while (my $qi = shift @{ $self->{to_deliver} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $qi = shift @{$$self{"to_deliver"};}) 0 0 0
118 0 $conn->send_stanza($qi->stanza);
119 0 $qi->callback->delivered;
120 # TODO: the connection might need to handle marking things as delivered
121 # otherwise we could run into a problem if the connection dies mid-stanza.
122 }
123 }
124  
125 0 ▶sub on_connection_failed {
Subroutine on_connection_failed: not called CC=2 SCAR=17.9
Pod undocumented
126 0 my ($self, $conn) = @_;
127 0 $logger->debug("connection failed for queue");
128 0 ▶ return unless $conn == $self->{connection};
Branch: 1 branch
BranchTrueFalseTotal
unless $conn == $self->{"connection"} 0 0 0
129 0 $logger->debug(" .. match");
130 0 return $self->failed_to_connect;
131 }
132  
133 0 ▶sub on_connection_error {
Subroutine on_connection_error: not called CC=4 SCAR=30.0
Pod undocumented
134 0 my ($self, $conn) = @_;
135 0 $logger->debug("connection error for queue");
136 0 ▶ return unless $conn == $self->{connection};
Branch: 1 branch
BranchTrueFalseTotal
unless $conn == $self->{"connection"} 0 0 0
137 0 $logger->debug(" .. match");
138 0 my $pre_state = $self->{state};
139  
140 0 $self->{state} = NO_CONN;
141 0 $self->{connection} = undef;
142  
143 0 ▶ if ($pre_state == CONNECTING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_state == \'connecting') { } 0 0 0
144 # died while connecting: no more luck
145 0 $self->give_up_connecting;
146 0 $self->on_final_error;
147 } else {
148 # died during an active connection, let's try again
149 0 ▶ if (@{ $self->{to_deliver} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"to_deliver"};}) 0 0 0
150 0 $logger->warn("Reconnecting to '$self->{domain}'");
151 0 $self->start_connecting;
152 }
153 }
154 }
155  
156 0 ▶sub on_final_error {
Subroutine on_final_error: not called CC=2 SCAR=17.9
Pod undocumented
157 0 my $self = shift;
158 0 ▶ while (my $qi = shift @{ $self->{to_deliver} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $qi = shift @{$$self{"to_deliver"};}) 0 0 0
159 0 $qi->callback->error("connection failure");
160 }
161 }
162  
163 0 ▶sub start_connecting {
Subroutine start_connecting: not called CC=3 SCAR=24.8
Pod undocumented
164 0 my $self = shift;
165 0 $logger->debug("Starting connection");
166 0 ▶ die unless $self->{state} == NO_CONN;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"state"} == \"no connection" 0 0 0
167  
168 0 my $endpoints = $self->{endpoints};
169  
170 0 ▶ unless (@$endpoints) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$endpoints) 0 0 0
171 0 $self->failed_to_connect;
172 0 return;
173 }
174  
175 0 $self->{state} = CONNECTING;
176  
177 0 my $endpt = $endpoints->[0];
178 0 my $conn = $self->new_connection(endpoint => $endpt, queue => $self);
179 0 $self->set_connection($conn);
180 0 $conn->start_connecting;
181  
182 }
183  
184 0 ▶sub new_connection {
Subroutine new_connection: not called CC=1 SCAR=6.9
Pod undocumented
185 0 die "Sorry, this method needs to be overridden in your subclass of " . ref( $_[0] ) . ".";
186 }
187  
188 package DJabberd::QueueItem;
189  
190 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
191 0 my ($class, $stanza, $cb) = @_;
192 0 return bless [ $stanza, $cb ], $class;
193 }
194  
195 0 ▶sub stanza { return $_[0][0] }
Subroutine stanza: not called CC=1 SCAR=6.9
196 0 ▶sub callback { return $_[0][1] }
Subroutine callback: not called CC=1 SCAR=6.9
197  
198 20 1;
« DJabberd/Connection/ComponentIn.pm ↑ Summary DJabberd/Stanza/DialbackResult.pm »