| 1 | package IO::Mux ; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 3 | ▶ | use 5.008 ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 3 | ▶ | use strict ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 3 | ▶ | use Symbol ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 3 | ▶ | use IO::Mux::Handle ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 3 | ▶ | use IO::Mux::Packet ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 3 | ▶ | use IO::Mux::Buffer ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 3 | ▶ | use IO::Handle ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 3 | ▶ | use IO::Select ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 3 | ▶ | use Carp ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | 3 | our $VERSION = '0.08' ; | |||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | 6 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 18 | 6 | my $class = shift ; | |||||||||||||||||
| 19 | 6 | my $fh = shift ; | |||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | 6 | my $this = {} ; | |||||||||||||||||
| 22 | 6 | ▶ | if (UNIVERSAL::isa($fh, 'GLOB')){ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 23 | # Make sure we save the actual IO bit, not the entire GLOB ref, because | ||||||||||||||||||
| 24 | # one typical usage could be to place \*STDOUT in a IO::Mux object and then | ||||||||||||||||||
| 25 | # do: *STDOUT = $mux. If we save the GLOB ref, that will create infinite | ||||||||||||||||||
| 26 | # recursion as the GLOB is deferenced each time to get the IO bit. | ||||||||||||||||||
| 27 | 5 | $this->{'glob'} = $fh ; | |||||||||||||||||
| 28 | 5 | $fh = *{$fh}{IO} ; | |||||||||||||||||
| 29 | } | ||||||||||||||||||
| 30 | 6 | $fh->autoflush(1) ; | |||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | 6 | $this->{fh} = $fh ; | |||||||||||||||||
| 33 | 6 | $this->{buffers} = {} ; | |||||||||||||||||
| 34 | 6 | $this->{'select'} = new IO::Select($fh) ; | |||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | 6 | return bless($this, $class) ; | |||||||||||||||||
| 37 | } | ||||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | 2 | ▶ | sub get_handle { | ||||||||||||||||
|
Subroutine
get_handle: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 41 | 2 | my $this = shift ; | |||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | 2 | ▶ | return (defined($this->{'glob'}) ? $this->{'glob'} : $this->{fh}) ; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 44 | } | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | 105 | ▶ | sub _get_handle { | ||||||||||||||||
|
Subroutine
_get_handle: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 48 | 105 | my $this = shift ; | |||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | 105 | return $this->{fh} ; | |||||||||||||||||
| 51 | } | ||||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | 6 | ▶ | sub new_handle { | ||||||||||||||||
|
Subroutine
new_handle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 55 | 6 | my $this = shift ; | |||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | 6 | return new IO::Mux::Handle($this) ; | |||||||||||||||||
| 58 | } | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | 179 | ▶ | sub _get_buffer { | ||||||||||||||||
|
Subroutine
_get_buffer: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 62 | 179 | my $this = shift ; | |||||||||||||||||
| 63 | 179 | my $id = shift ; | |||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | 179 | ▶ | if (! $this->_buffer_exists($id)){ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 66 | 18 | $this->{buffers}->{$id} = new IO::Mux::Buffer() ; | |||||||||||||||||
| 67 | } | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | 179 | return $this->{buffers}->{$id} ; | |||||||||||||||||
| 70 | } | ||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | 198 | ▶ | sub _buffer_exists { | ||||||||||||||||
|
Subroutine
_buffer_exists: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 74 | 198 | my $this = shift ; | |||||||||||||||||
| 75 | 198 | my $id = shift ; | |||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | 198 | return defined($this->{buffers}->{$id}) ; | |||||||||||||||||
| 78 | } | ||||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | |||||||||||||||||||
| 81 | 18 | ▶ | sub _kill_buffer { | ||||||||||||||||
|
Subroutine
_kill_buffer: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 82 | 18 | my $this = shift ; | |||||||||||||||||
| 83 | 18 | my $id = shift ; | |||||||||||||||||
| 84 | |||||||||||||||||||
| 85 | 18 | delete $this->{buffers}->{$id} ; | |||||||||||||||||
| 86 | } | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | 34 | ▶ | sub _read { | ||||||||||||||||
|
Subroutine
_read: called CC=7 SCAR=19.5
| |||||||||||||||||||
| 90 | 34 | my $this = shift ; | |||||||||||||||||
| 91 | 34 | my $id = shift ; | |||||||||||||||||
| 92 | 34 | my $blocking = shift ; | |||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | 34 | my $p = undef ; | |||||||||||||||||
| 95 | 34 | ▶ | while (! defined($p)){ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 96 | 38 | my $tp = $this->_read_packet($blocking) ; | |||||||||||||||||
| 97 | 37 | ▶ | if (! defined($tp)){ | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 98 | 0 | return undef ; | |||||||||||||||||
| 99 | } | ||||||||||||||||||
| 100 | elsif (! $tp){ | ||||||||||||||||||
| 101 | 1 | return 0 ; | |||||||||||||||||
| 102 | } | ||||||||||||||||||
| 103 | elsif ($tp == -1){ | ||||||||||||||||||
| 104 | # No packet available in non-blocking mode. | ||||||||||||||||||
| 105 | 10 | return -1 ; | |||||||||||||||||
| 106 | } | ||||||||||||||||||
| 107 | else { | ||||||||||||||||||
| 108 | 26 | ▶ | if ($tp->get_id() eq $id){ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 109 | 22 | ▶ | if (! $tp->is_eof()){ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 110 | 18 | $p = $tp ; | |||||||||||||||||
| 111 | } | ||||||||||||||||||
| 112 | else { | ||||||||||||||||||
| 113 | 4 | return 0 ; | |||||||||||||||||
| 114 | } | ||||||||||||||||||
| 115 | } | ||||||||||||||||||
| 116 | } | ||||||||||||||||||
| 117 | } | ||||||||||||||||||
| 118 | |||||||||||||||||||
| 119 | 18 | return $p->get_length() ; | |||||||||||||||||
| 120 | } | ||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | |||||||||||||||||||
| 123 | 27 | ▶ | sub _is_packet_available { | ||||||||||||||||
|
Subroutine
_is_packet_available: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 124 | 27 | my $this = shift ; | |||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | 27 | my @ready = $this->{'select'}->can_read(0) ; | |||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | 27 | return scalar(@ready) ; | |||||||||||||||||
| 129 | } | ||||||||||||||||||
| 130 | |||||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | # Returns a packet, 0 on real handle EOF or undef on error. | ||||||||||||||||||
| 133 | 49 | ▶ | sub _read_packet { | ||||||||||||||||
|
Subroutine
_read_packet: called CC=5 SCAR=16.2
| |||||||||||||||||||
| 134 | 49 | my $this = shift ; | |||||||||||||||||
| 135 | 49 | my $blocking = shift ; | |||||||||||||||||
| 136 | |||||||||||||||||||
| 137 | 49 | ▶ | if (! $blocking){ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 138 | 27 | ▶ | return -1 unless $this->_is_packet_available() ; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 139 | } | ||||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | 35 | my $p = IO::Mux::Packet->read($this->_get_handle()) ; | |||||||||||||||||
| 142 | 34 | ▶ | if (! defined($p)){ | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 143 | 0 | return undef ; | |||||||||||||||||
| 144 | } | ||||||||||||||||||
| 145 | elsif (! $p){ | ||||||||||||||||||
| 146 | 3 | return 0 ; | |||||||||||||||||
| 147 | } | ||||||||||||||||||
| 148 | else { | ||||||||||||||||||
| 149 | # Append the packet data to the correct buffer | ||||||||||||||||||
| 150 | 31 | my $buf = $this->_get_buffer($p->get_id()) ; | |||||||||||||||||
| 151 | 31 | $buf->push_packet($p) ; | |||||||||||||||||
| 152 | |||||||||||||||||||
| 153 | 31 | return $p ; | |||||||||||||||||
| 154 | } | ||||||||||||||||||
| 155 | } | ||||||||||||||||||
| 156 | |||||||||||||||||||
| 157 | |||||||||||||||||||
| 158 | 43 | ▶ | sub _write { | ||||||||||||||||
|
Subroutine
_write: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 159 | 43 | my $this = shift ; | |||||||||||||||||
| 160 | 43 | my $packet = shift ; | |||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | 43 | return $packet->write($this->_get_handle()) ; | |||||||||||||||||
| 163 | } | ||||||||||||||||||
| 164 | |||||||||||||||||||
| 165 | |||||||||||||||||||
| 166 | |||||||||||||||||||
| 167 | 3 | 1 ; | |||||||||||||||||
| 168 | __END__ | ||||||||||||||||||