| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Inferno::RegMgr::Monitor; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 1227 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 40 |  | 
| 4 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 34 |  | 
| 5 | 1 |  |  | 1 |  | 8 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 84 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | # update POD & Changes & README | 
| 8 | 1 |  |  | 1 |  | 7 | use version; our $VERSION = qv('0.1.0'); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | # update DEPENDENCIES in POD & Makefile.PL & README | 
| 11 | 1 |  |  | 1 |  | 77 | use Scalar::Util qw( weaken ); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 46 |  | 
| 12 | 1 |  |  | 1 |  | 5 | use Inferno::RegMgr::Utils qw( run_callback ); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 1 |  |  | 1 |  | 64 | use Inferno::RegMgr::Lookup; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 650 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub new { | 
| 18 | 0 |  |  | 0 | 1 |  | my ($class, $opt) = @_; | 
| 19 | 0 |  |  |  |  |  | my $self = { | 
| 20 |  |  |  |  |  |  | lookup      => undef, | 
| 21 |  |  |  |  |  |  | cb_add      => $opt->{cb_add}, | 
| 22 |  |  |  |  |  |  | cb_del      => $opt->{cb_del}, | 
| 23 |  |  |  |  |  |  | cb_mod      => $opt->{cb_mod}, | 
| 24 |  |  |  |  |  |  | method_add  => $opt->{method_add}, | 
| 25 |  |  |  |  |  |  | method_del  => $opt->{method_del}, | 
| 26 |  |  |  |  |  |  | method_mod  => $opt->{method_mod}, | 
| 27 |  |  |  |  |  |  | manager     => undef, | 
| 28 |  |  |  |  |  |  | cur         => {}, | 
| 29 |  |  |  |  |  |  | }; | 
| 30 | 0 |  |  |  |  |  | bless $self, $class; | 
| 31 | 0 |  |  |  |  |  | weaken( my $this = $self ); | 
| 32 |  |  |  |  |  |  | $self->{lookup} = Inferno::RegMgr::Lookup->new({ | 
| 33 |  |  |  |  |  |  | attr    => $opt->{attr}, | 
| 34 | 0 |  |  | 0 |  |  | cb      => sub { $this->_cb_monitor(@_) }, | 
| 35 | 0 |  |  |  |  |  | }); | 
| 36 | 0 |  |  |  |  |  | return $self; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub START { | 
| 40 | 0 |  |  | 0 | 0 |  | my ($self) = @_; | 
| 41 | 0 |  |  |  |  |  | $self->{manager}->attach( $self->{lookup} ); | 
| 42 | 0 |  |  |  |  |  | return; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub _cb_monitor { | 
| 46 | 0 |  |  | 0 |  |  | my ($self, $svc) = @_; | 
| 47 | 0 |  |  |  |  |  | my @prev = keys %{ $self->{cur} }; | 
|  | 0 |  |  |  |  |  |  | 
| 48 | 0 |  |  |  |  |  | for my $name (@prev) { | 
| 49 | 0 | 0 |  |  |  |  | if (exists $svc->{ $name }) { | 
| 50 | 0 |  |  |  |  |  | my $attr = delete $svc->{ $name }; | 
| 51 | 0 | 0 |  |  |  |  | if (_is_differ($self->{cur}{ $name }, $attr)) { | 
| 52 | 0 |  |  |  |  |  | $self->{cur}{ $name } = $attr; | 
| 53 | 0 | 0 |  |  |  |  | if (defined $self->{cb_mod}) { | 
| 54 | 0 |  |  |  |  |  | run_callback( $self->{cb_mod}, $self->{method_mod}, $name => $attr ); | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  | else { | 
| 59 | 0 |  |  |  |  |  | my $attr = delete $self->{cur}{ $name }; | 
| 60 | 0 | 0 |  |  |  |  | if (defined $self->{cb_del}) { | 
| 61 | 0 |  |  |  |  |  | run_callback( $self->{cb_del}, $self->{method_del}, $name => $attr ); | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  | } | 
| 65 | 0 |  |  |  |  |  | for my $name (keys %{ $svc }) { | 
|  | 0 |  |  |  |  |  |  | 
| 66 | 0 |  |  |  |  |  | my $attr = $svc->{ $name }; | 
| 67 | 0 |  |  |  |  |  | $self->{cur}{ $name } = $attr; | 
| 68 | 0 | 0 |  |  |  |  | if (defined $self->{cb_add}) { | 
| 69 | 0 |  |  |  |  |  | run_callback( $self->{cb_add}, $self->{method_add}, $name => $attr ); | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  | } | 
| 72 | 0 |  |  |  |  |  | return; | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | sub _is_differ { | 
| 76 | 0 |  |  | 0 |  |  | my ($h1, $h2) = @_; | 
| 77 | 0 | 0 |  |  |  |  | return 1 if keys %{ $h1 } != keys %{ $h2 }; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 78 | 0 |  |  |  |  |  | for my $key (keys %{ $h1 }) { | 
|  | 0 |  |  |  |  |  |  | 
| 79 | 0 | 0 |  |  |  |  | return 1 if !exists $h2->{ $key }; | 
| 80 | 0 | 0 |  |  |  |  | return 1 if $h1->{ $key } ne $h2->{ $key }; | 
| 81 |  |  |  |  |  |  | } | 
| 82 | 0 |  |  |  |  |  | return 0; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub STOP { | 
| 86 | 0 |  |  | 0 | 0 |  | my ($self) = @_; | 
| 87 |  |  |  |  |  |  | # Inferno::RegMgr::Lookup may already detach itself (after finishing search). | 
| 88 | 0 | 0 |  |  |  |  | if ($self->{lookup}{manager}) { | 
| 89 | 0 |  |  |  |  |  | $self->{manager}->detach( $self->{lookup} ); | 
| 90 |  |  |  |  |  |  | } | 
| 91 | 0 |  |  |  |  |  | return; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub REFRESH { | 
| 95 | 0 |  |  | 0 | 0 |  | my ($self) = @_; | 
| 96 | 0 |  |  |  |  |  | $self->STOP(); | 
| 97 | 0 |  |  |  |  |  | $self->START(); | 
| 98 | 0 |  |  |  |  |  | return; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | 1; # Magic true value required at end of module | 
| 103 |  |  |  |  |  |  | __END__ |