added more extantLock methods and test for clientList monitoring (using FileWatcher...
authorjprocter <jprocter@compbio.dundee.ac.uk>
Tue, 25 Oct 2005 13:28:45 +0000 (13:28 +0000)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Tue, 25 Oct 2005 13:28:45 +0000 (13:28 +0000)
git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@77 be28352e-c001-0410-b1a7-c7978e42abec

src/org/vamsas/client/simpleclient/ClientsFile.java
src/org/vamsas/client/simpleclient/FileWatcher.java
src/org/vamsas/test/simpleclient/ClientsFileTest.java

index e5cbc72..d0fe340 100644 (file)
@@ -147,7 +147,33 @@ public class ClientsFile {
     }
     return null;
   }
-
+  /**
+   * adds clientHandle me to the clientList under an existing lock extantLock.
+   * @param me
+   * @param extantLock
+   * @return client index in list or 0 if lock was invalid or addClient operation failed.
+   */
+  public int addClient(ClientHandle me, Lock extantLock) {
+    return addClient(me, true, extantLock);
+  }
+  
+  /**
+   * adds clientHandle me to the clientList under an existing lock.
+   * @param me - clientHandle
+   * @param disambig - if true then add will fail if an identical clientHandle already exists
+   * @param extantLock - existing lock
+   * @return client index in list or 0 if addClient (or the lock) failed.
+   */
+  
+  public int addClient(ClientHandle me, boolean disambig, Lock extantLock) {
+    if (lockList(extantLock)) {
+      syncnum = addClient(me, disambig);
+      unlockList();
+      return syncnum;
+    }
+    return 0;
+  }
+  
   /**
    * adds the ClientHandle to the list - if it is not unique, then the
    * ClientHandle object is modified to make it unique in the list. returns the
index 34897a8..40038ad 100644 (file)
@@ -93,12 +93,16 @@ public class FileWatcher {
     }
     return false;
   }
-  
+  /**
+   * updates internal record of file state when caller has intentionally
+   * modified subject.
+   */
   public void setState() {
     if (subject!=null) {
-      lastStat = getStat(subject);
-      exists = subject.exists();
-      waslocked = checkLock();
+      if (exists = subject.exists()) {
+        lastStat = getStat(subject);
+        waslocked = false;
+      }
     }
   }
   
index a149a16..6ccd736 100644 (file)
@@ -18,6 +18,7 @@ public class ClientsFileTest {
     ClientsFileTest.commands.add(new String("list"));    
     ClientsFileTest.commands.add(new String("clear"));
     ClientsFileTest.commands.add(new String("watch"));    
+    ClientsFileTest.commands.add(new String("monitor"));    
   }
   private static void complainArgs(int argl, int argpos, String cmd, int argneed, String msg) {
     if (argl-argpos<argneed)
@@ -109,7 +110,50 @@ public class ClientsFileTest {
               }
               
             }
-            
+            break;
+          case 5:
+            // monitor
+            ClientsFileTest.complainArgs(args.length, argc, "monitor", 2, "for the Client's 'Name' and 'Version'");
+            int clpos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
+            argc+=2;
+            if (clpos!=0)
+              System.out.println("Monitor Client added at "+clpos+" as urn:"+ch.getClientUrn());
+            else {
+              System.err.println("Monitor Client was not added.");
+              break;
+            }
+            FileWatcher mon=new FileWatcher(cf);
+            while (cf.exists()) {
+              // get watcher's lock to ensure state change is fixed for retrieval
+              Lock chlock=mon.getChangedState();
+              if (chlock!=null) {
+                ClientHandle[] cl = cfhand.retrieveClientList(chlock);
+                System.out.println("-- Monitor "+cf.getName());
+                //while (w.hasChanged())
+                //  ;
+                int newpos=-1;
+                if (cl!=null) {
+                  for (int chi=0,che=cl.length; chi<che; chi++) {
+                    if (ch.equals(cl[chi]))
+                      newpos=chi+1;
+                  }
+                }
+                if (newpos==-1) {
+                  // add self again to cleared list.
+                  newpos=cfhand.addClient(ch);
+                  mon.setState();
+                  if (newpos==0) {
+                    System.err.println("Monitor client could not be re-added to list.");
+                    break;
+                  }
+                }
+                if (newpos!=clpos) {
+                  System.out.println("Monitor client moved from "+clpos+" to "+newpos);
+                  clpos=newpos;
+                }
+              }
+            }
+              
           }
           coms = null;
         }