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