X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Forg%2Fvamsas%2Fclient%2Fsimpleclient%2FClientsFile.java;h=d0fe34034e9ebc01be2ffe05dddab7cad3b5fee7;hb=37e460acf0f82dfb3dfeeb022a84f737759921ba;hp=8f938f6569aa64b4e3fbbded18ec0165a0546da0;hpb=60d3fb8b4c31842b601284e79906e8f8bc63c4f8;p=vamsas.git diff --git a/src/org/vamsas/client/simpleclient/ClientsFile.java b/src/org/vamsas/client/simpleclient/ClientsFile.java index 8f938f6..d0fe340 100644 --- a/src/org/vamsas/client/simpleclient/ClientsFile.java +++ b/src/org/vamsas/client/simpleclient/ClientsFile.java @@ -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,24 +36,36 @@ 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()) { // TODO: see if we need to loop-wait for locks or they just block until // lock is made... - // do { - // listlock = new Lock(filelist); // TODO: wait around if we can't get - // the lock. - // } while (!listlock.isLocked()); - listlock = new Lock(filelist); + do { + if (listlock!=null) + listlock.release(); + listlock = new Lock(filelist); // TODO: wait around if we can't get the lock. + } while (!listlock.isLocked()); + // listlock = new Lock(filelist); return listlock.isLocked(); } } else @@ -69,22 +81,11 @@ public class ClientsFile { * * @return true if lock was released. */ - protected boolean unlockList() { + protected void unlockList() { if (listlock != null) { - - if (listlock.isLocked()) { - try { - listlock.lock.release(); - } catch (IOException e) { - // TODO Deal with unlock Lock.release exception! - e.printStackTrace(System.err); - } - } - + listlock.release(); listlock = null; - return true; } - return false; } /** @@ -122,18 +123,57 @@ 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() { if (lockList()) { ClientHandle[] clients = retrieveClientHandles(); - if (unlockList()) - return clients; + unlockList(); + return clients; } 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 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 @@ -151,14 +191,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; @@ -172,7 +213,7 @@ public class ClientsFile { for (int k=0,i = 0, j = clients.length; i < j; i++) if (i!=mynum) newlist[k++] = clients[i]; - if (!putClientList(clients)) + if (!putClientList(newlist)) throw new Error("Failed to write new clientList!"); // failed to put the clientList to disk. } } @@ -187,7 +228,7 @@ public class ClientsFile { * Note: Caller is left to release the lock on the ClientList. * @param me * @param disambiguate - - * flag indicating if the URN for me should be disambiguate to + * flag indicating if the URN for me should be disambiguated to * differentiate between sessions. * @return index of clientHandle in new list, or -1-position of existing * clientHandle (if disambiguate is true)