opentick/Socket.pm

filter: Statementstmt 75.2% 191 / 254 Branchbran 51.5% 34 / 66 Conditioncond 48.3% 15 / 31 MC/DCmcdc 10.5% 2 / 19 Subroutinesub 70.4% 43 / 61 Podpod 100% 12 / 12 totaltotal 67.0% 297 / 443 CC 47 SCAR 12.5
CC
47
Cov
68%
retry_connect
11.9
_get_server_list
10.0
connect
6.2
CRAP 3.5
« opentick/Constants.pm ↑ Summary opentick/Record.pm »
1 package POE::Component::Client::opentick::Socket;
2 #
3 # opentick.com POE client
4 #
5 # Socket handling
6 #
7 # infi/2008
8 #
9 # $Id: Socket.pm 56 2009-01-08 16:51:14Z infidel $
10 #
11 # Full POD documentation after __END__
12 #
13  
14 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use POE qw( Wheel::SocketFactory Wheel::ReadWrite
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 Driver::SysRW Filter::Stream );
21  
22 # Ours
23 3 ▶use POE::Component::Client::opentick::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use POE::Component::Client::opentick::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use POE::Component::Client::opentick::Output;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use POE::Component::Client::opentick::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 ###
29 ### Variables
30 ###
31  
32 3 ▶use vars qw( $VERSION $TRUE $FALSE $KEEP $DELETE $poe_kernel );
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 3 ($VERSION) = q$Revision: 56 $ =~ /(\d+)/;
35 3 *TRUE = \1;
36 3 *FALSE = \0;
37 3 *KEEP = \0;
38 3 *DELETE = \1;
39  
40 # Arguments are for this object.
41 3 my %valid_args = (
42 alias => $KEEP,
43 debug => $KEEP,
44 servers => $DELETE,
45 port => $DELETE,
46 realtime => $DELETE,
47 conntimeout => $DELETE,
48 autoreconnect => $DELETE,
49 reconninterval => $DELETE,
50 reconnretries => $DELETE,
51 bindaddress => $DELETE,
52 bindport => $DELETE,
53 );
54  
55 ########################################################################
56 ### Public methods ###
57 ########################################################################
58  
59 sub new
60 1 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod documented
61 1 my( $class, @args ) = @_;
62 1 ▶ croak( "$class requires an even number of parameters" ) if( @args & 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @args & 1 0 1 1
63  
64 1 my $self = {
65 alias => OTDefault( 'alias' ),
66 debug => $FALSE,
67 servers => undef,
68 myserver => undef,
69 port => undef,
70 state => OTConstant( 'OT_STATUS_INACTIVE' ),
71 realtime => OTDefault( 'realtime' ),
72 redirected => $FALSE, # were we redirected?
73 # For reconnection logic
74 conntimeout => OTDefault( 'conntimeout' ),
75 autoreconnect => OTDefault( 'autoreconnect' ),
76 reconninterval => OTDefault( 'reconninterval' ),
77 reconnretries => OTDefault( 'reconnretries' ),
78 reconncount => 0,
79 bindaddress => undef,
80 bindport => undef,
81 # 'socket' => undef,
82 socket_buffer => [], # outgoing socket FIFO
83 # Statistical parameters
84 packets_sent => 0,
85 packets_recv => 0,
86 bytes_sent => 0,
87 bytes_recv => 0,
88 connect_time => 0,
89 };
90  
91 1 bless( $self, $class );
92  
93 1 $self->initialize( @args );
94  
95 1 return( $self );
96 }
97  
98 # Initialize this object instance
99 sub initialize
100 1 ▶{
Subroutine initialize: called CC=3 SCAR=11.0
Pod documented
101 1 my( $self, %args ) = @_;
102  
103 # Store things. Things that make us go.
104 # We're a leaf node; go ahead and delete.
105 1 for( keys( %args ) )
106 {
107 $self->{lc $_} = delete( $args{ $_ } )
108 7 ▶ if( exists( $valid_args{lc $_} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $valid_args{lc $_} 3 4 7
109 }
110  
111 1 $self->{servers} = $self->_get_server_list( $self->{servers} );
112 1 $self->{port} = $self->_get_port( $self->{port} );
113  
114 1 return;
115 }
116  
117 # High level manual disconnect method
118 # NOTE: HYBRID POE EVENT HANDLER/METHOD
119 sub disconnect
120 2 ▶{
Subroutine disconnect: called CC=4 SCAR=13.9
Pod documented
121 2 my( $self ) = @_;
122  
123 2 $self->_pause_autoreconnect();
124 2 $self->_reset_reconn_count();
125  
126 # Step through and back out for each step.
127 2 my $state = $self->_get_state();
128 2 ▶ if( $state >= OTConstant( 'OT_STATUS_LOGGED_IN' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($state >= OTConstant('OT_STATUS_LOGGED_IN')) { } 1 1 2
129 {
130 1 $poe_kernel->call( $self->{alias}, 'logout' );
131 }
132 else
133 {
134 1 ▶ if( $state >= OTConstant( 'OT_STATUS_CONNECTED' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($state >= OTConstant("OT_STATUS_CONNECTED")) 1 0 1
135 {
136 # Disconnect. This should do it.
137 1 delete( $self->{socket} );
138 }
139 1 ▶ if( $state >= OTConstant( 'OT_STATUS_CONNECTING' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($state >= OTConstant("OT_STATUS_CONNECTING")) 1 0 1
140 {
141 # Cancel connection and clean up
142 1 delete( $self->{SocketFactory} );
143 1 $self->{myserver} = undef;
144 }
145 1 $self->_set_state( OTConstant( 'OT_STATUS_INACTIVE' ) );
146 # $self->_set_redirected_flag( $FALSE );
147 }
148  
149  
150 2 return;
151 }
152  
153 # High level reconnect method
154 sub reconnect
155 0 ▶{
Subroutine reconnect: not called CC=1 SCAR=6.9
Pod documented
156 0 my( $self ) = @_;
157  
158 0 $poe_kernel->yield( 'disconnect' );
159 0 $poe_kernel->yield( 'connect' );
160  
161 0 return;
162 }
163  
164 # High level redirect METHOD (only)
165 # Server can send host redirect response; we must comply.
166 # High priority, so we call it synchronously.
167 sub redirect
168 1 ▶{
Subroutine redirect: called CC=1 SCAR=0.0
Pod documented
169 1 my( $self, $host, $port ) = @_;
170  
171 1 $self->_set_redirected_flag( $TRUE );
172 1 $poe_kernel->call( $poe_kernel->get_active_session(), 'disconnect' );
173 1 $poe_kernel->call( $poe_kernel->get_active_session(),
174 'connect', $host, $port );
175  
176 1 return;
177 }
178  
179 ### Statistical accessors
180  
181 sub get_packets_recv
182 0 ▶{
Subroutine get_packets_recv: not called CC=1 SCAR=6.9
Pod documented
183 0 my( $self ) = @_;
184  
185 0 return( $self->{packets_recv} );
186 }
187  
188 sub get_packets_sent
189 0 ▶{
Subroutine get_packets_sent: not called CC=1 SCAR=6.9
Pod documented
190 0 my( $self ) = @_;
191  
192 0 return( $self->{packets_sent} );
193 }
194  
195 sub get_bytes_recv
196 0 ▶{
Subroutine get_bytes_recv: not called CC=1 SCAR=6.9
Pod documented
197 0 my( $self ) = @_;
198  
199 0 return( $self->{bytes_recv} );
200 }
201  
202 sub get_bytes_sent
203 0 ▶{
Subroutine get_bytes_sent: not called CC=1 SCAR=6.9
Pod documented
204 0 my( $self ) = @_;
205  
206 0 return( $self->{bytes_sent} );
207 }
208  
209 sub get_connect_time
210 0 ▶{
Subroutine get_connect_time: not called CC=2 SCAR=17.9
Pod documented
211 0 my( $self ) = @_;
212  
213 0 ▶ return( $self->{connect_time} ? time - $self->{connect_time} : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'connect_time'} ? : 0 0 0
214 }
215  
216 ########################################################################
217 ### POE event handlers ###
218 ########################################################################
219  
220 # Public event handlers
221  
222 # quick event handler to marshal args over to redirect method
223 sub _redirect
224 0 ▶{
Subroutine _redirect: not called CC=1 SCAR=6.9
225 0 my( $self, $host, $port ) = @_[OBJECT,ARG0,ARG1];
226  
227 0 $self->redirect( $host, $port );
228  
229 0 return;
230 }
231  
232 # Connect to the OT server
233 sub connect
234 2 ▶{
Subroutine connect: called CC=6 SCAR=18.2
Pod documented
235 2 my( $self, $kernel, $host, $port ) = @_[OBJECT,KERNEL,ARG0,ARG1];
236  
237 2 ▶ if( $self->_get_state() == OTConstant( 'OT_STATUS_INACTIVE' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_state == OTConstant("OT_STATUS_INACTIVE")) 2 0 2
238 {
239 2 $self->_reset_autoreconnect();
240  
241 2 ▶ $self->{myserver} = $host || $self->_get_server();
Condition66%2/3
$host || $self->_get_server
l!l&&r!l&&!r
110
MC/DC0%0/2
$host || $self->_get_server
$host $self->_get_server
Truth table66%2/3
A || B
A= $hostB= $self->_get_server
ABresult
1X1
011
000
242 2 O_NOTICE( "Connecting to " . $self->{myserver} . "..." );
243  
244 my $wheel = POE::Wheel::SocketFactory->new(
245 SocketDomain => AF_INET,
246 SocketType => SOCK_STREAM,
247 SocketProtocol => 'tcp',
248 BindAddress => $self->{bindaddress},
249 BindPort => $self->{bindport},
250 Reuse => $TRUE,
251 RemoteAddress => $self->{myserver},
252 2 ▶ RemotePort => $port || $self->_get_port(),
Condition66%2/3
$port || $self->_get_port
l!l&&r!l&&!r
110
MC/DC0%0/2
$port || $self->_get_port
$port $self->_get_port
Truth table66%2/3
A || B
A= $portB= $self->_get_port
ABresult
1X1
011
000
253 SuccessEvent => '_ot_sock_connected',
254 FailureEvent => '_ot_sock_connfail',
255 );
256 2 $self->{SocketFactory} = $wheel;
257  
258 2 $self->_set_state( OTConstant( 'OT_STATUS_CONNECTING' ) );
259  
260 2 ▶ if( $self->_get_conn_timeout() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_conn_timeout) 2 0 2
261 {
262 $kernel->alarm_remove( delete( $self->{timeout_id} ) )
263 2 ▶ if( $self->{timeout_id} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"timeout_id"} 1 1 2
264 $self->{timeout_id}
265 2 = $kernel->alarm_set( '_ot_sock_conntimeout',
266 time + $self->_get_conn_timeout() );
267 }
268 }
269  
270 2 return;
271 }
272  
273 ### Connection initiation handling
274  
275 # Successfully connected!
276 sub _ot_sock_connected
277 2 ▶{
Subroutine _ot_sock_connected: called CC=1 SCAR=0.0
278 2 my( $self, $kernel, $socket ) = @_[OBJECT, KERNEL, ARG0];
279  
280 2 my ($port, $addr) = sockaddr_in( getpeername( $socket ) );
281  
282 2 O_NOTICE( sprintf( "Connected to %s [%s]:%s.",
283 scalar( gethostbyaddr( $addr, AF_INET ) ),
284 inet_ntoa( $addr ), $port ) );
285  
286 # We don't need no steenkeen factory anymore.
287 2 delete( $self->{SocketFactory} );
288  
289 # Leave the alarm removal until opentick.pm:_logged_in().
290 # $kernel->alarm_remove( delete( $self->{timeout_id} ) )
291 # if( $self->{timeout_id} );
292  
293 # Create the socket handler.
294 2 $self->{'socket'} = POE::Wheel::ReadWrite->new(
295 Handle => $socket,
296 Driver => POE::Driver::SysRW->new(),
297 Filter => POE::Filter::Stream->new(),
298 InputEvent => '_ot_sock_receive_packet',
299 ErrorEvent => '_ot_sock_error',
300 );
301  
302 # Set the state variables
303 2 $self->_reset_object();
304 2 $self->_set_connect_time( time );
305  
306 # Send login command
307 2 $self->_set_state( OTConstant( 'OT_STATUS_CONNECTED' ) );
308 2 $kernel->call( $kernel->get_active_session(),
309 '_ot_proto_issue_command',
310 OTConstant( 'OT_LOGIN' ) );
311  
312 # Flush queue, if we have queued up commands
313 2 $self->_flush_queue();
314  
315 2 return;
316 }
317  
318 # Connection failed for whatever reason.
319 sub _ot_sock_connfail
320 0 ▶{
Subroutine _ot_sock_connfail: not called CC=1 SCAR=6.9
321 0 my( $self, $kernel, $op, $err_code, $err_str, $wheel )
322 = @_[OBJECT, KERNEL, ARG0..ARG3];
323  
324 0 O_DEBUG( "Connection failed. $op() returned $err_code: $err_str" );
325 0 delete( $self->{'socket'} );
326  
327 0 retry_connect( @_ );
328 }
329  
330 # Connection timed out.
331 sub _ot_sock_conntimeout
332 0 ▶{
Subroutine _ot_sock_conntimeout: not called CC=1 SCAR=6.9
333 0 my( $self, $kernel ) = @_[OBJECT, KERNEL];
334  
335 0 O_DEBUG( "Connection timed out." );
336 0 delete( $self->{'socket'} );
337  
338 0 retry_connect( @_ );
339 }
340  
341 # Retry a connection ReconnRetries times, or give up.
342 sub retry_connect
343 1 ▶{
Subroutine retry_connect: called CC=5 SCAR=24.7
Pod documented
344 1 my( $self, $kernel ) = @_[OBJECT, KERNEL];
345  
346 # Fix our states
347 1 $self->_set_state( OTConstant( 'OT_STATUS_INACTIVE' ) );
348 $kernel->alarm_remove( delete( $self->{timeout_id} ) )
349 1 ▶ if( exists( $self->{timeout_id} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"timeout_id"} 0 1 1
350  
351 # Retry
352 1 ▶ if( $self->_get_autoreconnect() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_autoreconnect) 0 1 1
353 {
354 0 ▶ if( $self->_inc_reconn_count() < $self->_get_reconn_retries() or
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_inc_reconn_count < $self->_get_reconn_retries or $self->_get_reconn_retries == 0) { } 0 0 0
Condition0%0/3
$self->_inc_reconn_count < $self->_get_reconn_retries or $self->_get_reconn_retries == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_inc_reconn_count < $self->_get_reconn_retries or $self->_get_reconn_retries == 0
$self->_inc_reconn_count < $self->_get_reconn_retries $self->_get_reconn_retries == 0
Truth table0%0/3
A or B
A= $self->_inc_reconn_count < $self->_get_reconn_retriesB= $self->_get_reconn_retries == 0
ABresult
1X1
011
000
355 $self->_get_reconn_retries() == 0 )
356 {
357 0 my $timeout = $self->_get_reconn_interval();
358 0 O_DEBUG( "Retrying connection in $timeout seconds..." );
359 0 $kernel->delay( 'connect', $timeout );
360 }
361 else
362 {
363 0 delete( $self->{SocketFactory} );
364 0 $kernel->yield( '_reconn_giveup', @_[ARG0..$#_] );
365 }
366 }
367  
368 1 return;
369 }
370  
371 # A socket error has occurred.
372 sub _ot_sock_error
373 1 ▶{
Subroutine _ot_sock_error: called CC=3 SCAR=11.3
374 1 my( $self, $kernel, $op, $err_code, $err_str, $wheel )
375 = @_[OBJECT,KERNEL,ARG0..ARG3];
376  
377 1 O_DEBUG( "Socket disconnected: $op() returned $err_code: $err_str" );
378  
379 # Socket disconnected
380 1 ▶ if( $op eq 'read' && $err_code == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "read" and $err_code == 0) 1 0 1
Condition33%1/3
$op eq "read" and $err_code == 0
!ll&&!rl&&r
001
MC/DC0%0/2
$op eq "read" and $err_code == 0
$op eq "read" $err_code == 0
Truth table33%1/3
A and B
A= $op eq "read"B= $err_code == 0
ABresult
0X0
100
111
381 {
382 # Stop heartbeats immediately and synchronously.
383 1 $kernel->yield( '_ot_proto_heartbeat_stop' );
384  
385 1 $self->_reset_object();
386 1 delete( $self->{'socket'} );
387  
388 1 retry_connect( @_ );
389 }
390  
391 1 return;
392 }
393  
394 ### Live connection handling
395  
396 # Got a packet!
397 sub _ot_sock_receive_packet
398 3 ▶{
Subroutine _ot_sock_receive_packet: called CC=1 SCAR=0.0
399 3 my( $self, $kernel, $packet ) = @_[OBJECT, KERNEL, ARG0];
400  
401 3 O_DEBUG( "_ot_sock_receive_packet( " . length( $packet ) . " )" );
402  
403 # Tell the protocol handler we got a packet
404 3 $kernel->yield( '_ot_proto_process_response', $packet );
405  
406 3 $self->_update_stats_recv( length( $packet ) );
407  
408 3 return;
409 }
410  
411 # Send a packet!
412 sub _ot_sock_send_packet
413 4 ▶{
Subroutine _ot_sock_send_packet: called CC=4 SCAR=14.6
414 4 my( $self, $packet ) = @_[OBJECT, ARG0];
415  
416 # Put the packet on the wire, or enqueue
417 4 my $buffered = $self->_put_or_enqueue( $packet );
418  
419 # Update the stats if appropriate
420 4 ▶ $self->_update_stats_sent( length( $packet ) ) unless( $buffered );
Branch: 1 branch
BranchTrueFalseTotal
unless $buffered 4 0 4
421  
422 4 ▶ O_DEBUG( sprintf "_ot_sock_send_packet( %d ): %s",
Branch: 1 branch
BranchTrueFalseTotal
$buffered ? : 0 4 4
423 length( $packet ),
424 $buffered ? "buffered" : "sent" );
425  
426 4 ▶ return( $buffered ? $TRUE : $FALSE );
Branch: 1 branch
BranchTrueFalseTotal
$buffered ? : 0 4 4
427 }
428  
429 ########################################################################
430 ### Private methods ###
431 ########################################################################
432  
433 # Return the correct port for initialization based on user preferences
434 sub _get_port
435 2 ▶{
Subroutine _get_port: called CC=5 SCAR=17.8
436 2 my( $self, $user_port ) = @_;
437  
438 my $port = ( defined( $user_port ) && $user_port =~ /^\d+/ )
439 ? $user_port
440 : $self->{port}
441 ? $self->{port}
442 : $self->{realtime}
443 2 ▶ ? OTDefault( 'port_realtime' )
Branch: 3 branches
BranchTrueFalseTotal
defined $user_port && $user_port =~ /^\d+/ ? : 1 1 2
$self->{'port'} ? : 1 0 1
$self->{'realtime'} ? : 0 0 0
Condition66%2/3
defined $user_port and $user_port =~ /^\d+/
!ll&&!rl&&r
101
MC/DC50%1/2
defined $user_port and $user_port =~ /^\d+/
defined $user_port $user_port =~ /^\d+/
Truth table66%2/3
A and B
A= defined $user_portB= $user_port =~ /^\d+/
ABresult
0X0
100
111
444 : OTDefault( 'port_delayed' );
445  
446 2 return( $port );
447 }
448  
449 # Return the server list for initialization based on user preferences
450 sub _get_server_list
451 1 ▶{
Subroutine _get_server_list: called CC=5 SCAR=23.0
452 1 my( $self, $user_list ) = @_;
453  
454 my $servers = ( defined( $user_list ) && ref( $user_list ) eq 'ARRAY' )
455 ? $user_list
456 : $self->{servers}
457 ? $self->{servers}
458 : $self->{realtime}
459 1 ▶ ? OTDefault( 'servers_realtime' )
Branch: 3 branches
BranchTrueFalseTotal
defined $user_list && ref $user_list eq 'ARRAY' ? : 1 0 1
$self->{'servers'} ? : 0 0 0
$self->{'realtime'} ? : 0 0 0
Condition33%1/3
defined $user_list and ref $user_list eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $user_list and ref $user_list eq "ARRAY"
defined $user_list ref $user_list eq "ARRAY"
Truth table33%1/3
A and B
A= defined $user_listB= ref $user_list eq "ARRAY"
ABresult
0X0
100
111
460 : OTDefault( 'servers_delayed' );
461  
462 1 return( $servers );
463 }
464  
465 sub _set_servers
466 0 ▶{
Subroutine _set_servers: not called CC=1 SCAR=6.9
467 0 my( $self, $user_list ) = @_;
468  
469 0 $self->{servers} = $user_list;
470  
471 0 return;
472 }
473  
474 sub _set_port
475 0 ▶{
Subroutine _set_port: not called CC=1 SCAR=6.9
476 0 my( $self, $port ) = @_;
477  
478 0 return( $self->{port} = $port );
479 }
480  
481 # Get one of the servers from our server list round-robin
482 { # CLOSURE
483 3 my $server_num = 0;
484 sub _get_server
485 1 ▶{
Subroutine _get_server: called CC=2 SCAR=7.0
486 1 my( $self ) = @_;
487  
488 1 my $server = $self->{servers}->[ $server_num++ ];
489 1 ▶ $server_num = 0 if $server_num > $#{ $self->{servers} };
Branch: 1 branch
BranchTrueFalseTotal
if $server_num > $#{$$self{"servers"};} 1 0 1
490  
491 1 return( $server );
492 }
493 } # /CLOSURE
494  
495 ### Accessor methods
496  
497 # The USER variable setting the number of retries to attempt
498 sub _get_reconn_retries
499 3 ▶{
Subroutine _get_reconn_retries: not called CC=1 SCAR=2.6
500 0 my( $self ) = @_;
501  
502 0 return( $self->{reconnretries} );
503 }
504  
505 sub _get_state
506 8 ▶{
Subroutine _get_state: called CC=1 SCAR=0.0
507 8 my( $self ) = @_;
508  
509 8 return( $self->{state} );
510 }
511  
512 sub _set_state
513 7 ▶{
Subroutine _set_state: called CC=3 SCAR=12.0
514 7 my( $self, $state ) = @_;
515  
516 7 ▶ throw( O_ERROR( 'Tried to set invalid state: ' . $state ) )
Branch: 1 branch
BranchTrueFalseTotal
if $state < OTConstant("OT_STATUS_INACTIVE") or $state > OTConstant("OT_STATUS_LOGGED_IN") 0 7 7
Condition33%1/3
$state < OTConstant("OT_STATUS_INACTIVE") or $state > OTConstant("OT_STATUS_LOGGED_IN")
l!l&&r!l&&!r
007
MC/DC0%0/2
$state < OTConstant("OT_STATUS_INACTIVE") or $state > OTConstant("OT_STATUS_LOGGED_IN")
$state < OTConstant("OT_STATUS_INACTIVE") $state > OTConstant("OT_STATUS_LOGGED_IN")
Truth table33%1/3
A or B
A= $state < OTConstant("OT_STATUS_INACTIVE")B= $state > OTConstant("OT_STATUS_LOGGED_IN")
ABresult
1X1
011
000
517 if( $state < OTConstant( 'OT_STATUS_INACTIVE') ||
518 $state > OTConstant( 'OT_STATUS_LOGGED_IN' ) );
519  
520 $poe_kernel->yield( '_notify_of_event',
521 OTEvent( 'OT_STATUS_CHANGED' ),
522 7 [ $self->{alias} ],
523 $state );
524  
525 7 return( $self->{state} = $state );
526 }
527  
528 sub _set_redirected_flag
529 1 ▶{
Subroutine _set_redirected_flag: called CC=3 SCAR=12.0
530 1 my( $self, $value ) = @_;
531  
532 1 ▶ $self->{redirected} = defined( $value )
Branch: 2 branches
BranchTrueFalseTotal
defined $value ? : 1 0 1
$value ? : 1 0 1
533 ? $value ? $TRUE : $FALSE
534 : $TRUE;
535 }
536  
537 sub _is_redirected
538 0 ▶{
Subroutine _is_redirected: not called CC=1 SCAR=6.9
539 0 my( $self ) = @_;
540  
541 0 return( $self->{redirected} );
542 }
543  
544 # The ACTUAL count of retry attempts
545 sub _reset_reconn_count
546 6 ▶{
Subroutine _reset_reconn_count: called CC=1 SCAR=0.0
547 6 my( $self ) = @_;
548  
549 6 return( $self->{reconncount} = 0 );
550 }
551  
552 # The ACTUAL count of retry attempts
553 sub _inc_reconn_count
554 0 ▶{
Subroutine _inc_reconn_count: not called CC=1 SCAR=6.9
555 0 my( $self ) = @_;
556  
557 0 return( ++$self->{reconncount} );
558 }
559  
560 # The ACTUAL count of retry attempts
561 sub _get_reconn_count
562 0 ▶{
Subroutine _get_reconn_count: not called CC=1 SCAR=6.9
563 0 my( $self ) = @_;
564  
565 0 return( $self->{reconncount} );
566 }
567  
568 sub _get_autoreconnect
569 3 ▶{
Subroutine _get_autoreconnect: called CC=1 SCAR=0.0
570 3 my( $self ) = @_;
571  
572 3 return( $self->{autoreconnect} );
573 }
574  
575 sub _set_autoreconnect
576 3 ▶{
Subroutine _set_autoreconnect: called CC=2 SCAR=6.9
577 3 my( $self, $value ) = @_;
578  
579 3 ▶ $self->{autoreconnect} = $value ? $TRUE : $FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 1 2 3
580  
581 3 return;
582 }
583  
584 sub _get_conn_timeout
585 4 ▶{
Subroutine _get_conn_timeout: called CC=1 SCAR=0.0
586 4 my( $self ) = @_;
587  
588 4 return( $self->{conntimeout} );
589 }
590  
591 sub _get_reconn_interval
592 0 ▶{
Subroutine _get_reconn_interval: not called CC=1 SCAR=6.9
593 0 my( $self ) = @_;
594  
595 0 return( $self->{reconninterval} );
596 }
597  
598 # Put or enqueue user-requested sent packets to FIFO
599 sub _put_or_enqueue
600 4 ▶{
Subroutine _put_or_enqueue: called CC=2 SCAR=8.1
601 4 my( $self, $packet ) = @_;
602  
603 4 my $buffered;
604 4 ▶ if( $self->{'socket'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'socket'}) { } 4 0 4
605 {
606 4 $buffered = $self->{'socket'}->put( $packet );
607 }
608 else
609 {
610 0 push( @{ $self->{socket_buffer} }, $packet );
611 0 $buffered = $TRUE;
612 }
613  
614 4 return( $buffered );
615 }
616  
617 # Flush queue of user-requested sent packets, when connected.
618 sub _flush_queue
619 2 ▶{
Subroutine _flush_queue: called CC=3 SCAR=14.2
620 2 my( $self ) = @_;
621  
622 2 ▶ return undef unless( $self->{'socket'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"socket"} 0 2 2
623  
624 2 my $count;
625 2 ▶ if( $count = @{ $self->{socket_buffer} } )
Branch: 1 branch
BranchTrueFalseTotal
if ($count = @{$self->{"socket_buffer"};}) 0 2 2
626 {
627 0 $self->{'socket'}->put( @{ $self->{socket_buffer} } );
628 $self->_update_stats_sent(
629 0 length( join( '', @{ $self->{socket_buffer} } ) )
630 );
631 0 $self->{socket_buffer} = []; # clear buffer
632 0 O_DEBUG( $count . " buffered packets sent." );
633 }
634  
635 2 return( $count );
636 }
637  
638 # Pause the autoreconnect state; save the current value
639 sub _pause_autoreconnect
640 2 ▶{
Subroutine _pause_autoreconnect: called CC=2 SCAR=7.0
641 2 my( $self ) = @_;
642  
643 # make idempotent
644 2 ▶ return if( $self->{autoreconnbak} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"autoreconnbak"} 0 2 2
645  
646 2 $self->{autoreconnbak} = $self->_get_autoreconnect();
647 2 $self->_set_autoreconnect( $FALSE );
648  
649 2 return;
650 }
651  
652 # Restore the autoreconnect state
653 sub _reset_autoreconnect
654 2 ▶{
Subroutine _reset_autoreconnect: called CC=2 SCAR=6.9
655 2 my( $self ) = @_;
656  
657 2 ▶ if( exists( $self->{autoreconnbak} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"autoreconnbak"}) 1 1 2
658 {
659 1 $self->_set_autoreconnect( $self->{autoreconnbak} );
660 1 delete( $self->{autoreconnbak} );
661 }
662  
663 2 return;
664 }
665  
666 ### Statistics logging
667  
668 sub _update_stats_recv
669 3 ▶{
Subroutine _update_stats_recv: called CC=1 SCAR=0.0
670 3 my( $self, $bytes ) = @_;
671  
672 3 $self->_inc_bytes_recv( $bytes );
673 3 $self->_inc_packets_recv();
674  
675 3 return;
676 }
677  
678 sub _update_stats_sent
679 4 ▶{
Subroutine _update_stats_sent: called CC=1 SCAR=0.0
680 4 my( $self, $bytes ) = @_;
681  
682 4 $self->_inc_bytes_sent( $bytes );
683 4 $self->_inc_packets_sent();
684  
685 4 return;
686 }
687  
688 sub _reset_stats_recv
689 0 ▶{
Subroutine _reset_stats_recv: not called CC=1 SCAR=6.9
690 0 my( $self ) = @_;
691  
692 0 $self->{packets_recv} = 0;
693 0 $self->{bytes_recv} = 0;
694  
695 0 return;
696 }
697  
698 sub _reset_stats_sent
699 0 ▶{
Subroutine _reset_stats_sent: not called CC=1 SCAR=6.9
700 0 my( $self ) = @_;
701  
702 0 $self->{packets_sent} = 0;
703 0 $self->{bytes_sent} = 0;
704  
705 0 return;
706 }
707  
708 sub _inc_bytes_recv
709 3 ▶{
Subroutine _inc_bytes_recv: called CC=2 SCAR=7.2
710 3 my( $self, $num ) = @_;
711  
712 3 ▶ return( $self->{bytes_recv} += $num || 1 );
Condition50%1/2
$num || 1
l!l
30
MC/DC0%0/1
$num || 1
$num
Truth table50%1/2
A
A= $num
Aresult
11
00
713 }
714  
715 sub _inc_bytes_sent
716 4 ▶{
Subroutine _inc_bytes_sent: called CC=2 SCAR=7.2
717 4 my( $self, $num ) = @_;
718  
719 4 ▶ return( $self->{bytes_sent} += $num || 1 );
Condition50%1/2
$num || 1
l!l
40
MC/DC0%0/1
$num || 1
$num
Truth table50%1/2
A
A= $num
Aresult
11
00
720 }
721  
722 sub _inc_packets_recv
723 3 ▶{
Subroutine _inc_packets_recv: called CC=2 SCAR=7.2
724 3 my( $self, $num ) = @_;
725  
726 3 ▶ return( $self->{packets_recv} += $num || 1 );
Condition50%1/2
$num || 1
l!l
03
MC/DC0%0/1
$num || 1
$num
Truth table50%1/2
A
A= $num
Aresult
11
00
727 }
728  
729 sub _inc_packets_sent
730 4 ▶{
Subroutine _inc_packets_sent: called CC=2 SCAR=7.2
731 4 my( $self, $num ) = @_;
732  
733 4 ▶ return( $self->{packets_sent} += $num || 1 );
Condition50%1/2
$num || 1
l!l
04
MC/DC0%0/1
$num || 1
$num
Truth table50%1/2
A
A= $num
Aresult
11
00
734 }
735  
736 sub _set_connect_time
737 6 ▶{
Subroutine _set_connect_time: called CC=2 SCAR=6.9
738 6 my( $self, $value ) = @_;
739  
740 6 ▶ return( $self->{connect_time} = $value || time );
Condition100%2/2
$value || time
l!l
24
MC/DC100%1/1
$value || time
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
741 }
742  
743 sub _reset_object
744 4 ▶{
Subroutine _reset_object: called CC=2 SCAR=7.0
745 4 my( $self ) = $_[OBJECT];
746 4 ▶ return if( $self->_get_state() == OTConstant( 'OT_STATUS_INACTIVE' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_get_state == OTConstant("OT_STATUS_INACTIVE") 0 4 4
747
748 # $self->_set_state( OTConstant( 'OT_STATUS_INACTIVE' ) );
749 4 $self->_set_connect_time( 0 );
750 4 $self->{myserver} = undef;
751 # $self->_set_redirected_flag( $FALSE );
752 4 $self->_reset_reconn_count();
753 # $self->_reset_stats_sent();
754 # $self->_reset_stats_recv();
755  
756 4 return;
757 }
758  
759 3 1;
760  
761 __END__
« opentick/Constants.pm ↑ Summary opentick/Record.pm »