$shm_manager->deleteShm();
}
-sub dump_shm {
- my $self = shift;
- my $shm_manager = $self->create_shm_manager();
- return $shm_manager->readShm();
-}
+
sub print_html{
my $self = shift;
$webpage->page_body();
}
-
-##################################################
-## Test routines ##
-##################################################
-
-sub test {
- my $self = shift;
- my %options = @_;
- print "This is the test message!\n";
- print "The test routine has received the following options:\n\n";
-
- for my $item ( keys %options ) {
- print "key: $item\tvalue: ".$options{$item}."\n";
- }
- exit;
-}
-
-
-
sub plot {
my $self = shift;
my %options = @_;
}
+##################################################
+## Test routines ##
+##################################################
+
+sub test {
+ my $self = shift;
+ my %options = @_;
+ print "This is the test message!\n";
+ print "The test routine has received the following options:\n\n";
+
+ for my $item ( keys %options ) {
+ print "key: $item\tvalue: ".$options{$item}."\n";
+ }
+ exit;
+}
+
+sub dump_shm {
+ my $self = shift;
+ my $shm_manager = $self->create_shm_manager();
+ return $shm_manager->readShm();
+}
+
+sub write_test_shm {
+ my $self = shift;
+ my $shm_manager = $self->create_shm_manager();
+ return $shm_manager->writeShm({a =>1, b =>2, c => 3});
+}
+
+
+sub update_shm {
+ my $self = shift;
+ my %options = @_;
+ my $shm_manager = $self->create_shm_manager();
+ return $shm_manager->updateShm(\%options);
+}
+
+
##################################################
## daemon routines ##
use warnings;
use POSIX;
-use Storable qw(lock_store lock_retrieve);
-
+use Storable qw(lock_store lock_retrieve fd_retrieve nstore_fd);
+use Fcntl qw(:DEFAULT :flock);
sub new {
my $class = shift;
$self->{shmFile} = "/dev/shm/".$self->{shmName};
$self->{dataDir} = $self->{shmFile}."_data";
$self->{dataFile} = $self->{dataDir}."/data";
-
-
+ $self->{shmFhLocked} = 0;
bless($self, $class);
}
sub deleteShm {
my $self = shift;
unlink $self->{shmFile};
-# unless (defined( $self->{plotDir} )){
-# $self->{plotDir} = $self->{shmFile}."_plots";
-# }
system("rm -rf ".$self->{dataDir});
}
}
}
-sub writeShm {
+sub lockAndReadShm {
my $self = shift;
- my $shmHash = shift;
+
if ( -e $self->{shmFile} ){
- lock_store($shmHash,$self->{shmFile});
+ die "Shm file handle already open and locked!\n" if $self->{shmFhLocked};
+ sysopen(my $fh, $self->{shmFile}, O_RDWR|O_CREAT, 0666)
+ or die "can't open shm file: $!";
+ flock($fh, LOCK_EX) or die "can't lock shm file: $!";
+ $self->{shmFhLocked} = 1;
+ $self->{shmFh} = $fh; # store file handle in object
+ # attention! file handle is now still open!
+ return fd_retrieve(*$fh);
} else {
- die "shm does not exist!\n";
+ die "shm does not exist!";
}
}
-sub updateShm {
+## deprecated
+
+# sub writeShm {
+# my $self = shift;
+# my $shmHash = shift;
+# if ( -e $self->{shmFile} ){
+# lock_store($shmHash,$self->{shmFile});
+# } else {
+# die "shm does not exist!\n";
+# }
+# }
+
+sub writeShm { # closes and unlocks shm file if already open
my $self = shift;
my $shmHash = shift;
- my $oldShmHash;
+
if ( -e $self->{shmFile} ){
- $oldShmHash = lock_retrieve($self->{shmFile});
- lock_store({%$oldShmHash,%$shmHash},$self->{shmFile});
+ my $fh=$self->{shmFh};
+ #check if file handle still open and locked
+ unless($self->{shmFhLocked}){
+ print "found locked shm\n";
+ sysopen($fh, $self->{shmFile}, O_RDWR|O_CREAT, 0666)
+ or die "can't open shm file: $!";
+ flock($fh, LOCK_EX) or die "can't lock shm file: $!";
+ }
+ #in any case, store your hash in shm file
+ seek($fh,0,0);
+ nstore_fd($shmHash, *$fh)
+ or die "can't store hash\n";
+ truncate($fh, tell($fh));
+ close($fh);
+ $self->{shmFhLocked} = 0;# mark file handle as unlocked
+ return $shmHash;
} else {
die "shm does not exist!";
}
+sub updateShm {
+ my $self = shift;
+ my $shmHash = shift;
+
+ my $oldShmHash = $self->lockAndReadShm();
+ my $compositeShmHash = {%$oldShmHash,%$shmHash};
+ $self->writeShm($compositeShmHash);
+ return $compositeShmHash;
+}
+
+
1;
\ No newline at end of file