Yenta/Monitor/Client.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 6 Conditioncond n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 66 CC 4 SCAR 13.9
CC
4
Cov
0%
new
6.0
shutdown
6.0
read
6.0
CRAP 4.0
1 # -*- perl -*-
2  
3 # Copyright (c) 2009 AdCopy
4 # Author: Jeff Weisberg
5 # Created: 2009-May-12 11:06 (EDT)
6 # Function:
7 #
8 # $Id: Client.pm,v 1.8 2010/12/07 19:11:43 jaw Exp $
9  
10 package AC::Yenta::Monitor::Client;
11 use AC::Yenta::Protocol;
12 use AC::Yenta::Config;
13 use AC::Yenta::Debug 'monitor_client';
14 use AC::Yenta::IO::TCP::Client;
15 use strict;
16 our @ISA = 'AC::Yenta::IO::TCP::Client';
17  
18  
19 my $HDRSIZE = AC::Yenta::Protocol->header_size();
20 my $TIMEOUT = 2;
21 my $msgid = $$;
22  
23 sub new {
24 my $class = shift;
25  
26 debug('starting monitor status client');
27 my $me = $class->SUPER::new( @_ );
28 unless($me){
29 return;
30 }
31  
32 $me->set_callback('timeout', \&timeout);
33 $me->set_callback('read', \&read);
34 $me->set_callback('shutdown', \&shutdown);
35  
36 $me->start();
37  
38 # build request
39 my $yp = AC::Yenta::Protocol->new( secret => conf_value('secret') );
40 my $hdr = $yp->encode_header(
41 type => 'heartbeat_request',
42 data_length => 0,
43 content_length => 0,
44 want_reply => 1,
45 msgid => $msgid++,
46 );
47  
48 # write request
49 $me->write( $hdr );
50  
51 $me->timeout_rel($TIMEOUT);
52  
53 return $me;
54 }
55  
56 sub timeout {
57 my $me = shift;
58 $me->shut();
59 }
60  
61 sub shutdown {
62 my $me = shift;
63  
64 unless( $me->{status_ok} ){
65 AC::Yenta::Monitor::isdown( $me->{monitor_peer} );
66 }
67 }
68  
69 sub read {
70 my $me = shift;
71 my $evt = shift;
72  
73 debug("recvd reply");
74  
75 my $yp = AC::Yenta::Protocol->new();
76 my($proto, $data, $content) = $yp->read_protocol( $me, $evt );
77 return unless $proto;
78  
79 $me->{status_ok} = 1;
80 AC::Yenta::Monitor::update( $me->{monitor_peer}, $data );
81  
82 $me->shut();
83 }
84  
85  
86 1;