Moose/Connection/Socket.pm

filter: Statementstmt 96.2% 77 / 80 Branchbran 62.5% 15 / 24 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 92.3% 12 / 13 Podpod 25.0% 1 / 4 totaltotal 85.4% 106 / 124 SCAR 26.6
CC
14
Cov
88%
Read
7.3
Write
5.1
BUILD
4.1
CRAP 14.4
« Moose/Instrument/Log.pm ↑ Summary Moose/Instrument/LinearStepSweep.pm »
1 package Lab::Moose::Connection::Socket;
2 3 $Lab::Moose::Connection::Socket::VERSION = '3.881';
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 sub BUILD {
51 1 ▶ my $self = shift;
Subroutine BUILD: called CC=4 SCAR=14.2
Pod undocumented
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
BranchTrueFalseTotal
my $client = "IO::Socket::INET"->new("PeerAddr", $host, "PeerPort", $port, "Proto", "tcp", "Timeout", $timeout, "ReuseAddr", 1) or croak("cannot open connection with $host on port ${port}: $!") 0 1 1
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
BranchTrueFalseTotal
$client->setsockopt(6, 1, 1) or croak("setsockopt: cannot enable TCP_NODELAY") 0 1 1
66 or croak "setsockopt: cannot enable TCP_NODELAY";
67  
68 1 ▶ my $select = IO::Select->new($client)
Branch: 1 branch
BranchTrueFalseTotal
my $select = "IO::Select"->new($client) or croak("cannot create IO::Select object: $!") 0 1 1
69 or croak "cannot create IO::Select object: $!";
70  
71 1 $self->_client($client);
72 1 $self->_select($select);
73 }
74  
75 sub Write {
76 3 ▶ my ( $self, %args ) = validated_hash(
Subroutine Write: called CC=5 SCAR=16.3
Pod undocumented
77 \@_,
78 timeout_param,
79 command => { isa => 'Str' },
80 );
81  
82 3 ▶ my $write_termchar = $self->write_termchar() // '';
Condition50%1/2
$self->write_termchar // ""
l!l
30
MC/DC0%0/1
$self->write_termchar // ""
$self->write_termchar
Truth table50%1/2
A
A= $self->write_termchar
Aresult
11
00
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
BranchTrueFalseTotal
if ($length) 3 3 6
94 3 ▶ if ( !$select->can_write($timeout) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($select->can_write($timeout)) 0 3 3
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
BranchTrueFalseTotal
my $bytes_written = $client->syswrite($command, $length, $written) or croak("Write: syswrite to host $host on port $port failed: $!") 0 3 3
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 sub Read {
105 3 ▶ my ( $self, %args ) = validated_hash(
Subroutine Read: called CC=7 SCAR=19.9
Pod documented
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
BranchTrueFalseTotal
if ($args{'read_length'}) { } 2 1 3
120  
121 # explicit read_length arg:
122 # Keep reading until we have $read_length bytes.
123 2 ▶ while ($read_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_length) 4 2 6
124 4 ▶ if ( !$select->can_read($timeout) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($select->can_read($timeout)) 0 4 4
125 0 croak "timeout in connection Read, host $host on port $port";
126 }
127 4 ▶ my $read_bytes
Branch: 1 branch
BranchTrueFalseTotal
my $read_bytes = $client->sysread($string, $read_length, $length) or croak("socket read error, host $host on port ${port}: $!") 0 4 4
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
BranchTrueFalseTotal
unless ($select->can_read($timeout)) 0 1 1
136 0 croak "timeout in connection Read, host $host on port $port";
137 }
138 1 ▶ $client->sysread( $string, $read_length )
Branch: 1 branch
BranchTrueFalseTotal
$client->sysread($string, $read_length) or croak("socket read error: $!") 0 1 1
139 or croak "socket read error: $!";
140 }
141  
142 3 return $string;
143 }
144  
145 ▶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__
159  
160 =pod
161  
162 =encoding UTF-8
163  
164 =head1 NAME
165  
166 Lab::Moose::Connection::Socket - Transfer IEEE 488.2 / SCPI messages over TCP
167  
168 =head1 VERSION
169  
170 version 3.881
171  
172 =head1 SYNOPSIS
173  
174 use Lab::Moose;
175  
176 my $instrument = instrument(
177 type => 'random_instrument',
178 connection_type => 'Socket',
179 connection_options => {
180 host => '132.199.11.2',
181 port => 5025
182 },
183 );
184  
185 =head1 DESCRIPTION
186  
187 This connection uses L<IO::Socket::INET> to interface with the operating
188 system's TCP stack. This works on most operating systems without installing any
189 additional software.
190  
191 Without knowing the syntax of the used command-messages there is no way for the
192 connection to determine when C<Read> is finished. This is unlike GPIB, USBTMC,
193 or VXI-11 which have explicit End of Message indicators. To deal with this, the
194 C<read_length> parameter has the following semantics:
195  
196 =over
197  
198 =item C<Read> is given an explicit C<read_length> parameter
199  
200 Keep calling sysread until C<read_length> bytes are read.
201  
202 =item C<Read> is not given an explicit C<read_length> parameter
203  
204 Do a single sysread with the connections default C<read_length>.
205  
206 =back
207  
208 For SCPI definite length blocks you will have to give the exact block length
209 with the C<read_length> parameter.
210  
211 =head2 CONNECTION OPTIONS
212  
213 =over
214  
215 =item host
216  
217 Host address.
218  
219 =item port
220  
221 Host port.
222  
223 =item write_termchar
224  
225 Append this to each write command. Default: C<"\n">.
226  
227 =back
228  
229 =head1 COPYRIGHT AND LICENSE
230  
231 This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
232  
233 Copyright 2017 Andreas K. Huettel, Simon Reinhardt
234 2020 Andreas K. Huettel
235 2021 Andreas K. Huettel, Simon Reinhardt
236  
237  
238 This is free software; you can redistribute it and/or modify it under
239 the same terms as the Perl 5 programming language system itself.
240  
241 =cut
« Moose/Instrument/Log.pm ↑ Summary Moose/Instrument/LinearStepSweep.pm »