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( @_ );
54 Returns currnet store plugin object
61 my $module = $self->module;
62 my $s = $self->only( ref($self).'::'.$module );
64 confess "unknown store module $module not one of ", dump( $self->possible_stores ) unless $s;
66 warn "#### current store = $s\n" if $self->debug > 4;
73 $store->update_state( ID => $ID, $state );
74 $store->update_state( uid => $uid, $state );
81 my ( $k, $v, $state ) = @_;
83 confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
84 confess "need $k value" unless $v;
85 confess "need state" unless $state;
87 warn "#### update_state( $k => $v, ", dump( $state ), " )\n" if $self->debug > 4;
92 if ( $uid = $self->ID_to_uid( $v, $state ) ) {
95 warn "## no uid for $v, first seen?\n" if $self->debug;
102 $self->current_store->update_uid_state( $uid, $state );
107 my $state = $store->get_state( ID => $ID );
108 my $state = $store->get_state( uid => $uid );
110 Returns normal unblessed hash (actually, in-memory copy of state in database).
117 confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
118 confess "need $k value" unless $v;
120 warn "#### get_state( $k => $v )\n" if $self->debug > 4;
125 if ( $uid = $self->ID_to_uid( $v ) ) {
128 warn "## no uid for $v so no state!\n" if $self->debug;
135 return $self->current_store->get_state( $uid );
141 my @cpe = $store->all_uids;
147 my @cpes = $self->current_store->all_uids;
148 warn "## all_uids = ", dump( @cpes ), "\n" if $self->debug;
154 my $CPE_uid = $store->ID_to_uid( $ID, $state );
156 It uses C<< DeviceID.SerialNumber >> from C<Inform> message as unique ID
165 my ( $ID, $state ) = @_;
167 confess "need ID" unless $ID;
169 warn "#### ID_to_uid",dump( $ID, $state ),$/ if $self->debug > 4;
171 $session->{ $ID }->{last_seen} = time();
175 if ( $uid = $session->{ $ID }->{ ID_to_uid } ) {
177 } elsif ( $uid = $state->{DeviceID}->{SerialNumber} ) {
178 warn "## created new session for $uid session $ID\n" if $self->debug;
179 $session->{ $ID } = {
185 warn "## can't find uid for ID $ID, first seen?\n";
188 # TODO: expire sessions longer than 30m