9ab68aca183173c28ca95a2285cddbf37b89de51
[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 = null;
91         cf.delete();
92         try {
93           cf.createNewFile();
94           cfhand = new ClientsFile(cf);
95         } catch (Exception e) {
96           System.err.println("Failed on new empty clientfile creation!");
97           e.printStackTrace(System.err);
98         }
99         break;
100       case 4:
101         // watch
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())
110             //  ;
111             if (cl != null) {
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() + ")");
116               }
117             } else {
118               System.out.println("Client list is empty.");
119             }
120           }
121
122         }
123         break;
124       case 5:
125         // monitor
126         int clpos = cfhand.addClient(ch = new ClientHandle(args[argc],
127             args[argc + 1]));
128         argc += 2;
129         if (clpos != 0)
130           System.out.println("Monitor Client added at " + clpos + " as urn:"
131               + ch.getClientUrn());
132         else {
133           System.err.println("Monitor Client was not added.");
134           break;
135         }
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())
144             //  ;
145             int newpos = -1;
146             if (cl != null) {
147               for (int chi = 0, che = cl.length; chi < che; chi++) {
148                 if (ch.equals(cl[chi]))
149                   newpos = chi + 1;
150               }
151             }
152             if (newpos == -1) {
153               // add self again to cleared list.
154               newpos = cfhand.addClient(ch);
155               mon.setState();
156               if (newpos == 0) {
157                 System.err
158                     .println("Monitor client could not be re-added to list.");
159                 break;
160               }
161             }
162             if (newpos != clpos) {
163               System.out.println("Monitor client moved from " + clpos + " to "
164                   + newpos);
165               clpos = newpos;
166             }
167           }
168         }
169         break;
170       default:
171         if (com == -1) {
172           System.err
173               .println("Unknown command : " + args[argc++] + "*Ignored!*");
174         } else
175           System.err.println("Command " + args[argc++]
176               + " *Ignored!* - its not implemented.");
177       }
178       
179       for (int j = 0; j < 900000; j++) {
180         Integer i = Integer.getInteger("1");
181         Integer q = i;
182       }
183     }
184
185   }
186
187   /*      Iterator coms = commands.iterator();
188    int com=-1;
189    while ((coms!=null) && coms.hasNext()) {
190    com++;
191    if (args[argc].toLowerCase().equals((String) coms.next())) {
192    System.out.println("Doing "+args[argc]);
193    ClientHandle ch;
194    argc++;
195    switch (com) {
196    case 0:
197    // Add
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]));
200    argc+=2;
201    if (pos!=0)
202    System.out.println("Client added at "+pos+" as urn:"+ch.getClientUrn());
203    else
204    System.out.println("Client was not added.");
205    break;
206    case 1:
207    // remove
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]);
211    argc+=3;
212    cfhand.removeClient(ch, null);
213    System.out.println("Client removed (apparently)");
214    break;
215    case 2:
216    // list
217    ClientHandle[] chlist = cfhand.retrieveClientList();
218    if (chlist!=null) {
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()+")");
221    }
222    } else {
223    System.out.println("Client list is empty.");
224    }
225    break;
226    case 3:
227    // clear
228    cfhand = null;
229    cf.delete();
230    try {
231    cf.createNewFile();
232    cfhand = new ClientsFile(cf);
233    }
234    catch (Exception e) {
235    System.err.println("Failed on new empty clientfile creation!");
236    e.printStackTrace(System.err);
237    }
238    break;
239    case 4:
240    // watch
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();
245    if (chlock!=null) {
246    ClientHandle[] cl = cfhand.retrieveClientList(chlock);
247    System.out.println("-- Watching "+cf.getName());
248    //while (w.hasChanged())
249    //  ;
250    if (cl!=null) {
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()+")");
253    }
254    } else {
255    System.out.println("Client list is empty.");
256    }
257    }
258    
259    }
260    break;
261    case 5:
262    // monitor
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]));
265    argc+=2;
266    if (clpos!=0)
267    System.out.println("Monitor Client added at "+clpos+" as urn:"+ch.getClientUrn());
268    else {
269    System.err.println("Monitor Client was not added.");
270    break;
271    }
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();
276    if (chlock!=null) {
277    ClientHandle[] cl = cfhand.retrieveClientList(chlock);
278    System.out.println("-- Monitor "+cf.getName());
279    //while (w.hasChanged())
280    //  ;
281    int newpos=-1;
282    if (cl!=null) {
283    for (int chi=0,che=cl.length; chi<che; chi++) {
284    if (ch.equals(cl[chi]))
285    newpos=chi+1;
286    }
287    }
288    if (newpos==-1) {
289    // add self again to cleared list.
290    newpos=cfhand.addClient(ch);
291    mon.setState();
292    if (newpos==0) {
293    System.err.println("Monitor client could not be re-added to list.");
294    break;
295    }
296    }
297    if (newpos!=clpos) {
298    System.out.println("Monitor client moved from "+clpos+" to "+newpos);
299    clpos=newpos;
300    }
301    }
302    }
303    
304    }
305    coms = null;
306    }
307    } */
308
309 }