| 1 | package Lab::Bus::Socket; | ||
| 2 | #ABSTRACT: IP network socket bus | ||
| 3 | $Lab::Bus::Socket::VERSION = '3.899'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use Scalar::Util qw(weaken); | ||
| 8 | use Time::HiRes qw (usleep sleep); | ||
| 9 | use Lab::Bus; | ||
| 10 | use Data::Dumper; | ||
| 11 | use Carp; | ||
| 12 | use IO::Socket; | ||
| 13 | use IO::Select; | ||
| 14 | |||
| 15 | our @ISA = ("Lab::Bus"); | ||
| 16 | |||
| 17 | our %fields = ( | ||
| 18 | type => 'Socket', | ||
| 19 | remote_addr => 'localhost', # Client for Write | ||
| 20 | remote_port => '6342', | ||
| 21 | open_server => 0, | ||
| 22 | local_addr => 'localhost', # Server for Read | ||
| 23 | local_port => '6342', | ||
| 24 | proto => 'tcp', | ||
| 25 | listen_queue => 1, | ||
| 26 | reuse => 1, | ||
| 27 | timeout => 60, | ||
| 28 | closechar => "\004", # EOT | ||
| 29 | brutal => 0, # brutal as default? | ||
| 30 | wait_query => 10e-6, # sec; | ||
| 31 | read_length => 1000, # bytes | ||
| 32 | query_length => 300, # bytes | ||
| 33 | query_long_length => 10240, #bytes | ||
| 34 | ); | ||
| 35 | |||
| 36 | sub new { | ||
| 37 | my $proto = shift; | ||
| 38 | my $class = ref($proto) || $proto; | ||
| 39 | my $twin = undef; | ||
| 40 | my $self = $class->SUPER::new(@_) | ||
| 41 | ; # getting fields and _permitted from parent class | ||
| 42 | $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__); | ||
| 43 | |||
| 44 | # parameter parsing | ||
| 45 | |||
| 46 | $self->remote_addr( $self->config('remote_addr') ) | ||
| 47 | if defined $self->config('remote_addr'); | ||
| 48 | $self->remote_port( $self->config('remote_port') ) | ||
| 49 | if defined $self->config('remote_port'); | ||
| 50 | $self->open_server( $self->config('open_server') ) | ||
| 51 | if defined $self->config('open_server'); | ||
| 52 | $self->local_addr( $self->config('local_addr') ) | ||
| 53 | if defined $self->config('local_addr'); | ||
| 54 | $self->local_port( $self->config('local_port') ) | ||
| 55 | if defined $self->config('local_port'); | ||
| 56 | $self->Proto( $self->config('Proto') ) if defined $self->config('Proto'); | ||
| 57 | $self->Timeout( $self->config('Timeout') ) | ||
| 58 | if defined $self->config('Timeout'); | ||
| 59 | $self->EnableTermChar( $self->config('EnableTermChar') ) | ||
| 60 | if defined $self->config('EnableTermChar'); | ||
| 61 | $self->TermChar( $self->config('TermChar') ) | ||
| 62 | if defined $self->config('TermChar'); | ||
| 63 | |||
| 64 | # search for twin in %Lab::Bus::BusList. If there's none, place $self there and weaken it. | ||
| 65 | if ( $class eq __PACKAGE__ ) | ||
| 66 | { # careful - do only if this is not a parent class constructor | ||
| 67 | if ( $twin = $self->_search_twin() ) { | ||
| 68 | undef $self; | ||
| 69 | return $twin; # ...and that's it. | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | $Lab::Bus::BusList{ $self->type() }->{'default'} = $self; | ||
| 73 | weaken( $Lab::Bus::BusList{ $self->type() }->{'default'} ); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | return $self; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub connection_new { # { gpib_address => primary address } | ||
| 81 | my $self = shift; | ||
| 82 | my $args = undef; | ||
| 83 | if ( ref $_[0] eq 'HASH' ) { | ||
| 84 | $args = shift; | ||
| 85 | } # try to be flexible about options as hash/hashref | ||
| 86 | else { $args = {@_} } | ||
| 87 | my $server = undef; | ||
| 88 | my $client = undef; | ||
| 89 | if ( $args->{'open_server'} ) { | ||
| 90 | die "server sockets not yet supported\n"; | ||
| 91 | $server = new IO::Socket::INET( | ||
| 92 | LocalHost => $args->{'local_addr'}, | ||
| 93 | LocalPort => $args->{'local_port'}, | ||
| 94 | Proto => $args->{'proto'}, | ||
| 95 | Listen => $args->{'listen_queue'}, | ||
| 96 | Reuse => $args->{'reuse'}, | ||
| 97 | ); | ||
| 98 | die "Could not create socket server: $!\n" unless $server; | ||
| 99 | } | ||
| 100 | $client = new IO::Socket::INET( | ||
| 101 | PeerAddr => $args->{'remote_addr'}, | ||
| 102 | PeerPort => $args->{'remote_port'}, | ||
| 103 | Proto => $args->{'proto'}, | ||
| 104 | ); | ||
| 105 | die "Could not create socket client: $!\n" unless $client; | ||
| 106 | $client->autoflush(1); | ||
| 107 | my $connection_handle = undef; | ||
| 108 | $connection_handle = { | ||
| 109 | valid => 1, | ||
| 110 | type => "SOCKET", | ||
| 111 | socket_client_handle => $client, | ||
| 112 | socket_server_handle => $server | ||
| 113 | }; #, | ||
| 114 | return $connection_handle; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub connection_write | ||
| 118 | { # @_ = ( $connection_handle, $args = { command, wait_status } | ||
| 119 | my $self = shift; | ||
| 120 | my $connection_handle = shift; | ||
| 121 | my $args = undef; | ||
| 122 | if ( ref $_[0] eq 'HASH' ) { | ||
| 123 | $args = shift; | ||
| 124 | } # try to be flexible about options as hash/hashref | ||
| 125 | else { $args = {@_} } | ||
| 126 | my $command = $args->{'command'} || undef; | ||
| 127 | my $brutal = $args->{'brutal'} || $self->brutal(); | ||
| 128 | my $read_length = $args->{'read_length'} || $self->read_length(); | ||
| 129 | if ( !defined $command ) { | ||
| 130 | Lab::Exception::CorruptParameter->throw( | ||
| 131 | error => "No command given to " | ||
| 132 | . __PACKAGE__ | ||
| 133 | . "::connection_write().\n", ); | ||
| 134 | } | ||
| 135 | else { | ||
| 136 | if ( $self->{'EnableTermChar'} ) { $command .= $self->{'TermChar'} } | ||
| 137 | my $sock = $connection_handle->{'socket_client_handle'}; | ||
| 138 | |||
| 139 | my @ready = IO::Select->new($sock)->can_write( $self->{'Timeout'} ); | ||
| 140 | if (@ready) { | ||
| 141 | $sock->send($command) or die "$! sending command"; | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | Lab::Exception::Timeout->throw( | ||
| 145 | error => "Socket write time out\n", | ||
| 146 | ); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | return 1; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub connection_read | ||
| 153 | { # @_ = ( $connection_handle, $args = { read_length, brutal } | ||
| 154 | my $self = shift; | ||
| 155 | my $connection_handle = shift; | ||
| 156 | my $args = undef; | ||
| 157 | if ( ref $_[0] eq 'HASH' ) { | ||
| 158 | $args = shift; | ||
| 159 | } # try to be flexible about options as hash/hashref | ||
| 160 | else { $args = {@_} } | ||
| 161 | |||
| 162 | my $sock = $connection_handle->{'socket_client_handle'} || undef; | ||
| 163 | my $brutal = $args->{'brutal'} || $self->brutal(); | ||
| 164 | my $read_length = $args->{'read_length'} || $self->read_length(); | ||
| 165 | |||
| 166 | my $raw = ""; | ||
| 167 | my $result = undef; | ||
| 168 | |||
| 169 | if ( !defined $sock ) { | ||
| 170 | Lab::Exception::CorruptParameter->throw( | ||
| 171 | error => "No Socket given to " | ||
| 172 | . __PACKAGE__ | ||
| 173 | . "::connection_read().\n", ); | ||
| 174 | } | ||
| 175 | else { | ||
| 176 | my @ready = IO::Select->new($sock)->can_read( $self->{'Timeout'} ); | ||
| 177 | if (@ready) { | ||
| 178 | $sock->recv( $result, $read_length ); | ||
| 179 | } | ||
| 180 | else { | ||
| 181 | Lab::Exception::Timeout->throw( | ||
| 182 | error => "Socket read time out\n", | ||
| 183 | ); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | $raw = $result; | ||
| 188 | $result =~ s/[\n\r\x00]*$//; | ||
| 189 | return $result; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub connection_query | ||
| 193 | { # @_ = ( $connection_handle, $args = { command, read_length, wait_status, wait_query, brutal } | ||
| 194 | my $self = shift; | ||
| 195 | my $connection_handle = shift; | ||
| 196 | my $args = undef; | ||
| 197 | if ( ref $_[0] eq 'HASH' ) { | ||
| 198 | $args = shift; | ||
| 199 | } # try to be flexible about options as hash/hashref | ||
| 200 | else { $args = {@_} } | ||
| 201 | |||
| 202 | my $command = $args->{'command'} || undef; | ||
| 203 | my $brutal = $args->{'brutal'} || $self->brutal(); | ||
| 204 | my $read_length = $args->{'read_length'} || $self->read_length(); | ||
| 205 | my $wait_query = $args->{'wait_query'} || $self->wait_query(); | ||
| 206 | my $result = undef; | ||
| 207 | |||
| 208 | $self->connection_write($args); | ||
| 209 | |||
| 210 | sleep($wait_query); #<---ensures that asked data presented from the device | ||
| 211 | |||
| 212 | $result = $self->connection_read($args); | ||
| 213 | return $result; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub serial_poll { | ||
| 217 | my $self = shift; | ||
| 218 | my $connection_handle = shift; | ||
| 219 | return undef; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub connection_clear { | ||
| 223 | my $self = shift; | ||
| 224 | my $connection_handle = shift; | ||
| 225 | return undef; | ||
| 226 | } | ||
| 227 | |||
| 228 | 1; | ||
| 229 | |||
| 230 | __END__ |