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.");
94 cfhand = new ClientsFile(cf);
95 } catch (Exception e) {
96 System.err.println("Failed on new empty clientfile creation!");
97 e.printStackTrace(System.err);
102 FileWatcher w = new FileWatcher(cf);
103 while (cf.exists()) {
104 // get watcher's lock to ensure state change is fixed for retrieval
105 Lock chlock = w.getChangedState();
106 if (chlock != null) {
107 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
108 System.out.println("-- Watching " + cf.getName());
109 //while (w.hasChanged())
112 for (int chi = 0, che = cl.length; chi < che; chi++) {
113 System.out.println("Client " + chi + " ("
114 + cl[chi].getClientName() + " " + cl[chi].getVersion()
115 + " " + cl[chi].getClientUrn() + ")");
118 System.out.println("Client list is empty.");
126 int clpos = cfhand.addClient(ch = new ClientHandle(args[argc],
130 System.out.println("Monitor Client added at " + clpos + " as urn:"
131 + ch.getClientUrn());
133 System.err.println("Monitor Client was not added.");
136 FileWatcher mon = new FileWatcher(cf);
137 while (cf.exists()) {
138 // get watcher's lock to ensure state change is fixed for retrieval
139 Lock chlock = mon.getChangedState();
140 if (chlock != null) {
141 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
142 System.out.println("-- Monitor " + cf.getName());
143 //while (w.hasChanged())
147 for (int chi = 0, che = cl.length; chi < che; chi++) {
148 if (ch.equals(cl[chi]))
153 // add self again to cleared list.
154 newpos = cfhand.addClient(ch);
158 .println("Monitor client could not be re-added to list.");
162 if (newpos != clpos) {
163 System.out.println("Monitor client moved from " + clpos + " to "
173 .println("Unknown command : " + args[argc++] + "*Ignored!*");
175 System.err.println("Command " + args[argc++]
176 + " *Ignored!* - its not implemented.");
179 for (int j = 0; j < 900000; j++) {
180 Integer i = Integer.getInteger("1");
187 /* Iterator coms = commands.iterator();
189 while ((coms!=null) && coms.hasNext()) {
191 if (args[argc].toLowerCase().equals((String) coms.next())) {
192 System.out.println("Doing "+args[argc]);
198 ClientsFileTest.complainArgs(args.length, argc, "add", 2, "for the Client's 'Name' and 'Version'");
199 int pos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
202 System.out.println("Client added at "+pos+" as urn:"+ch.getClientUrn());
204 System.out.println("Client was not added.");
208 ClientsFileTest.complainArgs(args.length, argc, "remove", 3, "for the Client's 'Name', Version and URN");
209 ch=new ClientHandle(args[argc], args[argc+1]);
210 ch.setClientUrn(args[argc+2]);
212 cfhand.removeClient(ch, null);
213 System.out.println("Client removed (apparently)");
217 ClientHandle[] chlist = cfhand.retrieveClientList();
219 for (int chi=0,che=chlist.length; chi<che; chi++) {
220 System.out.println("Client "+chi+" ("+chlist[chi].getClientName()+" "+chlist[chi].getVersion()+" "+chlist[chi].getClientUrn()+")");
223 System.out.println("Client list is empty.");
232 cfhand = new ClientsFile(cf);
234 catch (Exception e) {
235 System.err.println("Failed on new empty clientfile creation!");
236 e.printStackTrace(System.err);
241 FileWatcher w=new FileWatcher(cf);
242 while (cf.exists()) {
243 // get watcher's lock to ensure state change is fixed for retrieval
244 Lock chlock=w.getChangedState();
246 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
247 System.out.println("-- Watching "+cf.getName());
248 //while (w.hasChanged())
251 for (int chi=0,che=cl.length; chi<che; chi++) {
252 System.out.println("Client "+chi+" ("+cl[chi].getClientName()+" "+cl[chi].getVersion()+" "+cl[chi].getClientUrn()+")");
255 System.out.println("Client list is empty.");
263 ClientsFileTest.complainArgs(args.length, argc, "monitor", 2, "for the Client's 'Name' and 'Version'");
264 int clpos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
267 System.out.println("Monitor Client added at "+clpos+" as urn:"+ch.getClientUrn());
269 System.err.println("Monitor Client was not added.");
272 FileWatcher mon=new FileWatcher(cf);
273 while (cf.exists()) {
274 // get watcher's lock to ensure state change is fixed for retrieval
275 Lock chlock=mon.getChangedState();
277 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
278 System.out.println("-- Monitor "+cf.getName());
279 //while (w.hasChanged())
283 for (int chi=0,che=cl.length; chi<che; chi++) {
284 if (ch.equals(cl[chi]))
289 // add self again to cleared list.
290 newpos=cfhand.addClient(ch);
293 System.err.println("Monitor client could not be re-added to list.");
298 System.out.println("Monitor client moved from "+clpos+" to "+newpos);