1 package org.vamsas.test.simpleclient;
4 import java.util.Iterator;
5 import java.util.Vector;
7 import org.vamsas.client.ClientHandle;
8 import org.vamsas.client.simpleclient.ClientsFile;
9 import org.vamsas.client.simpleclient.FileWatcher;
10 import org.vamsas.client.simpleclient.Lock;
12 public class ClientsFileTest {
13 private static Vector commands;
15 ClientsFileTest.commands=new Vector();
16 ClientsFileTest.commands.add(new String("add"));
17 ClientsFileTest.commands.add(new String("remove"));
18 ClientsFileTest.commands.add(new String("list"));
19 ClientsFileTest.commands.add(new String("clear"));
20 ClientsFileTest.commands.add(new String("watch"));
21 ClientsFileTest.commands.add(new String("monitor"));
23 private static void complainArgs(int argl, int argpos, String cmd, int argneed, String msg) {
24 if (argl-argpos<argneed)
25 throw new Error(cmd+" needs "+argneed+" arguments : "+msg);
27 public static void main(String[] args) {
28 java.io.File cf = new java.io.File(args[0]);
29 System.out.println("Connecting to clientFile "+args[0]);
32 cfhand = new ClientsFile(cf);
35 e.printStackTrace(System.err);
39 while (argc<args.length) {
40 Iterator coms = commands.iterator();
42 while ((coms!=null) && coms.hasNext()) {
44 if (args[argc].toLowerCase().equals((String) coms.next())) {
45 System.out.println("Doing "+args[argc]);
51 ClientsFileTest.complainArgs(args.length, argc, "add", 2, "for the Client's 'Name' and 'Version'");
52 int pos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
55 System.out.println("Client added at "+pos+" as urn:"+ch.getClientUrn());
57 System.out.println("Client was not added.");
61 ClientsFileTest.complainArgs(args.length, argc, "remove", 3, "for the Client's 'Name', Version and URN");
62 ch=new ClientHandle(args[argc], args[argc+1]);
63 ch.setClientUrn(args[argc+2]);
65 cfhand.removeClient(ch, null);
66 System.out.println("Client removed (apparently)");
70 ClientHandle[] chlist = cfhand.retrieveClientList();
72 for (int chi=0,che=chlist.length; chi<che; chi++) {
73 System.out.println("Client "+chi+" ("+chlist[chi].getClientName()+" "+chlist[chi].getVersion()+" "+chlist[chi].getClientUrn()+")");
76 System.out.println("Client list is empty.");
85 cfhand = new ClientsFile(cf);
88 System.err.println("Failed on new empty clientfile creation!");
89 e.printStackTrace(System.err);
94 FileWatcher w=new FileWatcher(cf);
96 // get watcher's lock to ensure state change is fixed for retrieval
97 Lock chlock=w.getChangedState();
99 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
100 System.out.println("-- Watching "+cf.getName());
101 //while (w.hasChanged())
104 for (int chi=0,che=cl.length; chi<che; chi++) {
105 System.out.println("Client "+chi+" ("+cl[chi].getClientName()+" "+cl[chi].getVersion()+" "+cl[chi].getClientUrn()+")");
108 System.out.println("Client list is empty.");
116 ClientsFileTest.complainArgs(args.length, argc, "monitor", 2, "for the Client's 'Name' and 'Version'");
117 int clpos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
120 System.out.println("Monitor Client added at "+clpos+" as urn:"+ch.getClientUrn());
122 System.err.println("Monitor Client was not added.");
125 FileWatcher mon=new FileWatcher(cf);
126 while (cf.exists()) {
127 // get watcher's lock to ensure state change is fixed for retrieval
128 Lock chlock=mon.getChangedState();
130 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
131 System.out.println("-- Monitor "+cf.getName());
132 //while (w.hasChanged())
136 for (int chi=0,che=cl.length; chi<che; chi++) {
137 if (ch.equals(cl[chi]))
142 // add self again to cleared list.
143 newpos=cfhand.addClient(ch);
146 System.err.println("Monitor client could not be re-added to list.");
151 System.out.println("Monitor client moved from "+clpos+" to "+newpos);
162 System.err.println("Unknown command : "+args[argc++] + "*Ignored!*");
164 for (int j=0; j<900000; j++) {
165 Integer i=Integer.getInteger("1");