File Coverage

blib/lib/Protocol/Sys/Virt/KeepAlive.pm
Criterion Covered Total %
statement 17 43 39.5
branch 0 8 0.0
condition n/a
subroutine 6 17 35.2
pod 5 5 100.0
total 28 73 38.3


line stmt bran cond sub pod time code
1             ####################################################################
2             #
3             # This file was generated using XDR::Parse version v1.0.1,
4             # XDR::Gen version 1.1.2 and LibVirt version v12.1.0
5             #
6             # Don't edit this file, use the source template instead
7             #
8             # ANY CHANGES HERE WILL BE LOST !
9             #
10             ####################################################################
11              
12 1     1   950 use v5.14;
  1         4  
13 1     1   3 use warnings;
  1         2  
  1         56  
14              
15             package Protocol::Sys::Virt::KeepAlive v12.1.0;
16              
17 1     1   4 use Carp qw(croak);
  1         1  
  1         40  
18 1     1   3 use Log::Any qw($log);
  1         1  
  1         5  
19              
20 1     1   573 use Protocol::Sys::Virt::KeepAlive::XDR;
  1         3  
  1         27  
21 1     1   4 use Protocol::Sys::Virt::Transport::XDR;
  1         2  
  1         391  
22             my $msgs = 'Protocol::Sys::Virt::KeepAlive::XDR';
23             my $type = 'Protocol::Sys::Virt::Transport::XDR';
24              
25             sub new {
26 0     0 1   my ($class, %args) = @_;
27             return bless {
28             inactive => 0,
29             max_inactive => 10,
30       0     on_ack => sub { },
31       0     on_fail => sub { },
32       0     on_ping => sub { },
33 0     0     sender => sub { croak 'Not registered with a transport'; },
34 0           %args
35             }, $class;
36             }
37              
38             sub _unexpected_msg {
39 0     0     croak 'Unexpected message';
40             }
41              
42             sub register {
43 0     0 1   my ($self, $transport) = @_;
44              
45             $self->{sender} = $transport->register(
46             $msgs->PROGRAM,
47             $msgs->PROTOCOL_VERSION,
48             {
49             on_reply => \&_unexpected_msg,
50             on_call => \&_unexpected_msg,
51             on_message => sub {
52 0     0     my %args = @_;
53              
54 0 0         if ($args{header}->{proc} == $msgs->PROC_PONG) {
55 0           $self->{inactive} = 0; # our PING; keep pinging
56 0           return $self->{on_ack}->($self, $transport);
57             }
58              
59 0           $self->mark_active;
60 0 0         if ($args{header}->{proc} == $msgs->PROC_PING) {
61 0           return $self->{on_ping}->($self, $transport);
62             }
63 0           return;
64             },
65 0           on_stream => \&_unexpected_msg
66             });
67             }
68              
69              
70             sub mark_active {
71 0     0 1   $_[0]->{inactive} = -1; # external activity
72             }
73              
74             sub ping {
75 0     0 1   my ($self) = @_;
76              
77 0           $self->{inactive}++;
78 0 0         if ($self->{inactive} > $self->{max_inactive}) {
79 0           return $self->{on_fail}->($self);
80             }
81 0 0         if ($self->{inactive}) {
82 0           $log->trace("Inactivity timer: $self->{inactive}");
83 0           return $self->{sender}->($msgs->PROC_PING, $type->MESSAGE, data => '');
84             }
85             else {
86 0           $log->trace("Activity found; no need to PING");
87 0           return;
88             }
89             }
90              
91             sub pong {
92 0     0 1   my ($self) = @_;
93 0           $self->{sender}->($msgs->PROC_PONG, $type->MESSAGE, data => '');
94             }
95              
96             1;
97              
98             __END__