blib/lib/Message/Passing/Input/Freeswitch.pm

filter: Statementstmt 73.7% 59 / 80 Branchbran 50.0% 7 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 75.0% 12 / 16 Podpod 0.0% 0 / 1 totaltotal 68.4% 78 / 114 CC 10 SCAR 11.0
CC
10
Cov
69%
_terminate_connection
6.0
__ANON__
4.9
__ANON__
4.3
CRAP 3.0
↑ Summary
1 package Message::Passing::Input::Freeswitch;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Scalar::Util qw/ weaken /;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Try::Tiny qw/ try catch /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.006';
9 2 $VERSION = eval $VERSION;
10  
11 2 with qw/
12 Message::Passing::Role::Input
13 Message::Passing::Role::HasHostnameAndPort
14 /;
15  
16 1 ▶sub _default_port { 8021 }
Subroutine _default_port: called CC=1 SCAR=0.0
17  
18 2 has secret => (
19 is => 'ro',
20 isa => 'Str',
21 required => 1,
22 );
23  
24 2 has connection_retry_timeout => (
25 is => 'ro',
26 isa => 'Int',
27 default => 3,
28 );
29  
30 has event_types => (
31 is => 'ro',
32 isa => 'ArrayRef[Str]',
33 1 ▶ default => sub { ['all'] },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 2 );
35  
36 has '_connection' => (
37 isa => 'ESL::ESLconnection',
38 lazy => 1,
39 1 ▶ default => sub {
Subroutine __ANON__: called CC=4 SCAR=16.0
40 1 my $self = shift;
41 1 require ESL;
42 # FIXME Retarded SWIG bindings want the port number as a string, not an int
43 # so we explicitly stringify it
44 1 my $con = new ESL::ESLconnection($self->hostname, $self->port."", $self->secret);
45 1 ▶ unless ($con) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($con) 0 1 1
46 0 warn("Could not connect to freeswitch on " . $self->hostname . ":" . $self->port);
47 0 $self->_terminate_connection($self->connection_retry_timeout);
48 0 $con = bless {}, 'ESL::ESLconnection';
49 }
50 1 foreach my $name (@{$self->event_types}) {
51 1 $con->events("plain", $name);
52 }
53 1 ▶ $con->connected() || do {
Branch: 1 branch
BranchTrueFalseTotal
$con->connected or do { $con->disconnect; $self->_terminate_connection($self->connection_retry_timeout) } 1 0 1
54 0 $con->disconnect;
55 0 $self->_terminate_connection($self->connection_retry_timeout);
56 };
57 1 return $con;
58 },
59 2 is => 'ro',
60 clearer => '_clear_connection',
61 handles => {
62 _connection_fd => 'socketDescriptor',
63 },
64 );
65  
66 1 ▶sub _try_rx {
Subroutine _try_rx: called CC=4 SCAR=14.4
67 1 my $self = shift;
68 1 my $con = $self->_connection;
69 1 ▶ if (!$con->connected) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($con->connected) 0 1 1
70 0 $self->_terminate_connection;
71 0 return;
72 }
73 1 my $e = $con->recvEventTimed(0);
74  
75 1 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 1 0 1
76 1 my %data;
77 1 my $h = $e->firstHeader();
78 1 ▶ while ($h) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h) 2 1 3
79 2 $data{$h} = $e->getHeader($h);
80 2 $h = $e->nextHeader();
81 }
82 1 $self->output_to->consume(\%data);
83 1 return 1;
84 }
85 0 return;
86 }
87  
88 has _io_reader => (
89 is => 'ro',
90 lazy => 1,
91 1 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
92 1 my $weak_self = shift;
93 1 weaken($weak_self);
94 1 my $fd = $weak_self->_connection_fd;
95 1 ▶ return unless $fd >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $fd >= 0 0 1 1
96 AE::io $fd, 0,
97 0 ▶ sub { my $more; do { $more = $weak_self->_try_rx } while ($more) };
Subroutine __ANON__: not called CC=2 SCAR=14.6
Branch: 1 branch
BranchTrueFalseTotal
$more and do { do { $more = $weak_self->_try_rx } } 0 0 0
98 },
99 2 clearer => '_clear_io_reader',
100 );
101  
102 0 ▶sub _terminate_connection {
Subroutine _terminate_connection: not called CC=2 SCAR=17.9
103 0 my ($self, $retry) = @_;
104 0 ▶ $retry ||= 0;
Condition0%0/2
$retry ||= 0
l!l
00
MC/DC0%0/1
$retry ||= 0
$retry
Truth table0%0/2
A
A= $retry
Aresult
11
00
105 0 weaken($self);
106 # Push building the io reader here as an idle task,
107 # to avoid blowing up the stack.... (We're already in a callback here)
108 # This probably isn't totally necessary, but avoids potential recursion issues.
109 0 my $i; $i = AnyEvent->timer(
110 after => $retry,
111 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 0 undef $i;
113 0 $self->_clear_io_reader;
114 0 $self->_clear_connection;
115 0 $self->_io_reader;
116 },
117 0 );
118 }
119  
120 has _connection_checker => (
121 is => 'ro',
122 lazy => 1,
123 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.2
124 1 my $self = shift;
125 1 weaken($self);
126 AnyEvent->timer( after => $self->connection_retry_timeout, every => $self->connection_retry_timeout,
127 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 $self->_try_rx;
129 },
130 1 );
131 },
132 2 );
133  
134 1 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
135 1 my $self = shift;
136 1 $self->_io_reader;
137 1 $self->_connection_checker;
138 }
139  
140 2 1;
141  
142 =head1 NAME
143  
144 Message::Passing::Input::Freeswitch - input messages from Freeswitch.
145  
146 =head1 SYNOPSIS
147  
148 message-pass --input Freeswitch --input_options \
149 '{"hostname":"127.0.0.1","secret":"s3kriTk3y"}' \
150 --output STDOUT
151  
152 =head1 DESCRIPTION
153  
154 Produces a message stream from a L<Freeswitch|http://www.freeswitch.org/>
155 instance.
156  
157 Uses the Freeswitch L<|Event Socket Library|http://wiki.freeswitch.org/wiki/Event_Socket_Library>
158 to connect to a local or remote Freeswitch instance and stream event messages.
159  
160 =head1 ATTRIBUTES
161  
162 =head2 hostname
163  
164 The Freeswitch host to connect to.
165  
166 =head2 secret
167  
168 The secret configured in Freeswitch for connecting to the event listener socket.
169  
170 =head2 connection_retry_timeout
171  
172 The number of seconds to wait after a disconnect before reconnecting. Default 3.
173  
174 =head2 port
175  
176 The port that Freeswitch's ESL socket is listening on. Defaults to 8021.
177  
178 =head2 events
179  
180 An arrayref of types of events to listen for. These types are documented
181 L<on the Freeswitch wiki|http://wiki.freeswitch.org/wiki/Event_List>.
182  
183 By default, the special type, C<all> is used - which means all event types
184 are subscribed to.
185  
186 =head1 SEE ALSO
187  
188 =over
189  
190 =item L<Message::Passing>
191  
192 =item L<http://www.freeswitch.org/>
193  
194 =item L<http://wiki.freeswitch.org/wiki/Event_Socket_Library>
195  
196 =item L<http://wiki.freeswitch.org/wiki/Event_List>
197  
198 =back
199  
200 =head1 AUTHOR
201  
202 Tomas (t0m) Doran <bobtfish@bobtfish.net>
203  
204 =head1 SPONSORSHIP
205  
206 This module exists due to the wonderful people at Suretec Systems Ltd.
207 <http://www.suretecsystems.com/> who sponsored it's development for its
208 VoIP division called SureVoIP <http://www.surevoip.co.uk/> for use with
209 the SureVoIP API -
210 <http://www.surevoip.co.uk/support/wiki/api_documentation>
211  
212 =head1 COPYRIGHT
213  
214 Copyright Suretec Systems Ltd. 2012.
215  
216 Logstash (upon which many ideas for this project is based, but
217 which we do not reuse any code from) is copyright 2010 Jorden Sissel.
218  
219 =head1 LICENSE
220  
221 GNU Affero General Public License, Version 3
222  
223 If you feel this is too restrictive to be able to use this software,
224 please talk to us as we'd be willing to consider re-licensing under
225 less restrictive terms.
226  
227 =cut
228  
↑ Summary