1 |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 06/22/07 14:35:38 CEST |
2 |
package CWMP::Store; |
3 |
|
4 |
use strict; |
5 |
use warnings; |
6 |
|
7 |
|
8 |
use base qw/Class::Accessor/; |
9 |
__PACKAGE__->mk_accessors( qw/ |
10 |
debug |
11 |
path |
12 |
|
13 |
db |
14 |
/ ); |
15 |
|
16 |
use Carp qw/confess/; |
17 |
use Data::Dump qw/dump/; |
18 |
use DBM::Deep; |
19 |
|
20 |
=head1 NAME |
21 |
|
22 |
CWMP::Store - parsist CPE state on disk |
23 |
|
24 |
=head1 METHODS |
25 |
|
26 |
=head2 new |
27 |
|
28 |
my $store = CWMP::Store->new({ |
29 |
path => '/path/to/state.db', |
30 |
debug => 1, |
31 |
}); |
32 |
|
33 |
=cut |
34 |
|
35 |
sub new { |
36 |
my $class = shift; |
37 |
my $self = $class->SUPER::new( @_ ); |
38 |
|
39 |
warn "created ", __PACKAGE__, "(", dump( @_ ), ") object\n" if $self->debug; |
40 |
|
41 |
confess "need path to state.db" unless ( $self->path ); |
42 |
|
43 |
$self->db( |
44 |
DBM::Deep->new( |
45 |
file => $self->path, |
46 |
locking => 1, |
47 |
autoflush => 1, |
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; |
56 |
} |
57 |
|
58 |
=head2 update_state |
59 |
|
60 |
$store->update_state( $ID, $state ); |
61 |
|
62 |
=cut |
63 |
|
64 |
sub update_state { |
65 |
my $self = shift; |
66 |
|
67 |
my ( $ID, $state ) = @_; |
68 |
|
69 |
confess "need ID" unless $ID; |
70 |
confess "need state" unless $state; |
71 |
|
72 |
if ( my $uid = $self->ID_to_uid( $ID, $state ) ) { |
73 |
if ( my $o = $self->db->get('state')->get( $uid ) ) { |
74 |
warn "## update state of $uid [$ID]\n" if $self->debug; |
75 |
$o->import( $state ); |
76 |
} else { |
77 |
warn "## create new state for $uid [$ID]\n" if $self->debug; |
78 |
$self->db->get('state')->put( $uid => $state ); |
79 |
} |
80 |
} else { |
81 |
warn "## no uid for $ID, first seen?\n" if $self->debug; |
82 |
} |
83 |
} |
84 |
|
85 |
=head2 state |
86 |
|
87 |
my $state = $store->state( $ID ); |
88 |
|
89 |
Returns normal unblessed hash (actually, in-memory copy of state in database). |
90 |
|
91 |
=cut |
92 |
|
93 |
sub state { |
94 |
my $self = shift; |
95 |
my ( $ID ) = @_; |
96 |
confess "need ID" unless $ID; |
97 |
if ( my $uid = $self->ID_to_uid( $ID ) ) { |
98 |
if ( my $state = $self->db->get('state')->get( $uid ) ) { |
99 |
return $state->export; |
100 |
} else { |
101 |
return; |
102 |
} |
103 |
} else { |
104 |
warn "## no uid for $ID so no state!\n" if $self->debug; |
105 |
return; |
106 |
} |
107 |
} |
108 |
|
109 |
=head2 known_CPE |
110 |
|
111 |
my @cpe = $store->known_CPE; |
112 |
|
113 |
=cut |
114 |
|
115 |
sub known_CPE { |
116 |
my $self = shift; |
117 |
my @cpes = keys %{ $self->db->{state} }; |
118 |
warn "all CPE: ", dump( @cpes ), "\n" if $self->debug; |
119 |
return @cpes; |
120 |
} |
121 |
|
122 |
=head2 ID_to_uid |
123 |
|
124 |
my $CPE_uid = $store->ID_to_uid( $ID, $state ); |
125 |
|
126 |
It uses C<< DeviceID.SerialNumber >> from C<Inform> message as unique ID |
127 |
for each CPE. |
128 |
|
129 |
=cut |
130 |
|
131 |
sub ID_to_uid { |
132 |
my $self = shift; |
133 |
my ( $ID, $state ) = @_; |
134 |
|
135 |
confess "need ID" unless $ID; |
136 |
|
137 |
warn "ID_to_uid",dump( $ID, $state ) if $self->debug; |
138 |
|
139 |
$self->db->{session}->{ $ID }->{last_seen} = time(); |
140 |
|
141 |
my $uid; |
142 |
|
143 |
if ( $uid = $self->db->{session}->{ $ID }->{ID_to_uid} ) { |
144 |
return $uid; |
145 |
} elsif ( $uid = $state->{DeviceID}->{SerialNumber} ) { |
146 |
warn "## created new session for $uid session $ID\n" if $self->debug; |
147 |
$self->db->{session}->{ $ID } = { |
148 |
last_seen => time(), |
149 |
ID_to_uid => $uid, |
150 |
}; |
151 |
return $uid; |
152 |
} else { |
153 |
warn "## can't find uid for ID $ID, first seen?\n"; |
154 |
return; |
155 |
} |
156 |
|
157 |
# TODO: expire sessions longer than 30m |
158 |
|
159 |
return; |
160 |
} |
161 |
|
162 |
1; |