| 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: Client.pm,v 1.14 2010/12/07 19:11:43 jaw Exp $ | ||
| 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 = 2; | ||
| 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 | $me->shut(); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub read { | ||
| 63 | my $me = shift; | ||
| 64 | my $evt = shift; | ||
| 65 | |||
| 66 | debug("recvd reply"); | ||
| 67 | |||
| 68 | my $yp = AC::Yenta::Protocol->new( secret => conf_value('secret') ); | ||
| 69 | my($proto, $data, $content) = $yp->read_protocol( $me, $evt ); | ||
| 70 | $me->timeout_rel($TIMEOUT) if $evt->{data} && !$proto; | ||
| 71 | return unless $proto; | ||
| 72 | $proto->{data} = $data; | ||
| 73 | $proto->{content} = $content; | ||
| 74 | eval { | ||
| 75 | my $yp = AC::Yenta::Protocol->new(); | ||
| 76 | $proto->{data} = $yp->decode_reply($proto) if $data; | ||
| 77 | }; | ||
| 78 | if(my $e = $@){ | ||
| 79 | problem("cannot decode reply: $e"); | ||
| 80 | } | ||
| 81 | |||
| 82 | # process | ||
| 83 | $me->{_store_ok} = 1; | ||
| 84 | if( $proto->{is_error} || $@ ){ | ||
| 85 | my $e = $@ || 'remote error'; | ||
| 86 | $me->run_callback('error', { | ||
| 87 | error => $e, | ||
| 88 | }); | ||
| 89 | }else{ | ||
| 90 | $me->run_callback('load', $proto); | ||
| 91 | } | ||
| 92 | |||
| 93 | $me->shut(); | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | 1; | ||
| 98 |