2 * This file is part of the Vamsas Client version 0.1.
\r
3 * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite,
\r
4 * Andrew Waterhouse and Dominik Lindner.
\r
6 * Earlier versions have also been incorporated into Jalview version 2.4
\r
7 * since 2008, and TOPALi version 2 since 2007.
\r
9 * The Vamsas Client is free software: you can redistribute it and/or modify
\r
10 * it under the terms of the GNU Lesser General Public License as published by
\r
11 * the Free Software Foundation, either version 3 of the License, or
\r
12 * (at your option) any later version.
\r
14 * The Vamsas Client is distributed in the hope that it will be useful,
\r
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
17 * GNU Lesser General Public License for more details.
\r
19 * You should have received a copy of the GNU Lesser General Public License
\r
20 * along with the Vamsas Client. If not, see <http://www.gnu.org/licenses/>.
\r
22 package uk.ac.vamsas.test.simpleclient;
\r
24 import java.io.File;
\r
25 import java.util.Iterator;
\r
26 import java.util.Vector;
\r
28 import uk.ac.vamsas.client.ClientHandle;
\r
29 import uk.ac.vamsas.client.simpleclient.ClientsFile;
\r
30 import uk.ac.vamsas.client.simpleclient.FileWatcher;
\r
31 import uk.ac.vamsas.client.simpleclient.Lock;
\r
33 public class ClientsFileTest {
\r
34 private static CommandProcessor cproc;
\r
36 private static Vector commands;
\r
38 cproc = new CommandProcessor();
\r
39 ClientsFileTest.commands = new Vector();
\r
40 ClientsFileTest.commands.add(new String("add"));
\r
41 cproc.addCommand("add", 2, "for the Client's 'Name' and 'Version'");
\r
42 ClientsFileTest.commands.add(new String("remove"));
\r
43 cproc.addCommand("remove", 3, "for the Client's 'Name', Version and URN");
\r
44 ClientsFileTest.commands.add(new String("list"));
\r
45 cproc.addCommand("list", 0, "no args needed");
\r
46 ClientsFileTest.commands.add(new String("clear"));
\r
47 cproc.addCommand("clear", 0, "no args needed");
\r
48 ClientsFileTest.commands.add(new String("watch"));
\r
49 cproc.addCommand("watch", 0, "no args needed");
\r
50 ClientsFileTest.commands.add(new String("monitor"));
\r
51 cproc.addCommand("monitor", 2, "for the Client's 'Name' and 'Version'");
\r
54 private static void complainArgs(int argl, int argpos, String cmd,
\r
55 int argneed, String msg) {
\r
56 if (argl - argpos < argneed)
\r
57 throw new Error(cmd + " needs " + argneed + " arguments : " + msg);
\r
60 public static void main(String[] args) {
\r
61 java.io.File cf = new java.io.File(args[0]);
\r
62 System.out.println("Connecting to clientFile " + args[0]);
\r
65 cfhand = new ClientsFile(cf);
\r
66 } catch (Exception e) {
\r
67 e.printStackTrace(System.err);
\r
71 while (argc < args.length) {
\r
72 // vars needed for operations
\r
74 int com = cproc.getCommand(args, argc);
\r
79 int pos = cfhand.addClient(ch = new ClientHandle(args[argc],
\r
83 System.out.println("Client added at " + pos + " as urn:"
\r
84 + ch.getClientUrn());
\r
86 System.out.println("Client was not added.");
\r
90 ch = new ClientHandle(args[argc], args[argc + 1]);
\r
91 ch.setClientUrn(args[argc + 2]);
\r
93 cfhand.removeClient(ch, null);
\r
94 System.out.println("Client removed (apparently)");
\r
98 ClientHandle[] chlist = cfhand.retrieveClientList();
\r
99 if (chlist != null) {
\r
100 for (int chi = 0, che = chlist.length; chi < che; chi++) {
\r
101 System.out.println("Client " + chi + " ("
\r
102 + chlist[chi].getClientName() + " " + chlist[chi].getVersion()
\r
103 + " " + chlist[chi].getClientUrn() + ")");
\r
106 System.out.println("Client list is empty.");
\r
111 // cfhand.get = null;
\r
115 cfhand.clearList();
\r
117 } catch (Exception e) {
\r
118 System.err.println("Failed on new empty clientfile creation!");
\r
119 e.printStackTrace(System.err);
\r
124 FileWatcher w = new FileWatcher(cf);
\r
125 while (cf.exists()) {
\r
126 // get watcher's lock to ensure state change is fixed for retrieval
\r
127 Lock chlock = w.getChangedState();
\r
128 if (chlock != null) {
\r
129 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
\r
130 System.out.println("-- Watching " + cf.getName());
\r
131 // while (w.hasChanged())
\r
134 for (int chi = 0, che = cl.length; chi < che; chi++) {
\r
135 System.out.println("Client " + chi + " ("
\r
136 + cl[chi].getClientName() + " " + cl[chi].getVersion()
\r
137 + " " + cl[chi].getClientUrn() + ")");
\r
140 System.out.println("Client list is empty.");
\r
148 int clpos = cfhand.addClient(ch = new ClientHandle(args[argc],
\r
152 System.out.println("Monitor Client added at " + clpos + " as urn:"
\r
153 + ch.getClientUrn());
\r
155 System.err.println("Monitor Client was not added.");
\r
158 FileWatcher mon = new FileWatcher(cf);
\r
159 while (cf.exists()) {
\r
160 // get watcher's lock to ensure state change is fixed for retrieval
\r
161 Lock chlock = mon.getChangedState();
\r
162 if (chlock != null) {
\r
163 ClientHandle[] cl = cfhand.retrieveClientList(chlock);
\r
164 System.out.println("-- Monitor " + cf.getName());
\r
165 // while (w.hasChanged())
\r
169 for (int chi = 0, che = cl.length; chi < che; chi++) {
\r
170 if (ch.equals(cl[chi]))
\r
174 if (newpos == -1) {
\r
175 // add self again to cleared list.
\r
176 newpos = cfhand.addClient(ch);
\r
180 .println("Monitor client could not be re-added to list.");
\r
184 if (newpos != clpos) {
\r
185 System.out.println("Monitor client moved from " + clpos + " to "
\r
195 .println("Unknown command : " + args[argc++] + "*Ignored!*");
\r
197 System.err.println("Command " + args[argc++]
\r
198 + " *Ignored!* - its not implemented.");
\r
201 for (int j = 0; j < 900000; j++) {
\r
202 Integer i = Integer.getInteger("1");
\r
210 * Iterator coms = commands.iterator(); int com=-1; while ((coms!=null) &&
\r
211 * coms.hasNext()) { com++; if (args[argc].toLowerCase().equals((String)
\r
212 * coms.next())) { System.out.println("Doing "+args[argc]); ClientHandle ch;
\r
213 * argc++; switch (com) { case 0: // Add
\r
214 * ClientsFileTest.complainArgs(args.length, argc, "add", 2,
\r
215 * "for the Client's 'Name' and 'Version'"); int pos = cfhand.addClient(ch=new
\r
216 * ClientHandle(args[argc],args[argc+1])); argc+=2; if (pos!=0)
\r
217 * System.out.println("Client added at "+pos+" as urn:"+ch.getClientUrn());
\r
218 * else System.out.println("Client was not added."); break; case 1: // remove
\r
219 * ClientsFileTest.complainArgs(args.length, argc, "remove", 3,
\r
220 * "for the Client's 'Name', Version and URN"); ch=new
\r
221 * ClientHandle(args[argc], args[argc+1]); ch.setClientUrn(args[argc+2]);
\r
222 * argc+=3; cfhand.removeClient(ch, null);
\r
223 * System.out.println("Client removed (apparently)"); break; case 2: // list
\r
224 * ClientHandle[] chlist = cfhand.retrieveClientList(); if (chlist!=null) {
\r
225 * for (int chi=0,che=chlist.length; chi<che; chi++) {
\r
226 * System.out.println("Client "
\r
227 * +chi+" ("+chlist[chi].getClientName()+" "+chlist
\r
228 * [chi].getVersion()+" "+chlist[chi].getClientUrn()+")"); } } else {
\r
229 * System.out.println("Client list is empty."); } break; case 3: // clear
\r
230 * cfhand = null; cf.delete(); try { cf.createNewFile(); cfhand = new
\r
231 * ClientsFile(cf); } catch (Exception e) {
\r
232 * System.err.println("Failed on new empty clientfile creation!");
\r
233 * e.printStackTrace(System.err); } break; case 4: // watch FileWatcher w=new
\r
234 * FileWatcher(cf); while (cf.exists()) { // get watcher's lock to ensure
\r
235 * state change is fixed for retrieval Lock chlock=w.getChangedState(); if
\r
236 * (chlock!=null) { ClientHandle[] cl = cfhand.retrieveClientList(chlock);
\r
237 * System.out.println("-- Watching "+cf.getName()); //while (w.hasChanged())
\r
238 * // ; if (cl!=null) { for (int chi=0,che=cl.length; chi<che; chi++) {
\r
240 * out.println("Client "+chi+" ("+cl[chi].getClientName()+" "+cl[chi].getVersion
\r
241 * ()+" "+cl[chi].getClientUrn()+")"); } } else {
\r
242 * System.out.println("Client list is empty."); } }
\r
244 * } break; case 5: // monitor ClientsFileTest.complainArgs(args.length, argc,
\r
245 * "monitor", 2, "for the Client's 'Name' and 'Version'"); int clpos =
\r
246 * cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1])); argc+=2; if
\r
248 * System.out.println("Monitor Client added at "+clpos+" as urn:"+ch
\r
249 * .getClientUrn()); else {
\r
250 * System.err.println("Monitor Client was not added."); break; } FileWatcher
\r
251 * mon=new FileWatcher(cf); while (cf.exists()) { // get watcher's lock to
\r
252 * ensure state change is fixed for retrieval Lock
\r
253 * chlock=mon.getChangedState(); if (chlock!=null) { ClientHandle[] cl =
\r
254 * cfhand.retrieveClientList(chlock);
\r
255 * System.out.println("-- Monitor "+cf.getName()); //while (w.hasChanged()) //
\r
256 * ; int newpos=-1; if (cl!=null) { for (int chi=0,che=cl.length; chi<che;
\r
257 * chi++) { if (ch.equals(cl[chi])) newpos=chi+1; } } if (newpos==-1) { // add
\r
258 * self again to cleared list. newpos=cfhand.addClient(ch); mon.setState(); if
\r
260 * System.err.println("Monitor client could not be re-added to list."); break;
\r
261 * } } if (newpos!=clpos) {
\r
262 * System.out.println("Monitor client moved from "+clpos+" to "+newpos);
\r
263 * clpos=newpos; } } }
\r
265 * } coms = null; } }
\r