/[cwmp]/google/trunk/lib/CWMP/Store.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /google/trunk/lib/CWMP/Store.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

google/lib/CWMP/Store.pm revision 77 by dpavlin, Fri Jun 22 13:09:08 2007 UTC google/trunk/lib/CWMP/Store.pm revision 170 by dpavlin, Sun Oct 28 13:05:01 2007 UTC
# Line 7  use warnings; Line 7  use warnings;
7    
8  use base qw/Class::Accessor/;  use base qw/Class::Accessor/;
9  __PACKAGE__->mk_accessors( qw/  __PACKAGE__->mk_accessors( qw/
10  debug  module
11  path  path
12    debug
 _db  
13  / );  / );
14    
15  #use Carp qw/confess/;  use Carp qw/confess/;
16  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
17  use DBM::Deep;  use Module::Pluggable search_path => 'CWMP::Store', sub_name => 'possible_stores', require => 1;
18    
19  =head1 NAME  =head1 NAME
20    
# Line 26  CWMP::Store - parsist CPE state on disk Line 25  CWMP::Store - parsist CPE state on disk
25  =head2 new  =head2 new
26    
27    my $store = CWMP::Store->new({    my $store = CWMP::Store->new({
28            module => 'DBMDeep',
29          path => '/path/to/state.db',          path => '/path/to/state.db',
30            clean => 1,
31          debug => 1,          debug => 1,
32    });    });
33    
# Line 36  sub new { Line 37  sub new {
37          my $class = shift;          my $class = shift;
38          my $self = $class->SUPER::new( @_ );          my $self = $class->SUPER::new( @_ );
39    
40          warn "created ", __PACKAGE__, "(", dump( @_ ), ") object\n" if $self->debug;          confess "requed parametar module is missing" unless $self->module;
41    
42            # XXX it's important to call possible_stores once, because current_store won't work
43            my @plugins = $self->possible_stores();
44    
45          $self->_db(          warn "Found store plugins: ", join(", ", @plugins ), "\n" if $self->debug;
46                  DBM::Deep->new(  
47                          file => $self->path,          $self->current_store->open( @_ );
48                          locking => 1,  
49                          autoflush => 1,          # so that we don't have to check if it's defined
50                  )          $self->debug( 0 ) unless $self->debug;
         );  
51    
52          return $self;          return $self;
53  }  }
54    
55    =head2 current_store
56    
57    Returns currnet store plugin object
58    
59    =cut
60    
61    sub current_store {
62            my $self = shift;
63    
64            my $module = $self->module;
65            my $s = $self->only( ref($self).'::'.$module );
66    
67            confess "unknown store module $module not one of ", dump( $self->possible_stores ) unless $s;
68    
69            warn "#### current store = $s\n" if $self->debug > 4;
70    
71            return $s;
72    }
73    
74    =head2 update_state
75    
76      $store->update_state( ID => $ID, $state );
77      $store->update_state( uid => $uid, $state );
78    
79    =cut
80    
81    sub update_state {
82            my $self = shift;
83    
84            my ( $k, $v, $state ) = @_;
85    
86            confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
87            confess "need $k value" unless $v;
88            confess "need state" unless $state;
89    
90            warn "#### update_state( $k => $v, ", dump( $state ), " )\n" if $self->debug > 4;
91    
92            my $uid;
93    
94            if ( $k eq 'ID' ) {
95                    if ( $uid = $self->ID_to_uid( $v, $state ) ) {
96                            # nop
97                    } else {
98                            warn "## no uid for $v, first seen?\n" if $self->debug;
99                            return;
100                    }
101            } else {
102                    $uid = $v;
103            }
104    
105            $self->current_store->update_uid_state( $uid, $state );
106    }
107    
108    =head2 get_state
109    
110      my $state = $store->get_state( ID => $ID );
111      my $state = $store->get_state( uid => $uid );
112    
113    Returns normal unblessed hash (actually, in-memory copy of state in database).
114    
115    =cut
116    
117    sub get_state {
118            my $self = shift;
119            my ( $k, $v ) = @_;
120            confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
121            confess "need $k value" unless $v;
122    
123            warn "#### get_state( $k => $v )\n" if $self->debug > 4;
124    
125            my $uid;
126    
127            if ( $k eq 'ID' ) {
128                    if ( $uid = $self->ID_to_uid( $v ) ) {
129                            # nop
130                    } else {
131                            warn "## no uid for $v so no state!\n" if $self->debug;
132                            return;
133                    }
134            } else {
135                    $uid = $v;
136            }
137    
138            return $self->current_store->get_state( $uid );
139    
140    }
141    
142    =head2 all_uids
143    
144      my @cpe = $store->all_uids;
145    
146    =cut
147    
148    sub all_uids {
149            my $self = shift;
150            my @cpes = $self->current_store->all_uids;
151            warn "## all_uids = ", dump( @cpes ), "\n" if $self->debug;
152            return @cpes;
153    }
154    
155    =head2 ID_to_uid
156    
157      my $CPE_uid = $store->ID_to_uid( $ID, $state );
158    
159    It uses C<< DeviceID.SerialNumber >> from C<Inform> message as unique ID
160    for each CPE.
161    
162    =cut
163    
164    my $session;
165    
166    sub ID_to_uid {
167            my $self = shift;
168            my ( $ID, $state ) = @_;
169    
170            confess "need ID" unless $ID;
171    
172            warn "#### ID_to_uid",dump( $ID, $state ),$/ if $self->debug > 4;
173    
174            $session->{ $ID }->{last_seen} = time();
175    
176            my $uid;
177    
178            if ( $uid = $session->{ $ID }->{ ID_to_uid } ) {
179                    return $uid;
180            } elsif ( $uid = $state->{DeviceID}->{SerialNumber} ) {
181                    warn "## created new session for $uid session $ID\n" if $self->debug;
182                    $session->{ $ID } = {
183                            last_seen => time(),
184                            ID_to_uid => $uid,
185                    };
186                    return $uid;
187            } else {
188                    warn "## can't find uid for ID $ID, first seen?\n";
189            }
190    
191            # TODO: expire sessions longer than 30m
192    
193            return;
194    }
195    
196  1;  1;

Legend:
Removed from v.77  
changed lines
  Added in v.170

  ViewVC Help
Powered by ViewVC 1.1.26