/[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

revision 77 by dpavlin, Fri Jun 22 13:09:08 2007 UTC revision 102 by dpavlin, Sun Jun 24 18:33:17 2007 UTC
# Line 10  __PACKAGE__->mk_accessors( qw/ Line 10  __PACKAGE__->mk_accessors( qw/
10  debug  debug
11  path  path
12    
13  _db  db
14  / );  / );
15    
16  #use Carp qw/confess/;  use Carp qw/confess/;
17  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
18  use DBM::Deep;  use DBM::Deep;
19    
# Line 38  sub new { Line 38  sub new {
38    
39          warn "created ", __PACKAGE__, "(", dump( @_ ), ") object\n" if $self->debug;          warn "created ", __PACKAGE__, "(", dump( @_ ), ") object\n" if $self->debug;
40    
41          $self->_db(          confess "need path to state.db" unless ( $self->path );
42    
43            $self->db(
44                  DBM::Deep->new(                  DBM::Deep->new(
45                          file => $self->path,                          file => $self->path,
46                          locking => 1,                          locking => 1,
# Line 46  sub new { Line 48  sub new {
48                  )                  )
49          );          );
50    
51            foreach my $init ( qw/ state session / ) {
52                    $self->db->put( $init => {} ) unless $self->db->get( $init );
53            }
54    
55          return $self;          return $self;
56  }  }
57    
58    =head2 update_state
59    
60      $store->update_state( ID => $ID, $state );
61      $store->update_state( uid => $uid, $state );
62    
63    =cut
64    
65    sub update_state {
66            my $self = shift;
67    
68            my ( $k, $v, $state ) = @_;
69    
70            confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
71            confess "need $k value" unless $v;
72            confess "need state" unless $state;
73    
74            warn "## update_state( $k => $v, ", dump( $state ), " )\n" if $self->debug;
75    
76            my $uid;
77    
78            if ( $k eq 'ID' ) {
79                    if ( $uid = $self->ID_to_uid( $v, $state ) ) {
80                            # nop
81                    } else {
82                            warn "## no uid for $v, first seen?\n" if $self->debug;
83                            return;
84                    }
85            } else {
86                    $uid = $v;
87            }
88    
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 );
92            } else {
93                    warn "## create new state for $uid [$v]\n" if $self->debug;
94                    return $self->db->get('state')->put( $uid => $state );
95            }
96    }
97    
98    =head2 state
99    
100      my $state = $store->state( ID => $ID );
101      my $state = $store->state( uid => $uid );
102    
103    Returns normal unblessed hash (actually, in-memory copy of state in database).
104    
105    =cut
106    
107    sub state {
108            my $self = shift;
109            my ( $k, $v ) = @_;
110            confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
111            confess "need $k value" unless $v;
112    
113            warn "## state( $k => $v )\n" if $self->debug;
114    
115            my $uid;
116    
117            if ( $k eq 'ID' ) {
118                    if ( $uid = $self->ID_to_uid( $v ) ) {
119                            # nop
120                    } else {
121                            warn "## no uid for $v so no state!\n" if $self->debug;
122                            return;
123                    }
124            } else {
125                    $uid = $v;
126            }
127    
128            if ( my $state = $self->db->get('state')->get( $uid ) ) {
129                    return $state->export;
130            } else {
131                    return;
132            }
133    
134    }
135    
136    =head2 known_CPE
137    
138      my @cpe = $store->known_CPE;
139    
140    =cut
141    
142    sub known_CPE {
143            my $self = shift;
144            my @cpes = keys %{ $self->db->{state} };
145            warn "all CPE: ", dump( @cpes ), "\n" if $self->debug;
146            return @cpes;
147    }
148    
149    =head2 ID_to_uid
150    
151      my $CPE_uid = $store->ID_to_uid( $ID, $state );
152    
153    It uses C<< DeviceID.SerialNumber >> from C<Inform> message as unique ID
154    for each CPE.
155    
156    =cut
157    
158    sub ID_to_uid {
159            my $self = shift;
160            my ( $ID, $state ) = @_;
161    
162            confess "need ID" unless $ID;
163    
164            warn "ID_to_uid",dump( $ID, $state ),$/ if $self->debug;
165    
166            $self->db->{session}->{ $ID }->{last_seen} = time();
167    
168            my $uid;
169    
170            if ( $uid = $self->db->{session}->{ $ID }->{ ID_to_uid } ) {
171                    return $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 } = {
175                            last_seen => time(),
176                            ID_to_uid => $uid,
177                    };
178                    return $uid;
179            } else {
180                    warn "## can't find uid for ID $ID, first seen?\n";
181                    return;
182            }
183    
184            # TODO: expire sessions longer than 30m
185    
186            return;
187    }
188    
189  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26