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 CommandProcessor cproc;
15 private static Vector commands;
17 cproc = new CommandProcessor();
18 ClientsFileTest.commands = new Vector();
19 ClientsFileTest.commands.add(new String("add"));
20 cproc.addCommand("add", 2, "for the Client's 'Name' and 'Version'");
21 ClientsFileTest.commands.add(new String("remove"));
22 cproc.addCommand("remove", 3, "for the Client's 'Name', Version and URN");
23 ClientsFileTest.commands.add(new String("list"));
24 cproc.addCommand("list", 0, "no args needed");
25 ClientsFileTest.commands.add(new String("clear"));
26 cproc.addCommand("clear", 0, "no args needed");
27 ClientsFileTest.commands.add(new String("watch"));
28 cproc.addCommand("watch", 0, "no args needed");
29 ClientsFileTest.commands.add(new String("monitor"));
30 cproc.addCommand("monitor", 2, "for the Client's 'Name' and 'Version'");
33 private static void complainArgs(int argl, int argpos, String cmd,
34 int argneed, String msg) {
35 if (argl - argpos < argneed)
36 throw new Error(cmd + " needs " + argneed + " arguments : " + msg);
39 public static void main(String[] args) {
40 java.io.File cf = new java.io.File(args[0]);
41 System.out.println("Connecting to clientFile " + args[0]);
44 cfhand = new ClientsFile(cf);
45 } catch (Exception e) {
46 e.printStackTrace(System.err);
50 while (argc < args.length) {
51 // vars needed for operations
53 int com = cproc.getCommand(args, argc);
58 int pos = cfhand.addClient(ch = new ClientHandle(args[argc],
62 System.out.println("Client added at " + pos + " as urn:"
65 System.out.println("Client was not added.");
69 ch = new ClientHandle(args[argc], args[argc + 1]);
70 ch.setClientUrn(args[argc + 2]);
72 cfhand.removeClient(ch, null);
73 System.out.println("Client removed (apparently)");
77 ClientHandle[] chlist = cfhand.retrieveClientList();
79 for (int chi = 0, che = chlist.length; chi < che; chi++) {
80 System.out.println("Client " + chi + " ("
81 + chlist[chi].getClientName() + " " + chlist[chi].getVersion()
82 + " " + chlist[chi].getClientUrn() + ")");
85 System.out.println("Client list is empty.");
96 } catch (Exception e) {
97 System.err.println("Failed on new empty clientfile creation!");
98 e.printStackTrace(System.err);
103 FileWatcher w = new FileWatcher(cf);
104 while (cf.exists()) {
105 // get watcher's lock to ensure state change is fixed for retrieval
106 Lock chlock = w.getChangedState();
107 if (chlock != null) {
108 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
109 System.out.println("-- Watching " + cf.getName());
110 //while (w.hasChanged())
113 for (int chi = 0, che = cl.length; chi < che; chi++) {
114 System.out.println("Client " + chi + " ("
115 + cl[chi].getClientName() + " " + cl[chi].getVersion()
116 + " " + cl[chi].getClientUrn() + ")");
119 System.out.println("Client list is empty.");
127 int clpos = cfhand.addClient(ch = new ClientHandle(args[argc],
131 System.out.println("Monitor Client added at " + clpos + " as urn:"
132 + ch.getClientUrn());
134 System.err.println("Monitor Client was not added.");
137 FileWatcher mon = new FileWatcher(cf);
138 while (cf.exists()) {
139 // get watcher's lock to ensure state change is fixed for retrieval
140 Lock chlock = mon.getChangedState();
141 if (chlock != null) {
142 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
143 System.out.println("-- Monitor " + cf.getName());
144 //while (w.hasChanged())
148 for (int chi = 0, che = cl.length; chi < che; chi++) {
149 if (ch.equals(cl[chi]))
154 // add self again to cleared list.
155 newpos = cfhand.addClient(ch);
159 .println("Monitor client could not be re-added to list.");
163 if (newpos != clpos) {
164 System.out.println("Monitor client moved from " + clpos + " to "
174 .println("Unknown command : " + args[argc++] + "*Ignored!*");
176 System.err.println("Command " + args[argc++]
177 + " *Ignored!* - its not implemented.");
180 for (int j = 0; j < 900000; j++) {
181 Integer i = Integer.getInteger("1");
188 /* Iterator coms = commands.iterator();
190 while ((coms!=null) && coms.hasNext()) {
192 if (args[argc].toLowerCase().equals((String) coms.next())) {
193 System.out.println("Doing "+args[argc]);
199 ClientsFileTest.complainArgs(args.length, argc, "add", 2, "for the Client's 'Name' and 'Version'");
200 int pos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
203 System.out.println("Client added at "+pos+" as urn:"+ch.getClientUrn());
205 System.out.println("Client was not added.");
209 ClientsFileTest.complainArgs(args.length, argc, "remove", 3, "for the Client's 'Name', Version and URN");
210 ch=new ClientHandle(args[argc], args[argc+1]);
211 ch.setClientUrn(args[argc+2]);
213 cfhand.removeClient(ch, null);
214 System.out.println("Client removed (apparently)");
218 ClientHandle[] chlist = cfhand.retrieveClientList();
220 for (int chi=0,che=chlist.length; chi<che; chi++) {
221 System.out.println("Client "+chi+" ("+chlist[chi].getClientName()+" "+chlist[chi].getVersion()+" "+chlist[chi].getClientUrn()+")");
224 System.out.println("Client list is empty.");
233 cfhand = new ClientsFile(cf);
235 catch (Exception e) {
236 System.err.println("Failed on new empty clientfile creation!");
237 e.printStackTrace(System.err);
242 FileWatcher w=new FileWatcher(cf);
243 while (cf.exists()) {
244 // get watcher's lock to ensure state change is fixed for retrieval
245 Lock chlock=w.getChangedState();
247 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
248 System.out.println("-- Watching "+cf.getName());
249 //while (w.hasChanged())
252 for (int chi=0,che=cl.length; chi<che; chi++) {
253 System.out.println("Client "+chi+" ("+cl[chi].getClientName()+" "+cl[chi].getVersion()+" "+cl[chi].getClientUrn()+")");
256 System.out.println("Client list is empty.");
264 ClientsFileTest.complainArgs(args.length, argc, "monitor", 2, "for the Client's 'Name' and 'Version'");
265 int clpos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
268 System.out.println("Monitor Client added at "+clpos+" as urn:"+ch.getClientUrn());
270 System.err.println("Monitor Client was not added.");
273 FileWatcher mon=new FileWatcher(cf);
274 while (cf.exists()) {
275 // get watcher's lock to ensure state change is fixed for retrieval
276 Lock chlock=mon.getChangedState();
278 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
279 System.out.println("-- Monitor "+cf.getName());
280 //while (w.hasChanged())
284 for (int chi=0,che=cl.length; chi<che; chi++) {
285 if (ch.equals(cl[chi]))
290 // add self again to cleared list.
291 newpos=cfhand.addClient(ch);
294 System.err.println("Monitor client could not be re-added to list.");
299 System.out.println("Monitor client moved from "+clpos+" to "+newpos);