blib/lib/Asynchat.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 222 CC 12 SCAR 27.2
CC
12
Cov
0%
handle_read
56.0
initiate_send
30.0
push_direct
6.0
CRAP 15.2
1 package Asynchat;
2 {
3 $Asynchat::VERSION = '0.01';
4 }
5  
6 #==============================================================================
7 #
8 # FILE: Asynchat.pm
9 #
10 # DESCRIPTION: porting in Perl of asynchat.py (python 2.7)
11 #
12 # FILES: ---
13 # BUGS: ---
14 # NOTES: ---
15 # AUTHOR: (Sebastiano Piccoli), <sebastiano.piccoli@gmail.com>
16 # COMPANY:
17 # VERSION: 0.01
18 # CREATED: 08/09/12 19:47:28 CEST
19 # REVISION: ---
20 #==============================================================================
21  
22 use strict;
23 use warnings;
24  
25 use Scalar::Util::Numeric qw(isnum isint);
26 use Asyncore;
27 use base qw( Asyncore::Dispatcher );
28 use Socket;
29 use Carp;
30  
31 our $ac_in_buffer_size = 4096;
32 our $ac_out_buffer_size = 4096;
33  
34 sub init {
35 my($self, $sock, $map) = @_;
36  
37 $self->{_ac_in_buffer} = '';
38 $self->{_incoming} = [];
39 $self->{_producer_fifo} = []; # a list of data;
40 $self->SUPER::init($sock, $map);
41
42 return $self;
43 }
44  
45 sub collect_incoming_data {
46 # overrided
47 }
48  
49 sub _collect_incoming_data {
50 my($self, $data) = @_;
51
52 push(@{ $self->{_incoming} }, $data);
53 }
54  
55 sub _get_data {
56 my $self = shift;
57  
58 my $data = join("", @{ $self->{_incoming} });
59 #delete $self->{_incoming};
60
61 return $data;
62 }
63  
64 sub found_terminator {
65 # overrided
66 }
67  
68 sub set_terminator {
69 my($self, $terminator) = @_;
70
71 # Set the input delimiter.
72 # Can be a fixed string of any length, an integer, or undef
73 $self->{_terminator} = $terminator;
74 }
75  
76 sub get_terminator {
77 my $self = shift;
78
79 return $self->{_terminator};
80 }
81  
82 sub handle_read {
83 my $self = shift;
84
85 my $data = $self->receive($ac_in_buffer_size);
86 # catch error todo
87
88 $self->{_ac_in_buffer} = $self->{_ac_in_buffer} . $data;
89
90 # Continue to search for terminator in ac_in_buffer,
91 # while calling collect_incoming_data. The while loop
92 # is necessary because we might read several data terminator
93 # combos with a single recv(4096).
94 while ($self->{_ac_in_buffer}) {
95 my $lb = length($self->{_ac_in_buffer});
96 my $terminator = $self->get_terminator();
97 if (not $terminator) {
98 # no terminator, collect it all
99 $self->collect_incoming_data($self->{_ac_in_buffer});
100 $self->{_ac_in_buffer} = '';
101 }
102 elsif ((isnum($terminator)) and (isint($terminator) > 0)) {
103 # numeric terminator
104 my $n = $terminator;
105 if ($lb < $n) {
106 $self->collect_incoming_data($self->{_ac_in_buffer});
107 $self->{_ac_in_buffer} = '';
108 $self->{_terminator} = $self->{_terminator} - $lb;
109 }
110 else {
111 # collect first n chars just to complete all the chars
112 $self->collect_incoming_data(substr($self->{_ac_in_buffer}, 0, $n));
113 $self->{_ac_in_buffer} = substr($self->{_ac_in_buffer}, $n, $lb);
114 $self->{_terminator} = 0;
115 $self->found_terminator();
116 }
117 }
118 else {
119 # 3 cases:
120 # 1) end of buffer matches terminator exactly:
121 # collect data, transition
122 # 2) end of buffer matches some prefix:
123 # collect data to the prefix
124 # 3) end of buffer does not match any prefix:
125 # collect data
126 my $terminator_length = length($terminator);
127 if ($self->{_ac_in_buffer} =~ m/$terminator/g) {
128 # we found the terminator so collect data just to terminator
129 my $offset = pos($self->{_ac_in_buffer});
130 if ($offset > 0) {
131 $self->collect_incoming_data(substr($self->{_ac_in_buffer}, 0, $offset));
132 }
133 # todo: check this sum
134 #$self->{_ac_in_buffer} = substr($self->{_ac_in_buffer}, $offset+$terminator_length);
135 $self->{_ac_in_buffer} = substr($self->{_ac_in_buffer}, $offset);
136 $self->found_terminator();
137 }
138 else {
139 if (find_prefix_at_end($self->{_ac_in_buffer}, $terminator)) {
140 # todo
141 }
142 else {
143 $self->collect_incoming_data($self->{_ac_in_buffer});
144 $self->{_ac_in_buffer} = "";
145 }
146 }
147 }
148 }
149 }
150  
151 sub handle_write {
152 my $self = shift;
153
154 $self->initiate_send();
155 }
156  
157 sub handle_close {
158 my $self = shift;
159
160 $self->close();
161 }
162  
163 sub push_direct {
164 my($self, $data) = @_;
165
166 my $sabs = $ac_in_buffer_size;
167 if (length($data) > $sabs) {
168 for (my $i = 0; $i < length($data); $i += $sabs) {
169 push(@{ $self->{_producer_fifo} }, substr($data, $i, $i + $sabs));
170 }
171 }
172 else {
173 push @{ $self->{_producer_fifo} }, $data;
174 }
175 $self->initiate_send();
176 }
177  
178 sub push_with_producer {
179 my($self, $producer) = @_;
180
181 push @{ $self->{_producer_fifo} }, $producer;
182 $self->initiate_send();
183 }
184  
185 sub readable {
186 return 1;
187 }
188  
189 sub writeable {
190 my $self = shift;
191
192 # check this
193 return $self->{_producer_fifo} || (not $self->{_connected});
194 }
195  
196 sub close_when_done {
197 my $self = shift;
198  
199 # automatically close this channel once the outgoing queue is empty
200 push @{ $self->{_producer_fifo} }, undef;
201 }
202  
203 sub initiate_send {
204 my $self = shift;
205
206 while ((@{ $self->{_producer_fifo} }) and ($self->{_connected})) {
207 my $first = shift @{ $self->{_producer_fifo} };
208
209 if (!defined $first) {
210 $self->handle_close();
211 return;
212 }
213 if ($first eq '') {
214 shift @{ $self->{_producer_fifo} };
215 }
216
217 my $data = substr($first, 0, $ac_out_buffer_size);
218
219 # catch exception todo
220 my $sock = $self->{_socket};
221 my $num_sent = send($sock, $data, 0);
222
223 if ($num_sent) {
224 if (($num_sent < length($data)) or
225 ($ac_out_buffer_size < length($first))) {
226
227 unshift @{ $self->{_producer_fifo} }, substr($first, $num_sent);
228 }
229 }
230
231 # we tried to send some actual data
232 return;
233 }
234 }
235  
236 sub discard_buffers {
237 my $self = shift;
238  
239 $self->{_ac_in_buffer} = '';
240 $self->{_incoming} = [];
241 $self->{_producer_fifo} = [];
242 }
243  
244 sub find_prefix_at_end {
245 # todo
246 }
247  
248  
249 package Asynchat::SimpleProducer;
250  
251 #==============================================================================
252 #
253 # FILE: Asynchat.pm
254 # PACKAGE: Asynchat::SimpleProducer
255 #
256 # DESCRIPTION:
257 #
258 # FILES: ---
259 # BUGS: ---
260 # NOTES: ---
261 # AUTHOR: (Sebastiano Piccoli), <sebastiano.piccoli@gmail.com>
262 # COMPANY:
263 # VERSION: 0.1
264 # CREATED: 08/02/13 14:34:00 CEST
265 # REVISION: ---
266 #==============================================================================
267  
268 use strict;
269 use warnings;
270  
271 sub init {
272 my $self = shift;
273  
274 # todo
275 }
276  
277 sub more {
278 my $self = shift;
279  
280 # todo
281 }
282  
283 1;
284  
285 __END__