OWNet.pm

filter: Statementstmt 90.4% 198 / 219 Branchbran 82.8% 58 / 70 Conditioncond 45.4% 5 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 80.4% 37 / 46 Podpod 100% 15 / 15 totaltotal 85.3% 314 / 368 CC 42 SCAR 13.7
CC
42
Cov
87%
__ANON__
8.0
_run_cmd
7.7
_msg
7.0
CRAP 3.9
↑ Summary OWNet/Constants.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package AnyEvent::OWNet;
4 3 $AnyEvent::OWNet::VERSION = '1.163170';
5 # ABSTRACT: Client for 1-wire File System server
6  
7  
8 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use constant DEBUG => $ENV{ANYEVENT_OWNET_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Sub::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use AnyEvent::OWNet::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use AnyEvent::OWNet::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
23 6 my ($pkg, %p) = @_;
24 6 bless
25 {
26 connect_queue => [],
27 host => '127.0.0.1',
28 port => 4304,
29 timeout => 5,
30 %p,
31 }, $pkg;
32 }
33  
34 23 ▶sub _msg {
Subroutine _msg: called CC=7 SCAR=19.5
35 23 my ($self, $req) = @_;
36 23 ▶ my $version = exists $req->{version} ? $req->{version} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $req->{'version'} ? : 1 22 23
37 23 ▶ my $data = exists $req->{data} ? $req->{data} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $req->{'data'} ? : 22 1 23
38 23 my $payload = length $data;
39 my $type =
40 23 ▶ exists $req->{type} ? $req->{type} : OWNET_MSG_READ; # default to read
Branch: 1 branch
BranchTrueFalseTotal
exists $req->{'type'} ? : 22 1 23
41 23 ▶ my $sg = exists $req->{sg} ? $req->{sg} : OWNET_DEFAULT_FLAGS;
Branch: 1 branch
BranchTrueFalseTotal
exists $req->{'sg'} ? : 1 22 23
42 23 ▶ my $size = exists $req->{size} ? $req->{size} : OWNET_DEFAULT_DATA_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
exists $req->{'size'} ? : 8 15 23
43 23 ▶ my $offset = exists $req->{offset} ? $req->{offset} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $req->{'offset'} ? : 1 22 23
44 23 return pack 'N6a*', $version, $payload, $type, $sg, $size, $offset, $data;
45 }
46  
47  
48 3 ▶sub read {
Subroutine read: called CC=1 SCAR=0.0
Pod documented
49 3 my ($self, $path, $sub) = @_;
50 3 $self->_run_cmd({ data => $path.chr(0), type => OWNET_MSG_READ }, $sub);
51 }
52  
53  
54 2 ▶sub write {
Subroutine write: called CC=1 SCAR=0.0
Pod documented
55 2 my ($self, $path, $value, $sub) = @_;
56 2 $self->_run_cmd({ data => $path.chr(0).$value,
57 size => length $value,
58 type => OWNET_MSG_WRITE }, $sub);
59 }
60  
61  
62 6 ▶sub dir {
Subroutine dir: called CC=1 SCAR=0.0
Pod documented
63 6 my ($self, $path, $sub) = @_;
64 6 $self->_run_cmd({ data => $path."\0", type => OWNET_MSG_DIR, size => 0 },
65 $sub);
66 }
67  
68  
69 2 ▶sub present {
Subroutine present: called CC=1 SCAR=0.0
Pod documented
70 2 my ($self, $path, $sub) = @_;
71 2 $self->_run_cmd({ data => $path."\0", type => OWNET_MSG_PRESENT }, $sub);
72 }
73  
74  
75 1 ▶sub dirall {
Subroutine dirall: called CC=1 SCAR=0.0
Pod documented
76 1 my ($self, $path, $sub) = @_;
77 1 $self->_run_cmd({ data => $path."\0", type => OWNET_MSG_DIRALL }, $sub);
78 }
79  
80  
81 2 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
82 2 my ($self, $path, $sub) = @_;
83 2 $self->_run_cmd({ data => $path."\0", type => OWNET_MSG_GET }, $sub);
84 }
85  
86  
87 1 ▶sub dirallslash {
Subroutine dirallslash: called CC=1 SCAR=0.0
Pod documented
88 1 my ($self, $path, $sub) = @_;
89 1 $self->_run_cmd({ data => $path."\0", type => OWNET_MSG_DIRALLSLASH }, $sub);
90 }
91  
92  
93 5 ▶sub getslash {
Subroutine getslash: called CC=1 SCAR=0.0
Pod documented
94 5 my ($self, $path, $sub) = @_;
95 5 $self->_run_cmd({ data => $path."\0", type => OWNET_MSG_GETSLASH }, $sub);
96 }
97  
98 22 ▶sub _run_cmd {
Subroutine _run_cmd: called CC=7 SCAR=20.4
99 22 my $self = shift;
100 22 my $cmd = shift;
101  
102 22 my ($cv, $cb);
103 22 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 22 0 22
104 22 ▶ $cv = pop if UNIVERSAL::isa($_[-1], 'AnyEvent::CondVar');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[-1], "AnyEvent::CondVar") 0 22 22
105 22 ▶ $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 7 15 22
106 }
107  
108 22 ▶ $cv ||= AnyEvent->condvar;
Condition33%1/3
$cv ||= "AnyEvent"->condvar
l!l&&r!l&&!r
0220
MC/DC0%0/2
$cv ||= "AnyEvent"->condvar
$cv "AnyEvent"->condvar
Truth table33%1/3
A ||= B
A= $cvB= "AnyEvent"->condvar
ABresult
1X1
011
000
109  
110 22 print STDERR "using condvar $cv\n" if DEBUG;
111  
112 7 ▶ $cv->cb(subname 'command_cb' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.6
113 7 my $cv = shift;
114 7 print STDERR "calling callback $cv\n" if DEBUG;
115 7 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
116 7 my $res = $cv->recv;
117 7 $cb->($res);
118 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
119 0 ▶ ($self->{on_error} || sub { die "ARGH: @_\n"; })->($_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$self->{'on_error'} || sub { die "ARGH: @_\n"; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'on_error'} || sub { die "ARGH: @_\n"; }
$self->{'on_error'} sub { die "ARGH: @_\n"; }
Truth table0%0/3
A || B
A= $self->{'on_error'}B= sub { die "ARGH: @_\n"; }
ABresult
1X1
011
000
120 }
121 22 ▶ }) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 7 15 22
122  
123  
124 22 print STDERR 'Running command, ', $cmd->{type}, "\n" if DEBUG;
125 22 ▶ if( $self->{handle} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'handle'}) { } 14 8 22
126 14 $self->_command_sub($cmd, $cv);
127 } else {
128 8 $self->connect($cmd, $cv );
129 }
130  
131 22 return $cv;
132 }
133  
134 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
135  
136  
137 39 ▶sub all_cv {
Subroutine all_cv: called CC=3 SCAR=11.2
Pod documented
138 39 my $self = shift;
139 39 ▶ $self->{all_cv} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 39 39
140 39 ▶ unless ($self->{all_cv}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"all_cv"}) 5 34 39
141 5 $self->{all_cv} = AnyEvent->condvar;
142 0 ▶ $self->{all_cv}->cb( sub { print STDERR "all_cv done\n" } ) if DEBUG;
Subroutine __ANON__: not called CC=1 SCAR=1.2
143 }
144 39 $self->{all_cv};
145 }
146  
147  
148 3 ▶sub cleanup {
Subroutine cleanup: called CC=5 SCAR=16.1
Pod documented
149 3 my $self = shift;
150 3 print STDERR "cleanup\n" if DEBUG;
151 3 ▶ $self->{all_cv}->croak(@_) if ($self->{all_cv});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"all_cv"} 2 1 3
152 3 ▶ $self->{connect_cv}->croak( @_ ) if $self->{connect_cv};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"connect_cv"} 1 2 3
153  
154 3 ▶ while (@{$self->{connect_queue}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"connect_queue"};}) 1 3 4
155 1 my $queue = shift @{$self->{connect_queue}};
156 1 my($cmd, $cv) = @$queue;
157 1 $cv->croak(@_);
158 }
159  
160 3 delete $self->{all_cv};
161 3 delete $self->{connect_cv};
162 3 delete $self->{sock};
163 3 delete $self->{handle};
164 3 ▶ $self->{on_error}->(@_) if $self->{on_error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_error"} 2 1 3
165 }
166  
167  
168 8 ▶sub connect {
Subroutine connect: called CC=3 SCAR=11.1
Pod documented
169 8 my( $self, $cmd, $cv ) = @_;
170  
171 8 ▶ push @{$self->{connect_queue}}, [ $cmd, $cv ]
Branch: 1 branch
BranchTrueFalseTotal
if @_ 8 0 8
172 if @_;
173  
174 # setup already ongoing
175 return $self->{connect_cv}
176 8 ▶ if $self->{connect_cv};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"connect_cv"} 2 6 8
177  
178 # or start setup
179 6 $self->{connect_cv} = AnyEvent->condvar;
180 0 ▶ $self->{connect_cv}->cb( sub { print STDERR "connect_cv done\n" } ) if DEBUG;
Subroutine __ANON__: not called CC=1 SCAR=1.2
181  
182 $self->{sock} = tcp_connect $self->{host}, $self->{port},
183 6 ▶ subname 'tcp_connect_cb' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
184  
185 my $fh = shift
186 6 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
my $fh = shift() or do { my $err = "Can't connect owserver: $!"; $self->cleanup($err); return } 5 1 6
187 1 my $err = "Can't connect owserver: $!";
188 1 $self->cleanup($err);
189 return
190 1 };
191  
192 5 warn "Connected\n" if DEBUG;
193  
194 $self->{handle} = AnyEvent::Handle->new(
195 fh => $fh,
196 1 ▶ on_error => subname('on_error_cb' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
197 1 print STDERR "handle error $_[2]\n" if DEBUG;
198 1 $_[0]->destroy;
199 1 ▶ if ($_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]) 1 0 1
200 1 $self->cleanup('Error: '.$_[2]);
201 }
202 }),
203 0 ▶ on_eof => subname('on_eof_cb' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
204 0 print STDERR "handle eof\n" if DEBUG;
205 0 $_[0]->destroy;
206 0 $self->cleanup('Connection closed');
207 }),
208 1 ▶ on_timeout => subname('on_timeout_cb' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
209 1 print STDERR "handle timeout\n" if DEBUG;
210 1 $_[0]->destroy;
211 1 $self->cleanup('Socket timeout');
212 })
213 5 );
214 5 ▶ while (@{$self->{connect_queue}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"connect_queue"};}) 7 5 12
215 7 my $queue = shift @{$self->{connect_queue}};
216 7 $self->_command_sub(@$queue );
217 }
218 5 $self->{connect_cv}->send(1);
219 5 delete $self->{connect_cv};
220 6 };
221  
222 6 return $self->{connect_cv};
223 }
224  
225  
226 21 ▶sub _command_sub {
Subroutine _command_sub: called CC=1 SCAR=0.0
227 21 my( $self, $command, $cv ) = @_;
228  
229 21 $self->all_cv->begin;
230  
231 21 my $msg = $self->_msg($command);
232 21 print STDERR "sending command ", $command->{type}, "\n" if DEBUG;
233 21 warn 'Sending: ', (unpack 'H*', $msg), "\n" if DEBUG;
234  
235 21 $self->{handle}->push_write($msg);
236 21 $self->{handle}->timeout($self->{timeout});
237  
238 18 ▶ $self->{handle}->push_read(ref $self, $command => subname 'push_read_cb' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
239 18 my($handle, $res, $err) = @_;
240 18 $handle->timeout(0);
241 18 print STDERR "read finished $cv\n" if DEBUG;
242 18 print STDERR "read ",
243 ($cv->ready ? "ready" : "not ready"), "\n" if DEBUG;
244 18 $self->all_cv->end;
245 18 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 18 18
246 0 print STDERR "returning error $err\n" if DEBUG;
247 0 return $cv->croak($res)
248 }
249 18 print STDERR "Sending $res\n" if DEBUG;
250 18 $cv->send($res);
251 21 });
252  
253 21 return $cv;
254 }
255  
256  
257 4 ▶sub devices {
Subroutine devices: called CC=3 SCAR=11.0
Pod documented
258 4 my ($self, $cb, $offset, $cv) = @_;
259 4 ▶ $offset ||= '/';
Condition100%2/2
$offset ||= "/"
l!l
31
MC/DC100%1/1
$offset ||= "/"
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
260 4 ▶ $cv ||= AnyEvent->condvar;
Condition66%2/3
$cv ||= "AnyEvent"->condvar
l!l&&r!l&&!r
310
MC/DC0%0/2
$cv ||= "AnyEvent"->condvar
$cv "AnyEvent"->condvar
Truth table66%2/3
A ||= B
A= $cvB= "AnyEvent"->condvar
ABresult
1X1
011
000
261 4 print STDERR "devices: $offset\n" if DEBUG;
262 4 $cv->begin;
263 4 ▶ $self->getslash($offset, subname 'devices_getslash_cb' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
264 4 my $res = shift;
265 4 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 4 0 4
266 4 foreach my $d ($res->data_list) {
267 14 ▶ if ($d =~ m!^.*/[0-9a-f]{2}\.[0-9a-f]{12}/$!i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d =~ m[^.*/[0-9a-f]{2}\.[0-9a-f]{12}/$]i) { } 2 12 14
elsif ($d =~ m[/(?:main|aux)/$]) { } 1 11 12
268 2 $cb->($d, $cv);
269 2 $self->devices($cb, $d, $cv);
270 } elsif ($d =~ m!/(?:main|aux)/$!) {
271 1 $self->devices($cb, $d, $cv);
272 }
273 }
274 } # TOFIX: propagate error?
275 4 $cv->end;
276 4 });
277 4 $cv;
278 }
279  
280  
281 0 ▶sub device_files {
Subroutine device_files: not called CC=2 SCAR=17.9
Pod documented
282 0 my ($self, $cb, $files, $offset, $cv) = @_;
283 0 ▶ $files = [$files] unless (ref $files);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $files 0 0 0
284 0 ▶ $cv = $self->devices(subname('device_files_devices_cb' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
285 0 my $dev = shift;
286 0 foreach my $file (@$files) {
287 0 $cv->begin;
288 $self->get($dev.$file,
289 0 ▶ subname 'device_files_get_cb' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
290 0 my $res = shift;
291 0 $cv->end;
292 0 my $value = $res->{data};
293 0 ▶ return unless (defined $value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
294 0 $cb->($dev, $file, 0+$value);
295 0 });
296 }
297 0 }), $offset, $cv);
298 }
299  
300  
301 21 ▶sub anyevent_read_type {
Subroutine anyevent_read_type: called CC=1 SCAR=0.0
Pod documented
302 21 my ($handle, $cb, $command) = @_;
303  
304 21 my $MAX_RETURN = 66000;
305 21 my @data;
306 41 ▶ subname 'anyevent_read_type_reader' => sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
307 41 my $rbuf = \$handle->{rbuf};
308  
309 47 ▶ REDO:
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$rbuf 7 40 47
310 return unless (defined $$rbuf);
311 40 my $len;
312  
313 my %result;
314 40 my $header;
315 do {
316 40 $len = length $$rbuf;
317 40 print STDERR "read_type has $len bytes\n" if DEBUG;
318 40 print STDERR "read_type has ", (unpack 'H*', $$rbuf), "\n" if DEBUG;
319 40 ▶ return unless ($len >= 24);
Branch: 1 branch
BranchTrueFalseTotal
unless $len >= 24 15 25 40
320 25 @result{qw/version payload ret sg size offset/} = unpack 'N6', $$rbuf;
321 25 $header = substr $$rbuf, 0, 24, '';
322 25 print STDERR "read_type header ", (unpack 'H*', $header), "\n" if DEBUG;
323 25 ▶ if ($result{'ret'} > $MAX_RETURN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result{"ret"} > $MAX_RETURN) 4 21 25
324 4 $cb->($handle, AnyEvent::OWNet::Response->new(%result));
325 4 return 1;
326 }
327 40 ▶ } while ($result{payload} > $MAX_RETURN);
Branch: 1 branch
BranchTrueFalseTotal
$result{"payload"} > $MAX_RETURN and do { do { $len = length $$rbuf; '???'; '???'; return unless $len >= 24; @result{"version", "payload", "ret", "sg", "size", "offset"} = unpack("N6", $$rbuf); $header = substr($$rbuf, 0, 24, ""); '???'; if ($result{"ret"} > $MAX_RETURN) { &$cb($handle, "AnyEvent::OWNet::Response"->new(%result)); return 1; } } } 0 21 21
328  
329 21 my $total_len = 24 + $result{payload};
330 21 print STDERR "read_type have ", $len, " need ", $total_len, "\n" if DEBUG;
331 21 ▶ unless ($len >= $total_len) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($len >= $total_len) 1 20 21
332 1 $$rbuf = $header.$$rbuf;
333 1 return;
334 }
335  
336 20 my $data = substr $$rbuf, 0, $result{payload}, '';
337 20 ▶ if ($command->{type} == OWNET_MSG_DIR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command->{'type'} == 4) { } 8 12 20
338 8 ▶ if ($data eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data eq '') { } 2 6 8
339 2 $result{data} = \@data;
340 } else {
341 6 push @data, substr $data, 0, -1;
342 6 goto REDO;
343 }
344 } else {
345 12 $result{data} = $data;
346 }
347 14 print STDERR "read_type complete\n" if DEBUG;
348 14 $cb->($handle, AnyEvent::OWNet::Response->new(%result));
349 14 return 1;
350 }
351 21 }
352  
353 3 1;
354  
355 __END__
↑ Summary OWNet/Constants.pm »