X-Git-Url: http://git.rot13.org/?p=perl-cwmp.git;a=blobdiff_plain;f=lib%2FCWMP%2FStore.pm;h=b90fd62e2a0c6f5a577d66924edac9caf29e6e04;hp=87871758575a44e4764dc38c3945201bff3f5ff5;hb=ab3c8b6b75a3c7fb9ddc5a0c474fb1aa7269bc83;hpb=de7bd7d0d60b42a12a93ba526d97b7ebaa5dcf2e diff --git a/lib/CWMP/Store.pm b/lib/CWMP/Store.pm index 8787175..b90fd62 100644 --- a/lib/CWMP/Store.pm +++ b/lib/CWMP/Store.pm @@ -27,6 +27,7 @@ CWMP::Store - parsist CPE state on disk my $store = CWMP::Store->new({ module => 'DBMDeep', path => '/path/to/state.db', + clean => 1, debug => 1, }); @@ -38,12 +39,16 @@ sub new { confess "requed parametar module is missing" unless $self->module; - warn "created ", __PACKAGE__, "(", dump( @_ ), ") object\n" if $self->debug; + # XXX it's important to call possible_stores once, because current_store won't work + my @plugins = $self->possible_stores(); - warn "Found store plugins: ", join(", ", __PACKAGE__->possible_stores() ); + warn "Found store plugins: ", join(", ", @plugins ), "\n" if $self->debug; $self->current_store->open( @_ ); + # so that we don't have to check if it's defined + $self->debug( 0 ) unless $self->debug; + return $self; } @@ -61,134 +66,95 @@ sub current_store { confess "unknown store module $module not one of ", dump( $self->possible_stores ) unless $s; - warn "current store = ",dump( $s ); +# warn "#### current store = $s\n" if $self->debug > 4; return $s; } =head2 update_state - $store->update_state( ID => $ID, $state ); - $store->update_state( uid => $uid, $state ); + $store->update_state( $state ); =cut sub update_state { my $self = shift; - my ( $k, $v, $state ) = @_; + my ( $state ) = @_; - confess "need ID or uid" unless $k =~ m/^(ID|uid)$/; - confess "need $k value" unless $v; confess "need state" unless $state; - warn "## update_state( $k => $v, ", dump( $state ), " )\n" if $self->debug; - - my $uid; - - if ( $k eq 'ID' ) { - if ( $uid = $self->ID_to_uid( $v, $state ) ) { - # nop - } else { - warn "## no uid for $v, first seen?\n" if $self->debug; - return; - } - } else { - $uid = $v; - } + my $uid = $self->state_to_uid( $state ); + warn "#### update_state( ", dump( $state ), " ) for $uid\n" if $self->debug > 2; $self->current_store->update_uid_state( $uid, $state ); } -=head2 state +=head2 get_state - my $state = $store->state( ID => $ID ); - my $state = $store->state( uid => $uid ); + my $state = $store->get_state( $uid ); Returns normal unblessed hash (actually, in-memory copy of state in database). =cut -sub state { +sub get_state { my $self = shift; - my ( $k, $v ) = @_; - confess "need ID or uid" unless $k =~ m/^(ID|uid)$/; - confess "need $k value" unless $v; - - warn "## state( $k => $v )\n" if $self->debug; + my ( $uid ) = @_; + confess "need uid" unless $uid; - my $uid; - - if ( $k eq 'ID' ) { - if ( $uid = $self->ID_to_uid( $v ) ) { - # nop - } else { - warn "## no uid for $v so no state!\n" if $self->debug; - return; - } - } else { - $uid = $v; - } + warn "#### get_state( $uid )\n" if $self->debug > 4; return $self->current_store->get_state( $uid ); } -=head2 known_CPE - my @cpe = $store->known_CPE; +=head2 set_state + + $store->set_state( $uid, $state ); =cut -sub known_CPE { +sub set_state { my $self = shift; - my @cpes = $self->current_store->all_uids; - warn "all CPE: ", dump( @cpes ), "\n" if $self->debug; - return @cpes; + return $self->current_store->set_state( @_ ); } -=head2 ID_to_uid - my $CPE_uid = $store->ID_to_uid( $ID, $state ); +=head2 all_uids -It uses C<< DeviceID.SerialNumber >> from C message as unique ID -for each CPE. + my @cpe = $store->all_uids; =cut -my $session; - -sub ID_to_uid { +sub all_uids { my $self = shift; - my ( $ID, $state ) = @_; + my @cpes = $self->current_store->all_uids; + warn "## all_uids = ", dump( @cpes ), "\n" if $self->debug; + return @cpes; +} - confess "need ID" unless $ID; +=head2 state_to_uid - warn "ID_to_uid",dump( $ID, $state ),$/ if $self->debug; + my $CPE_uid = $store->ID_to_uid( $state ); - $session->{ $ID }->{last_seen} = time(); +It uses C<< DeviceId.SerialNumber >> from C message as unique ID +for each CPE. - my $uid; +=cut - if ( $uid = $session->{ $ID }->{ ID_to_uid } ) { - return $uid; - } elsif ( $uid = $state->{DeviceID}->{SerialNumber} ) { - warn "## created new session for $uid session $ID\n" if $self->debug; - $session->{ $ID } = { - last_seen => time(), - ID_to_uid => $uid, - }; - return $uid; - } else { - warn "## can't find uid for ID $ID, first seen?\n"; - return; - } +sub state_to_uid { + my $self = shift; + my ( $state ) = @_; - # TODO: expire sessions longer than 30m + warn "#### state_to_uid",dump( $state ),$/ if $self->debug > 4; - warn "current session = ",dump( $session ); + my $uid = $state->{DeviceId}->{SerialNumber} || + confess "no DeviceId.SerialNumber in ",dump( $state ); + chomp($uid); - return; + return $uid; } 1;