eris/Server.pm

filter: Statementstmt 68.2% 206 / 302 Branchbran 39.7% 27 / 68 Conditioncond 24.0% 6 / 25 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 79.5% 35 / 44 Podpod 0.0% 0 / 25 totaltotal 57.4% 275 / 479 CC 67 SCAR 32.9
CC
67
Cov
61%
_dispatch_messages
119.8
stats
25.5
__ANON__
12.0
CRAP 26.7
↑ Summary eris/Client.pm »
1 package AnyEvent::eris::Server;
2 # ABSTRACT: eris pub/sub Server
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 Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Sys::Hostname;
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 AnyEvent::Graphite;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 my @_STREAM_NAMES = qw(subscription match debug full regex);
13 12 my %_STREAM_ASSISTERS = (
14 subscription => 'programs',
15 match => 'words',
16 );
17  
18 # Precompiled Regular Expressions
19 12 my %_PRE = (
20 program => qr/\s+\d+:\d+:\d+\s+\S+\s+([^:\s]+)(:|\s)/,
21 );
22  
23 0 ▶sub _server_error {
Subroutine _server_error: not called CC=2 SCAR=17.9
24 0 my ( $self, $err_str, $fatal ) = @_;
25 0 my $err_num = $!+0;
26 0 AE::log debug => "SERVER ERROR: $err_num, $err_str";
27  
28 0 ▶ $fatal and $self->{'_cv'}->send;
Branch: 1 branch
BranchTrueFalseTotal
$fatal and $self->{"_cv"}->send 0 0 0
29 }
30  
31 12 my %client_commands = (
32 fullfeed => qr{^fullfeed},
33 nofullfeed => qr{^nofull(feed)?},
34 subscribe => qr{^sub(?:scribe)?\s(.*)},
35 unsubscribe => qr{^unsub(?:scribe)?\s(.*)},
36 match => qr{^match (.*)},
37 nomatch => qr{^nomatch (.*)},
38 debug => qr{^debug},
39 nobug => qr{^no(de)?bug},
40 regex => qr{^re(?:gex)?\s(.*)},
41 noregex => qr{^nore(gex)?},
42 status => qr{^status},
43 dump => qr{^dump\s(\S+)},
44 quit => qr{(exit|q(uit)?)},
45 );
46  
47 2 ▶sub handle_subscribe {
Subroutine handle_subscribe: called CC=2 SCAR=6.9
Pod undocumented
48 2 my ( $self, $handle, $SID, $args ) = @_;
49  
50 2 $self->remove_stream( $SID, 'full' );
51  
52 2 my @programs = map lc, split /[\s,]+/, $args;
53 2 foreach my $program (@programs) {
54 10 $self->clients->{$SID}{'subscription'}{$program} = 1;
55  
56 # number of registered programs
57 10 $self->{'programs'}{$program}++;
58 }
59  
60 $handle->push_write(
61 2 'Subscribed to : ' .
62 join( ',', @programs ) .
63 "\n"
64 );
65 }
66  
67 1 ▶sub handle_unsubscribe {
Subroutine handle_unsubscribe: called CC=3 SCAR=11.0
Pod undocumented
68 1 my ( $self, $handle, $SID, $args ) = @_;
69  
70 1 my @programs = map lc, split /[\s,]+/, $args;
71 1 foreach my $program (@programs) {
72 5 delete $self->clients->{$SID}{'subscription'}{$program};
73  
74 --$self->{'programs'}{$program} <= 0
75 5 ▶ and delete $self->{'programs'}{$program};
Branch: 1 branch
BranchTrueFalseTotal
--$self->{"programs"}{$program} <= 0 and delete $self->{"programs"}{$program} 5 0 5
76 }
77  
78 1 delete $self->clients->{$SID}{'subscription'};
79  
80 1 $handle->push_write(
81 'Subscription removed for : ' .
82 join( ',', @programs ) .
83 "\n"
84 );
85 }
86  
87 3 ▶sub handle_fullfeed {
Subroutine handle_fullfeed: called CC=1 SCAR=0.0
Pod undocumented
88 3 my ( $self, $handle, $SID ) = @_;
89  
90 3 $self->remove_all_streams($SID);
91  
92 3 $self->clients->{$SID}{'full'} = 1;
93  
94 3 $handle->push_write(
95 "Full feed enabled, all other functions disabled.\n"
96 );
97 }
98  
99 1 ▶sub handle_nofullfeed {
Subroutine handle_nofullfeed: called CC=1 SCAR=0.0
Pod undocumented
100 1 my ( $self, $handle, $SID ) = @_;
101  
102 1 $self->remove_all_streams($SID);
103  
104 # XXX: Not in original implementation
105 1 delete $self->clients->{$SID}{'full'};
106  
107 1 $handle->push_write("Full feed disabled.\n");
108 }
109  
110 1 ▶sub handle_match {
Subroutine handle_match: called CC=2 SCAR=6.9
Pod undocumented
111 1 my ( $self, $handle, $SID, $args ) = @_;
112  
113 1 $self->remove_stream( $SID, 'full' );
114  
115 1 my @words = map lc, split /[\s,]+/, $args;
116 1 foreach my $word (@words) {
117 2 $self->{'words'}{$word}++;
118 2 $self->clients->{$SID}{'match'}{$word} = 1;
119 }
120  
121 $handle->push_write(
122 1 'Receiving messages matching : ' .
123 join( ', ', @words ) .
124 "\n"
125 );
126 }
127  
128 1 ▶sub handle_nomatch {
Subroutine handle_nomatch: called CC=3 SCAR=11.0
Pod undocumented
129 1 my ( $self, $handle, $SID, $args ) = @_;
130  
131 1 my @words = map lc, split /[\s,]+/, $args;
132 1 foreach my $word (@words) {
133 2 delete $self->clients->{$SID}{'match'}{$word};
134  
135 # Remove the word from searching if this was the last client
136 --$self->{'words'}{$word} <= 0
137 2 ▶ and delete $self->{'words'}{$word};
Branch: 1 branch
BranchTrueFalseTotal
--$self->{"words"}{$word} <= 0 and delete $self->{"words"}{$word} 2 0 2
138 }
139  
140 $handle->push_write(
141 1 'No longer receiving messages matching : ' .
142 join( ', ', @words ) .
143 "\n"
144 );
145 }
146  
147 1 ▶sub handle_debug {
Subroutine handle_debug: called CC=1 SCAR=0.0
Pod undocumented
148 1 my ( $self, $handle, $SID ) = @_;
149  
150 1 $self->remove_stream( $SID, 'full' );
151  
152 1 $self->clients->{$SID}{'debug'} = 1;
153 1 $handle->push_write("Debugging enabled.\n");
154 }
155  
156 1 ▶sub handle_nobug {
Subroutine handle_nobug: called CC=1 SCAR=0.0
Pod undocumented
157 1 my ( $self, $handle, $SID ) = @_;
158  
159 1 $self->remove_stream( $SID, 'debug' );
160 1 delete $self->clients->{$SID}{'debug'};
161 1 $handle->push_write("Debugging disabled.\n");
162 }
163  
164 1 ▶sub handle_regex {
Subroutine handle_regex: called CC=6 SCAR=22.4
Pod undocumented
165 1 my ( $self, $handle, $SID, $args ) = @_;
166  
167 # do not handle a regex if it's already full subscription
168 1 ▶ $self->clients->{$SID}{'full'}
Branch: 1 branch
BranchTrueFalseTotal
$self->clients->{$SID}{"full"} and return 0 1 1
169 and return;
170  
171 1 my $regex;
172 eval {
173 1 ▶ defined $args && length $args
Branch: 1 branch
BranchTrueFalseTotal
defined $args and length $args and $regex = qr/$args/ 1 0 1
Condition33%1/3
defined $args and length $args
!ll&&!rl&&r
001
MC/DC0%0/2
defined $args and length $args
defined $args length $args
Truth table33%1/3
A and B
A= defined $argsB= length $args
ABresult
0X0
100
111
174 and $regex = qr{$args};
175 1 1;
176 1 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { defined $args and length $args and $regex = qr/$args/; 1 } } or do { my $error = $@ || "Zombie error"; $handle->push_write("Invalid regular expression '${args}', see: perldoc perlre\n"); return } 1 0 1
177 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
178  
179 0 $handle->push_write(
180 "Invalid regular expression '$args', see: perldoc perlre\n"
181 );
182  
183 0 return;
184 };
185  
186 1 $self->clients->{$SID}{'regex'}{$regex} = 1;
187 1 $handle->push_write(
188 "Receiving messages matching regex : $args\n"
189 );
190 }
191  
192 1 ▶sub handle_noregex {
Subroutine handle_noregex: called CC=1 SCAR=0.0
Pod undocumented
193 1 my ( $self, $handle, $SID ) = @_;
194  
195 1 $self->remove_stream( $SID, 'regex' );
196 1 delete $self->clients->{$SID}{'regex'};
197 1 $handle->push_write("No longer receiving regex-based matches\n");
198 }
199  
200 1 ▶sub handle_status {
Subroutine handle_status: called CC=8 SCAR=20.8
Pod undocumented
201 1 my ( $self, $handle, $SID ) = @_;
202 1 my $clients = $self->clients;
203 1 my $client_count = scalar keys %{$clients};
204  
205 1 my @details = ();
206 1 foreach my $stream (@_STREAM_NAMES) {
207 # add streams from all SIDs
208 5 my $stream_count = 0;
209 5 my $assist_count = 0;
210 5 foreach my $SID ( keys %{$clients} ) {
211 5 ▶ $clients->{$SID}{$stream}
Branch: 1 branch
BranchTrueFalseTotal
$clients->{$SID}{$stream} and ++$stream_count 1 4 5
212 and $stream_count++;
213  
214 5 my $assist; $assist = $_STREAM_ASSISTERS{$stream}
215 5 ▶ and $assist_count += scalar keys %{ $self->{$assist} || {} };
Branch: 1 branch
BranchTrueFalseTotal
$assist = $_STREAM_ASSISTERS{$stream} and $assist_count += scalar keys %{$self->{$assist} or {};} 2 3 5
Condition100%2/2
$self->{$assist} || {}
l!l
11
MC/DC100%1/1
$self->{$assist} || {}
$self->{$assist}
Truth table100%2/2
A
A= $self->{$assist}
Aresult
11
00
216 }
217  
218 5 ▶ $stream_count == 0
Branch: 1 branch
BranchTrueFalseTotal
$stream_count == 0 and next 4 1 5
219 and next;
220  
221 1 my $single_details = "$stream=$stream_count";
222 1 ▶ $assist_count and $single_details .= ":$assist_count";
Branch: 1 branch
BranchTrueFalseTotal
$assist_count and $single_details .= ":$assist_count" 1 0 1
223  
224 1 push @details, $single_details;
225 }
226  
227 1 my $details = join ', ', @details;
228 1 $handle->push_write("STATUS[0]: $client_count connections: $details\n");
229 }
230  
231 1 ▶sub handle_dump {
Subroutine handle_dump: called CC=2 SCAR=8.6
Pod undocumented
232 1 my ( $self, $handle, $SID, $type ) = @_;
233 1 my $clients = $self->clients;
234  
235 my %dispatch = (
236 0 ▶ assisters => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
237 0 my @details = ();
238 0 foreach my $asst ( values %_STREAM_ASSISTERS ) {
239 0 ▶ $self->{$asst} or next;
Branch: 1 branch
BranchTrueFalseTotal
$self->{$asst} or next 0 0 0
240 0 my @SIDs = grep $clients->{$_}{$asst}, keys %{$clients};
241 push @details,
242 0 "$asst -> " . join ',', keys %{ $self->{$asst} };
243 }
244  
245 0 return @details;
246 },
247  
248 0 ▶ stats => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
249 my @details = map +(
250 "$_ -> $self->{'stats'}{$_}"
251 0 ), keys %{ $self->{'stats'} };
252  
253 0 return @details;
254 },
255  
256 1 ▶ streams => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
257 1 my @details = ();
258  
259 1 foreach my $stream (@_STREAM_NAMES) {
260 5 my @SIDs;
261 5 foreach my $SID ( keys %{$clients} ) {
262 5 ▶ $clients->{$SID}{$stream}
Branch: 1 branch
BranchTrueFalseTotal
$clients->{$SID}{$stream} or next 1 4 5
263 or next;
264  
265 1 my $stream_data = $clients->{$SID}{$stream};
266 push @SIDs, ref $stream_data eq 'HASH'
267 0 ▶ ? "$SID:" . join ',', keys %{$stream_data}
Branch: 1 branch
BranchTrueFalseTotal
ref $stream_data eq 'HASH' ? : 0 1 1
268 : $SID;
269 }
270  
271 5 push @details, "$stream -> " . join '; ', @SIDs;
272 }
273  
274 1 return @details;
275 },
276 1 );
277  
278 1 ▶ if ( my $cb = $dispatch{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $dispatch{$type}) { } 1 0 1
279 1 my @msgs = $cb->();
280 1 my $msgs = join( "\n", @msgs ) . "\n";
281 1 $handle->push_write($msgs);
282 } else {
283 0 $handle->push_write("DUMP[-1]: No comprende.\n");
284 }
285 }
286  
287 0 ▶sub handle_quit {
Subroutine handle_quit: not called CC=1 SCAR=6.9
Pod undocumented
288 0 my ( $self, $handle, $SID ) = @_;
289 0 $handle->push_write('Terminating connection on your request.');
290 0 $self->hangup_client($SID);
291 0 $self->{'_cv'}->send;
292 }
293  
294 0 ▶sub hangup_client {
Subroutine hangup_client: not called CC=1 SCAR=6.9
Pod undocumented
295 0 my ( $self, $SID ) = @_;
296 0 delete $self->clients->{$SID};
297 0 AE::log debug => "Client Termination Posted: $SID";
298 }
299  
300 26 ▶sub remove_stream {
Subroutine remove_stream: called CC=5 SCAR=24.5
Pod undocumented
301 26 my ( $self, $SID, $stream ) = @_;
302 26 AE::log debug => "Removing '$stream' for $SID";
303  
304 26 my $client_streams = delete $self->clients->{$SID}{'streams'}{$stream};
305  
306 # FIXME:
307 # I *think* what this is supposed to do is delete assists
308 # that were registered for this client, which it doesn't
309 # - it deletes global assists instead - this needs to be
310 # looked into
311 26 ▶ if ($client_streams) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client_streams) 0 26 26
312 0 ▶ if ( my $assist = $_STREAM_ASSISTERS{$stream} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $assist = $_STREAM_ASSISTERS{$stream}) 0 0 0
313 0 foreach my $key ( keys %{$client_streams} ) {
314 --$self->{'assists'}{$assist}{$key} <= 0
315 0 ▶ and delete $self->{'assists'}{$assist}{$key}
Branch: 1 branch
BranchTrueFalseTotal
--$self->{"assists"}{$assist}{$key} <= 0 and delete $self->{"assists"}{$assist}{$key} 0 0 0
316 }
317 }
318 }
319 }
320  
321 4 ▶sub remove_all_streams {
Subroutine remove_all_streams: called CC=2 SCAR=6.9
Pod undocumented
322 4 my ( $self, $SID ) = @_;
323 4 foreach my $stream (@_STREAM_NAMES) {
324 20 $self->remove_stream( $SID, $stream );
325 }
326 }
327  
328 12 ▶sub new {
Subroutine new: called CC=3 SCAR=11.9
Pod undocumented
329 12 my $class = shift;
330 12 my $hostname = ( split '.', hostname )[0];
331 12 my $self = bless {
332 ListenAddress => '127.0.0.1', # "localhost" doesn't work :/
333 ListenPort => 9514,
334 GraphitePort => 2003,
335 GraphitePrefix => 'eris.dispatcher',
336 hostname => $hostname,
337  
338 @_,
339  
340 clients => {},
341 buffers => {},
342 }, $class;
343  
344 12 my ( $host, $port ) = @{$self}{qw<ListenAddress ListenPort>};
345 12 Scalar::Util::weaken( my $inner_self = $self );
346  
347 10 ▶ $self->{'_tcp_server_guard'} ||= tcp_server $host, $port, sub {
Subroutine __ANON__: called CC=2 SCAR=8.3
348 10 ▶ my ($fh) = @_
Branch: 1 branch
BranchTrueFalseTotal
my($fh) = or return $inner_self->_server_error($!) 10 0 10
349 or return $inner_self->_server_error($!);
350  
351 10 my $handle; $handle = AnyEvent::Handle->new(
352 fh => $fh,
353 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
354 0 my ( $hdl, $fatal, $msg ) = @_;
355 0 my $SID = $inner_self->_session_id($hdl);
356 0 $inner_self->hangup_client($SID);
357 0 $inner_self->_server_error( $msg, $fatal );
358 0 $hdl->destroy;
359 },
360  
361 0 ▶ on_eof => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
362 0 my ($hdl) = @_;
363 0 my $SID = $inner_self->_session_id($hdl);
364 0 $inner_self->hangup_client($SID);
365 0 $hdl->destroy;
366 0 AE::log debug => "SERVER, client $SID disconnected.";
367 },
368  
369 15 ▶ on_read => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
370 15 my ($hdl) = @_;
371 15 chomp( my $line = delete $hdl->{'rbuf'} );
372 15 my $SID = $inner_self->_session_id($hdl);
373  
374 15 foreach my $command ( keys %client_commands ) {
375 123 my $regex = $client_commands{$command};
376 123 ▶ if ( my ($args) = ( $line =~ /$regex/i ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($args) = $line =~ /$regex/i) 15 108 123
377 15 my $method = "handle_$command";
378 15 return $inner_self->$method( $hdl, $SID, $args );
379 }
380 }
381  
382 0 $hdl->push_write("UNKNOWN COMMAND, Ignored.\015\012");
383 },
384 10 );
385  
386 10 my $SID = $inner_self->_session_id($handle);
387 10 $handle->push_write("EHLO Streamer (KERNEL: $$:$SID)\n");
388 10 $inner_self->register_client( $SID, $handle );
389 12 ▶ };
Condition33%1/3
$self->{"_tcp_server_guard"} ||= &tcp_server($host, $port, sub { my($fh) = @_ or return $inner_self->_server_error($!); my $handle; $handle = "AnyEvent::Handle"->new("fh", $fh, "on_error", sub { my($hdl, $fatal, $msg) = @_; my $SID = $inner_self->_session_id($hdl); $inner_self->hangup_client($SID); $inner_self->_server_error($msg, $fatal); $hdl->destroy; } , "on_eof", sub { my($hdl) = @_; my $SID = $inner_self->_session_id($hdl); $inner_self->hangup_client($SID); $hdl->destroy; &AE::log("debug", "SERVER, client $SID disconnected."); } , "on_read", sub { my($hdl) = @_; chomp(my $line = delete $hdl->{"rbuf"}); my $SID = $inner_self->_session_id($hdl); foreach my $command (keys %client_commands) { my $regex = $client_commands{$command}; if (my($args) = $line =~ /$regex/i) { my $method = "handle_$command"; return $inner_self->$method($hdl, $SID, $args); } ; } ; $hdl->push_write("UNKNOWN COMMAND, Ignored.\r\n"); } ); my $SID = $inner_self->_session_id($handle); $handle->push_write("EHLO Streamer (KERNEL: ${$}:$SID)\n"); $inner_self->register_client($SID, $handle); } )
l!l&&r!l&&!r
0120
MC/DC0%0/2
$self->{"_tcp_server_guard"} ||= &tcp_server($host, $port, sub { my($fh) = @_ or return $inner_self->_server_error($!); my $handle; $handle = "AnyEvent::Handle"->new("fh", $fh, "on_error", sub { my($hdl, $fatal, $msg) = @_; my $SID = $inner_self->_session_id($hdl); $inner_self->hangup_client($SID); $inner_self->_server_error($msg, $fatal); $hdl->destroy; } , "on_eof", sub { my($hdl) = @_; my $SID = $inner_self->_session_id($hdl); $inner_self->hangup_client($SID); $hdl->destroy; &AE::log("debug", "SERVER, client $SID disconnected."); } , "on_read", sub { my($hdl) = @_; chomp(my $line = delete $hdl->{"rbuf"}); my $SID = $inner_self->_session_id($hdl); foreach my $command (keys %client_commands) { my $regex = $client_commands{$command}; if (my($args) = $line =~ /$regex/i) { my $method = "handle_$command"; return $inner_self->$method($hdl, $SID, $args); } ; } ; $hdl->push_write("UNKNOWN COMMAND, Ignored.\r\n"); } ); my $SID = $inner_self->_session_id($handle); $handle->push_write("EHLO Streamer (KERNEL: ${$}:$SID)\n"); $inner_self->register_client($SID, $handle); } )
$self->{"_tcp_server_guard"} &tcp_server($host, $port, sub { my($fh) = @_ or return $inner_self->_server_error($!); my $handle; $handle = "AnyEvent::Handle"->new("fh", $fh, "on_error", sub { my($hdl, $fatal, $msg) = @_; my $SID = $inner_self->_session_id($hdl); $inner_self->hangup_client($SID); $inner_self->_server_error($msg, $fatal); $hdl->destroy; } , "on_eof", sub { my($hdl) = @_; my $SID = $inner_self->_session_id($hdl); $inner_self->hangup_client($SID); $hdl->destroy; &AE::log("debug", "SERVER, client $SID disconnected."); } , "on_read", sub { my($hdl) = @_; chomp(my $line = delete $hdl->{"rbuf"}); my $SID = $inner_self->_session_id($hdl); foreach my $command (keys %client_commands) { my $regex = $client_commands{$command}; if (my($args) = $line =~ /$regex/i) { my $method = "handle_$command"; return $inner_self->$method($hdl, $SID, $args); } ; } ; $hdl->push_write("UNKNOWN COMMAND, Ignored.\r\n"); } ); my $SID = $inner_self->_session_id($handle); $handle->push_write("EHLO Streamer (KERNEL: ${$}:$SID)\n"); $inner_self->register_client($SID, $handle); } )
Truth table33%1/3
A ||= B
A= $self->{"_tcp_server_guard"}B= &tcp_server($host, $port, sub { my($fh) = @_ or return $inner_self->_server_error($!); my $handle; $handle = "AnyEvent::Handle"->new("fh", $fh, "on_error", sub { my($hdl, $fatal, $msg) = @_; my $SID = $inner_self->_session_id($hdl); $inner_self->hangup_client($SID); $inner_self->_server_error($msg, $fatal); $hdl->destroy; } , "on_eof", sub { my($hdl) = @_; my $SID = $inner_self->_session_id($hdl); $inner_self->hangup_client($SID); $hdl->destroy; &AE::log("debug", "SERVER, client $SID disconnected."); } , "on_read", sub { my($hdl) = @_; chomp(my $line = delete $hdl->{"rbuf"}); my $SID = $inner_self->_session_id($hdl); foreach my $command (keys %client_commands) { my $regex = $client_commands{$command}; if (my($args) = $line =~ /$regex/i) { my $method = "handle_$command"; return $inner_self->$method($hdl, $SID, $args); } ; } ; $hdl->push_write("UNKNOWN COMMAND, Ignored.\r\n"); } ); my $SID = $inner_self->_session_id($handle); $handle->push_write("EHLO Streamer (KERNEL: ${$}:$SID)\n"); $inner_self->register_client($SID, $handle); } )
ABresult
1X1
011
000
390  
391 4 ▶ $self->{'_timers'}{'flush'} = AE::timer 0.1, 0.1, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
392 4 $inner_self->flush_client;
393 12 };
394  
395 11 ▶ $self->{'_timers'}{'stats'} = AE::timer 0, 60, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
396 11 $inner_self->stats;
397 12 };
398  
399 # Statistics Tracking
400 12 ▶ $self->{'config'}{'GraphiteHost'}
Branch: 1 branch
BranchTrueFalseTotal
$self->{"config"}{"GraphiteHost"} and $self->graphite_connect 0 12 12
401 and $self->graphite_connect;
402  
403 12 return $self;
404 }
405  
406 4 ▶sub flush_client {
Subroutine flush_client: called CC=3 SCAR=11.0
Pod undocumented
407 4 my $self = shift;
408 4 my $clients = $self->{'clients'};
409 4 my $buffers = $self->{'buffers'};
410  
411 4 foreach my $SID ( keys %{$buffers} ) {
412 2 my $msgs = $buffers->{$SID};
413 2 ▶ @{$msgs} > 0 or next;
Branch: 1 branch
BranchTrueFalseTotal
@{$msgs;} > 0 or next 2 0 2
414  
415 # write the messages to the SID
416 2 my $msgs_str = join "\n", @{$msgs};
417  
418 2 $clients->{$SID}{'handle'}->push_write("$msgs_str\n");
419 2 $buffers->{$SID} = [];
420 }
421 }
422  
423 0 ▶sub graphite_connect {
Subroutine graphite_connect: not called CC=3 SCAR=24.8
Pod undocumented
424 0 my $self = shift;
425  
426 eval {
427 $self->{'_graphite'} = AnyEvent::Graphite->new(
428 host => $self->{'config'}{'GraphiteHost'},
429 0 port => $self->{'config'}{'GraphitePort'},
430 );
431  
432 0 1;
433 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"_graphite"} = "AnyEvent::Graphite"->new("host", $self->{"config"}{"GraphiteHost"}, "port", $self->{"config"}{"GraphitePort"}); 1 } } or do { my $error = $@ || "Zombie error"; &AE::log("debug", "Graphite server setup failed: $error") } 0 0 0
434 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
435 0 AE::log debug => "Graphite server setup failed: $error";
436 }
437 }
438  
439 11 ▶sub stats {
Subroutine stats: called CC=6 SCAR=32.4
Pod undocumented
440 11 my $self = shift;
441  
442 11 ▶ if ( ! exists $self->{'stats'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"stats"}) 11 0 11
443 11 $self->{'stats'} = {
444 map +( $_ => 0 ), qw<
445 received received_bytes dispatched dispatched _bytes
446 >
447 };
448  
449 11 return;
450 }
451  
452 0 my $stats = delete $self->{'stats'};
453  
454 0 ▶ if ( $self->{'_graphite'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_graphite"}) 0 0 0
455 0 my $time = AE::now;
456 0 foreach my $stat ( keys %{$stats}) {
457 my $metric = join '.', $self->{'config'}{'GraphitePrefix'},
458 0 $self->{'hostname'},
459 $stat;
460 eval {
461 0 $self->{'_graphite'}->send($metric, $stats->{$stat}, $time);
462 0 1;
463 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"_graphite"}->send($metric, $stats->{$stat}, $time); 1 } } or do { my $error = $@ || "Zombie error"; &AE::log("debug", "Error sending statistics, reconnecting."); $self->graphite_connect; last } 0 0 0
464 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
465 0 AE::log debug => 'Error sending statistics, reconnecting.';
466 0 $self->graphite_connect;
467 0 last;
468 }
469 }
470 }
471  
472 AE::log debug => 'STATS: ' .
473 0 join ', ', map "$_:$stats->{$_}", keys %{$stats};
474 }
475  
476 11 ▶sub run {
Subroutine run: called CC=2 SCAR=7.6
Pod undocumented
477 11 my $self = shift;
478 11 ▶ $self->{'_cv'} = shift || AE::cv;
Condition33%1/3
shift() || &AE::cv()
l!l&&r!l&&!r
1100
MC/DC0%0/2
shift() || &AE::cv()
shift() &AE::cv()
Truth table33%1/3
A || B
A= shift()B= &AE::cv()
ABresult
1X1
011
000
479 11 $self->{'_cv'}->recv;
480 }
481  
482 87 ▶sub clients {
Subroutine clients: called CC=2 SCAR=7.2
Pod undocumented
483 87 my $self = shift;
484 87 ▶ $self->{'clients'} ||= {};
Condition50%1/2
$self->{"clients"} ||= {}
l!l
870
MC/DC0%0/1
$self->{"clients"} ||= {}
$self->{"clients"}
Truth table50%1/2
A
A= $self->{"clients"}
Aresult
11
00
485 }
486  
487 10 ▶sub register_client {
Subroutine register_client: called CC=1 SCAR=0.0
Pod undocumented
488 10 my ( $self, $SID, $handle ) = @_;
489  
490 10 $self->clients->{$SID} = { handle => $handle };
491 }
492  
493 1 ▶sub dispatch_message {
Subroutine dispatch_message: called CC=1 SCAR=0.0
Pod undocumented
494 1 my ( $self, $msg ) = @_;
495 1 $self->_dispatch_messages( [$msg] );
496 }
497  
498 0 ▶sub dispatch_messages {
Subroutine dispatch_messages: not called CC=1 SCAR=6.9
Pod undocumented
499 0 my ( $self, $msgs ) = @_;
500 0 $self->_dispatch_messages( [ split /\n/, $msgs ] );
501 }
502  
503 1 ▶sub _dispatch_messages {
Subroutine _dispatch_messages: called CC=20 SCAR=47.9
504 1 my ( $self, $msgs ) = @_;
505  
506 1 my $clients = $self->{'clients'};
507 1 my $buffers = $self->{'buffers'};
508 1 my $dispatched = 0;
509 1 my $bytes = 0;
510  
511 # Handle fullfeeds
512 1 foreach my $SID ( keys %{$clients} ) {
513 1 push @{ $buffers->{$SID} }, @{$msgs};
514 1 $dispatched += scalar @{$msgs};
515 1 $bytes += length $_ for @{$msgs};
516 }
517  
518 1 foreach my $msg ( @{$msgs} ) {
519 # Grab statitics;
520 1 $self->{'stats'}{'received'}++;
521 1 $self->{'stats'}{'received_bytes'} += length $msg;
522  
523 # Program based subscriptions
524 1 ▶ if ( my ($program) = map lc, ( $msg =~ $_PRE{'program'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($program) = map(lc($_), $msg =~ /$_PRE{"program"}/)) 0 1 1
525 # remove the sub process and PID from the program
526 0 $program =~ s/\(.*//g;
527 0 $program =~ s/\[.*//g;
528  
529 0 ▶ if ( exists $self->{'programs'}{$program} && $self->{'programs'}{$program} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"programs"}{$program} and $self->{"programs"}{$program} > 0) 0 0 0
Condition0%0/3
exists $self->{"programs"}{$program} and $self->{"programs"}{$program} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"programs"}{$program} and $self->{"programs"}{$program} > 0
exists $self->{"programs"}{$program} $self->{"programs"}{$program} > 0
Truth table0%0/3
A and B
A= exists $self->{"programs"}{$program}B= $self->{"programs"}{$program} > 0
ABresult
0X0
100
111
530 0 foreach my $SID ( keys %{$clients} ) {
531 0 ▶ exists $clients->{$SID}{'subscription'}{$program}
Branch: 1 branch
BranchTrueFalseTotal
exists $clients->{$SID}{"subscription"}{$program} or next 0 0 0
532 or next;
533  
534 0 push @{ $buffers->{$SID} }, $msg;
535 0 $dispatched++;
536 0 $bytes += length $msg;
537 }
538 }
539 }
540  
541 # Match based subscriptions
542 1 ▶ if ( keys %{ $self->{'words'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$self{"words"};}) 0 1 1
543 0 foreach my $word ( keys %{ $self->{'words'} } ) {
544 0 ▶ if ( index ( $msg, $word ) != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($msg, $word) != -1) 0 0 0
545 0 foreach my $SID ( keys %{$clients} ) {
546 0 ▶ exists $clients->{$SID}{'match'}{$word}
Branch: 1 branch
BranchTrueFalseTotal
exists $clients->{$SID}{"match"}{$word} or next 0 0 0
547 or next;
548  
549 0 push @{ $buffers->{$SID} }, $msg;
550 0 $dispatched++;
551 0 $bytes += length $msg;
552 }
553 }
554 }
555 }
556  
557 # Regex based subscriptions
558 1 ▶ if ( keys %{ $self->{'regex'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$self{"regex"};}) 0 1 1
559 0 my %hit = ();
560 0 foreach my $SID ( keys %{$clients} ) {
561 0 foreach my $re ( keys %{ $clients->{$SID}{'regex'} } ) {
562 0 ▶ if ( $hit{$re} || $msg =~ /$re/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hit{$re} or $msg =~ /$re/) 0 0 0
Condition0%0/3
$hit{$re} or $msg =~ /$re/
l!l&&r!l&&!r
000
MC/DC0%0/2
$hit{$re} or $msg =~ /$re/
$hit{$re} $msg =~ /$re/
Truth table0%0/3
A or B
A= $hit{$re}B= $msg =~ /$re/
ABresult
1X1
011
000
563 0 $hit{$re} = 1;
564 0 push @{ $buffers->{$SID} }, $msg;
565 0 $dispatched++;
566 0 $bytes += length $msg;
567 }
568 }
569 }
570 }
571 }
572  
573 # Report statistics for dispatched messages
574 1 ▶ if ( $dispatched > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dispatched > 0) 1 0 1
575 1 $self->{'stats'}{'dispatched'} += $dispatched;
576 1 $self->{'stats'}{'dispatched_bytes'} += $bytes;
577 }
578 }
579  
580 25 ▶sub _session_id {
Subroutine _session_id: called CC=1 SCAR=0.0
581 25 my ( $self, $handle ) = @_;
582 # AnyEvent::Handle=HASH(0x1bb30f0)
583 25 "$handle" =~ /\D0x([a-fA-F0-9]+)/;
584 25 return $1;
585 }
586  
587 12 1;
588  
589 __END__
↑ Summary eris/Client.pm »