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()) {
*/
protected void unlockList() {
if (listlock != null) {
-
- if (listlock.isLocked()) {
- listlock.release();
- }
-
+ listlock.release();
listlock = null;
}
}
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() {
}
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
/**
* 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;