| 1 | package Lab::Moose::Connection::Socket; | ||||||||||||
| 2 | 3 | $Lab::Moose::Connection::Socket::VERSION = '3.931'; | |||||||||||
| 3 | #ABSTRACT: Transfer IEEE 488.2 / SCPI messages over TCP | ||||||||||||
| 4 | |||||||||||||
| 5 | 3 | ▶ | use v5.20; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 3 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 3 | ▶ | use MooseX::Params::Validate; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 3 | ▶ | use Socket qw(IPPROTO_TCP TCP_NODELAY); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 3 | ▶ | use IO::Socket::INET; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 3 | ▶ | use IO::Select; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 3 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 3 | ▶ | use Lab::Moose::Instrument qw/timeout_param read_length_param/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 3 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 3 | has client => ( | |||||||||||
| 19 | is => 'ro', | ||||||||||||
| 20 | isa => 'IO::Socket::INET', | ||||||||||||
| 21 | writer => '_client', | ||||||||||||
| 22 | init_arg => undef, | ||||||||||||
| 23 | ); | ||||||||||||
| 24 | |||||||||||||
| 25 | 3 | has select => ( | |||||||||||
| 26 | is => 'ro', | ||||||||||||
| 27 | isa => 'IO::Select', | ||||||||||||
| 28 | writer => '_select', | ||||||||||||
| 29 | init_arg => undef, | ||||||||||||
| 30 | ); | ||||||||||||
| 31 | |||||||||||||
| 32 | 3 | has host => ( | |||||||||||
| 33 | is => 'ro', | ||||||||||||
| 34 | isa => 'Str', | ||||||||||||
| 35 | required => 1, | ||||||||||||
| 36 | ); | ||||||||||||
| 37 | |||||||||||||
| 38 | 3 | has port => ( | |||||||||||
| 39 | is => 'ro', | ||||||||||||
| 40 | isa => 'Int', | ||||||||||||
| 41 | required => 1, | ||||||||||||
| 42 | ); | ||||||||||||
| 43 | |||||||||||||
| 44 | 3 | has write_termchar => ( | |||||||||||
| 45 | is => 'ro', | ||||||||||||
| 46 | isa => 'Maybe[Str]', | ||||||||||||
| 47 | default => "\n", | ||||||||||||
| 48 | ); | ||||||||||||
| 49 | |||||||||||||
| 50 | 1 | ▶ | sub BUILD { | ||||||||||
|
Subroutine
BUILD: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||
| 51 | 1 | my $self = shift; | |||||||||||
| 52 | 1 | my $host = $self->host(); | |||||||||||
| 53 | 1 | my $port = $self->port(); | |||||||||||
| 54 | 1 | my $timeout = $self->timeout(); | |||||||||||
| 55 | 1 | ▶ | my $client = IO::Socket::INET->new( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | PeerAddr => $host, | ||||||||||||
| 57 | PeerPort => $port, | ||||||||||||
| 58 | Proto => 'tcp', | ||||||||||||
| 59 | Timeout => $timeout, | ||||||||||||
| 60 | |||||||||||||
| 61 | # enable use of orphaned connection after a script was killed by user | ||||||||||||
| 62 | ReuseAddr => 1, | ||||||||||||
| 63 | ) or croak "cannot open connection with $host on port $port: $!"; | ||||||||||||
| 64 | |||||||||||||
| 65 | 1 | ▶ | $client->setsockopt( IPPROTO_TCP, TCP_NODELAY, 1 ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | or croak "setsockopt: cannot enable TCP_NODELAY"; | ||||||||||||
| 67 | |||||||||||||
| 68 | 1 | ▶ | my $select = IO::Select->new($client) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | or croak "cannot create IO::Select object: $!"; | ||||||||||||
| 70 | |||||||||||||
| 71 | 1 | $self->_client($client); | |||||||||||
| 72 | 1 | $self->_select($select); | |||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | 3 | ▶ | sub Write { | ||||||||||
|
Subroutine
Write: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||
| 76 | 3 | my ( $self, %args ) = validated_hash( | |||||||||||
| 77 | \@_, | ||||||||||||
| 78 | timeout_param, | ||||||||||||
| 79 | command => { isa => 'Str' }, | ||||||||||||
| 80 | ); | ||||||||||||
| 81 | |||||||||||||
| 82 | 3 | ▶ | my $write_termchar = $self->write_termchar() // ''; | ||||||||||
|
Condition50%1/2
$self->write_termchar // ""
MC/DC0%0/1
$self->write_termchar // ""
$self->write_termchar
Truth table50%1/2
A
A= $self->write_termchar
| |||||||||||||
| 83 | 3 | my $command = $args{command} . $write_termchar; | |||||||||||
| 84 | 3 | my $timeout = $self->_timeout_arg(%args); | |||||||||||
| 85 | 3 | my $host = $self->host(); | |||||||||||
| 86 | 3 | my $port = $self->port(); | |||||||||||
| 87 | |||||||||||||
| 88 | 3 | my $client = $self->client(); | |||||||||||
| 89 | |||||||||||||
| 90 | 3 | my $length = length($command); | |||||||||||
| 91 | 3 | my $written = 0; | |||||||||||
| 92 | 3 | my $select = $self->select(); | |||||||||||
| 93 | 3 | ▶ | while ($length) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | 3 | ▶ | if ( !$select->can_write($timeout) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 0 | croak "timeout in Socket connection Write, host $host on port $port"; | |||||||||||
| 96 | } | ||||||||||||
| 97 | 3 | ▶ | my $bytes_written = $client->syswrite( $command, $length, $written ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | or croak("Write: syswrite to host $host on port $port failed: $!"); | ||||||||||||
| 99 | 3 | $written += $bytes_written; | |||||||||||
| 100 | 3 | $length -= $bytes_written; | |||||||||||
| 101 | } | ||||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | 3 | ▶ | sub Read { | ||||||||||
|
Subroutine
Read: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||||
| 105 | 3 | my ( $self, %args ) = validated_hash( | |||||||||||
| 106 | \@_, | ||||||||||||
| 107 | timeout_param(), | ||||||||||||
| 108 | read_length_param(), | ||||||||||||
| 109 | ); | ||||||||||||
| 110 | 3 | my $timeout = $self->_timeout_arg(%args); | |||||||||||
| 111 | 3 | my $read_length = $self->_read_length_arg(%args); | |||||||||||
| 112 | 3 | my $client = $self->client(); | |||||||||||
| 113 | 3 | my $select = $self->select(); | |||||||||||
| 114 | 3 | my $host = $self->host(); | |||||||||||
| 115 | 3 | my $port = $self->port(); | |||||||||||
| 116 | |||||||||||||
| 117 | 3 | my $string; | |||||||||||
| 118 | 3 | my $length = 0; | |||||||||||
| 119 | 3 | ▶ | if ( $args{read_length} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | |||||||||||||
| 121 | # explicit read_length arg: | ||||||||||||
| 122 | # Keep reading until we have $read_length bytes. | ||||||||||||
| 123 | 2 | ▶ | while ($read_length) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 124 | 4 | ▶ | if ( !$select->can_read($timeout) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | 0 | croak "timeout in connection Read, host $host on port $port"; | |||||||||||
| 126 | } | ||||||||||||
| 127 | 4 | ▶ | my $read_bytes | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 128 | = $client->sysread( $string, $read_length, $length ) | ||||||||||||
| 129 | or croak "socket read error, host $host on port $port: $!"; | ||||||||||||
| 130 | 4 | $read_length -= $read_bytes; | |||||||||||
| 131 | 4 | $length += $read_bytes; | |||||||||||
| 132 | } | ||||||||||||
| 133 | } | ||||||||||||
| 134 | else { | ||||||||||||
| 135 | 1 | ▶ | if ( !$select->can_read($timeout) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 136 | 0 | croak "timeout in connection Read, host $host on port $port"; | |||||||||||
| 137 | } | ||||||||||||
| 138 | 1 | ▶ | $client->sysread( $string, $read_length ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 139 | or croak "socket read error: $!"; | ||||||||||||
| 140 | } | ||||||||||||
| 141 | |||||||||||||
| 142 | 3 | return $string; | |||||||||||
| 143 | } | ||||||||||||
| 144 | |||||||||||||
| 145 | 0 | ▶ | sub Clear { | ||||||||||
|
Subroutine
Clear: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 146 | |||||||||||||
| 147 | # Some instruments provide an additional control port. | ||||||||||||
| 148 | } | ||||||||||||
| 149 | |||||||||||||
| 150 | 3 | with qw/ | |||||||||||
| 151 | Lab::Moose::Connection | ||||||||||||
| 152 | /; | ||||||||||||
| 153 | |||||||||||||
| 154 | 3 | __PACKAGE__->meta->make_immutable(); | |||||||||||
| 155 | |||||||||||||
| 156 | 3 | 1; | |||||||||||
| 157 | |||||||||||||
| 158 | __END__ | ||||||||||||