Yenta/Kibitz/Store/Client.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 109 CC 8 SCAR 27.4
CC
8
Cov
0%
read
42.0
new
6.0
shutdown
6.0
CRAP 15.5
1 # -*- perl -*-
2  
3 # Copyright (c) 2009 AdCopy
4 # Author: Jeff Weisberg
5 # Created: 2009-Apr-01 16:31 (EDT)
6 # Function: client side interface for storage subsystem
7 #
8 # $Id$
9  
10 package AC::Yenta::Kibitz::Store::Client;
11 use AC::Yenta::Debug 'store_client';
12 use AC::Yenta::Config;
13 use AC::Yenta::Protocol;
14 use AC::Yenta::IO::TCP::Client;
15 require 'AC/protobuf/yenta_getset.pl';
16 require 'AC/protobuf/yenta_check.pl';
17 use strict;
18  
19 our @ISA = 'AC::Yenta::IO::TCP::Client';
20  
21 my $TIMEOUT = 5;
22  
23 sub new {
24 my $class = shift;
25 my $addr = shift;
26 my $port = shift;
27 my $req = shift;
28  
29 debug('starting kibitz store client');
30 my $me = $class->SUPER::new( $addr, $port, info => "kibitz store client $addr:$port", @_ );
31 return unless $me;
32  
33 $me->{_req} = $req;
34 $me->set_callback('timeout', \&timeout);
35 $me->set_callback('read', \&read);
36 $me->set_callback('shutdown', \&shutdown);
37  
38 return $me;
39 }
40  
41 sub start {
42 my $me = shift;
43  
44 $me->SUPER::start();
45 $me->write( $me->{_req} );
46 $me->timeout_rel($TIMEOUT);
47 return $me;
48 }
49  
50 sub shutdown {
51 my $me = shift;
52  
53 # maybe call error handler
54 $me->run_callback('error', undef) unless $me->{_store_ok};
55 }
56  
57 sub timeout {
58 my $me = shift;
59  
60 $me->shut();
61 }
62  
63 sub read {
64 my $me = shift;
65 my $evt = shift;
66  
67 debug("recvd reply");
68  
69 my $yp = AC::Yenta::Protocol->new( secret => conf_value('secret') );
70 my($proto, $data, $content) = $yp->read_protocol( $me, $evt );
71 $me->timeout_rel($TIMEOUT) if $evt->{data} && !$proto;
72 return unless $proto;
73 $proto->{data} = $data;
74 $proto->{content} = $content;
75 eval {
76 my $yp = AC::Yenta::Protocol->new();
77 $proto->{data} = $yp->decode_reply($proto) if $data;
78 };
79 if(my $e = $@){
80 problem("cannot decode reply: $e");
81 }
82  
83 # process
84 $me->{_store_ok} = 1;
85 if( $proto->{is_error} || $@ ){
86 my $e = $@ || 'remote error';
87 $me->run_callback('error', {
88 error => $e,
89 });
90 }else{
91 $me->run_callback('load', $proto);
92 }
93  
94 $me->shut();
95 }
96  
97  
98 1;
99