}
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
}
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;
+ }
}
}
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)
}
}
-
+ 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;
}