new fileWatcher method passes lock for changed file back to the caller.
[vamsas.git] / src / org / vamsas / client / simpleclient / ClientsFile.java
index c624100..e5cbc72 100644 (file)
@@ -17,7 +17,7 @@ import java.io.OutputStream;
 import java.util.Vector;
 
 /**
- * @author jim Handler for the clientsFile within a vamsas session.
+ * @author jim Handler for the clientsFile within a vamsas session thread.
  */
 public class ClientsFile {
   private File filelist;
@@ -36,14 +36,25 @@ public class ClientsFile {
 
   private Lock listlock = null;
 
+  protected boolean lockList(Lock extantlock) {
+    if (listlock!=null && !listlock.isLocked()) {
+      listlock.release();// tidy up invalid lock
+    }
+    listlock=extantlock;
+    return lockList();
+  }
   /**
    * Get a lock for the ClientsFile
    * 
    * @return true if lock was made
    */
   protected boolean lockList() {
-    if (listlock != null && listlock.isLocked())
-      return true;
+    if (listlock != null)
+      if (listlock.isLocked())
+        return true;
+      else 
+        // lock failed for some reason.
+        listlock.release();
     listlock = null;
     if (filelist != null) {
       if (filelist.exists()) {
@@ -72,11 +83,7 @@ public class ClientsFile {
    */
   protected void unlockList() {
     if (listlock != null) {
-
-      if (listlock.isLocked()) {
-        listlock.release();
-      }
-
+      listlock.release();    
       listlock = null;
     }
   }
@@ -116,7 +123,7 @@ public class ClientsFile {
     return null;
   }
   /**
-   * get the clientList from the file. May return false if lock failed!
+   * get the clientList from the file. May return null if lock failed!
    * @return clientList
    */
   public ClientHandle[] retrieveClientList() {
@@ -127,6 +134,19 @@ public class ClientsFile {
     }
     return null;
   }
+  /**
+   * get list from the locked ClientList.
+   * @param extantlock
+   * @return clientList or null if lock failed (or file was empty)
+   */
+  public ClientHandle[] retrieveClientList(Lock extantlock) {
+    if (lockList(extantlock)) {
+      ClientHandle[] clients = retrieveClientHandles();
+      unlockList();
+      return clients;
+    }
+    return null;
+  }
 
   /**
    * adds the ClientHandle to the list - if it is not unique, then the
@@ -145,14 +165,15 @@ public class ClientsFile {
 
   /**
    * removes 'me' from the session ClientList without complaint if 'me' isn't in the clientList already.
-   * @param me
+   * @param me client handle to be removed
+   * @param clientlock existing lock passed from watcher.
    */
-  public void removeClient(ClientHandle me) {
+  public void removeClient(ClientHandle me, Lock clientlock) {
     int mynum=-1;
-    if (lockList()) {
+    if (lockList(clientlock)) {
       ClientHandle[] clients = retrieveClientHandles();
       if (clients != null) {
-        if (clients[syncnum-1]!=me) {
+        if ((syncnum<=0 || syncnum>clients.length) || clients[syncnum-1]!=me) {
           for (int i = 0, j = clients.length; i < j; i++) 
             if (clients[i].equals(me)) {
               mynum=i;