File Coverage

blib/lib/Riak/Light/Connector.pm
Criterion Covered Total %
statement 37 37 100.0
branch 12 12 100.0
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 56 58 96.5


line stmt bran cond sub pod time code
1             #
2             # This file is part of Riak-Light
3             #
4             # This software is copyright (c) 2013 by Weborama.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9             ## no critic (RequireUseStrict, RequireUseWarnings)
10             package Riak::Light::Connector;
11             {
12             $Riak::Light::Connector::VERSION = '0.10';
13             }
14             ## use critic
15              
16 15     15   58471 use Moo;
  15         315174  
  15         91  
17 15     15   50645 use Types::Standard -types;
  15         1302054  
  15         250  
18             require bytes;
19              
20             # ABSTRACT: Riak Connector, abstraction to deal with binary messages
21              
22             has socket => ( is => 'ro', required => 1 );
23              
24             sub perform_request {
25 23     23 0 7588 my ( $self, $message ) = @_;
26 23         635 my $bytes = pack( 'N a*', bytes::length($message), $message );
27              
28 23         10804 $self->_send_all($bytes); # send request
29             }
30              
31             sub read_response {
32 16     16 0 64 my ($self) = @_;
33 16         181 my $length = $self->_read_length(); # read first four bytes
34 16 100       188 return unless ($length);
35 8         117 $self->_read_all($length); # read the message
36             }
37              
38             sub _read_length {
39 16     16   90 my ($self) = @_;
40              
41 16         194 my $first_four_bytes = $self->_read_all(4);
42              
43 16 100       747 return unpack( 'N', $first_four_bytes ) if defined $first_four_bytes;
44              
45 8         208 undef;
46             }
47              
48             sub _send_all {
49 23     23   99 my ( $self, $bytes ) = @_;
50              
51 23         77 my $length = bytes::length($bytes);
52 23         337 my $offset = 0;
53 23         59 my $sended = 0;
54 23         44 do {
55 26         346 $sended = $self->socket->syswrite( $bytes, $length, $offset );
56              
57             # error in $!
58 26 100       2565 return unless defined $sended;
59              
60             # test if $sended == 0 and $! EAGAIN, EWOULDBLOCK, ETC...
61 20 100       82 return unless $sended;
62              
63 19         92 $offset += $sended;
64             } while ( $offset < $length );
65              
66 16         253 $offset;
67             }
68              
69             sub _read_all {
70 24     24   152 my ( $self, $bufsiz ) = @_;
71              
72 24         104 my $buffer;
73 24         49 my $offset = 0;
74 24         41 my $readed = 0;
75 24         59 do {
76 24         432 $readed = $self->socket->sysread( $buffer, $bufsiz, $offset );
77              
78             # error in $!
79 24 100       1999189 return unless defined $readed;
80              
81             # test if $sended == 0 and $! EAGAIN, EWOULDBLOCK, ETC...
82 18 100       198 return unless $readed;
83              
84 16         127 $offset += $readed;
85             } while ( $offset < $bufsiz );
86              
87 16         228 $buffer;
88             }
89              
90             1;
91              
92              
93             =pod
94              
95             =encoding UTF-8
96              
97             =head1 NAME
98              
99             Riak::Light::Connector - Riak Connector, abstraction to deal with binary messages
100              
101             =head1 VERSION
102              
103             version 0.10
104              
105             =head1 DESCRIPTION
106              
107             Internal class
108              
109             =head1 AUTHORS
110              
111             =over 4
112              
113             =item *
114              
115             Tiago Peczenyj
116              
117             =item *
118              
119             Damien Krotkine
120              
121             =back
122              
123             =head1 COPYRIGHT AND LICENSE
124              
125             This software is copyright (c) 2013 by Weborama.
126              
127             This is free software; you can redistribute it and/or modify it under
128             the same terms as the Perl 5 programming language system itself.
129              
130             =cut
131              
132              
133             __END__