adding various state flags for events
authorjprocter <jprocter@compbio.dundee.ac.uk>
Wed, 15 Mar 2006 19:35:11 +0000 (19:35 +0000)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Wed, 15 Mar 2006 19:35:11 +0000 (19:35 +0000)
git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@181 be28352e-c001-0410-b1a7-c7978e42abec

docs/VamsasSessionEventAnalysis.xls
src/org/vamsas/client/simpleclient/EventGeneratorThread.java
src/org/vamsas/client/simpleclient/FileWatcher.java
src/org/vamsas/client/simpleclient/SimpleClient.java
src/org/vamsas/client/simpleclient/VamsasSession.java

index 2ddf60a..dbc3c6d 100644 (file)
Binary files a/docs/VamsasSessionEventAnalysis.xls and b/docs/VamsasSessionEventAnalysis.xls differ
index 2a1db1f..d9aa1db 100644 (file)
@@ -1,6 +1,7 @@
 package org.vamsas.client.simpleclient;
 
 import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
 import java.beans.PropertyChangeSupport;
 import java.util.Hashtable;
 
@@ -172,17 +173,21 @@ public class EventGeneratorThread extends Thread implements Runnable {
   }
     
   
-  
+  private boolean block_document_updates=false;
   int STORE_WAIT=5; // how many units before we decide all clients have finalized their appdatas
   
   /**
    * client App requests offline storage of vamsas data. 
    * Call blocks whilst other apps do any appData finalizing
    * and then returns (after locking the vamsasDocument in the session)
+   * Note - the calling app may also receive events through the EventGeneratorThread for document updates.
+   * 
    * @return Lock for session.vamArchive 
    * @param STORE_WAIT indicates how lock the call will block for when nothing appears to be happening to the session.
    */
   protected Lock want_to_store() {
+    log.debug("Setting flag for document_update requests to be ignored");
+    block_document_updates=true;
     log.debug("Waiting for other apps to do FinalizeApp handling.");
     try {
       session.addStoreDocumentRequest(client.getClientHandle(), client.getUserHandle());
@@ -191,7 +196,13 @@ public class EventGeneratorThread extends Thread implements Runnable {
           e);
       log.info("trying to continue.");
     }
-    
+    // LATER: refine this semaphore process 
+    // to make a robust signalling mechanism:
+    // app1 requests, app1..n do something (or don't - they may be dead), 
+    // app1 realises all apps have done their thing, it then continues with synchronized data.
+    // this probably needs two files - a request file, 
+    //  and a response file which is acknowledged by the app1 requestor for each app.
+    //  eventually, no more responses are received for the request, and the app can then only continue with its store.
     int units = 0;
     while (units<STORE_WAIT) {
       wait(1);
@@ -199,11 +210,38 @@ public class EventGeneratorThread extends Thread implements Runnable {
         units=0;
       else
         units++;
-    }  
+    }
+    
+    block_document_updates=false;
+    log.debug("Cleared flag for ignoring document_update requests");
+    // wait around again (until our own watcher has woken up and synchronized).
+    while (units<STORE_WAIT) {
+      wait(1);
+      if (storeFile.hasChanged() || vamsasfile.hasChanged())
+        units=0;
+      else
+        units++;
+    }
+    
+    
     log.debug("finished waiting.");
     return session.vamArchive.getLock();
   }
-  
+  /**
+   * count handlers for a particular vamsas event 
+   * @param event string enumeration from org.vamsas.client.Events
+   * @return -1 for an invalid event, otherwise the number of handlers
+   */
+  protected int countHandlersFor(String event) {
+    if (handlers.containsKey(event)) {
+      PropertyChangeSupport handler = (PropertyChangeSupport) handlers.get(event);
+      PropertyChangeListener[] listeners;
+      if (handler!=null)
+        return ((listeners=handler.getPropertyChangeListeners())==null) 
+        ? -1 : listeners.length;
+    }
+    return -1;
+  }
   /**
    * probably don't need any of these below.
    */
index bebf1a4..7854b48 100644 (file)
@@ -148,4 +148,21 @@ public class FileWatcher {
       return null;
     }
   }
+  /**
+   * safely? getting current state of the watched file
+   * @return
+   */
+  public long[] getCurrentState() {
+    return getStat(subject);
+  }
+  /**
+   * safely compare an externally recorded state with the current state
+   * for significant modifications.
+   * @param a
+   * @param b
+   * @return
+   */
+  public boolean diffState(long[] a, long[] b) {
+    return compStat(a,b);
+  }
 }
index 43779dd..daf3ca5 100644 (file)
@@ -254,9 +254,11 @@ public class SimpleClient implements IClient {
     // Events.DOCUMENT_FINALIZEAPPDATA
     try {
       _session.writeVamsasDocument(location, vamlock);
+      _session.updateLastStoredStat();
     } catch (Exception e) {
       log.warn("Exception whilst trying to store document in "+location,e);
     }
+    
     vamlock.release();
   }
   
@@ -319,7 +321,10 @@ public class SimpleClient implements IClient {
       log.warn("Failed to start EventGenerator thread.");
       throw new Exception("Failed to start event generator thread - client cannot be instantiated.");
     }
-    
+    if (evgen.countHandlersFor(Events.DOCUMENT_CREATE)>0) {
+      //TODO: check vamsas document for presence of an AppData entry for this application
+      //evgen.raise(Events.DOCUMENT_CREATE);
+    }
   }
   
   
index 1424ee8..7971f57 100644 (file)
@@ -62,6 +62,13 @@ public class VamsasSession {
    */
   public static final String CLOSEANDSAVE_FILE="stored.log";
   /**
+   * state of vamsas document when it was last stored outside the session directory
+   */
+  protected long last_stored_stat[]=null;
+  protected void updateLastStoredStat() {
+    
+  }
+  /**
    * log file location
    */
   public static final String SESSION_LOG="Log.txt";
@@ -211,6 +218,8 @@ public class VamsasSession {
   }
   /**
    * write session as a new vamsas Document (this will overwrite any existing file without warning)
+   * TODO: test
+   * TODO: verify that lock should be released for vamsas document.
    * @param destarchive
    */
   protected void writeVamsasDocument(File destarchive, Lock extlock) throws IOException {