File Coverage

lib/App/MtAws/LineProtocol.pm
Criterion Covered Total %
statement 48 48 100.0
branch 18 24 75.0
condition 9 18 50.0
subroutine 11 11 100.0
pod 0 4 0.0
total 86 105 81.9


line stmt bran cond sub pod time code
1             # mt-aws-glacier - Amazon Glacier sync client
2             # Copyright (C) 2012-2014 Victor Efimov
3             # http://mt-aws.com (also http://vs-dev.com) vs@vs-dev.com
4             # License: GPLv3
5             #
6             # This file is part of "mt-aws-glacier"
7             #
8             # mt-aws-glacier is free software: you can redistribute it and/or modify
9             # it under the terms of the GNU General Public License as published by
10             # the Free Software Foundation, either version 3 of the License, or
11             # (at your option) any later version.
12             #
13             # mt-aws-glacier is distributed in the hope that it will be useful,
14             # but WITHOUT ANY WARRANTY; without even the implied warranty of
15             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16             # GNU General Public License for more details.
17             #
18             # You should have received a copy of the GNU General Public License
19             # along with this program. If not, see <http://www.gnu.org/licenses/>.
20              
21             package App::MtAws::LineProtocol;
22              
23             our $VERSION = '1.114_2';
24              
25 19     19   83921 use strict;
  19         24  
  19         459  
26 19     19   63 use warnings;
  19         24  
  19         365  
27 19     19   73 use utf8;
  19         19  
  19         67  
28 19     19   272 use Carp;
  19         21  
  19         868  
29              
30 19     19   12040 use JSON::XS;
  19         76581  
  19         1013  
31 19     19   123 use App::MtAws::Utils;
  19         27  
  19         2255  
32              
33 19     19   92 use Exporter 'import';
  19         19  
  19         9245  
34              
35             our @EXPORT = qw/ get_data send_data/;
36             our @EXPORT_OK = qw/escape unescape encode_data decode_data/;
37              
38             # yes, a module, so we can unit-test it (JSON and YAML have different serialization implementeation)
39             my $json_coder = JSON::XS->new->ascii(1)->allow_nonref;
40              
41             sub decode_data
42             {
43 61     61 0 256 my ($data_e) = @_;
44 61         938 return $json_coder->decode($data_e);
45             }
46              
47             sub encode_data
48             {
49 67     67 0 165669 my ($data) = @_;
50 67         820 return $json_coder->encode($data);
51             }
52              
53              
54             sub get_data
55             {
56 11     11 0 3620 my ($fh) = @_;
57              
58 11         22 my ($len, $line);
59              
60 11 50 33     54 sysreadfull_chk($fh, $len, 8) &&
61             sysreadfull_chk($fh, $line, $len+0) or
62             return;
63              
64 11         46 chomp $line;
65 11         68 my ($pid, $action, $taskid, $datasize, $attachmentsize) = split /\t/, $line;
66 11 50       37 sysreadfull_chk($fh, my $data_e, $datasize) or
67             return;
68 11         22 my $attachment = undef;
69 11 100       26 if ($attachmentsize) {
70 7 50       23 sysreadfull_chk($fh, $attachment, $attachmentsize) or
71             return;
72             }
73 11         36 my $data = decode_data($data_e);
74 11 100       86 return ($pid, $action, $taskid, $data, defined($attachment) ? \$attachment : ());
75             }
76              
77             sub send_data
78             {
79 15     15 0 49095 my ($fh, $action, $taskid, $data, $attachmentref) = @_;
80 15         60 my $data_e = encode_data($data);
81 15 50       88 confess if is_wide_string($data_e);
82 15 100       52 if ($attachmentref) {
83 11 100       43 confess "Attachment should be a binary string" if is_wide_string($$attachmentref);
84 9 100 100     519 confess "Attachment should not be empty" unless defined($$attachmentref) && length($$attachmentref);
85             }
86 11 100       47 my $attachmentsize = $attachmentref ? length($$attachmentref) : 0;
87 11         20 my $datasize = length($data_e);
88 11         102 my $line = "$$\t$action\t$taskid\t$datasize\t$attachmentsize\n"; # encode_data returns ASCII-7bit data, so ok here
89 11 50       45 confess if is_wide_string($line);
90 11 50 33     120 syswritefull_chk($fh, sprintf("%08d", length($line))) &&
      33        
      66        
      33        
91             syswritefull_chk($fh, $line) &&
92             syswritefull_chk($fh, $data_e) &&
93             (!$attachmentsize || syswritefull_chk($fh, $$attachmentref)) or
94             return;
95 11         76 return 1;
96             }
97              
98              
99              
100             1;
101              
102             __END__