1 package org.vamsas.test.simpleclient;
4 import java.util.Iterator;
5 import java.util.Vector;
8 import uk.ac.vamsas.client.ClientHandle;
9 import uk.ac.vamsas.client.simpleclient.ClientsFile;
10 import uk.ac.vamsas.client.simpleclient.FileWatcher;
11 import uk.ac.vamsas.client.simpleclient.Lock;
13 public class ClientsFileTest {
14 private static CommandProcessor cproc;
16 private static Vector commands;
18 cproc = new CommandProcessor();
19 ClientsFileTest.commands = new Vector();
20 ClientsFileTest.commands.add(new String("add"));
21 cproc.addCommand("add", 2, "for the Client's 'Name' and 'Version'");
22 ClientsFileTest.commands.add(new String("remove"));
23 cproc.addCommand("remove", 3, "for the Client's 'Name', Version and URN");
24 ClientsFileTest.commands.add(new String("list"));
25 cproc.addCommand("list", 0, "no args needed");
26 ClientsFileTest.commands.add(new String("clear"));
27 cproc.addCommand("clear", 0, "no args needed");
28 ClientsFileTest.commands.add(new String("watch"));
29 cproc.addCommand("watch", 0, "no args needed");
30 ClientsFileTest.commands.add(new String("monitor"));
31 cproc.addCommand("monitor", 2, "for the Client's 'Name' and 'Version'");
34 private static void complainArgs(int argl, int argpos, String cmd,
35 int argneed, String msg) {
36 if (argl - argpos < argneed)
37 throw new Error(cmd + " needs " + argneed + " arguments : " + msg);
40 public static void main(String[] args) {
41 java.io.File cf = new java.io.File(args[0]);
42 System.out.println("Connecting to clientFile " + args[0]);
45 cfhand = new ClientsFile(cf);
46 } catch (Exception e) {
47 e.printStackTrace(System.err);
51 while (argc < args.length) {
52 // vars needed for operations
54 int com = cproc.getCommand(args, argc);
59 int pos = cfhand.addClient(ch = new ClientHandle(args[argc],
63 System.out.println("Client added at " + pos + " as urn:"
66 System.out.println("Client was not added.");
70 ch = new ClientHandle(args[argc], args[argc + 1]);
71 ch.setClientUrn(args[argc + 2]);
73 cfhand.removeClient(ch, null);
74 System.out.println("Client removed (apparently)");
78 ClientHandle[] chlist = cfhand.retrieveClientList();
80 for (int chi = 0, che = chlist.length; chi < che; chi++) {
81 System.out.println("Client " + chi + " ("
82 + chlist[chi].getClientName() + " " + chlist[chi].getVersion()
83 + " " + chlist[chi].getClientUrn() + ")");
86 System.out.println("Client list is empty.");
97 } catch (Exception e) {
98 System.err.println("Failed on new empty clientfile creation!");
99 e.printStackTrace(System.err);
104 FileWatcher w = new FileWatcher(cf);
105 while (cf.exists()) {
106 // get watcher's lock to ensure state change is fixed for retrieval
107 Lock chlock = w.getChangedState();
108 if (chlock != null) {
109 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
110 System.out.println("-- Watching " + cf.getName());
111 //while (w.hasChanged())
114 for (int chi = 0, che = cl.length; chi < che; chi++) {
115 System.out.println("Client " + chi + " ("
116 + cl[chi].getClientName() + " " + cl[chi].getVersion()
117 + " " + cl[chi].getClientUrn() + ")");
120 System.out.println("Client list is empty.");
128 int clpos = cfhand.addClient(ch = new ClientHandle(args[argc],
132 System.out.println("Monitor Client added at " + clpos + " as urn:"
133 + ch.getClientUrn());
135 System.err.println("Monitor Client was not added.");
138 FileWatcher mon = new FileWatcher(cf);
139 while (cf.exists()) {
140 // get watcher's lock to ensure state change is fixed for retrieval
141 Lock chlock = mon.getChangedState();
142 if (chlock != null) {
143 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
144 System.out.println("-- Monitor " + cf.getName());
145 //while (w.hasChanged())
149 for (int chi = 0, che = cl.length; chi < che; chi++) {
150 if (ch.equals(cl[chi]))
155 // add self again to cleared list.
156 newpos = cfhand.addClient(ch);
160 .println("Monitor client could not be re-added to list.");
164 if (newpos != clpos) {
165 System.out.println("Monitor client moved from " + clpos + " to "
175 .println("Unknown command : " + args[argc++] + "*Ignored!*");
177 System.err.println("Command " + args[argc++]
178 + " *Ignored!* - its not implemented.");
181 for (int j = 0; j < 900000; j++) {
182 Integer i = Integer.getInteger("1");
189 /* Iterator coms = commands.iterator();
191 while ((coms!=null) && coms.hasNext()) {
193 if (args[argc].toLowerCase().equals((String) coms.next())) {
194 System.out.println("Doing "+args[argc]);
200 ClientsFileTest.complainArgs(args.length, argc, "add", 2, "for the Client's 'Name' and 'Version'");
201 int pos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
204 System.out.println("Client added at "+pos+" as urn:"+ch.getClientUrn());
206 System.out.println("Client was not added.");
210 ClientsFileTest.complainArgs(args.length, argc, "remove", 3, "for the Client's 'Name', Version and URN");
211 ch=new ClientHandle(args[argc], args[argc+1]);
212 ch.setClientUrn(args[argc+2]);
214 cfhand.removeClient(ch, null);
215 System.out.println("Client removed (apparently)");
219 ClientHandle[] chlist = cfhand.retrieveClientList();
221 for (int chi=0,che=chlist.length; chi<che; chi++) {
222 System.out.println("Client "+chi+" ("+chlist[chi].getClientName()+" "+chlist[chi].getVersion()+" "+chlist[chi].getClientUrn()+")");
225 System.out.println("Client list is empty.");
234 cfhand = new ClientsFile(cf);
236 catch (Exception e) {
237 System.err.println("Failed on new empty clientfile creation!");
238 e.printStackTrace(System.err);
243 FileWatcher w=new FileWatcher(cf);
244 while (cf.exists()) {
245 // get watcher's lock to ensure state change is fixed for retrieval
246 Lock chlock=w.getChangedState();
248 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
249 System.out.println("-- Watching "+cf.getName());
250 //while (w.hasChanged())
253 for (int chi=0,che=cl.length; chi<che; chi++) {
254 System.out.println("Client "+chi+" ("+cl[chi].getClientName()+" "+cl[chi].getVersion()+" "+cl[chi].getClientUrn()+")");
257 System.out.println("Client list is empty.");
265 ClientsFileTest.complainArgs(args.length, argc, "monitor", 2, "for the Client's 'Name' and 'Version'");
266 int clpos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
269 System.out.println("Monitor Client added at "+clpos+" as urn:"+ch.getClientUrn());
271 System.err.println("Monitor Client was not added.");
274 FileWatcher mon=new FileWatcher(cf);
275 while (cf.exists()) {
276 // get watcher's lock to ensure state change is fixed for retrieval
277 Lock chlock=mon.getChangedState();
279 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
280 System.out.println("-- Monitor "+cf.getName());
281 //while (w.hasChanged())
285 for (int chi=0,che=cl.length; chi<che; chi++) {
286 if (ch.equals(cl[chi]))
291 // add self again to cleared list.
292 newpos=cfhand.addClient(ch);
295 System.err.println("Monitor client could not be re-added to list.");
300 System.out.println("Monitor client moved from "+clpos+" to "+newpos);