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 $uid = $self->ID_to_uid( $ID, $state ) ) { |
warn "## update_state( $k => $v, ", dump( $state ), " )\n" if $self->debug; |
75 |
if ( my $o = $self->db->get('state')->get( $uid ) ) { |
|
76 |
warn "## update state of $uid [$ID]\n" if $self->debug; |
my $uid; |
77 |
$o->import( $state ); |
|
78 |
|
if ( $k eq 'ID' ) { |
79 |
|
if ( $uid = $self->ID_to_uid( $v, $state ) ) { |
80 |
|
# nop |
81 |
} else { |
} else { |
82 |
warn "## create new state for $uid [$ID]\n" if $self->debug; |
warn "## no uid for $v, first seen?\n" if $self->debug; |
83 |
$self->db->get('state')->put( $uid => $state ); |
return; |
84 |
} |
} |
85 |
} else { |
} else { |
86 |
warn "## no uid for $ID, first seen?\n" if $self->debug; |
$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 |
=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 |
|
|
106 |
|
|
107 |
sub state { |
sub state { |
108 |
my $self = shift; |
my $self = shift; |
109 |
my ( $ID ) = @_; |
my ( $k, $v ) = @_; |
110 |
confess "need ID" unless $ID; |
confess "need ID or uid" unless $k =~ m/^(ID|uid)$/; |
111 |
if ( my $uid = $self->ID_to_uid( $ID ) ) { |
confess "need $k value" unless $v; |
112 |
if ( my $state = $self->db->get('state')->get( $uid ) ) { |
|
113 |
return $state->export; |
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 { |
} else { |
121 |
|
warn "## no uid for $v so no state!\n" if $self->debug; |
122 |
return; |
return; |
123 |
} |
} |
124 |
} else { |
} else { |
125 |
warn "## no uid for $ID so no state!\n" if $self->debug; |
$uid = $v; |
126 |
|
} |
127 |
|
|
128 |
|
if ( my $state = $self->db->get('state')->get( $uid ) ) { |
129 |
|
return $state->export; |
130 |
|
} else { |
131 |
return; |
return; |
132 |
} |
} |
133 |
|
|
134 |
} |
} |
135 |
|
|
136 |
=head2 known_CPE |
=head2 known_CPE |
161 |
|
|
162 |
confess "need ID" unless $ID; |
confess "need ID" unless $ID; |
163 |
|
|
164 |
warn "ID_to_uid",dump( $ID, $state ) if $self->debug; |
warn "ID_to_uid",dump( $ID, $state ),$/ if $self->debug; |
165 |
|
|
166 |
$self->db->{session}->{ $ID }->{last_seen} = time(); |
$self->db->{session}->{ $ID }->{last_seen} = time(); |
167 |
|
|
168 |
my $uid; |
my $uid; |
169 |
|
|
170 |
if ( $uid = $self->db->{session}->{ $ID }->{ID_to_uid} ) { |
if ( $uid = $self->db->{session}->{ $ID }->{ ID_to_uid } ) { |
171 |
return $uid; |
return $uid; |
172 |
} elsif ( $uid = $state->{DeviceID}->{SerialNumber} ) { |
} elsif ( $uid = $state->{DeviceID}->{SerialNumber} ) { |
173 |
warn "## created new session for $uid session $ID\n" if $self->debug; |
warn "## created new session for $uid session $ID\n" if $self->debug; |