9d76bfbaf07329ed6003ed43a61700c2f41cd017
[vamsas.git] / src / uk / ac / vamsas / test / simpleclient / ClientsFileTest.java
1 package uk.ac.vamsas.test.simpleclient;
2
3 import java.io.File;
4 import java.util.Iterator;
5 import java.util.Vector;
6
7
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;
12
13 public class ClientsFileTest {
14   private static CommandProcessor cproc;
15
16   private static Vector commands;
17   static {
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'");
32   }
33
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);
38   }
39
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]);
43     ClientsFile cfhand;
44     try {
45       cfhand = new ClientsFile(cf);
46     } catch (Exception e) {
47       e.printStackTrace(System.err);
48       return;
49     }
50     int argc = 1;
51     while (argc < args.length) {
52       // vars needed for operations
53       ClientHandle ch;
54       int com = cproc.getCommand(args, argc);
55       argc++;
56       switch (com) {
57       case 0:
58         // Add
59         int pos = cfhand.addClient(ch = new ClientHandle(args[argc],
60             args[argc + 1]));
61         argc += 2;
62         if (pos != 0)
63           System.out.println("Client added at " + pos + " as urn:"
64               + ch.getClientUrn());
65         else
66           System.out.println("Client was not added.");
67         break;
68       case 1:
69         // remove
70         ch = new ClientHandle(args[argc], args[argc + 1]);
71         ch.setClientUrn(args[argc + 2]);
72         argc += 3;
73         cfhand.removeClient(ch, null);
74         System.out.println("Client removed (apparently)");
75         break;
76       case 2:
77         // list
78         ClientHandle[] chlist = cfhand.retrieveClientList();
79         if (chlist != null) {
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() + ")");
84           }
85         } else {
86           System.out.println("Client list is empty.");
87         }
88         break;
89       case 3:
90         // clear
91         //cfhand.get = null;
92         //cf.delete();
93         try {
94           
95           cfhand.clearList();
96           
97         } catch (Exception e) {
98           System.err.println("Failed on new empty clientfile creation!");
99           e.printStackTrace(System.err);
100         }
101         break;
102       case 4:
103         // watch
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())
112             //  ;
113             if (cl != null) {
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() + ")");
118               }
119             } else {
120               System.out.println("Client list is empty.");
121             }
122           }
123
124         }
125         break;
126       case 5:
127         // monitor
128         int clpos = cfhand.addClient(ch = new ClientHandle(args[argc],
129             args[argc + 1]));
130         argc += 2;
131         if (clpos != 0)
132           System.out.println("Monitor Client added at " + clpos + " as urn:"
133               + ch.getClientUrn());
134         else {
135           System.err.println("Monitor Client was not added.");
136           break;
137         }
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())
146             //  ;
147             int newpos = -1;
148             if (cl != null) {
149               for (int chi = 0, che = cl.length; chi < che; chi++) {
150                 if (ch.equals(cl[chi]))
151                   newpos = chi + 1;
152               }
153             }
154             if (newpos == -1) {
155               // add self again to cleared list.
156               newpos = cfhand.addClient(ch);
157               mon.setState();
158               if (newpos == 0) {
159                 System.err
160                     .println("Monitor client could not be re-added to list.");
161                 break;
162               }
163             }
164             if (newpos != clpos) {
165               System.out.println("Monitor client moved from " + clpos + " to "
166                   + newpos);
167               clpos = newpos;
168             }
169           }
170         }
171         break;
172       default:
173         if (com == -1) {
174           System.err
175               .println("Unknown command : " + args[argc++] + "*Ignored!*");
176         } else
177           System.err.println("Command " + args[argc++]
178               + " *Ignored!* - its not implemented.");
179       }
180       
181       for (int j = 0; j < 900000; j++) {
182         Integer i = Integer.getInteger("1");
183         Integer q = i;
184       }
185     }
186
187   }
188
189   /*      Iterator coms = commands.iterator();
190    int com=-1;
191    while ((coms!=null) && coms.hasNext()) {
192    com++;
193    if (args[argc].toLowerCase().equals((String) coms.next())) {
194    System.out.println("Doing "+args[argc]);
195    ClientHandle ch;
196    argc++;
197    switch (com) {
198    case 0:
199    // Add
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]));
202    argc+=2;
203    if (pos!=0)
204    System.out.println("Client added at "+pos+" as urn:"+ch.getClientUrn());
205    else
206    System.out.println("Client was not added.");
207    break;
208    case 1:
209    // remove
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]);
213    argc+=3;
214    cfhand.removeClient(ch, null);
215    System.out.println("Client removed (apparently)");
216    break;
217    case 2:
218    // list
219    ClientHandle[] chlist = cfhand.retrieveClientList();
220    if (chlist!=null) {
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()+")");
223    }
224    } else {
225    System.out.println("Client list is empty.");
226    }
227    break;
228    case 3:
229    // clear
230    cfhand = null;
231    cf.delete();
232    try {
233    cf.createNewFile();
234    cfhand = new ClientsFile(cf);
235    }
236    catch (Exception e) {
237    System.err.println("Failed on new empty clientfile creation!");
238    e.printStackTrace(System.err);
239    }
240    break;
241    case 4:
242    // watch
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();
247    if (chlock!=null) {
248    ClientHandle[] cl = cfhand.retrieveClientList(chlock);
249    System.out.println("-- Watching "+cf.getName());
250    //while (w.hasChanged())
251    //  ;
252    if (cl!=null) {
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()+")");
255    }
256    } else {
257    System.out.println("Client list is empty.");
258    }
259    }
260    
261    }
262    break;
263    case 5:
264    // monitor
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]));
267    argc+=2;
268    if (clpos!=0)
269    System.out.println("Monitor Client added at "+clpos+" as urn:"+ch.getClientUrn());
270    else {
271    System.err.println("Monitor Client was not added.");
272    break;
273    }
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();
278    if (chlock!=null) {
279    ClientHandle[] cl = cfhand.retrieveClientList(chlock);
280    System.out.println("-- Monitor "+cf.getName());
281    //while (w.hasChanged())
282    //  ;
283    int newpos=-1;
284    if (cl!=null) {
285    for (int chi=0,che=cl.length; chi<che; chi++) {
286    if (ch.equals(cl[chi]))
287    newpos=chi+1;
288    }
289    }
290    if (newpos==-1) {
291    // add self again to cleared list.
292    newpos=cfhand.addClient(ch);
293    mon.setState();
294    if (newpos==0) {
295    System.err.println("Monitor client could not be re-added to list.");
296    break;
297    }
298    }
299    if (newpos!=clpos) {
300    System.out.println("Monitor client moved from "+clpos+" to "+newpos);
301    clpos=newpos;
302    }
303    }
304    }
305    
306    }
307    coms = null;
308    }
309    } */
310
311 }