File Coverage

blib/lib/Alien/Taco/Transport.pm
Criterion Covered Total %
statement 33 33 100.0
branch 5 6 83.3
condition n/a
subroutine 5 5 100.0
pod 3 3 100.0
total 46 47 97.8


line stmt bran cond sub pod time code
1             # Taco Perl transport module.
2             # Copyright (C) 2013-2023 Graham Bell
3             #
4             # This program is free software: you can redistribute it and/or modify
5             # it under the terms of the GNU General Public License as published by
6             # the Free Software Foundation, either version 3 of the License, or
7             # (at your option) any later version.
8             #
9             # This program is distributed in the hope that it will be useful,
10             # but WITHOUT ANY WARRANTY; without even the implied warranty of
11             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12             # GNU General Public License for more details.
13             #
14             # You should have received a copy of the GNU General Public License
15             # along with this program. If not, see <http://www.gnu.org/licenses/>.
16              
17             =head1 NAME
18              
19             Alien::Taco::Transport - Taco Perl transport module
20              
21             =head1 DESCRIPTION
22              
23             This package implements the communication between Taco clients
24             and servers.
25              
26             =cut
27              
28             package Alien::Taco::Transport;
29              
30 7     7   114360 use JSON;
  7         80619  
  7         40  
31              
32 7     7   1527 use strict;
  7         14  
  7         2234  
33              
34             our $VERSION = '0.003';
35              
36             =head1 METHODS
37              
38             =over 4
39              
40             =item new(in => $input, out => $output)
41              
42             Construct a new object. This stores the given input and output file
43             handles and instantiates a JSON processor object.
44              
45             =cut
46              
47             sub new {
48 3     3 1 242 my $class = shift;
49 3         17 my %opts = @_;
50              
51 3         38 my $json = new JSON();
52 3         37 $json->convert_blessed(1);
53 3         16 $json->ascii(1);
54              
55 3 100       17 if (exists $opts{'filter_single'}) {
56 2         6 $json->filter_json_single_key_object(@{$opts{'filter_single'}});
  2         17  
57             }
58              
59 3         28 binmode $opts{'in'}, ':encoding(UTF-8)';
60 3         29 binmode $opts{'out'}, ':encoding(UTF-8)';
61              
62             my $self = {
63             in => $opts{'in'},
64 3         30 out => $opts{'out'},
65             json => $json,
66             };
67              
68 3         19 return bless $self, $class;
69             }
70              
71             =item read()
72              
73             Attempt to read a message from the input filehandle. Returns the decoded
74             message as a data structure or undef if nothing was read.
75              
76             =cut
77              
78             sub read {
79 6     6 1 936 my $self = shift;
80 6         13 my $in = $self->{'in'};
81              
82 6         12 my $text = '';
83 6         24 while (<$in>) {
84 12 100       350 last if /^\/\/ END/;
85 6         22 $text .= $_;
86             }
87              
88 6 50       19 return undef unless $text;
89 6         59 return $self->{'json'}->decode($text);
90             }
91              
92             =item write(\%message)
93              
94             Encode the message and write it to the output filehandle.
95              
96             =cut
97              
98             sub write {
99 5     5 1 13 my $self = shift;
100 5         12 my $out = $self->{'out'};
101              
102 5         44 my $text = $self->{'json'}->encode(shift);
103              
104 5         25 local $\ = '';
105 5         20 print $out $text;
106 5         149 print $out "\n\/\/ END\n";
107 5         118 $out->flush();
108             }
109              
110             1;
111              
112             __END__
113              
114             =back
115              
116             =cut