]> jspc29.x-matter.uni-frankfurt.de Git - labtools.git/commitdiff
huge success in shm_manager
authorMichael Wiebusch <m.wiebusch@gsi.de>
Thu, 27 Nov 2014 15:37:07 +0000 (16:37 +0100)
committerMichael Wiebusch <m.wiebusch@gsi.de>
Thu, 27 Nov 2014 15:37:07 +0000 (16:37 +0100)
peltier_controller/webGUI/index.pl
peltier_controller/webGUI/plotting_tool.pm
peltier_controller/webGUI/shm_manager.pm

index a81076c9402925f97d38fdb573fa65c890097323..7435f5599b6655ff8b6f02a85f5fd25d4e6efe99 100755 (executable)
@@ -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               ##
index 46bc6553a177f1956a129e4e4cfc5f9ad8587df5..7e96a94b4932c3173400459a6fac19861c79faa9 100644 (file)
@@ -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};
index 69af120a25c9925cb4c2fb3e5792ff4ce5d0c0e3..1d3e15f6d6400f788e193fe14729122dd512c8a4 100644 (file)
@@ -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