| 1 | package Iterator::File::State::IPCShareable; | ||
| 2 | |||
| 3 | ## $Id: IPCShareable.pm,v 1.4 2008/06/11 05:20:07 wdr1 Exp $ | ||
| 4 | |||
| 5 | use 5.006; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Carp; | ||
| 10 | use Data::Dumper; | ||
| 11 | use IO::File; | ||
| 12 | use Digest::MD5 qw|md5_hex|; | ||
| 13 | |||
| 14 | use IPC::Shareable; | ||
| 15 | |||
| 16 | use Iterator::File::State::Interface; | ||
| 17 | |||
| 18 | our @ISA = qw(Iterator::File::State::Interface); | ||
| 19 | our $VERSION = substr(q$Revision: 1.4 $, 10); | ||
| 20 | |||
| 21 | our %default_config = (); | ||
| 22 | |||
| 23 | |||
| 24 | sub new { | ||
| 25 | my ($class, %config) = @_; | ||
| 26 | |||
| 27 | confess("No IPC key given!") unless ($config{ipc_key}); | ||
| 28 | |||
| 29 | %config = (%default_config, %config); | ||
| 30 | my $self = $class->SUPER::new( %config ); | ||
| 31 | bless($self, $class); | ||
| 32 | |||
| 33 | $self->_verbose("IPC key is '", $self->{'ipc_key'}, "'...\n"); | ||
| 34 | |||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | sub initialize { | ||
| 41 | my ($self) = @_; | ||
| 42 | |||
| 43 | $self->_debug( __PACKAGE__ . " initializing... "); | ||
| 44 | |||
| 45 | my $ipc_key = $self->{'ipc_key'}; | ||
| 46 | my $marker; | ||
| 47 | my $ipc_object = tie $marker, 'IPC::Shareable', $ipc_key, { 'create' => 1 }; | ||
| 48 | confess( "Unable to shared memory segment! Key: '$ipc_key'" ) | ||
| 49 | unless (defined $ipc_object); | ||
| 50 | |||
| 51 | $marker ||= 0; | ||
| 52 | |||
| 53 | $self->{'ipc_object'} = $ipc_object; | ||
| 54 | $self->{'marker'} = $marker; | ||
| 55 | |||
| 56 | $self->_verbose( "Starting marker location is '$marker'..." ); | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | sub set_marker { | ||
| 62 | my ($self, $num) = @_; | ||
| 63 | |||
| 64 | $self->{'marker'} = $num; | ||
| 65 | $self->{'ipc_object'}->STORE( $self->{'marker'} ); | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | sub finish { | ||
| 71 | my ($self) = @_; | ||
| 72 | |||
| 73 | $self->_verbose("Removing '", $self->{'ipc_key'}, "'...\n"); | ||
| 74 | $self->{'ipc_object'}->remove(); | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | sub marker { | ||
| 80 | my ($self) = @_; | ||
| 81 | |||
| 82 | return $self->{'marker'}; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | sub ipc_key { | ||
| 88 | my ($self) = @_; | ||
| 89 | |||
| 90 | return $self->{'ipc_key'}; | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | # Preloaded methods go here. | ||
| 96 | |||
| 97 | 1; | ||
| 98 | __END__ |