From e0f9797803162d1eadb77727e050696e45a96455 Mon Sep 17 00:00:00 2001 From: Michael Wiebusch Date: Thu, 27 Nov 2014 16:37:07 +0100 Subject: [PATCH] huge success in shm_manager --- peltier_controller/webGUI/index.pl | 62 +++++++++++-------- peltier_controller/webGUI/plotting_tool.pm | 4 +- peltier_controller/webGUI/shm_manager.pm | 70 +++++++++++++++++----- 3 files changed, 95 insertions(+), 41 deletions(-) diff --git a/peltier_controller/webGUI/index.pl b/peltier_controller/webGUI/index.pl index a81076c..7435f55 100755 --- a/peltier_controller/webGUI/index.pl +++ b/peltier_controller/webGUI/index.pl @@ -159,11 +159,7 @@ sub clear_records { $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; @@ -173,25 +169,6 @@ sub print_html{ $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 = @_; @@ -248,6 +225,43 @@ EOF } +################################################## +## 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 ## diff --git a/peltier_controller/webGUI/plotting_tool.pm b/peltier_controller/webGUI/plotting_tool.pm index 46bc655..7e96a94 100644 --- a/peltier_controller/webGUI/plotting_tool.pm +++ b/peltier_controller/webGUI/plotting_tool.pm @@ -72,10 +72,10 @@ EOF print GNUPLOT " , "; } print GNUPLOT qq%"< tail -$last_n $dataFile" using 1:$i title "$columnName" w lines%; - print GNUPLOT " , "; - print GNUPLOT qq%"< tail -1 $dataFile" using 1:$i:$i title "" w labels%; $i++; } + print GNUPLOT " , "; + print GNUPLOT qq%"< tail -1 $dataFile" using 1:2:2 title "" w labels%; print GNUPLOT "\n"; close(GNUPLOT); return $self->{plotFile}; diff --git a/peltier_controller/webGUI/shm_manager.pm b/peltier_controller/webGUI/shm_manager.pm index 69af120..1d3e15f 100644 --- a/peltier_controller/webGUI/shm_manager.pm +++ b/peltier_controller/webGUI/shm_manager.pm @@ -5,8 +5,8 @@ use strict; 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; @@ -20,9 +20,8 @@ sub new { $self->{shmFile} = "/dev/shm/".$self->{shmName}; $self->{dataDir} = $self->{shmFile}."_data"; $self->{dataFile} = $self->{dataDir}."/data"; - - + $self->{shmFhLocked} = 0; bless($self, $class); } @@ -46,9 +45,6 @@ sub createShm { sub deleteShm { my $self = shift; unlink $self->{shmFile}; -# unless (defined( $self->{plotDir} )){ -# $self->{plotDir} = $self->{shmFile}."_plots"; -# } system("rm -rf ".$self->{dataDir}); } @@ -62,23 +58,56 @@ sub readShm { } } -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!"; } @@ -86,4 +115,15 @@ sub updateShm { +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 -- 2.43.0