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