Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[a8e391]: whip / Whip / State.pm Maximize Restore History

Download this file

State.pm    263 lines (205 with data), 5.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# Wrapper for a whip request/response transaction.
package Whip::State;
use warnings;
use strict;
use Carp qw(croak);
use YAML qw(Load Store);
use Digest::SHA1 qw(sha1_hex);
use Time::HiRes;
use Fcntl qw(:flock);
use Symbol qw(gensym);
use CGI qw(escapeHTML);
sub STATE_VALUES () { 0 }
sub STATE_ID () { 1 }
### Static functions to lock and unlock resources. They should be
### moved into main or a general-purpose Whip class when other things
### need to do locking later.
my %locks;
sub _lock {
my $resource = shift;
my $lock_file = main::DIR_LOCK . "/$resource";
if (exists $locks{$lock_file}) {
Whip->error( 500, "Attempt To Double Lock",
"Could not lock file <tt>" . escapeHTML($lock_file) .
"</tt> because it already is locked."
);
}
my $lock_fh = gensym();
unless (open $lock_fh, ">", $lock_file) {
Whip->error( 500, "Could Not Open Lock",
"Could not open lock file <tt>" . escapeHTML($lock_file) .
"</tt>: $!"
);
}
unless (flock($lock_fh, LOCK_EX)) {
Whip->error( 500, "Could Not Acquire Lock",
"Could not acquire lock on <tt>" .
escapeHTML($lock_file) . "</tt>: $!"
);
}
$locks{$lock_file} = $lock_fh;
}
sub _unlock {
my $resource = shift;
my $lock_file = main::DIR_LOCK . "/$resource";
my $lock_fh = delete $locks{$lock_file};
unless (defined $lock_fh) {
Whip->error( 500, "Attempt To Unlock Unlocked Resource",
"Could not unlock <tt>" . escapeHTML($lock_file) .
"</tt> because it already is unlocked."
);
}
unless (flock($lock_fh, LOCK_UN)) {
Whip->error( 500, "Could Not Release Lock",
"Could not release lock on <tt>" .
escapeHTML($lock_file) . "</tt>: $!"
);
}
close $lock_fh;
}
### Create an initial state.
sub new {
my ($class, $values) = @_;
my $self = bless
[ $values, # STATE_VALUES
undef, # STATE_ID
], $class;
return $self;
}
### Store a state into a file. Return the state's unique ID.
sub freeze {
my $self = shift;
# Lock the state directory.
_lock("state");
# Create a new state ID if we don't have one.
my $id = $self->[STATE_ID];
my $state_path = &main::DIR_STATE;
my $state_file;
if (defined $id) {
$state_file = "$state_path/$id";
}
else {
while (1) {
$id = sha1_hex(Time::HiRes::time() . "-$$-" . rand());
$state_file = "$state_path/$id";
last unless -e $state_file;
}
$self->[STATE_ID] = $id;
}
# Store the state there.
unless (open(STATE, ">", $state_file)) {
_unlock("state");
Whip->error( 500, "Error Saving State",
"Could not save state in <tt>" . escapeHTML($state_file) .
"</tt>: $!"
);
}
print STATE Store($self);
close STATE;
# Unlock the state directory.
_unlock("state");
return $id;
}
### Fetch a state from a file.
sub thaw {
my ($class, $id) = @_;
# Lock the state directory.
_lock("state");
# Load and thaw a state.
my $state_file = &main::DIR_STATE . "/" . $id;
unless (open(STATE, "<", $state_file)) {
my $error = $!;
_unlock("state");
Whip->error( 500, "Error Loading State",
"Could not load state from <tt>" . escapeHTML($state_file) .
"</tt>: $error"
);
}
local $/;
my $self = Load scalar <STATE>;
close STATE;
_unlock("state");
die unless ref($self) eq __PACKAGE__;
return $self;
}
### Destroy a state. Removes its file.
sub destroy {
my $self = shift;
unless (defined $self->[STATE_ID]) {
Whip->error( 500, "Error Destroying State",
"Could not destroy non-existent state."
);
}
my $state_file = &main::DIR_STATE . "/" . $self->[STATE_ID];
_lock("state");
my $unlink_status = unlink($state_file);
_unlock("state");
unless (unlink $state_file) {
Whip->error( 500, "Error Destroying State",
"Could not destroy state in <tt>" . escapeHTML($state_file) .
"</tt>: $!"
);
}
}
### Return the values hash from this state.
sub get_values {
my $self = shift;
return %{$self->[STATE_VALUES]};
}
### Absorb another state into this one.
sub absorb {
my ($self, $other_state) = @_;
my %other_state = $other_state->get_values();
while (my ($attribute, $value) = each %other_state) {
$self->store($attribute, $value);
}
}
### Store a value into a state.
sub store {
my $self = shift;
while (@_ >= 2) {
my $attribute = lc(shift);
my $value = shift;
my @value;
if (ref($value) eq "ARRAY") {
@value = @$value;
}
else {
@value = $value;
}
unless (exists $self->[STATE_VALUES]->{$attribute}) {
$self->[STATE_VALUES]->{$attribute} = [ ];
}
push @{$self->[STATE_VALUES]->{$attribute}}, @value;
}
croak "Whip::State->store() called with an odd number of parameters" if @_;
}
### Remove a value and return it.
sub delete {
my ($self, $attribute) = @_;
my $value = delete $self->[STATE_VALUES]->{lc($attribute)};
return unless $value;
if (wantarray) {
return @$value;
}
return $value->[0];
}
### Fetch a scalar, possibly inheriting it from elsewhere.
sub fetch {
my ($self, $attribute, @default) = @_;
$attribute = lc($attribute);
if (exists $self->[STATE_VALUES]->{$attribute}) {
if (wantarray) {
return @{$self->[STATE_VALUES]->{$attribute}};
}
return $self->[STATE_VALUES]->{$attribute}->[0];
}
return @default;
}
### Rename _value to something else.
sub rename_value {
my ($self, $new_name) = @_;
$new_name = lc($new_name);
$self->[STATE_VALUES]->{$new_name} = delete $self->[STATE_VALUES]->{_value};
}
1;