new fileWatcher method passes lock for changed file back to the caller.
[vamsas.git] / src / org / vamsas / test / simpleclient / ClientsFileTest.java
1 package org.vamsas.test.simpleclient;
2
3 import java.io.File;
4 import java.util.Iterator;
5 import java.util.Vector;
6
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;
11
12 public class ClientsFileTest {
13   private static Vector commands;
14   static {
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   }
22   private static void complainArgs(int argl, int argpos, String cmd, int argneed, String msg) {
23     if (argl-argpos<argneed)
24       throw new Error(cmd+" needs "+argneed+" arguments : "+msg);
25   }
26   public static void main(String[] args) {
27     java.io.File cf = new java.io.File(args[0]);
28     System.out.println("Connecting to clientFile "+args[0]);
29     ClientsFile cfhand;
30     try {
31       cfhand = new ClientsFile(cf);
32     }
33     catch (Exception e) {
34       e.printStackTrace(System.err);
35       return;
36     }
37     int argc=1;
38     while (argc<args.length) {
39       Iterator coms = commands.iterator();
40       int com=-1;
41       while ((coms!=null) && coms.hasNext()) {
42         com++;
43         if (args[argc].toLowerCase().equals((String) coms.next())) {
44           System.out.println("Doing "+args[argc]);
45           ClientHandle ch;
46           argc++;
47           switch (com) {
48           case 0:
49             // Add
50             ClientsFileTest.complainArgs(args.length, argc, "add", 2, "for the Client's 'Name' and 'Version'");
51             int pos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
52             argc+=2;
53             if (pos!=0)
54               System.out.println("Client added at "+pos+" as urn:"+ch.getClientUrn());
55             else
56               System.out.println("Client was not added.");
57             break;
58           case 1:
59             // remove
60             ClientsFileTest.complainArgs(args.length, argc, "remove", 3, "for the Client's 'Name', Version and URN");
61             ch=new ClientHandle(args[argc], args[argc+1]);
62             ch.setClientUrn(args[argc+2]);
63             argc+=3;
64             cfhand.removeClient(ch, null);
65             System.out.println("Client removed (apparently)");
66             break;
67           case 2:
68             // list
69             ClientHandle[] chlist = cfhand.retrieveClientList();
70             if (chlist!=null) {
71               for (int chi=0,che=chlist.length; chi<che; chi++) {
72                 System.out.println("Client "+chi+" ("+chlist[chi].getClientName()+" "+chlist[chi].getVersion()+" "+chlist[chi].getClientUrn()+")");
73               }
74             } else {
75               System.out.println("Client list is empty.");
76             }
77             break;
78           case 3:
79             // clear
80             cfhand = null;
81             cf.delete();
82             try {
83               cf.createNewFile();
84               cfhand = new ClientsFile(cf);
85             }
86             catch (Exception e) {
87               System.err.println("Failed on new empty clientfile creation!");
88               e.printStackTrace(System.err);
89             }
90             break;
91           case 4:
92             // watch
93             FileWatcher w=new FileWatcher(cf);
94             while (cf.exists()) {
95               // get watcher's lock to ensure state change is fixed for retrieval
96               Lock chlock=w.getChangedState();
97               if (chlock!=null) {
98                 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
99                 System.out.println("-- Watching "+cf.getName());
100                 //while (w.hasChanged())
101                 //  ;
102                 if (cl!=null) {
103                   for (int chi=0,che=cl.length; chi<che; chi++) {
104                     System.out.println("Client "+chi+" ("+cl[chi].getClientName()+" "+cl[chi].getVersion()+" "+cl[chi].getClientUrn()+")");
105                   }
106                 } else {
107                   System.out.println("Client list is empty.");
108                 }
109               }
110               
111             }
112             
113           }
114           coms = null;
115         }
116       }
117       if (coms!=null) {
118         System.err.println("Unknown command : "+args[argc++] + "*Ignored!*");
119       };
120       for (int j=0; j<900000; j++) {
121         Integer i=Integer.getInteger("1");
122         Integer q=i;
123       }
124     }
125   }
126
127 }