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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
#!/usr/bin/perl -w
# Aliases were originally called Names.
# Sessions with aliases will remain active even if they have nothing
# to do. They still get SIGZOMBIE when all the other sessions run out
# of things to do, so programs with aliased sessions won't run
# forever. Aliases are mainly useful for creating "daemon" sessions
# that can be called upon by other sessions.
# This example is kind of obsolete. Session postbacks have been
# created in the meantime, allowing it to totally avoid the kludgey
# timer loops.
use strict;
use lib '../lib';
use POE;
#==============================================================================
# The LockDaemon package defines a session that provides simple
# resource locking. This is only available within the current
# process.
package LockDaemon;
use strict;
use POE::Session;
#------------------------------------------------------------------------------
# Create the LockDaemon. This illustrates non-POE objects that
# register themselves with POE during construction.
sub new {
my $type = shift;
my $self = bless { }, $type;
# hello, world!
print "> $self created\n";
# give this object to POE
POE::Session->create(
object_states => [
$self, [ qw(_start _stop lock unlock sighandler) ]
]
);
# Don't let the caller have a reference. It's not very nice, but it
# also prevents the caller from holding onto the reference and
# possibly leaking memory.
undef;
}
#------------------------------------------------------------------------------
# Destroy the server. This will happen after its POE::Session stops
# and lets go of the object reference.
sub DESTROY {
my $self = shift;
print "< $self destroyed\n";
}
#------------------------------------------------------------------------------
# This method handles POE's standard _start message. It registers an
# alias for the session, sets up signal handlers, and tells the world
# what it has done.
sub _start {
my $kernel = $_[KERNEL];
# Set the alias. This really should check alias_set's return value,
# but it's being lame.
$kernel->alias_set('lockd');
# register signal handlers
$kernel->sig('INT', 'sighandler');
$kernel->sig('IDLE', 'sighandler');
$kernel->sig('ZOMBIE', 'sighandler');
# hello, world!
print "+ lockd started.\n";
}
#------------------------------------------------------------------------------
# This method handles signals. It really only acknowledges that a
# signal has been received.
sub sighandler {
my $signal_name = $_[ARG0];
print "@ lockd caught and handled SIG$signal_name\n";
# Returning a boolean true value indicates to the kernel that the
# signal was handled. This usually means that the session will not
# be stopped.
return 1;
}
#------------------------------------------------------------------------------
# This method handles POE's standard _stop event. It cleans up after
# the session by removing its alias.
sub _stop {
my ($object, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
$kernel->alias_remove('lockd');
print "- lockd stopped.\n";
}
#------------------------------------------------------------------------------
# Attempt to acquire a lock. This implements a very basic callback
# protocol. If the lock can be acquired, the caller's $success state
# is invoked. If the lock fails, the caller's $failure state is
# invoked. It's up to the caller to keep itself alive, most likely
# with a timeout event.
sub lock {
my ($kernel, $heap, $sender, $lock_name, $success, $failure) =
@_[KERNEL, HEAP, SENDER, ARG0, ARG1, ARG2];
# if the lock already exists...
if (exists $heap->{$lock_name}) {
# ... check the current lock
my ($owner, $time) = @{$heap->{$lock_name}};
# ... same owner?
if ($owner eq $sender) {
# ... ... refresh lock & succeed
$heap->{$lock_name}->[1] = time();
$kernel->post($sender, $success);
return 0;
}
# ... different owner? fail!
$kernel->post($sender, $failure);
return 0;
}
# no pre-existing lock; so acquire ok
$heap->{$lock_name} = [ $sender, time() ];
$kernel->post($sender, $success);
}
#------------------------------------------------------------------------------
# Attempt to release a lock. This implements a very basic callback
# protocol, similar to lock's.
sub unlock {
my ($kernel, $heap, $sender, $lock_name, $success, $failure) =
@_[KERNEL, HEAP, SENDER, ARG0, ARG1, ARG2];
# if the lock exists...
if (exists $heap->{$lock_name}) {
# ... check the existing lock
my ($owner, $time) = @{$heap->{$lock_name}};
# ... same owner?
if ($owner eq $sender) {
# ... ... release the lock & succeed
delete $heap->{$lock_name};
$kernel->post($sender, $success);
return 0;
}
}
# no lock by that name; fail
$kernel->post($sender, $failure);
return 0;
}
#==============================================================================
# The LockClient package defines a session that wants to do some
# things to a resource that it must hold a lock for, and some other
# things when it doesn't need to hold a lock.
package LockClient;
use strict;
use POE::Session;
#------------------------------------------------------------------------------
# Create the LockClient. This also illustrates non-POE objects that
# register themselves with POE during construction. The LockDaemon
# constructor is better documented, though.
sub new {
my ($type, $name) = @_;
my $self = bless { 'name' => $name }, $type;
# hello, world!
print "> $self created\n";
# give this object to POE
POE::Session->create(
object_states => [
$self,
[ qw(_start _stop
acquire_lock retry_acquire
release_lock retry_release
perform_locked_operation perform_unlocked_operation
)
],
]
);
# it will manage itself, thank you
undef;
}
#------------------------------------------------------------------------------
# Destroy the client. This will happen after its POE::Session stops
# and lets go of the object reference.
sub DESTROY {
my $self = shift;
print "< $self destroyed\n";
}
#------------------------------------------------------------------------------
# This method handles POE's standard _start message. It starts the
# client's main loop by first performing an operation without holding
# a lock.
sub _start {
my ($kernel, $session, $object) = @_[KERNEL, SESSION, OBJECT];
# display some impressive output :)
print "+ client $object->{'name'} started\n";
# move to the next state in the cycle
$kernel->post($session, 'perform_unlocked_operation');
}
#------------------------------------------------------------------------------
# This method handles POE's standard _stop message. Normally it would
# clean up any resources it has allocated, but this test doesn't care.
sub _stop {
my $object = $_[OBJECT];
print "+ client $object->{'name'} stopped\n";
}
#------------------------------------------------------------------------------
# This is a cheezy hack to keep the session alive while it waits for
# the lock daemon to respond. All it does is wake up every ten
# seconds and set another alarm.
sub timer_loop {
my ($object, $kernel) = @_[OBJECT, KERNEL];
print "*** client $object->{'name'} alarm rang\n";
$kernel->delay('timer_loop', 10);
}
#------------------------------------------------------------------------------
# Attempt to acquire a lock.
sub acquire_lock {
my ($object, $kernel) = @_[OBJECT, KERNEL];
print "??? client $object->{'name'} attempting to acquire lock...\n";
# retry after waiting a little while
$kernel->delay('acquire_lock', 10);
# uses the lock daemon's protocol
$kernel->post('lockd', 'lock',
'lock name', 'perform_locked_operation', 'retry_acquire'
);
}
#------------------------------------------------------------------------------
# Acquire failed. Wait one second and retry.
sub retry_acquire {
my ($object, $kernel) = @_[OBJECT, KERNEL];
print "--- client $object->{'name'} acquire failed... retrying...\n";
$kernel->delay('acquire_lock', 1);
}
#------------------------------------------------------------------------------
# Attempt to release a held lock.
sub release_lock {
my ($object, $kernel) = @_[OBJECT, KERNEL];
print "??? client $object->{'name'} attempting to release lock...\n";
# retry after waiting a little while
$kernel->delay('release_lock', 10);
$kernel->post('lockd', 'unlock',
'lock name', 'perform_unlocked_operation', 'retry_release'
);
}
#------------------------------------------------------------------------------
# Release failed. Wait one second and retry.
sub retry_release {
my ($object, $kernel) = @_[OBJECT, KERNEL];
print "--- client $object->{'name'} release failed... retrying...\n";
$kernel->delay('release_lock', 1);
}
#------------------------------------------------------------------------------
# Do something while holding the lock.
sub perform_locked_operation {
my ($object, $kernel) = @_[OBJECT, KERNEL];
# clear the alarm!
$kernel->delay('acquire_lock');
print "+++ client $object->{'name'} acquired lock... processing...\n";
$kernel->delay('release_lock', 1);
}
#------------------------------------------------------------------------------
# Do something while not holding the lock.
sub perform_unlocked_operation {
my ($object, $kernel) = @_[OBJECT, KERNEL];
# clear the alarm!
$kernel->delay('release_lock');
print "+++ client $object->{'name'} released lock... processing...\n";
$kernel->delay('acquire_lock', 1);
}
#==============================================================================
# Create the lock daemon and five clients. Run them until someone
# sends a SIGINT.
package main;
# start the lock daemon
LockDaemon->new();
# start the clients
foreach (1..5) { LockClient->new($_); }
# run until it's time to stop
$poe_kernel->run();
exit;
|