| 1 | package IO::Mux::Select ; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use IO::Select ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use IO::Mux ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use IO::Mux::Handle ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use IO::Mux::Packet ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our $VERSION = '0.08' ; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 1 | my $class = shift ; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | my $this = {} ; | |||||||||||||||||||||||||||
| 18 | 1 | $this->{'select'} = new IO::Select() ; | |||||||||||||||||||||||||||
| 19 | 1 | $this->{mux_handles} = {} ; | |||||||||||||||||||||||||||
| 20 | 1 | bless($this, $class) ; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | $this->add(@_) ; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | return $this ; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 50 | ▶ | sub _get_select { | ||||||||||||||||||||||||||
|
Subroutine
_get_select: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 50 | my $this = shift ; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 50 | return $this->{'select'} ; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 42 | ▶ | sub _get_mux_handles { | ||||||||||||||||||||||||||
|
Subroutine
_get_mux_handles: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 42 | my $this = shift ; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 42 | return $this->{mux_handles} ; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 4 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 4 | my $this = shift ; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 4 | foreach my $h (@_){ | |||||||||||||||||||||||||||
| 46 | 3 | ▶ | if ($h->isa('IO::Mux::Handle')){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 2 | $this->_get_mux_handles()->{$h->_get_tie()->_get_id()} = $h ; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||||||||||
| 50 | 1 | $this->_get_select()->add($h) ; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 3 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 3 | my $this = shift ; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | foreach my $h (@_){ | |||||||||||||||||||||||||||
| 60 | 3 | ▶ | if ($h->isa('IO::Mux::Handle')){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 61 | 1 | delete $this->_get_mux_handles()->{$h->_get_tie()->_get_id()} ; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | elsif ($this->_get_select()->exists($h)){ | ||||||||||||||||||||||||||||
| 64 | 1 | $this->_get_select()->remove($h) ; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 5 | ▶ | sub exists { | ||||||||||||||||||||||||||
|
Subroutine
exists: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 5 | my $this = shift ; | |||||||||||||||||||||||||||
| 72 | 5 | my $h = shift ; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 5 | ▶ | if ($h->isa('IO::Mux::Handle')){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 3 | return $this->_get_mux_handles()->{$h->_get_tie()->_get_id()} ; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||
| 78 | 2 | return $this->_get_select()->exists($h) ; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 3 | ▶ | sub handles { | ||||||||||||||||||||||||||
|
Subroutine
handles: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | 3 | my $this = shift ; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 3 | my @ret = () ; | |||||||||||||||||||||||||||
| 87 | 3 | push @ret, values %{$this->_get_mux_handles()} ; | |||||||||||||||||||||||||||
| 88 | 3 | push @ret, $this->_get_select()->handles() ; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 3 | return @ret ; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 3 | ▶ | sub count { | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 3 | my $this = shift ; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 3 | return scalar($this->handles()) ; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 15 | ▶ | sub can_read { | ||||||||||||||||||||||||||
|
Subroutine
can_read: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 15 | my $this = shift ; | |||||||||||||||||||||||||||
| 103 | 15 | my $timeout = shift ; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # First, we will check to see if the IO::Mux::Handles have data in their buffers. | ||||||||||||||||||||||||||||
| 106 | 15 | my @ready = () ; | |||||||||||||||||||||||||||
| 107 | 15 | foreach my $h (values %{$this->_get_mux_handles()}){ | |||||||||||||||||||||||||||
| 108 | 17 | ▶ | if ((eof($h))||($h->_get_tie()->_get_buffer()->get_length() > 0)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
eof $h or $h->_get_tie->_get_buffer->get_length > 0
MC/DC100%2/2
eof $h or $h->_get_tie->_get_buffer->get_length > 0
eof $h
$h->_get_tie->_get_buffer->get_length > 0
Truth table100%3/3
A or B
A= eof $hB= $h->_get_tie->_get_buffer->get_length > 0
| |||||||||||||||||||||||||||||
| 109 | 2 | push @ready, $h ; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 15 | ▶ | if (scalar(@ready)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | # Maybe some real handles are immediately ready | ||||||||||||||||||||||||||||
| 115 | 2 | push @ready, $this->_get_select()->can_read(0) ; | |||||||||||||||||||||||||||
| 116 | 2 | return @ready ; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # So it seems we may have to wait after all. We now need to build a list | ||||||||||||||||||||||||||||
| 120 | # of all the REAL handles underneath all the IO::Mux::Handles. | ||||||||||||||||||||||||||||
| 121 | 13 | my %mux_objects = () ; | |||||||||||||||||||||||||||
| 122 | 13 | foreach my $h (values %{$this->_get_mux_handles()}){ | |||||||||||||||||||||||||||
| 123 | 14 | my $mux = $h->_get_tie()->_get_mux() ; | |||||||||||||||||||||||||||
| 124 | 14 | my $rh = $mux->_get_handle() ; | |||||||||||||||||||||||||||
| 125 | 14 | ▶ | if (! exists($mux_objects{$rh})){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 12 | $mux_objects{$rh} = {mux => $mux, mux_handles => {}} ; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 14 | $mux_objects{$rh}->{mux_handles}->{$h} = $h ; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 13 | my @real_handles = map {$_->{mux}->_get_handle()} values(%mux_objects) ; | |||||||||||||||||||||||||||
| 132 | 13 | $this->_get_select()->add(@real_handles) ; | |||||||||||||||||||||||||||
| 133 | 13 | @ready = $this->_get_select()->can_read($timeout) ; | |||||||||||||||||||||||||||
| 134 | 13 | $this->_get_select()->remove(@real_handles) ; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 13 | ▶ | if (scalar(@ready)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 9 | my @tmp = @ready ; | |||||||||||||||||||||||||||
| 138 | 9 | my %ready = () ; | |||||||||||||||||||||||||||
| 139 | 9 | @ready = () ; | |||||||||||||||||||||||||||
| 140 | 9 | foreach my $h (@tmp){ | |||||||||||||||||||||||||||
| 141 | 10 | my $mux_data = $mux_objects{$h} ; | |||||||||||||||||||||||||||
| 142 | 10 | ▶ | if ($mux_data){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 6 | my $mux = $mux_data->{mux} ; | |||||||||||||||||||||||||||
| 144 | # We have data ready on the REAL handle. Let's consume the packet | ||||||||||||||||||||||||||||
| 145 | # and add the corresponding IO::Mux::Handle in the new ready list. | ||||||||||||||||||||||||||||
| 146 | 6 | ▶ | while ((my $p = $mux->_read_packet(0)) != -1){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 7 | ▶ | if ((! defined($p))||(! $p)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $p && $p
MC/DC50%1/2
defined $p && $p
defined $p
$p
Truth table66%2/3
A && B
A= defined $pB= $p
| |||||||||||||||||||||||||||||
| 148 | # ERROR or EOF on the real handle. Return all mux_handles | ||||||||||||||||||||||||||||
| 149 | # as they all now are at EOF or have an error state. | ||||||||||||||||||||||||||||
| 150 | 2 | foreach my $mh (values %{$mux_data->{mux_handles}}){ | |||||||||||||||||||||||||||
| 151 | 2 | ▶ | if (! $ready{$mh}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 2 | push @ready, $mh ; | |||||||||||||||||||||||||||
| 153 | 2 | $ready{$mh} = 1 ; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | 2 | last ; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | else { | ||||||||||||||||||||||||||||
| 159 | 5 | my $mh = $this->_get_mux_handles()->{$p->get_id()} ; | |||||||||||||||||||||||||||
| 160 | 5 | ▶ | next unless defined($mh) ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 5 | ▶ | if (! $ready{$mh}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 5 | push @ready, $mh ; | |||||||||||||||||||||||||||
| 163 | 5 | ▶ | if ($p->is_eof()){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 1 | $mh->_get_tie()->_set_eof() ; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 5 | $ready{$mh} = 1 ; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | else { | ||||||||||||||||||||||||||||
| 172 | # REAL handle, we simply push it. | ||||||||||||||||||||||||||||
| 173 | 4 | push @ready, $h ; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 13 | return @ready ; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1 | 1 ; | |||||||||||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||||||||||