X-Git-Url: http://git.rot13.org/?p=perl-cwmp.git;a=blobdiff_plain;f=lib%2FCWMP%2FStore.pm;h=6d9a784bd78e4d12dd57f82bc5b5c03e2ec56cc5;hp=37c3a8445f7fff6d749bdd7b0517b32f61d4b865;hb=73114373a890391990bba21f0b4eadb18a4a859b;hpb=404719834facfd31ccd878872a42697bab78d368 diff --git a/lib/CWMP/Store.pm b/lib/CWMP/Store.pm index 37c3a84..6d9a784 100644 --- a/lib/CWMP/Store.pm +++ b/lib/CWMP/Store.pm @@ -46,6 +46,9 @@ sub new { $self->current_store->open( @_ ); + # so that we don't have to check if it's defined + $self->debug( 0 ) unless $self->debug; + return $self; } @@ -63,49 +66,33 @@ sub current_store { confess "unknown store module $module not one of ", dump( $self->possible_stores ) unless $s; - warn "#### current store = $s\n" if $self->debug > 4; +# 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 > 4; - - 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 get_state - my $state = $store->get_state( ID => $ID ); - my $state = $store->get_state( uid => $uid ); + my $state = $store->get_state( $uid ); Returns normal unblessed hash (actually, in-memory copy of state in database). @@ -113,24 +100,10 @@ Returns normal unblessed hash (actually, in-memory copy of state in database). 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 "#### get_state( $k => $v )\n" if $self->debug > 4; - - my $uid; + my ( $uid ) = @_; + confess "need uid" unless $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 ); @@ -149,45 +122,25 @@ sub all_uids { return @cpes; } -=head2 ID_to_uid +=head2 state_to_uid - my $CPE_uid = $store->ID_to_uid( $ID, $state ); + my $CPE_uid = $store->ID_to_uid( $state ); It uses C<< DeviceID.SerialNumber >> from C message as unique ID for each CPE. =cut -my $session; - -sub ID_to_uid { +sub state_to_uid { my $self = shift; - my ( $ID, $state ) = @_; - - confess "need ID" unless $ID; - - warn "#### ID_to_uid",dump( $ID, $state ),$/ if $self->debug > 4; - - $session->{ $ID }->{last_seen} = time(); - - my $uid; + my ( $state ) = @_; - 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"; - } + warn "#### state_to_uid",dump( $state ),$/ if $self->debug > 4; - # TODO: expire sessions longer than 30m + my $uid = $state->{DeviceID}->{SerialNumber} || + confess "no DeviceID.SerialNumber in ",dump( $state ); - return; + return $uid; } 1;