| 1 | package XAS::Lib::Net::Client; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $VERSION = '0.03'; | |||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use IO::Socket; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use IO::Select; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Errno ':POSIX'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | use XAS::Class | ||||||||||
| 10 | 1 | debug => 0, | |||||||||
| 11 | version => $VERSION, | ||||||||||
| 12 | base => 'XAS::Base', | ||||||||||
| 13 | mixin => 'XAS::Lib::Mixins::Bufops', | ||||||||||
| 14 | utils => 'trim dotid', | ||||||||||
| 15 | accessors => 'handle select attempts', | ||||||||||
| 16 | mutators => 'timeout', | ||||||||||
| 17 | import => 'class', | ||||||||||
| 18 | vars => { | ||||||||||
| 19 | PARAMS => { | ||||||||||
| 20 | -port => 1, | ||||||||||
| 21 | -host => 1, | ||||||||||
| 22 | -timeout => { optional => 1, default => 60 }, | ||||||||||
| 23 | -eol => { optional => 1, default => "\015\012" }, | ||||||||||
| 24 | }, | ||||||||||
| 25 | ERRNO => 0, | ||||||||||
| 26 | ERRSTR => '', | ||||||||||
| 27 | } | ||||||||||
| 28 | 1 | ▶ | ; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | |||||||||||
| 30 | |||||||||||
| 31 | #use Data::Hexdumper; | ||||||||||
| 32 | |||||||||||
| 33 | # ---------------------------------------------------------------------- | ||||||||||
| 34 | # Public Methods | ||||||||||
| 35 | # ---------------------------------------------------------------------- | ||||||||||
| 36 | |||||||||||
| 37 | sub connect { | ||||||||||
| 38 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
connect: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 39 | |||||||||||
| 40 | 0 | $self->class->var('ERRNO', 0); | |||||||||
| 41 | 0 | $self->class->var('ERRSTR', ''); | |||||||||
| 42 | |||||||||||
| 43 | $self->{handle} = IO::Socket::INET->new( | ||||||||||
| 44 | Proto => 'tcp', | ||||||||||
| 45 | PeerPort => $self->port, | ||||||||||
| 46 | PeerAddr => $self->host, | ||||||||||
| 47 | Timeout => $self->timeout, | ||||||||||
| 48 | 0 | ▶ | ) or do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | |||||||||||
| 50 | 0 | my $errno = $! + 0; | |||||||||
| 51 | 0 | my $errstr = $!; | |||||||||
| 52 | |||||||||||
| 53 | 0 | $self->class->var('ERRNO', $errno); | |||||||||
| 54 | 0 | $self->class->var('ERRSTR', $errstr); | |||||||||
| 55 | |||||||||||
| 56 | 0 | $self->throw_msg( | |||||||||
| 57 | dotid($self->class) . '.connect.noconnect', | ||||||||||
| 58 | 'net_client_noconnect', | ||||||||||
| 59 | $self->host, | ||||||||||
| 60 | $self->port, | ||||||||||
| 61 | $errstr | ||||||||||
| 62 | ); | ||||||||||
| 63 | |||||||||||
| 64 | }; | ||||||||||
| 65 | |||||||||||
| 66 | 0 | $self->handle->blocking(0); | |||||||||
| 67 | 0 | $self->{select} = IO::Select->new($self->handle); | |||||||||
| 68 | |||||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | sub pending { | ||||||||||
| 72 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
pending: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 73 | |||||||||||
| 74 | 0 | return length($self->{buffer}); | |||||||||
| 75 | |||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | sub disconnect { | ||||||||||
| 79 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
disconnect: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | if ($self->handle->connected) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | |||||||||||
| 83 | 0 | $self->handle->close(); | |||||||||
| 84 | |||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | sub get { | ||||||||||
| 90 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 91 | 0 | my ($length) = $self->validate_params(\@_, [ | |||||||||
| 92 | { optional => 1, default => 512 } | ||||||||||
| 93 | ]); | ||||||||||
| 94 | |||||||||||
| 95 | 0 | my $output; | |||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | if ($self->pending > $length) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | |||||||||||
| 99 | 0 | $output = $self->buf_slurp(\$self->{buffer}, $length); | |||||||||
| 100 | |||||||||||
| 101 | } else { | ||||||||||
| 102 | |||||||||||
| 103 | 0 | $self->_fill_buffer(); | |||||||||
| 104 | |||||||||||
| 105 | 0 | ▶ | my $l = ($self->pending > $length) ? $length : $self->pending; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 0 | $output = $self->buf_slurp(\$self->{buffer}, $l); | |||||||||
| 107 | |||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 0 | return $output; | |||||||||
| 111 | |||||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | sub gets { | ||||||||||
| 115 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
gets: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 116 | |||||||||||
| 117 | 0 | my $buffer; | |||||||||
| 118 | 0 | my $output = ''; | |||||||||
| 119 | |||||||||||
| 120 | 0 | ▶ | while (my $buf = $self->get()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | |||||||||||
| 122 | 0 | $buffer .= $buf; | |||||||||
| 123 | |||||||||||
| 124 | 0 | ▶ | if ($output = $self->buf_get_line(\$buffer, $self->eol)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | |||||||||||
| 126 | 0 | $self->{buffer} = $buffer . $self->{buffer}; | |||||||||
| 127 | 0 | last; | |||||||||
| 128 | |||||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 0 | return trim($output); | |||||||||
| 134 | |||||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | sub put { | ||||||||||
| 138 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
put: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||
| 139 | 0 | my ($buffer) = $self->validate_params(\@_, [1]); | |||||||||
| 140 | |||||||||||
| 141 | 0 | my $counter = 0; | |||||||||
| 142 | 0 | my $working = 1; | |||||||||
| 143 | 0 | my $written = 0; | |||||||||
| 144 | 0 | my $timeout = $self->timeout; | |||||||||
| 145 | 0 | my $bufsize = length($buffer); | |||||||||
| 146 | |||||||||||
| 147 | 0 | $self->class->var('ERRNO', 0); | |||||||||
| 148 | 0 | $self->class->var('ERRSTR', ''); | |||||||||
| 149 | |||||||||||
| 150 | 0 | ▶ | while ($working) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | |||||||||||
| 152 | 0 | $self->handle->clearerr(); | |||||||||
| 153 | |||||||||||
| 154 | 0 | ▶ | if ($self->select->can_write($timeout)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | |||||||||||
| 156 | 0 | ▶ | if (my $bytes = $self->handle->syswrite($buffer, $bufsize)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | |||||||||||
| 158 | 0 | $written += $bytes; | |||||||||
| 159 | 0 | $buffer = substr($buffer, $bytes); | |||||||||
| 160 | 0 | ▶ | $working = 0 if ($written >= $bufsize); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | |||||||||||
| 162 | } else { | ||||||||||
| 163 | |||||||||||
| 164 | 0 | ▶ | if ($self->handle->error) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 165 | |||||||||||
| 166 | 0 | my $errno = $! + 0; | |||||||||
| 167 | 0 | my $errstr = $!; | |||||||||
| 168 | |||||||||||
| 169 | 0 | ▶ | if ($errno == EAGAIN) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | |||||||||||
| 171 | 0 | $counter++; | |||||||||
| 172 | 0 | ▶ | $working = 0 if ($counter > $self->attempts); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | |||||||||||
| 174 | } else { | ||||||||||
| 175 | |||||||||||
| 176 | 0 | $self->class->var('ERRNO', $errno); | |||||||||
| 177 | 0 | $self->class->var('ERRSTR', $errstr); | |||||||||
| 178 | |||||||||||
| 179 | 0 | $self->throw_msg( | |||||||||
| 180 | dotid($self->class) . '.put', | ||||||||||
| 181 | 'net_client_network', | ||||||||||
| 182 | $errstr | ||||||||||
| 183 | ); | ||||||||||
| 184 | |||||||||||
| 185 | } | ||||||||||
| 186 | |||||||||||
| 187 | } | ||||||||||
| 188 | |||||||||||
| 189 | } | ||||||||||
| 190 | |||||||||||
| 191 | } else { | ||||||||||
| 192 | |||||||||||
| 193 | 0 | $working = 0; | |||||||||
| 194 | |||||||||||
| 195 | } | ||||||||||
| 196 | |||||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | 0 | return $written; | |||||||||
| 200 | |||||||||||
| 201 | } | ||||||||||
| 202 | |||||||||||
| 203 | sub puts { | ||||||||||
| 204 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
puts: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 205 | 0 | my ($buffer) = $self->validate_params(\@_, [1]); | |||||||||
| 206 | |||||||||||
| 207 | 0 | my $data = sprintf("%s%s", trim($buffer), $self->eol); | |||||||||
| 208 | 0 | my $written = $self->put($data); | |||||||||
| 209 | |||||||||||
| 210 | 0 | return $written; | |||||||||
| 211 | |||||||||||
| 212 | } | ||||||||||
| 213 | |||||||||||
| 214 | sub errno { | ||||||||||
| 215 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
errno: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 216 | 0 | my ($value) = XAS::Base->validate_params(\@_, [ | |||||||||
| 217 | { optional => 1, default => undef } | ||||||||||
| 218 | ]); | ||||||||||
| 219 | |||||||||||
| 220 | 0 | ▶ | class->var('ERRNO', $value) if (defined($value)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 221 | |||||||||||
| 222 | 0 | return class->var('ERRNO'); | |||||||||
| 223 | |||||||||||
| 224 | } | ||||||||||
| 225 | |||||||||||
| 226 | sub errstr { | ||||||||||
| 227 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
errstr: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 228 | 0 | my ($value) = XAS::Base->validate_params(\@_, [ | |||||||||
| 229 | { optional => 1, default => undef } | ||||||||||
| 230 | ]); | ||||||||||
| 231 | |||||||||||
| 232 | 0 | ▶ | class->var('ERRSTR', $value) if (defined($value)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 233 | |||||||||||
| 234 | 0 | return class->var('ERRSTR'); | |||||||||
| 235 | |||||||||||
| 236 | } | ||||||||||
| 237 | |||||||||||
| 238 | sub setup { | ||||||||||
| 239 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
setup: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 240 | |||||||||||
| 241 | } | ||||||||||
| 242 | |||||||||||
| 243 | # ---------------------------------------------------------------------- | ||||||||||
| 244 | # Private Methods | ||||||||||
| 245 | # ---------------------------------------------------------------------- | ||||||||||
| 246 | |||||||||||
| 247 | sub init { | ||||||||||
| 248 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 249 | |||||||||||
| 250 | 0 | my $self = $class->SUPER::init(@_); | |||||||||
| 251 | |||||||||||
| 252 | 0 | $self->{attempts} = 5; | |||||||||
| 253 | 0 | $self->{buffer} = ''; | |||||||||
| 254 | |||||||||||
| 255 | 0 | return $self; | |||||||||
| 256 | |||||||||||
| 257 | } | ||||||||||
| 258 | |||||||||||
| 259 | sub _fill_buffer { | ||||||||||
| 260 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_fill_buffer: not called CC=7 SCAR=40.3
| |||||||||||
| 261 | |||||||||||
| 262 | 0 | my $counter = 0; | |||||||||
| 263 | 0 | my $working = 1; | |||||||||
| 264 | 0 | my $read = 0; | |||||||||
| 265 | 0 | my $timeout = $self->handle->timeout; | |||||||||
| 266 | |||||||||||
| 267 | 0 | $self->class->var('ERRNO', 0); | |||||||||
| 268 | 0 | $self->class->var('ERRSTR', ''); | |||||||||
| 269 | |||||||||||
| 270 | 0 | ▶ | while ($working) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 271 | |||||||||||
| 272 | 0 | my $buf; | |||||||||
| 273 | |||||||||||
| 274 | 0 | $self->handle->clearerr(); | |||||||||
| 275 | |||||||||||
| 276 | 0 | ▶ | if ($self->select->can_read($timeout)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 277 | |||||||||||
| 278 | 0 | ▶ | if (my $bytes = $self->handle->sysread($buf, 512)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 279 | |||||||||||
| 280 | 0 | $self->{buffer} .= $buf; | |||||||||
| 281 | 0 | $read += $bytes; | |||||||||
| 282 | |||||||||||
| 283 | } else { | ||||||||||
| 284 | |||||||||||
| 285 | 0 | ▶ | if ($self->handle->error) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 286 | |||||||||||
| 287 | 0 | my $errno = $! + 0; | |||||||||
| 288 | 0 | my $errstr = $!; | |||||||||
| 289 | |||||||||||
| 290 | 0 | $self->log->debug("get: errno = $errno"); | |||||||||
| 291 | |||||||||||
| 292 | 0 | ▶ | if ($errno == EAGAIN) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 293 | |||||||||||
| 294 | 0 | $counter++; | |||||||||
| 295 | 0 | ▶ | $working = 0 if ($counter > $self->attempts); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 296 | |||||||||||
| 297 | } else { | ||||||||||
| 298 | |||||||||||
| 299 | 0 | $self->class->var('ERRNO', $errno); | |||||||||
| 300 | 0 | $self->class->var('ERRSTR', $errstr); | |||||||||
| 301 | |||||||||||
| 302 | 0 | $self->throw_msg( | |||||||||
| 303 | dotid($self->class) . '.get', | ||||||||||
| 304 | 'net_client_network', | ||||||||||
| 305 | $errstr | ||||||||||
| 306 | ); | ||||||||||
| 307 | |||||||||||
| 308 | } | ||||||||||
| 309 | |||||||||||
| 310 | } | ||||||||||
| 311 | |||||||||||
| 312 | } | ||||||||||
| 313 | |||||||||||
| 314 | } else { | ||||||||||
| 315 | |||||||||||
| 316 | 0 | $working = 0; | |||||||||
| 317 | |||||||||||
| 318 | } | ||||||||||
| 319 | |||||||||||
| 320 | } | ||||||||||
| 321 | |||||||||||
| 322 | 0 | return $read; | |||||||||
| 323 | |||||||||||
| 324 | } | ||||||||||
| 325 | |||||||||||
| 326 | 1 | 1; | |||||||||
| 327 | |||||||||||
| 328 | __END__ | ||||||||||
| 329 | |||||||||||
| 330 | =head1 NAME | ||||||||||
| 331 | |||||||||||
| 332 | XAS::Lib::Net::Client - The network client interface for the XAS environment | ||||||||||
| 333 | |||||||||||
| 334 | =head1 SYNOPSIS | ||||||||||
| 335 | |||||||||||
| 336 | my $rpc = XAS::Lib::Net::Client->new( | ||||||||||
| 337 | -port => 9505, | ||||||||||
| 338 | -host => 'localhost', | ||||||||||
| 339 | }; | ||||||||||
| 340 | |||||||||||
| 341 | =head1 DESCRIPTION | ||||||||||
| 342 | |||||||||||
| 343 | This module implements a simple text orientated network protocol. All "packets" | ||||||||||
| 344 | will have an explicit "\015\012" appended. This delineates the "packets" and is | ||||||||||
| 345 | network neutral. No attempt is made to decipher these "packets". | ||||||||||
| 346 | |||||||||||
| 347 | =head1 METHODS | ||||||||||
| 348 | |||||||||||
| 349 | =head2 new | ||||||||||
| 350 | |||||||||||
| 351 | This initializes the module and can take these parameters. It doesn't actually | ||||||||||
| 352 | make a network connection. | ||||||||||
| 353 | |||||||||||
| 354 | =over 4 | ||||||||||
| 355 | |||||||||||
| 356 | =item B<-port> | ||||||||||
| 357 | |||||||||||
| 358 | The port number to attach too. | ||||||||||
| 359 | |||||||||||
| 360 | =item B<-host> | ||||||||||
| 361 | |||||||||||
| 362 | The host to use for the connection. This can be an IP address or | ||||||||||
| 363 | a host name. | ||||||||||
| 364 | |||||||||||
| 365 | =item B<-timeout> | ||||||||||
| 366 | |||||||||||
| 367 | An optional timeout, it defaults to 60 seconds. | ||||||||||
| 368 | |||||||||||
| 369 | =item B<-eol> | ||||||||||
| 370 | |||||||||||
| 371 | An optional eol. The default is "\015\012". Which is network netural. | ||||||||||
| 372 | |||||||||||
| 373 | =back | ||||||||||
| 374 | |||||||||||
| 375 | =head2 connect | ||||||||||
| 376 | |||||||||||
| 377 | Connect to the defined socket. | ||||||||||
| 378 | |||||||||||
| 379 | =head2 disconnect | ||||||||||
| 380 | |||||||||||
| 381 | Disconnect from the defined socket. | ||||||||||
| 382 | |||||||||||
| 383 | =head2 put($buffer) | ||||||||||
| 384 | |||||||||||
| 385 | This writes a buffer to the socket. Returns the number of bytes written. | ||||||||||
| 386 | |||||||||||
| 387 | =over 4 | ||||||||||
| 388 | |||||||||||
| 389 | =item B<$buffer> | ||||||||||
| 390 | |||||||||||
| 391 | The buffer to send over the socket. | ||||||||||
| 392 | |||||||||||
| 393 | =back | ||||||||||
| 394 | |||||||||||
| 395 | =head2 puts($buffer) | ||||||||||
| 396 | |||||||||||
| 397 | This writes a buffer that is terminated with eol to the socket. Returns the | ||||||||||
| 398 | number of bytes written. | ||||||||||
| 399 | |||||||||||
| 400 | =over 4 | ||||||||||
| 401 | |||||||||||
| 402 | =item B<$buffer> | ||||||||||
| 403 | |||||||||||
| 404 | The buffer to send over the socket. | ||||||||||
| 405 | |||||||||||
| 406 | =back | ||||||||||
| 407 | |||||||||||
| 408 | =head2 get($length) | ||||||||||
| 409 | |||||||||||
| 410 | This block reads data from the socket. A buffer is returned when it reaches | ||||||||||
| 411 | $length or timeout. | ||||||||||
| 412 | |||||||||||
| 413 | =over 4 | ||||||||||
| 414 | |||||||||||
| 415 | =item B<$length> | ||||||||||
| 416 | |||||||||||
| 417 | An optional length for the buffer. Defaults to 512 bytes. | ||||||||||
| 418 | |||||||||||
| 419 | =back | ||||||||||
| 420 | |||||||||||
| 421 | =head2 gets | ||||||||||
| 422 | |||||||||||
| 423 | This reads a buffer delimited by the eol from the socket. | ||||||||||
| 424 | |||||||||||
| 425 | =head2 pending | ||||||||||
| 426 | |||||||||||
| 427 | This returns the size of the internal read buffer. | ||||||||||
| 428 | |||||||||||
| 429 | =head2 errno | ||||||||||
| 430 | |||||||||||
| 431 | A class method to return the error number. | ||||||||||
| 432 | |||||||||||
| 433 | =head2 errstr | ||||||||||
| 434 | |||||||||||
| 435 | A class method to return the error string. | ||||||||||
| 436 | |||||||||||
| 437 | =head1 SEE ALSO | ||||||||||
| 438 | |||||||||||
| 439 | =over 4 | ||||||||||
| 440 | |||||||||||
| 441 | =item L<XAS|XAS> | ||||||||||
| 442 | |||||||||||
| 443 | =back | ||||||||||
| 444 | |||||||||||
| 445 | =head1 AUTHOR | ||||||||||
| 446 | |||||||||||
| 447 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||||||||||
| 448 | |||||||||||
| 449 | Copyright (C) 2014 Kevin L. Esteb | ||||||||||
| 450 | |||||||||||
| 451 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 452 | the terms of the Artistic License 2.0. For details, see the full text | ||||||||||
| 453 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||||||||||
| 454 | |||||||||||
| 455 | =cut | ||||||||||