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;
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 {
+ 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 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;
}
/**
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
/**
* 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;