MrGamoo/API/Client.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 85 CC 5 SCAR 19.9
CC
5
Cov
0%
_read
20.0
_shutdown
6.0
new
2.0
CRAP 7.3
1 # -*- perl -*-
2  
3 # Copyright (c) 2010 AdCopy
4 # Author: Jeff Weisberg
5 # Created: 2010-Jan-14 14:06 (EST)
6 # Function: send requests to server
7 #
8 # $Id: Client.pm,v 1.1 2010/11/01 18:41:50 jaw Exp $
9  
10 package AC::MrGamoo::API::Client;
11 use AC::MrGamoo::Debug 'client';
12 use AC::MrGamoo::Protocol;
13 use AC::DC::IO::TCP::Client;
14 use Socket;
15 our @ISA = 'AC::DC::IO::TCP::Client';
16  
17 use strict;
18  
19 my $TIMEOUT = 15;
20  
21 sub new {
22 my $class = shift;
23 my $addr = shift;
24 my $port = shift;
25 my $info = shift;
26 my $req = shift;
27 my $data = shift;
28  
29 debug("new client type: $req->{type} to $addr:$port");
30 my $send = AC::MrGamoo::Protocol->encode_request( $req, $data );
31 my $me = $class->SUPER::new( $addr, $port,
32 info => "client $req->{type} to $addr:$port; $info",
33 request => $send,
34 );
35  
36 return $me;
37 }
38  
39 sub start {
40 my $me = shift;
41  
42 $me->set_callback('timeout', \&_timeout);
43 $me->set_callback('read', \&_read);
44 $me->set_callback('shutdown', \&_shutdown);
45  
46 $me->SUPER::start();
47 $me->timeout_rel($TIMEOUT);
48 $me->write( $me->{request} );
49  
50 return $me;
51 }
52  
53 sub _timeout {
54 my $me = shift;
55 $me->shut();
56 }
57  
58 sub _shutdown {
59 my $me = shift;
60  
61 if( $me->{status_ok} ){
62 $me->run_callback('on_success', { result => $me->{result} } );
63 }else{
64 $me->run_callback('on_failure');
65 }
66 }
67  
68 sub _read {
69 my $me = shift;
70 my $evt = shift;
71  
72 debug("recvd reply to $me->{info}");
73  
74 my($proto, $data, $content) = read_protocol_no_content( $me, $evt );
75 return unless $proto;
76  
77 # check response
78 if( $proto->{is_error} ){
79 return $me->_uhoh("rcvd error response");
80 }
81  
82 $proto->{data} = AC::MrGamoo::Protocol->decode_reply($proto, $data);
83 debug("recvd reply to $me->{info} - $proto->{data}{status_code} $proto->{data}{status_message}");
84  
85 if( $proto->{data}{status_code} != 200 ){
86 return $me->_uh_oh("recvd error reply $proto->{data}{status_code} $proto->{data}{status_message}");
87 }
88  
89 $me->{result} = $proto;
90 $me->{status_ok} = 1;
91 $me->shut();
92 }
93  
94 sub _uh_oh {
95 my $me = shift;
96 my $msg = shift;
97  
98 debug("error $msg");
99 $me->run_callback('error', { error => $msg } );
100 $me->shut();
101 }
102  
103  
104 1;
105