6ccd7364633add2a6530a5d145c91ddaaedbf804
[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     ClientsFileTest.commands.add(new String("monitor"));    
22   }
23   private static void complainArgs(int argl, int argpos, String cmd, int argneed, String msg) {
24     if (argl-argpos<argneed)
25       throw new Error(cmd+" needs "+argneed+" arguments : "+msg);
26   }
27   public static void main(String[] args) {
28     java.io.File cf = new java.io.File(args[0]);
29     System.out.println("Connecting to clientFile "+args[0]);
30     ClientsFile cfhand;
31     try {
32       cfhand = new ClientsFile(cf);
33     }
34     catch (Exception e) {
35       e.printStackTrace(System.err);
36       return;
37     }
38     int argc=1;
39     while (argc<args.length) {
40       Iterator coms = commands.iterator();
41       int com=-1;
42       while ((coms!=null) && coms.hasNext()) {
43         com++;
44         if (args[argc].toLowerCase().equals((String) coms.next())) {
45           System.out.println("Doing "+args[argc]);
46           ClientHandle ch;
47           argc++;
48           switch (com) {
49           case 0:
50             // Add
51             ClientsFileTest.complainArgs(args.length, argc, "add", 2, "for the Client's 'Name' and 'Version'");
52             int pos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
53             argc+=2;
54             if (pos!=0)
55               System.out.println("Client added at "+pos+" as urn:"+ch.getClientUrn());
56             else
57               System.out.println("Client was not added.");
58             break;
59           case 1:
60             // remove
61             ClientsFileTest.complainArgs(args.length, argc, "remove", 3, "for the Client's 'Name', Version and URN");
62             ch=new ClientHandle(args[argc], args[argc+1]);
63             ch.setClientUrn(args[argc+2]);
64             argc+=3;
65             cfhand.removeClient(ch, null);
66             System.out.println("Client removed (apparently)");
67             break;
68           case 2:
69             // list
70             ClientHandle[] chlist = cfhand.retrieveClientList();
71             if (chlist!=null) {
72               for (int chi=0,che=chlist.length; chi<che; chi++) {
73                 System.out.println("Client "+chi+" ("+chlist[chi].getClientName()+" "+chlist[chi].getVersion()+" "+chlist[chi].getClientUrn()+")");
74               }
75             } else {
76               System.out.println("Client list is empty.");
77             }
78             break;
79           case 3:
80             // clear
81             cfhand = null;
82             cf.delete();
83             try {
84               cf.createNewFile();
85               cfhand = new ClientsFile(cf);
86             }
87             catch (Exception e) {
88               System.err.println("Failed on new empty clientfile creation!");
89               e.printStackTrace(System.err);
90             }
91             break;
92           case 4:
93             // watch
94             FileWatcher w=new FileWatcher(cf);
95             while (cf.exists()) {
96               // get watcher's lock to ensure state change is fixed for retrieval
97               Lock chlock=w.getChangedState();
98               if (chlock!=null) {
99                 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
100                 System.out.println("-- Watching "+cf.getName());
101                 //while (w.hasChanged())
102                 //  ;
103                 if (cl!=null) {
104                   for (int chi=0,che=cl.length; chi<che; chi++) {
105                     System.out.println("Client "+chi+" ("+cl[chi].getClientName()+" "+cl[chi].getVersion()+" "+cl[chi].getClientUrn()+")");
106                   }
107                 } else {
108                   System.out.println("Client list is empty.");
109                 }
110               }
111               
112             }
113             break;
114           case 5:
115             // monitor
116             ClientsFileTest.complainArgs(args.length, argc, "monitor", 2, "for the Client's 'Name' and 'Version'");
117             int clpos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
118             argc+=2;
119             if (clpos!=0)
120               System.out.println("Monitor Client added at "+clpos+" as urn:"+ch.getClientUrn());
121             else {
122               System.err.println("Monitor Client was not added.");
123               break;
124             }
125             FileWatcher mon=new FileWatcher(cf);
126             while (cf.exists()) {
127               // get watcher's lock to ensure state change is fixed for retrieval
128               Lock chlock=mon.getChangedState();
129               if (chlock!=null) {
130                 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
131                 System.out.println("-- Monitor "+cf.getName());
132                 //while (w.hasChanged())
133                 //  ;
134                 int newpos=-1;
135                 if (cl!=null) {
136                   for (int chi=0,che=cl.length; chi<che; chi++) {
137                     if (ch.equals(cl[chi]))
138                       newpos=chi+1;
139                   }
140                 }
141                 if (newpos==-1) {
142                   // add self again to cleared list.
143                   newpos=cfhand.addClient(ch);
144                   mon.setState();
145                   if (newpos==0) {
146                     System.err.println("Monitor client could not be re-added to list.");
147                     break;
148                   }
149                 }
150                 if (newpos!=clpos) {
151                   System.out.println("Monitor client moved from "+clpos+" to "+newpos);
152                   clpos=newpos;
153                 }
154               }
155             }
156               
157           }
158           coms = null;
159         }
160       }
161       if (coms!=null) {
162         System.err.println("Unknown command : "+args[argc++] + "*Ignored!*");
163       };
164       for (int j=0; j<900000; j++) {
165         Integer i=Integer.getInteger("1");
166         Integer q=i;
167       }
168     }
169   }
170
171 }