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',
37 my $self = $class->SUPER::new( @_ );
39 confess "requed parametar module is missing" unless $self->module;
41 warn "created ", __PACKAGE__, "(", dump( @_ ), ") object\n" if $self->debug;
43 warn "Found store plugins: ", join(", ", __PACKAGE__->possible_stores() );
45 $self->current_store->open( @_ );
52 Returns currnet store plugin object
59 my $module = $self->module;
60 my $s = $self->only( ref($self).'::'.$module );
62 confess "unknown store module $module not one of ", dump( $self->possible_stores ) unless $s;
64 warn "current store = ",dump( $s );
71 $store->update_state( ID => $ID, $state );
72 $store->update_state( uid => $uid, $state );
79 my ( $k, $v, $state ) = @_;
81 confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
82 confess "need $k value" unless $v;
83 confess "need state" unless $state;
85 warn "## update_state( $k => $v, ", dump( $state ), " )\n" if $self->debug;
90 if ( $uid = $self->ID_to_uid( $v, $state ) ) {
93 warn "## no uid for $v, first seen?\n" if $self->debug;
100 $self->current_store->update_uid_state( $uid, $state );
105 my $state = $store->state( ID => $ID );
106 my $state = $store->state( uid => $uid );
108 Returns normal unblessed hash (actually, in-memory copy of state in database).
115 confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
116 confess "need $k value" unless $v;
118 warn "## state( $k => $v )\n" if $self->debug;
123 if ( $uid = $self->ID_to_uid( $v ) ) {
126 warn "## no uid for $v so no state!\n" if $self->debug;
133 return $self->current_store->get_state( $uid );
139 my @cpe = $store->known_CPE;
145 my @cpes = $self->current_store->all_uids;
146 warn "all CPE: ", dump( @cpes ), "\n" if $self->debug;
152 my $CPE_uid = $store->ID_to_uid( $ID, $state );
154 It uses C<< DeviceID.SerialNumber >> from C<Inform> message as unique ID
163 my ( $ID, $state ) = @_;
165 confess "need ID" unless $ID;
167 warn "ID_to_uid",dump( $ID, $state ),$/ if $self->debug;
169 $session->{ $ID }->{last_seen} = time();
173 if ( $uid = $session->{ $ID }->{ ID_to_uid } ) {
175 } elsif ( $uid = $state->{DeviceID}->{SerialNumber} ) {
176 warn "## created new session for $uid session $ID\n" if $self->debug;
177 $session->{ $ID } = {
183 warn "## can't find uid for ID $ID, first seen?\n";
187 # TODO: expire sessions longer than 30m
189 warn "current session = ",dump( $session );