eris/Client.pm

filter: Statementstmt 46.0% 47 / 102 Branchbran 20.0% 6 / 30 Conditioncond 10.0% 1 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 48.0% 12 / 25 Podpod 0.0% 0 / 5 totaltotal 37.0% 66 / 178 CC 22 SCAR 30.6
CC
22
Cov
38%
setup_pipe
72.0
handle_message
42.3
_connect
9.3
CRAP 21.3
« eris/Server.pm ↑ Summary eris.pm »
1 package AnyEvent::eris::Client;
2 # ABSTRACT: eris pub/sub Client
3  
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Parse::Syslog::Line 'parse_syslog_line';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # we recognize these
15 12 my @PROTOCOL_LINE_PREFIXES = (
16 'Subscribe to :',
17 'Receiving ',
18 'Full feed enabled',
19 'EHLO Streamer',
20 );
21  
22 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
23 3 my ( $class, %opts ) = @_;
24  
25 3 my $self = bless {
26 RemoteAddress => '127.0.0.1',
27 RemotePort => 9514,
28 ReturnType => 'hash',
29 Subscribe => undef,
30 Match => undef,
31 MessageHandler => undef,
32 %opts,
33 }, $class;
34  
35 3 ▶ $opts{'MessageHandler'}
Branch: 1 branch
BranchTrueFalseTotal
$opts{"MessageHandler"} or &AE::log("fatal", "You must provide a MessageHandler") 2 1 3
36 or AE::log fatal => 'You must provide a MessageHandler';
37  
38 2 ▶ ref $opts{'MessageHandler'} eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{"MessageHandler"} eq "CODE" or &AE::log("fatal", "You need to specify a subroutine reference to the 'MessageHandler' parameter.") 1 1 2
39 or AE::log fatal => 'You need to specify a subroutine reference to the \'MessageHandler\' parameter.';
40  
41 1 $self->_connect;
42  
43 1 return $self;
44 }
45  
46 1 ▶sub _connect {
Subroutine _connect: called CC=4 SCAR=22.3
47 1 my $self = shift;
48  
49 1 my $block = $self->{'ReturnType'} eq 'block';
50 1 ▶ my $separator = $block ? "\n" : '';
Branch: 1 branch
BranchTrueFalseTotal
$block ? : 0 1 1
51 1 my ( $addr, $port ) = @{$self}{qw<RemoteAddress RemotePort>};
52  
53 # FIXME: TODO item for this
54 # in second thought, this should just be removed because
55 # it's meant for internal manual buffering, which we don't need
56 1 ▶ $block
Branch: 1 branch
BranchTrueFalseTotal
$block and &AE::log("fatal", "Block option not supported") 0 1 1
57 and AE::log fatal => 'Block option not supported';
58  
59 1 Scalar::Util::weaken( my $inner_self = $self );
60  
61 0 ▶ $self->{'_client'} ||= tcp_connect $addr, $port, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
62 0 ▶ my ($fh) = @_
Branch: 1 branch
BranchTrueFalseTotal
my($fh) = or &AE::log("fatal", "Connect failed: $!") 0 0 0
63 or AE::log fatal => "Connect failed: $!";
64  
65 0 my $hdl; $hdl = AnyEvent::Handle->new(
66 fh => $fh,
67 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 AE::log error => $_[2];
69 0 $_[0]->destroy;
70 0 ▶ $inner_self->{'_reconnect_timer'} = AE::timer 10, 0, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 undef $inner_self->{'_reconnect_timer'};
72 0 $inner_self->_connect;
73 0 };
74 },
75  
76 0 ▶ on_eof => sub { $hdl->destroy; AE::log info => 'Done.' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
77  
78 0 ▶ on_read => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 0 ▶ $hdl->push_read (line => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
80 0 my ($hdl, $line) = @_;
81  
82 0 ▶ List::Util::first {
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 0 substr( $line, 0, length $_ ) eq $_
84 0 ▶ } @PROTOCOL_LINE_PREFIXES and return;
Branch: 1 branch
BranchTrueFalseTotal
&List::Util::first(sub { substr($line, 0, length $_) eq $_; } , @PROTOCOL_LINE_PREFIXES) and return 0 0 0
85  
86 0 $inner_self->handle_message( $line, $hdl );
87 0 });
88 },
89 0 );
90  
91 0 $inner_self->{'buffer'} = '';
92  
93 # FIXME: should this really be in a timer?
94 # all the actions relating to a socket are deferred anyway
95 0 ▶ $inner_self->{'_setup_pipe_timer'} = AE::timer 0, 0, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 undef $inner_self->{'_setup_pipe_timer'};
97 0 $inner_self->setup_pipe($hdl);
98 0 };
99 1 ▶ };
Condition33%1/3
$self->{"_client"} ||= &tcp_connect($addr, $port, sub { my($fh) = @_ or &AE::log("fatal", "Connect failed: $!"); my $hdl; $hdl = "AnyEvent::Handle"->new("fh", $fh, "on_error", sub { &AE::log("error", $_[2]); $_[0]->destroy; $inner_self->{"_reconnect_timer"} = &AE::timer(10, 0, sub { undef $inner_self->{"_reconnect_timer"}; $inner_self->_connect; } ); } , "on_eof", sub { $hdl->destroy; &AE::log("info", "Done."); } , "on_read", sub { $hdl->push_read("line", sub { my($hdl, $line) = @_; &List::Util::first(sub { substr($line, 0, length $_) eq $_; } , @PROTOCOL_LINE_PREFIXES) and return; $inner_self->handle_message($line, $hdl); } ); } ); $inner_self->{"buffer"} = ""; $inner_self->{"_setup_pipe_timer"} = &AE::timer(0, 0, sub { undef $inner_self->{"_setup_pipe_timer"}; $inner_self->setup_pipe($hdl); } ); } )
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"_client"} ||= &tcp_connect($addr, $port, sub { my($fh) = @_ or &AE::log("fatal", "Connect failed: $!"); my $hdl; $hdl = "AnyEvent::Handle"->new("fh", $fh, "on_error", sub { &AE::log("error", $_[2]); $_[0]->destroy; $inner_self->{"_reconnect_timer"} = &AE::timer(10, 0, sub { undef $inner_self->{"_reconnect_timer"}; $inner_self->_connect; } ); } , "on_eof", sub { $hdl->destroy; &AE::log("info", "Done."); } , "on_read", sub { $hdl->push_read("line", sub { my($hdl, $line) = @_; &List::Util::first(sub { substr($line, 0, length $_) eq $_; } , @PROTOCOL_LINE_PREFIXES) and return; $inner_self->handle_message($line, $hdl); } ); } ); $inner_self->{"buffer"} = ""; $inner_self->{"_setup_pipe_timer"} = &AE::timer(0, 0, sub { undef $inner_self->{"_setup_pipe_timer"}; $inner_self->setup_pipe($hdl); } ); } )
$self->{"_client"} &tcp_connect($addr, $port, sub { my($fh) = @_ or &AE::log("fatal", "Connect failed: $!"); my $hdl; $hdl = "AnyEvent::Handle"->new("fh", $fh, "on_error", sub { &AE::log("error", $_[2]); $_[0]->destroy; $inner_self->{"_reconnect_timer"} = &AE::timer(10, 0, sub { undef $inner_self->{"_reconnect_timer"}; $inner_self->_connect; } ); } , "on_eof", sub { $hdl->destroy; &AE::log("info", "Done."); } , "on_read", sub { $hdl->push_read("line", sub { my($hdl, $line) = @_; &List::Util::first(sub { substr($line, 0, length $_) eq $_; } , @PROTOCOL_LINE_PREFIXES) and return; $inner_self->handle_message($line, $hdl); } ); } ); $inner_self->{"buffer"} = ""; $inner_self->{"_setup_pipe_timer"} = &AE::timer(0, 0, sub { undef $inner_self->{"_setup_pipe_timer"}; $inner_self->setup_pipe($hdl); } ); } )
Truth table33%1/3
A ||= B
A= $self->{"_client"}B= &tcp_connect($addr, $port, sub { my($fh) = @_ or &AE::log("fatal", "Connect failed: $!"); my $hdl; $hdl = "AnyEvent::Handle"->new("fh", $fh, "on_error", sub { &AE::log("error", $_[2]); $_[0]->destroy; $inner_self->{"_reconnect_timer"} = &AE::timer(10, 0, sub { undef $inner_self->{"_reconnect_timer"}; $inner_self->_connect; } ); } , "on_eof", sub { $hdl->destroy; &AE::log("info", "Done."); } , "on_read", sub { $hdl->push_read("line", sub { my($hdl, $line) = @_; &List::Util::first(sub { substr($line, 0, length $_) eq $_; } , @PROTOCOL_LINE_PREFIXES) and return; $inner_self->handle_message($line, $hdl); } ); } ); $inner_self->{"buffer"} = ""; $inner_self->{"_setup_pipe_timer"} = &AE::timer(0, 0, sub { undef $inner_self->{"_setup_pipe_timer"}; $inner_self->setup_pipe($hdl); } ); } )
ABresult
1X1
011
000
100  
101 1 return $self;
102 }
103  
104 0 ▶sub setup_pipe {
Subroutine setup_pipe: not called CC=8 SCAR=42.8
Pod undocumented
105 0 my ( $self, $handle ) = @_;
106  
107 # Parse for Subscriptions or Matches
108 0 my %data;
109 0 foreach my $target (qw(Subscribe Match)) {
110 0 ▶ if ( defined $self->{$target} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$target}) 0 0 0
111 my @data = ref $self->{$target} eq 'ARRAY'
112 0 ? @{ $self->{$target} }
113 0 ▶ : $self->{$target};
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{$target} eq 'ARRAY' ? : 0 0 0
114  
115 0 ▶ @data = map lc, @data if $target eq 'Subscribe';
Branch: 1 branch
BranchTrueFalseTotal
if $target eq "Subscribe" 0 0 0
116 0 ▶ next unless scalar @data > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @data > 0 0 0 0
117 0 $data{$target} = \@data;
118 }
119 }
120  
121 # Check to make sure we're doing something
122 0 ▶ keys %data
Branch: 1 branch
BranchTrueFalseTotal
keys %data or &AE::log("fatal", "Must specify a subscription or a match parameters!") 0 0 0
123 or AE::log fatal => 'Must specify a subscription or a match parameters!';
124  
125 # Send the Subscription
126 0 foreach my $target ( sort keys %data ) {
127 0 my $subname = 'do_' . lc $target;
128 0 $self->$subname( $handle, $data{$target} );
129 }
130 }
131  
132 0 ▶sub do_subscribe {
Subroutine do_subscribe: not called CC=2 SCAR=17.9
Pod undocumented
133 0 my ( $self, $handle, $subs ) = @_;
134  
135 0 ▶ if ( List::Util::first { $_ eq 'fullfeed' } @{$subs} ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&List::Util::first(sub { $_ eq 'fullfeed'; } , @{$subs;})) { } 0 0 0
136 0 $handle->push_write("fullfeed\n");
137 } else {
138 $handle->push_write(
139 'sub ' .
140 0 join( ', ', @{$subs} ) .
141 "\n"
142 );
143 }
144 }
145  
146 0 ▶sub do_match {
Subroutine do_match: not called CC=1 SCAR=6.9
Pod undocumented
147 0 my ( $self, $handle, $matches ) = @_;
148 $handle->push_write(
149 'match ' .
150 0 join( ', ', @{$matches} ) .
151 "\n"
152 );
153 }
154  
155 0 ▶sub handle_message {
Subroutine handle_message: not called CC=7 SCAR=37.5
Pod undocumented
156 0 my ( $self, $line, $handle ) = @_;
157  
158 0 my $msg;
159 my $success = eval {
160 12 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
161 0 $msg = parse_syslog_line($line);
162 0 1;
163 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
my $success = eval { do { no warnings; $msg = parse_syslog_line($line); 1 } } or do { my $error = $@ || "Zombie error"; &AE::log("error", "Could not parse line: $line ($error)\n") } 0 0 0
164 0 ▶ my $error = $@ || 'Zombie error';
Condition0%0/2
$@ || 'Zombie error'
l!l
00
MC/DC0%0/1
$@ || 'Zombie error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
165 0 AE::log error => "Could not parse line: $line ($error)\n";
166 };
167  
168 0 ▶ $success && $msg or return;
Branch: 1 branch
BranchTrueFalseTotal
$success and $msg or return 0 0 0
Condition0%0/3
$success and $msg
!ll&&!rl&&r
000
MC/DC0%0/2
$success and $msg
$success $msg
Truth table0%0/3
A and B
A= $successB= $msg
ABresult
0X0
100
111
169  
170 # Try the Message Handler, eventually we can do statistics here.
171 eval {
172 0 $self->{'MessageHandler'}->($msg);
173 0 1;
174 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"MessageHandler"}->($msg); 1 } } or do { my $error = $@ || "Zombie error"; &AE::log("error", "MessageHandler failed: $error") } 0 0 0
175 0 ▶ my $error = $@ || 'Zombie error';
Condition0%0/2
$@ || 'Zombie error'
l!l
00
MC/DC0%0/1
$@ || 'Zombie error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
176 0 AE::log error => "MessageHandler failed: $error";
177 };
178 }
179  
180 12 1;
181  
182 __END__
« eris/Server.pm ↑ Summary eris.pm »