| 1 | #$Id: Socket.pm 865 2010-10-26 06:45:14Z zag $ | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package Net::RTorrent::Socket; | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use RPC::XML; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use RPC::XML::Client; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use IO::Socket::INET; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use IO::Socket::UNIX; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | ▶ | use RPC::XML::Parser; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 2 | ▶ | use 5.005; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | Net::RTorrent::Socket - Direct connect to rtorrent via scgi proto | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | my $scli1 = new Net::RTorrent::Socket:: 'localhost:5000'; | ||||||||||||||||||||
| 22 | my $req = RPC::XML::request->new('get_memory_usage'); | ||||||||||||||||||||
| 23 | my $res = $scli3->send_request($req); | ||||||||||||||||||||
| 24 | print $res->value | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =head1 ABSTRACT | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | Perl interface to rtorrent via scgi | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | Net::RTorrent::Socket - Direct connect to rtorrent via scgi proto | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 2 | our @ISA = qw(); | |||||||||||||||||||
| 38 | 2 | our $VERSION = '0.06'; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 3 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 41 | 3 | my $class = shift; | |||||||||||||||||||
| 42 | 3 | ▶ | $class = ref $class if ref $class; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 3 | my $self = bless( {}, $class ); | |||||||||||||||||||
| 44 | 3 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | 2 | my $rpc_addr = shift; | |||||||||||||||||||
| 46 | 2 | $self->{addr} = $rpc_addr; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | else { | ||||||||||||||||||||
| 49 | 1 | carp "need xmlrpc server URL"; | |||||||||||||||||||
| 50 | 1 | return; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | 2 | return $self; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | ▶ | sub _create_socket { | ||||||||||||||||||
|
Subroutine
_create_socket: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 56 | 0 | my $self = shift; | |||||||||||||||||||
| 57 | 0 | ▶ | my $addr = shift || $self->{addr}; | ||||||||||||||||||
|
Condition0%0/3
shift() || $self->{'addr'}
MC/DC0%0/2
shift() || $self->{'addr'}
shift()
$self->{'addr'}
Truth table0%0/3
A || B
A= shift()B= $self->{'addr'}
| |||||||||||||||||||||
| 58 | 0 | ▶ | my $type = $addr =~ m%/% ? "IO::Socket::UNIX" : "IO::Socket::INET"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | my $socket = $type->new( $addr ); | |||||||||||||||||||
| 60 | 0 | my $old_fh = select($socket); | |||||||||||||||||||
| 61 | 0 | $| = 1; | |||||||||||||||||||
| 62 | 0 | select($old_fh); | |||||||||||||||||||
| 63 | 0 | return $socket | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | ▶ | sub send_request { | ||||||||||||||||||
|
Subroutine
send_request: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||
| 68 | 0 | my ( $self, $req, @args ) = @_; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | ▶ | if ( !UNIVERSAL::isa( $req, 'RPC::XML::request' ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | # Assume that $req is the name of the routine to be called | ||||||||||||||||||||
| 73 | 0 | $req = RPC::XML::request->new( $req, @args ); | |||||||||||||||||||
| 74 | 0 | ▶ | return "Error creating RPC::XML::request object: $RPC::XML::ERROR" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | unless ($req); # $RPC::XML::ERROR is already set | ||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | # get socket | ||||||||||||||||||||
| 79 | 0 | my $socket = $self->_create_socket( $self->{addr} ); | |||||||||||||||||||
| 80 | 0 | my $len = $req->length; | |||||||||||||||||||
| 81 | 0 | my $oheaders = | |||||||||||||||||||
| 82 | "CONTENT_LENGTH\0$len\0" | ||||||||||||||||||||
| 83 | . "SCGI\0" . '1' . "\0" | ||||||||||||||||||||
| 84 | . "REQUEST_METHOD\0POST\0"; | ||||||||||||||||||||
| 85 | 0 | print $socket length($oheaders) . ":" . $oheaders . "," . $req->as_string; | |||||||||||||||||||
| 86 | 0 | my $ans = ''; | |||||||||||||||||||
| 87 | 0 | ▶ | while (<$socket>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 0 | $ans .= $_; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | 0 | $socket->close; | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | my ( $header, $xml ) = split( /\n\s?\n/, $ans ); | |||||||||||||||||||
| 93 | 0 | my $p = RPC::XML::Parser->new(); | |||||||||||||||||||
| 94 | 0 | my $value; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 0 | eval { $value = $p->parse($xml) }; | |||||||||||||||||||
| 97 | 0 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 0 | return $@ | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | 0 | $value->value; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | 2 | 1; | |||||||||||||||||||