1 # Dobrica Pavlinusic, <dpavlin@rot13.org> 06/22/07 14:35:38 CEST
8 use base qw/Class::Accessor/;
9 __PACKAGE__->mk_accessors( qw/
16 use Data::Dump qw/dump/;
17 use Module::Pluggable search_path => 'CWMP::Store', sub_name => 'possible_stores', require => 1;
21 CWMP::Store - parsist CPE state on disk
27 my $store = CWMP::Store->new({
29 path => '/path/to/state.db',
38 my $self = $class->SUPER::new( @_ );
40 confess "requed parametar module is missing" unless $self->module;
42 # XXX it's important to call possible_stores once, because current_store won't work
43 my @plugins = $self->possible_stores();
45 warn "Found store plugins: ", join(", ", @plugins ), "\n" if $self->debug;
47 $self->current_store->open( @_ );
49 # so that we don't have to check if it's defined
50 $self->debug( 0 ) unless $self->debug;
57 Returns currnet store plugin object
64 my $module = $self->module;
65 my $s = $self->only( ref($self).'::'.$module );
67 confess "unknown store module $module not one of ", dump( $self->possible_stores ) unless $s;
69 # warn "#### current store = $s\n" if $self->debug > 4;
76 $store->update_state( ID => $ID, $state );
77 $store->update_state( uid => $uid, $state );
84 my ( $k, $v, $state ) = @_;
86 confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
87 confess "need $k value" unless $v;
88 confess "need state" unless $state;
90 warn "#### update_state( $k => $v, ", dump( $state ), " )\n" if $self->debug > 4;
95 if ( $uid = $self->ID_to_uid( $v, $state ) ) {
98 warn "## no uid for $v, first seen?\n" if $self->debug;
105 $self->current_store->update_uid_state( $uid, $state );
110 my $state = $store->get_state( ID => $ID );
111 my $state = $store->get_state( uid => $uid );
113 Returns normal unblessed hash (actually, in-memory copy of state in database).
120 confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
121 confess "need $k value" unless $v;
123 warn "#### get_state( $k => $v )\n" if $self->debug > 4;
128 if ( $uid = $self->ID_to_uid( $v ) ) {
131 warn "## no uid for $v so no state!\n" if $self->debug;
138 return $self->current_store->get_state( $uid );
144 my @cpe = $store->all_uids;
150 my @cpes = $self->current_store->all_uids;
151 warn "## all_uids = ", dump( @cpes ), "\n" if $self->debug;
157 my $CPE_uid = $store->ID_to_uid( $ID, $state );
159 It uses C<< DeviceID.SerialNumber >> from C<Inform> message as unique ID
168 my ( $ID, $state ) = @_;
170 confess "need ID" unless $ID;
172 warn "#### ID_to_uid",dump( $ID, $state ),$/ if $self->debug > 4;
174 warn "##### current session = ",dump( $session ), $/ if $self->debug > 5;
176 $session->{ $ID }->{last_seen} = time();
180 if ( $uid = $session->{ $ID }->{ ID_to_uid } ) {
182 } elsif ( $uid = $state->{DeviceID}->{SerialNumber} ) {
183 warn "## created new session for $uid session $ID\n" if $self->debug;
184 $session->{ $ID } = {
190 warn "## can't find uid for ID $ID, first seen?\n";
193 # TODO: expire sessions longer than 30m