File Coverage

blib/lib/RFID/Reader/TCP.pm
Criterion Covered Total %
statement 44 50 88.0
branch 9 20 45.0
condition 1 3 33.3
subroutine 8 9 88.8
pod 1 1 100.0
total 63 83 75.9


line stmt bran cond sub pod time code
1             package RFID::Reader::TCP;
2 4     4   154007 use RFID::Reader qw(ref_tainted); $VERSION=$RFID::Reader::VERSION;
  4         12  
  4         365  
3              
4             # Written by Scott Gifford
5             # Copyright (C) 2004-2006 The Regents of the University of Michigan.
6             # See the file LICENSE included with the distribution for license
7             # information.
8              
9             =head1 NAME
10              
11             RFID::Reader::TCP - Abstract base class for RFID readers implemented over a TCP connection
12              
13             =head1 SYNOPSIS
14              
15             This is an abstract base class used for building an RFID Reader class
16             implemented over a TCP connection. It provides the basic I/O methods
17             that an object based on L will expect, and
18             generally a reader based on this class will simply inherit from it and
19             add a few details. In other words, this class is fairly complete, and
20             you shouldn't have to add much to it to make it workable.
21              
22             =head1 DESCRIPTION
23              
24             =cut
25              
26 4     4   1025 use IO::Socket::INET;
  4         28499  
  4         138  
27 4     4   14082 use IO::Select;
  4         9017  
  4         3984  
28              
29             =head2 Constructor
30              
31             =head3 new
32              
33             This constructor accepts all arguments to the constructor for
34             L, and passes them along to it.
35             Any other settings are intrepeted as parameters to the
36             L method.
37              
38             Currently, the Timeout parameter is sometimes ignored. That will be
39             fixed in the future.
40              
41             =cut
42              
43             sub new
44             {
45 2     2 1 2005335 my $class = shift;
46 2         182 my(%p)=@_;
47 2         21 my $buf;
48              
49 2         26 my $self = {};
50 2         768 bless $self,$class;
51              
52             # For IO::Socket::INET
53 2 50 33     426 if ($p{timeout} && !$p{Timeout})
54             {
55 0         0 $p{Timeout}=$p{timeout};
56             }
57              
58 2 50       272 $self->{_sock}=IO::Socket::INET->new(%p)
59             or die "Couldn't create socket: $!\n";
60            
61 2 50       8230 $self->{_select}=IO::Select->new($self->{_sock})
62             or die "Couldn't create IO::Select: $!\n";
63              
64             # Clear out any gibberish that's waiting for us
65 2         142 while ($self->{_select}->can_read(1))
66             {
67 0         0 ($self->{_sock}->sysread($buf,8192));
68 0         0 warn "Ignoring initial text '$buf'\n";
69             }
70              
71 2         2004741 $self->_init(%p);
72              
73 2         28 $self;
74             }
75              
76             sub _init
77 2     2   11 {
78             }
79              
80             sub _readbytes
81             {
82 1     1   2885 my $self = shift;
83 1         3 my($bytesleft)=@_;
84 1         3 my $data = "";
85              
86 1         6 while($bytesleft > 0)
87             {
88 1         1 my $moredata;
89 1 50       5 if ($self->{timeout})
90             {
91 0 0       0 $self->{_select}->can_read($self->{timeout})
92             or die "Read timed out.\n";
93             }
94 1 50       82 my $rb = $self->{_sock}->sysread($moredata,$bytesleft)
95             or die "Socket unexpectedly closed!\n";
96 1         28 $bytesleft -= $rb;
97 1         4 $data .= $moredata;
98             }
99 1         8 $data;
100             }
101              
102             sub _readuntil
103             {
104 2     2   5 my $self = shift;
105 2         14 my($delim) = @_;
106              
107 2         19 local $/ = $delim;
108 2         5 my $fh = $self->{_sock};
109 2 50       35 defined(my $data = <$fh>)
110             or die "Couldn't read from socket: $!\n";
111 2         5 chomp($data);
112 2         13 $data;
113             }
114              
115             sub _writebytes
116             {
117 2     2   6348 my $self = shift;
118 2         15 my $wb = join("",@_);
119 2 100       20 if (ref_tainted(\$wb)) { die "Attempt to send tainted data to reader"; }
  1         21  
120 1 50       6 if ($self->{timeout})
121             {
122 0 0       0 $self->{_select}->can_write($self->{timeout})
123             or die "Write timed out.\n";
124             }
125 1         30 $self->{_sock}->syswrite($wb);
126             }
127              
128             sub _connected
129             {
130 0     0     return $self->{_sock};
131             }
132              
133             =head1 SEE ALSO
134              
135             L, L, L.
136              
137             =head1 AUTHOR
138              
139             Scott Gifford Egifford@umich.eduE, Esgifford@suspectclass.comE
140              
141             Copyright (C) 2004-2006 The Regents of the University of Michigan.
142              
143             See the file LICENSE included with the distribution for license
144             information.
145              
146             =cut
147              
148             1;