adding various state flags for events
[vamsas.git] / src / org / vamsas / client / simpleclient / EventGeneratorThread.java
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.
    */