1 # Dobrica Pavlinusic, <dpavlin@rot13.org> 06/22/07 14:35:38 CEST
8 use base qw/Class::Accessor/;
9 __PACKAGE__->mk_accessors( qw/
17 use Data::Dump qw/dump/;
22 CWMP::Store - parsist CPE state on disk
28 my $store = CWMP::Store->new({
29 path => '/path/to/state.db',
37 my $self = $class->SUPER::new( @_ );
39 warn "created ", __PACKAGE__, "(", dump( @_ ), ") object\n" if $self->debug;
41 confess "need path to state.db" unless ( $self->path );
51 foreach my $init ( qw/ state session / ) {
52 $self->db->put( $init => {} ) unless $self->db->get( $init );
60 $store->update_state( ID => $ID, $state );
61 $store->update_state( uid => $uid, $state );
68 my ( $k, $v, $state ) = @_;
70 confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
71 confess "need $k value" unless $v;
72 confess "need state" unless $state;
74 warn "## update_state( $k => $v, ", dump( $state ), " )\n" if $self->debug;
79 if ( $uid = $self->ID_to_uid( $v, $state ) ) {
82 warn "## no uid for $v, first seen?\n" if $self->debug;
89 if ( my $o = $self->db->get('state')->get( $uid ) ) {
90 warn "## update state of $uid [$v]\n" if $self->debug;
91 return $o->import( $state );
93 warn "## create new state for $uid [$v]\n" if $self->debug;
94 return $self->db->get('state')->put( $uid => $state );
100 my $state = $store->state( ID => $ID );
101 my $state = $store->state( uid => $uid );
103 Returns normal unblessed hash (actually, in-memory copy of state in database).
110 confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
111 confess "need $k value" unless $v;
113 warn "## state( $k => $v )\n" if $self->debug;
118 if ( $uid = $self->ID_to_uid( $v ) ) {
121 warn "## no uid for $v so no state!\n" if $self->debug;
128 if ( my $state = $self->db->get('state')->get( $uid ) ) {
129 return $state->export;
138 my @cpe = $store->known_CPE;
144 my @cpes = keys %{ $self->db->{state} };
145 warn "all CPE: ", dump( @cpes ), "\n" if $self->debug;
151 my $CPE_uid = $store->ID_to_uid( $ID, $state );
153 It uses C<< DeviceID.SerialNumber >> from C<Inform> message as unique ID
160 my ( $ID, $state ) = @_;
162 confess "need ID" unless $ID;
164 warn "ID_to_uid",dump( $ID, $state ),$/ if $self->debug;
166 $self->db->{session}->{ $ID }->{last_seen} = time();
170 if ( $uid = $self->db->{session}->{ $ID }->{ ID_to_uid } ) {
172 } elsif ( $uid = $state->{DeviceID}->{SerialNumber} ) {
173 warn "## created new session for $uid session $ID\n" if $self->debug;
174 $self->db->{session}->{ $ID } = {
180 warn "## can't find uid for ID $ID, first seen?\n";
184 # TODO: expire sessions longer than 30m