File Coverage

lib/IOMux/Bundle.pm
Criterion Covered Total %
statement 45 72 62.5
branch 11 26 42.3
condition 1 3 33.3
subroutine 14 28 50.0
pod 20 21 95.2
total 91 150 60.6


line stmt bran cond sub pod time code
1             # Copyrights 2011 by Mark Overmeer.
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 1.07.
5 3     3   964 use warnings;
  3         5  
  3         90  
6 3     3   16 use strict;
  3         6  
  3         115  
7              
8             package IOMux::Bundle;
9 3     3   13 use vars '$VERSION';
  3         6  
  3         145  
10             $VERSION = '0.12';
11              
12 3     3   15 use base 'IOMux::Handler::Read', 'IOMux::Handler::Write';
  3         4  
  3         3129  
13              
14 3     3   18 use Log::Report 'iomux';
  3         8  
  3         14  
15              
16             ##### WORK IN PROGRESS!
17              
18              
19             sub init($)
20 1     1 0 20 { my ($self, $args) = @_;
21              
22             # stdin to be a writer is a bit counter-intuitive, therefore some
23             # extra tests.
24 1         23 my $name = $args->{name};
25              
26 1 50       27 my $in = $self->{IMB_stdin} = $args->{stdin}
27             or error __x"no stdin handler for {name}", name => $name;
28 1 50       50 UNIVERSAL::isa($in, 'IOMux::Handler::Write')
29             or error __x"stdin {name} is not at writer", name => $name;
30              
31 1 50       51 my $out = $self->{IMB_stdout} = $args->{stdout}
32             or error __x"no stdout handler for {name}", name => $name;
33 1 50       12 UNIVERSAL::isa($out, 'IOMux::Handler::Read')
34             or error __x"stdout {name} is not at reader", name => $name;
35              
36 1         10 my $err = $self->{IMB_stderr} = $args->{stderr};
37 1 50 33     13 !$err || UNIVERSAL::isa($out, 'IOMux::Handler::Read')
38             or error __x"stderr {name} is not at reader", name => $name;
39              
40 1         69 my @filenos = ($in->fileno, $out->fileno);
41 1 50       10 push @filenos, $err->fileno if $err;
42              
43 1         10 $self->{IMB_filenos} = \@filenos;
44 1         18 $args->{name} .= ', ('.join(',',@filenos).')';
45              
46 1         52 $self->SUPER::init($args);
47 1         3 $self;
48             }
49              
50             #---------------
51              
52 2     2 1 1002119 sub stdin() {shift->{IMB_stdin}}
53 0     0 1 0 sub stdout() {shift->{IMB_stdout}}
54 0     0 1 0 sub stderr() {shift->{IMB_stderr}}
55              
56             sub connections()
57 1     1 1 2 { my $self = shift;
58 1 50       37 ( $self->{IMB_stdin}
59             , $self->{IMB_stdout},
60             , ($self->{IMB_stderr} ? $self->{IMB_stderr} : ())
61             );
62             }
63              
64             #---------------
65              
66             # say, print and printf use write()
67 2     2 1 32 sub write(@) { shift->{IMB_stdin}->write(@_) }
68 0     0 1 0 sub mux_outbuffer_empty() { shift->{IMB_stdin}->mux_outbuffer_empty(@_) }
69 0     0 1 0 sub mux_output_waiting() { shift->{IMB_stdin}->mux_output_waiting(@_) }
70 0     0 1 0 sub mux_write_flagged() { shift->{IMB_stdin}->mux_write_flagged(@_) }
71              
72 0     0 1 0 sub readline(@) { shift->{IMB_stdout}->readline(@_) }
73 1     1 1 15 sub slurp(@) { shift->{IMB_stdout}->slurp(@_) }
74 0     0 1 0 sub mux_input($) { shift->{IMB_stdout}->mux_input(@_) }
75 0     0 1 0 sub mux_eof($) { shift->{IMB_stdout}->mux_eof(@_) }
76              
77             sub mux_read_flagged($)
78 0     0 1 0 { my ($self, $fileno) = @_;
79 0 0       0 if(my $e = $self->{IMB_stderr})
80 0 0       0 { return $e->mux_read_flagged(@_)
81             if $fileno==$e->fileno;
82             }
83 0         0 $self->{IMB_stdin}->mux_read_flagged(@_);
84             }
85              
86 0     0 1 0 sub timeout() { shift->{IMB_stdin}->timeout(@_) }
87              
88             sub close(;$)
89 1     1 1 3 { my ($self, $cb) = @_;
90             my $close_error = sub
91 1 50   1   20 { if(my $err = $self->{IMB_stderr}) { $err->close($cb) }
  0 50       0  
92 0         0 elsif($cb) { $cb->($self) }
93 1         8 };
94              
95             my $close_out = sub
96 1 50   1   5 { if(my $out = $self->{IMB_stdout}) { $out->close($close_error) }
  1         7  
97 0         0 else { $close_error->() }
98 1         7 };
99              
100 1 50       5 if(my $in = $self->{IMB_stdin}) { $in->close($close_out) }
  1         6  
101 0         0 else { $close_out->() }
102             }
103              
104             sub mux_remove()
105 0     0 1 0 { my $self = shift;
106 0         0 $_->mux_remove for $self->connections;
107 0         0 trace "mux remove bundle ".$self->name;
108             }
109              
110             sub mux_init($)
111 1     1 1 2 { my ($self, $mux) = @_;
112              
113             $_->mux_init($mux, $self) # I want control
114 1         27 for $self->connections;
115              
116 1         53 trace "mux add bundle ".$self->name;
117             }
118              
119             #---------------
120            
121             sub mux_error($)
122 0     0 1   { my ($self, $errbuf) = @_;
123 0           print STDERR $$errbuf;
124 0           $$errbuf = '';
125             }
126              
127             #---------------
128              
129             sub show()
130 0     0 1   { my $self = shift;
131 0           join "\n", map({$_->show} $self->connections),'';
  0            
132             }
133              
134 0     0 1   sub fdset() {panic}
135              
136             1;