| 1 | package Net::DVBStreamer::Client; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | ################ | ||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||
| 5 | # DVBStreamer client module | ||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||
| 7 | # Nicholas J Humfrey | ||||||||||||||||||||||
| 8 | # njh@cpan.org | ||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | 1 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 1 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | 1 | ▶ | use vars qw/$VERSION/; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 17 | 1 | our $VERSION="0.01"; | |||||||||||||||||||||
| 18 | 1 | our $AUTOLOAD; | |||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | 1 | my $BASE_PORT = 54197; | |||||||||||||||||||||
| 21 | 1 | my $RESPONSE_PREFIX = 'DVBStreamer/'; | |||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | |||||||||||||||||||||||
| 25 | 0 | ▶ | sub new { | ||||||||||||||||||||
|
Subroutine
new: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||
| 26 | 0 | my $class = shift; | |||||||||||||||||||||
| 27 | 0 | my ($host, $adaptor) = @_; | |||||||||||||||||||||
| 28 | |||||||||||||||||||||||
| 29 | # Create self | ||||||||||||||||||||||
| 30 | 0 | ▶ | my $self = { | ||||||||||||||||||||
|
Condition0%0/4
$host || 'localhost'
$adaptor || 0
MC/DC0%0/1
$host || 'localhost'
$host
MC/DC0%0/1
$adaptor || 0
$adaptor
Truth table0%0/2
A
A= $host
Truth table0%0/2
A
A= $adaptor
| |||||||||||||||||||||||
| 31 | 'host' => $host || 'localhost', | ||||||||||||||||||||||
| 32 | 'adaptor' => $adaptor || 0, | ||||||||||||||||||||||
| 33 | 'server_version' => undef, | ||||||||||||||||||||||
| 34 | 'errno' => 0, | ||||||||||||||||||||||
| 35 | 'response' => '', | ||||||||||||||||||||||
| 36 | 'sock' => undef, | ||||||||||||||||||||||
| 37 | }; | ||||||||||||||||||||||
| 38 | 0 | bless $self, $class; | |||||||||||||||||||||
| 39 | |||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | # Create INET Socket | ||||||||||||||||||||||
| 42 | $self->{'sock'} = new IO::Socket::INET( | ||||||||||||||||||||||
| 43 | PeerAddr => $self->{'host'}, | ||||||||||||||||||||||
| 44 | 0 | ▶ | PeerPort => $BASE_PORT + $self->{'adaptor'}, | ||||||||||||||||||||
|
Condition0%0/2
'IO::Socket::INET'->new('PeerAddr', $self->{'host'}, 'PeerPort', $BASE_PORT + $self->{'adaptor'}, 'Proto', 'tcp') || croak("Error: failed to connect to DVBStreamer server '$self->{'host'}'.")
MC/DC0%0/1
'IO::Socket::INET'->new('PeerAddr', $self->{'host'}, 'PeerPort', $BASE_PORT + $self->{'adaptor'}, 'Proto', 'tcp') || croak("Error: failed to connect to DVBStreamer server '$self->{'host'}'.")
'IO::Socket::INET'->new('PeerAddr', $self->{'host'}, 'PeerPort', $BASE_PORT + $self->{'adaptor'}, 'Proto', 'tcp')
Truth table0%0/2
A
A= 'IO::Socket::INET'->new('PeerAddr', $self->{'host'}, 'PeerPort', $BASE_PORT + $self->{'adaptor'}, 'Proto', 'tcp')
| |||||||||||||||||||||||
| 45 | Proto => 'tcp') || | ||||||||||||||||||||||
| 46 | croak( "Error: failed to connect to DVBStreamer server '$self->{'host'}'." ); | ||||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | # Read the response line and check it is a DVBStreamer server | ||||||||||||||||||||||
| 50 | 0 | my $line = $self->{'sock'}->getline(); | |||||||||||||||||||||
| 51 | 0 | ▶ | croak "Error: remote server is not a DVBStreamer server.\n" if ($line !~ /^$RESPONSE_PREFIX/); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 52 | |||||||||||||||||||||||
| 53 | # Parse the rest of the response line | ||||||||||||||||||||||
| 54 | 0 | my ($errno, $response) = $self->_parse_reponse_line( $line ); | |||||||||||||||||||||
| 55 | 0 | ▶ | croak "Error: remote server is returned error number $errno: $response.\n" if ($errno != 0); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 56 | 0 | ▶ | carp "Warning: remote server is not ready: $response.\n" if ($response ne 'Ready'); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 57 | |||||||||||||||||||||||
| 58 | 0 | return $self; | |||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||
| 60 | |||||||||||||||||||||||
| 61 | |||||||||||||||||||||||
| 62 | # | ||||||||||||||||||||||
| 63 | # Send a command a parse the response | ||||||||||||||||||||||
| 64 | # | ||||||||||||||||||||||
| 65 | 0 | ▶ | sub send_command { | ||||||||||||||||||||
|
Subroutine
send_command: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||
| 66 | 0 | my $self = shift; | |||||||||||||||||||||
| 67 | 0 | my ($command, @params) = @_; | |||||||||||||||||||||
| 68 | 0 | my @result = (); | |||||||||||||||||||||
| 69 | |||||||||||||||||||||||
| 70 | 0 | ▶ | croak "Usage: send_command( $command, [@params] )" unless (defined $command); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 71 | |||||||||||||||||||||||
| 72 | |||||||||||||||||||||||
| 73 | # Send the command | ||||||||||||||||||||||
| 74 | 0 | $self->{'sock'}->print( join(' ', $command, @params)."\n" ); | |||||||||||||||||||||
| 75 | |||||||||||||||||||||||
| 76 | # Read the result line by line | ||||||||||||||||||||||
| 77 | 0 | ▶ | while (my $line = $self->{'sock'}->getline()) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 78 | 0 | ▶ | if ($line =~ /^$RESPONSE_PREFIX/) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 79 | 0 | my ($errno, $response) = $self->_parse_reponse_line( $line ); | |||||||||||||||||||||
| 80 | 0 | ▶ | if ($errno != 0) { | ||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||
| 81 | # Error | ||||||||||||||||||||||
| 82 | 0 | return undef; | |||||||||||||||||||||
| 83 | } elsif (scalar(@result)==1) { | ||||||||||||||||||||||
| 84 | # Success - return the single line as scalar | ||||||||||||||||||||||
| 85 | 0 | return $result[0]; | |||||||||||||||||||||
| 86 | } elsif (scalar(@result)) { | ||||||||||||||||||||||
| 87 | # Success - return the multiple lines as array | ||||||||||||||||||||||
| 88 | 0 | return @result; | |||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||
| 90 | # Success - return the response message | ||||||||||||||||||||||
| 91 | 0 | return $response; | |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||
| 94 | 0 | chomp( $line ); | |||||||||||||||||||||
| 95 | 0 | push(@result, $line); | |||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | |||||||||||||||||||||||
| 100 | # Never saw a response line | ||||||||||||||||||||||
| 101 | 0 | $self->{'errno'} = -1; | |||||||||||||||||||||
| 102 | 0 | $self->{'response'} = "Failed to read response from server"; | |||||||||||||||||||||
| 103 | 0 | return undef; | |||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||
| 105 | |||||||||||||||||||||||
| 106 | # | ||||||||||||||||||||||
| 107 | # Return the version number of the remote server | ||||||||||||||||||||||
| 108 | # | ||||||||||||||||||||||
| 109 | 0 | ▶ | sub server_version { | ||||||||||||||||||||
|
Subroutine
server_version: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 110 | 0 | my $self = shift; | |||||||||||||||||||||
| 111 | 0 | return $self->{'server_version'}; | |||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||
| 113 | |||||||||||||||||||||||
| 114 | # | ||||||||||||||||||||||
| 115 | # Returns the error number from the last command sent | ||||||||||||||||||||||
| 116 | # | ||||||||||||||||||||||
| 117 | 0 | ▶ | sub errno { | ||||||||||||||||||||
|
Subroutine
errno: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 118 | 0 | my $self = shift; | |||||||||||||||||||||
| 119 | 0 | return $self->{'errno'}; | |||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||
| 121 | |||||||||||||||||||||||
| 122 | # | ||||||||||||||||||||||
| 123 | # Returns the response message from the last command sent | ||||||||||||||||||||||
| 124 | # | ||||||||||||||||||||||
| 125 | 0 | ▶ | sub response { | ||||||||||||||||||||
|
Subroutine
response: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 126 | 0 | my $self = shift; | |||||||||||||||||||||
| 127 | 0 | return $self->{'response'}; | |||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||
| 129 | |||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | # | ||||||||||||||||||||||
| 132 | # Authenticate with the remote server | ||||||||||||||||||||||
| 133 | # | ||||||||||||||||||||||
| 134 | 0 | ▶ | sub authenticate { | ||||||||||||||||||||
|
Subroutine
authenticate: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 135 | 0 | my $self = shift; | |||||||||||||||||||||
| 136 | 0 | return $self->send_command( 'auth', @_ ); | |||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||
| 138 | |||||||||||||||||||||||
| 139 | |||||||||||||||||||||||
| 140 | |||||||||||||||||||||||
| 141 | |||||||||||||||||||||||
| 142 | # | ||||||||||||||||||||||
| 143 | # Internal method to parse a server response message | ||||||||||||||||||||||
| 144 | # | ||||||||||||||||||||||
| 145 | 0 | ▶ | sub _parse_reponse_line { | ||||||||||||||||||||
|
Subroutine
_parse_reponse_line: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 146 | 0 | my $self = shift; | |||||||||||||||||||||
| 147 | 0 | my ($line) = @_; | |||||||||||||||||||||
| 148 | |||||||||||||||||||||||
| 149 | 0 | my ($version, $errno, $response) = ($line =~ /^$RESPONSE_PREFIX([\d\.]+)\/(\d+)\s(.*)\n$/); | |||||||||||||||||||||
| 150 | 0 | $self->{'server_version'} = $version; | |||||||||||||||||||||
| 151 | 0 | $self->{'errno'} = $errno; | |||||||||||||||||||||
| 152 | 0 | $self->{'response'} = $response; | |||||||||||||||||||||
| 153 | |||||||||||||||||||||||
| 154 | 0 | return ($errno, $response); | |||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||
| 156 | |||||||||||||||||||||||
| 157 | |||||||||||||||||||||||
| 158 | # | ||||||||||||||||||||||
| 159 | # Pass unhandled method calls on to send_command() | ||||||||||||||||||||||
| 160 | # | ||||||||||||||||||||||
| 161 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 162 | 0 | my $self = shift; | |||||||||||||||||||||
| 163 | |||||||||||||||||||||||
| 164 | 0 | my $cmd = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2); | |||||||||||||||||||||
| 165 | 0 | return $self->send_command( $cmd, @_ ); | |||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||
| 167 | |||||||||||||||||||||||
| 168 | |||||||||||||||||||||||
| 169 | # | ||||||||||||||||||||||
| 170 | # Close the socket before the object is destroyed | ||||||||||||||||||||||
| 171 | # | ||||||||||||||||||||||
| 172 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 173 | 0 | my $self=shift; | |||||||||||||||||||||
| 174 | |||||||||||||||||||||||
| 175 | 0 | ▶ | if (defined $self->{'sock'}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 176 | 0 | $self->{'sock'}->close(); | |||||||||||||||||||||
| 177 | 0 | undef $self->{'sock'}; | |||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||
| 180 | |||||||||||||||||||||||
| 181 | |||||||||||||||||||||||
| 182 | 1 | 1; | |||||||||||||||||||||
| 183 | |||||||||||||||||||||||
| 184 | __END__ | ||||||||||||||||||||||