/[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 85 by dpavlin, Fri Jun 22 18:36:09 2007 UTC google/trunk/lib/CWMP/Store.pm revision 149 by dpavlin, Sat Oct 27 22:51:15 2007 UTC
# Line 48  sub new { Line 48  sub new {
48                  )                  )
49          );          );
50    
51            foreach my $init ( qw/ state / ) {
52                    $self->db->put( $init => {} ) unless $self->db->get( $init );
53            }
54    
55          return $self;          return $self;
56  }  }
57    
58  =head2 update_state  =head2 update_state
59    
60    $store->update_state( $ID, $state );    $store->update_state( ID => $ID, $state );
61      $store->update_state( uid => $uid, $state );
62    
63  =cut  =cut
64    
65  sub update_state {  sub update_state {
66          my $self = shift;          my $self = shift;
67    
68          my ( $ID, $state ) = @_;          my ( $k, $v, $state ) = @_;
69    
70          confess "need ID" unless $ID;          confess "need ID or uid" unless $k =~ m/^(ID|uid)$/;
71            confess "need $k value" unless $v;
72          confess "need state" unless $state;          confess "need state" unless $state;
73    
74          if ( my $o = $self->db->get( $ID ) ) {          warn "## update_state( $k => $v, ", dump( $state ), " )\n" if $self->debug;
75                  warn "## update state of $ID\n" if $self->debug;  
76                  $o->import( $state );          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 {          } else {
86                  warn "## create new state for $ID\n" if $self->debug;                  $uid = $v;
87                  $self->db->put( $ID => $state );          }
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  =head2 state
99    
100    my $state = $store->state( $ID );    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).  Returns normal unblessed hash (actually, in-memory copy of state in database).
104    
# Line 85  Returns normal unblessed hash (actually, Line 106  Returns normal unblessed hash (actually,
106    
107  sub state {  sub state {
108          my $self = shift;          my $self = shift;
109          my ( $ID ) = @_;          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    my $session;
159    
160    sub ID_to_uid {
161            my $self = shift;
162            my ( $ID, $state ) = @_;
163    
164          confess "need ID" unless $ID;          confess "need ID" unless $ID;
165          return $self->db->get( $ID )->export;  
166            warn "ID_to_uid",dump( $ID, $state ),$/ if $self->debug;
167    
168            $session->{ $ID }->{last_seen} = time();
169    
170            my $uid;
171    
172            if ( $uid = $session->{ $ID }->{ ID_to_uid } ) {
173                    return $uid;
174            } elsif ( $uid = $state->{DeviceID}->{SerialNumber} ) {
175                    warn "## created new session for $uid session $ID\n" if $self->debug;
176                    $session->{ $ID } = {
177                            last_seen => time(),
178                            ID_to_uid => $uid,
179                    };
180                    return $uid;
181            } else {
182                    warn "## can't find uid for ID $ID, first seen?\n";
183                    return;
184            }
185    
186            # TODO: expire sessions longer than 30m
187    
188            warn "current session = ",dump( $session );
189    
190            return;
191  }  }
192    
193  1;  1;

Legend:
Removed from v.85  
changed lines
  Added in v.149

  ViewVC Help
Powered by ViewVC 1.1.26