-package uk.ac.vamsas.test.simpleclient;
-
-import java.io.File;
-import java.util.Iterator;
-import java.util.Vector;
-
-
-import uk.ac.vamsas.client.ClientHandle;
-import uk.ac.vamsas.client.simpleclient.ClientsFile;
-import uk.ac.vamsas.client.simpleclient.FileWatcher;
-import uk.ac.vamsas.client.simpleclient.Lock;
-
-public class ClientsFileTest {
- private static CommandProcessor cproc;
-
- private static Vector commands;
- static {
- cproc = new CommandProcessor();
- ClientsFileTest.commands = new Vector();
- ClientsFileTest.commands.add(new String("add"));
- cproc.addCommand("add", 2, "for the Client's 'Name' and 'Version'");
- ClientsFileTest.commands.add(new String("remove"));
- cproc.addCommand("remove", 3, "for the Client's 'Name', Version and URN");
- ClientsFileTest.commands.add(new String("list"));
- cproc.addCommand("list", 0, "no args needed");
- ClientsFileTest.commands.add(new String("clear"));
- cproc.addCommand("clear", 0, "no args needed");
- ClientsFileTest.commands.add(new String("watch"));
- cproc.addCommand("watch", 0, "no args needed");
- ClientsFileTest.commands.add(new String("monitor"));
- cproc.addCommand("monitor", 2, "for the Client's 'Name' and 'Version'");
- }
-
- private static void complainArgs(int argl, int argpos, String cmd,
- int argneed, String msg) {
- if (argl - argpos < argneed)
- throw new Error(cmd + " needs " + argneed + " arguments : " + msg);
- }
-
- public static void main(String[] args) {
- java.io.File cf = new java.io.File(args[0]);
- System.out.println("Connecting to clientFile " + args[0]);
- ClientsFile cfhand;
- try {
- cfhand = new ClientsFile(cf);
- } catch (Exception e) {
- e.printStackTrace(System.err);
- return;
- }
- int argc = 1;
- while (argc < args.length) {
- // vars needed for operations
- ClientHandle ch;
- int com = cproc.getCommand(args, argc);
- argc++;
- switch (com) {
- case 0:
- // Add
- int pos = cfhand.addClient(ch = new ClientHandle(args[argc],
- args[argc + 1]));
- argc += 2;
- if (pos != 0)
- System.out.println("Client added at " + pos + " as urn:"
- + ch.getClientUrn());
- else
- System.out.println("Client was not added.");
- break;
- case 1:
- // remove
- ch = new ClientHandle(args[argc], args[argc + 1]);
- ch.setClientUrn(args[argc + 2]);
- argc += 3;
- cfhand.removeClient(ch, null);
- System.out.println("Client removed (apparently)");
- break;
- case 2:
- // list
- ClientHandle[] chlist = cfhand.retrieveClientList();
- if (chlist != null) {
- for (int chi = 0, che = chlist.length; chi < che; chi++) {
- System.out.println("Client " + chi + " ("
- + chlist[chi].getClientName() + " " + chlist[chi].getVersion()
- + " " + chlist[chi].getClientUrn() + ")");
- }
- } else {
- System.out.println("Client list is empty.");
- }
- break;
- case 3:
- // clear
- //cfhand.get = null;
- //cf.delete();
- try {
-
- cfhand.clearList();
-
- } catch (Exception e) {
- System.err.println("Failed on new empty clientfile creation!");
- e.printStackTrace(System.err);
- }
- break;
- case 4:
- // watch
- FileWatcher w = new FileWatcher(cf);
- while (cf.exists()) {
- // get watcher's lock to ensure state change is fixed for retrieval
- Lock chlock = w.getChangedState();
- if (chlock != null) {
- ClientHandle[] cl = cfhand.retrieveClientList(chlock);
- System.out.println("-- Watching " + cf.getName());
- //while (w.hasChanged())
- // ;
- if (cl != null) {
- for (int chi = 0, che = cl.length; chi < che; chi++) {
- System.out.println("Client " + chi + " ("
- + cl[chi].getClientName() + " " + cl[chi].getVersion()
- + " " + cl[chi].getClientUrn() + ")");
- }
- } else {
- System.out.println("Client list is empty.");
- }
- }
-
- }
- break;
- case 5:
- // monitor
- int clpos = cfhand.addClient(ch = new ClientHandle(args[argc],
- args[argc + 1]));
- argc += 2;
- if (clpos != 0)
- System.out.println("Monitor Client added at " + clpos + " as urn:"
- + ch.getClientUrn());
- else {
- System.err.println("Monitor Client was not added.");
- break;
- }
- FileWatcher mon = new FileWatcher(cf);
- while (cf.exists()) {
- // get watcher's lock to ensure state change is fixed for retrieval
- Lock chlock = mon.getChangedState();
- if (chlock != null) {
- ClientHandle[] cl = cfhand.retrieveClientList(chlock);
- System.out.println("-- Monitor " + cf.getName());
- //while (w.hasChanged())
- // ;
- int newpos = -1;
- if (cl != null) {
- for (int chi = 0, che = cl.length; chi < che; chi++) {
- if (ch.equals(cl[chi]))
- newpos = chi + 1;
- }
- }
- if (newpos == -1) {
- // add self again to cleared list.
- newpos = cfhand.addClient(ch);
- mon.setState();
- if (newpos == 0) {
- System.err
- .println("Monitor client could not be re-added to list.");
- break;
- }
- }
- if (newpos != clpos) {
- System.out.println("Monitor client moved from " + clpos + " to "
- + newpos);
- clpos = newpos;
- }
- }
- }
- break;
- default:
- if (com == -1) {
- System.err
- .println("Unknown command : " + args[argc++] + "*Ignored!*");
- } else
- System.err.println("Command " + args[argc++]
- + " *Ignored!* - its not implemented.");
- }
-
- for (int j = 0; j < 900000; j++) {
- Integer i = Integer.getInteger("1");
- Integer q = i;
- }
- }
-
- }
-
- /* Iterator coms = commands.iterator();
- int com=-1;
- while ((coms!=null) && coms.hasNext()) {
- com++;
- if (args[argc].toLowerCase().equals((String) coms.next())) {
- System.out.println("Doing "+args[argc]);
- ClientHandle ch;
- argc++;
- switch (com) {
- case 0:
- // Add
- ClientsFileTest.complainArgs(args.length, argc, "add", 2, "for the Client's 'Name' and 'Version'");
- int pos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
- argc+=2;
- if (pos!=0)
- System.out.println("Client added at "+pos+" as urn:"+ch.getClientUrn());
- else
- System.out.println("Client was not added.");
- break;
- case 1:
- // remove
- ClientsFileTest.complainArgs(args.length, argc, "remove", 3, "for the Client's 'Name', Version and URN");
- ch=new ClientHandle(args[argc], args[argc+1]);
- ch.setClientUrn(args[argc+2]);
- argc+=3;
- cfhand.removeClient(ch, null);
- System.out.println("Client removed (apparently)");
- break;
- case 2:
- // list
- ClientHandle[] chlist = cfhand.retrieveClientList();
- if (chlist!=null) {
- for (int chi=0,che=chlist.length; chi<che; chi++) {
- System.out.println("Client "+chi+" ("+chlist[chi].getClientName()+" "+chlist[chi].getVersion()+" "+chlist[chi].getClientUrn()+")");
- }
- } else {
- System.out.println("Client list is empty.");
- }
- break;
- case 3:
- // clear
- cfhand = null;
- cf.delete();
- try {
- cf.createNewFile();
- cfhand = new ClientsFile(cf);
- }
- catch (Exception e) {
- System.err.println("Failed on new empty clientfile creation!");
- e.printStackTrace(System.err);
- }
- break;
- case 4:
- // watch
- FileWatcher w=new FileWatcher(cf);
- while (cf.exists()) {
- // get watcher's lock to ensure state change is fixed for retrieval
- Lock chlock=w.getChangedState();
- if (chlock!=null) {
- ClientHandle[] cl = cfhand.retrieveClientList(chlock);
- System.out.println("-- Watching "+cf.getName());
- //while (w.hasChanged())
- // ;
- if (cl!=null) {
- for (int chi=0,che=cl.length; chi<che; chi++) {
- System.out.println("Client "+chi+" ("+cl[chi].getClientName()+" "+cl[chi].getVersion()+" "+cl[chi].getClientUrn()+")");
- }
- } else {
- System.out.println("Client list is empty.");
- }
- }
-
- }
- break;
- case 5:
- // monitor
- ClientsFileTest.complainArgs(args.length, argc, "monitor", 2, "for the Client's 'Name' and 'Version'");
- int clpos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
- argc+=2;
- if (clpos!=0)
- System.out.println("Monitor Client added at "+clpos+" as urn:"+ch.getClientUrn());
- else {
- System.err.println("Monitor Client was not added.");
- break;
- }
- FileWatcher mon=new FileWatcher(cf);
- while (cf.exists()) {
- // get watcher's lock to ensure state change is fixed for retrieval
- Lock chlock=mon.getChangedState();
- if (chlock!=null) {
- ClientHandle[] cl = cfhand.retrieveClientList(chlock);
- System.out.println("-- Monitor "+cf.getName());
- //while (w.hasChanged())
- // ;
- int newpos=-1;
- if (cl!=null) {
- for (int chi=0,che=cl.length; chi<che; chi++) {
- if (ch.equals(cl[chi]))
- newpos=chi+1;
- }
- }
- if (newpos==-1) {
- // add self again to cleared list.
- newpos=cfhand.addClient(ch);
- mon.setState();
- if (newpos==0) {
- System.err.println("Monitor client could not be re-added to list.");
- break;
- }
- }
- if (newpos!=clpos) {
- System.out.println("Monitor client moved from "+clpos+" to "+newpos);
- clpos=newpos;
- }
- }
- }
-
- }
- coms = null;
- }
- } */
-
-}
+/*\r
+ * This file is part of the Vamsas Client version 0.1. \r
+ * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
+ * Andrew Waterhouse and Dominik Lindner.\r
+ * \r
+ * Earlier versions have also been incorporated into Jalview version 2.4 \r
+ * since 2008, and TOPALi version 2 since 2007.\r
+ * \r
+ * The Vamsas Client is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU Lesser General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ * \r
+ * The Vamsas Client is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public License\r
+ * along with the Vamsas Client. If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+package uk.ac.vamsas.test.simpleclient;\r
+\r
+import java.io.File;\r
+import java.util.Iterator;\r
+import java.util.Vector;\r
+\r
+import uk.ac.vamsas.client.ClientHandle;\r
+import uk.ac.vamsas.client.simpleclient.ClientsFile;\r
+import uk.ac.vamsas.client.simpleclient.FileWatcher;\r
+import uk.ac.vamsas.client.simpleclient.Lock;\r
+\r
+public class ClientsFileTest {\r
+ private static CommandProcessor cproc;\r
+\r
+ private static Vector commands;\r
+ static {\r
+ cproc = new CommandProcessor();\r
+ ClientsFileTest.commands = new Vector();\r
+ ClientsFileTest.commands.add(new String("add"));\r
+ cproc.addCommand("add", 2, "for the Client's 'Name' and 'Version'");\r
+ ClientsFileTest.commands.add(new String("remove"));\r
+ cproc.addCommand("remove", 3, "for the Client's 'Name', Version and URN");\r
+ ClientsFileTest.commands.add(new String("list"));\r
+ cproc.addCommand("list", 0, "no args needed");\r
+ ClientsFileTest.commands.add(new String("clear"));\r
+ cproc.addCommand("clear", 0, "no args needed");\r
+ ClientsFileTest.commands.add(new String("watch"));\r
+ cproc.addCommand("watch", 0, "no args needed");\r
+ ClientsFileTest.commands.add(new String("monitor"));\r
+ cproc.addCommand("monitor", 2, "for the Client's 'Name' and 'Version'");\r
+ }\r
+\r
+ private static void complainArgs(int argl, int argpos, String cmd,\r
+ int argneed, String msg) {\r
+ if (argl - argpos < argneed)\r
+ throw new Error(cmd + " needs " + argneed + " arguments : " + msg);\r
+ }\r
+\r
+ public static void main(String[] args) {\r
+ java.io.File cf = new java.io.File(args[0]);\r
+ System.out.println("Connecting to clientFile " + args[0]);\r
+ ClientsFile cfhand;\r
+ try {\r
+ cfhand = new ClientsFile(cf);\r
+ } catch (Exception e) {\r
+ e.printStackTrace(System.err);\r
+ return;\r
+ }\r
+ int argc = 1;\r
+ while (argc < args.length) {\r
+ // vars needed for operations\r
+ ClientHandle ch;\r
+ int com = cproc.getCommand(args, argc);\r
+ argc++;\r
+ switch (com) {\r
+ case 0:\r
+ // Add\r
+ int pos = cfhand.addClient(ch = new ClientHandle(args[argc],\r
+ args[argc + 1]));\r
+ argc += 2;\r
+ if (pos != 0)\r
+ System.out.println("Client added at " + pos + " as urn:"\r
+ + ch.getClientUrn());\r
+ else\r
+ System.out.println("Client was not added.");\r
+ break;\r
+ case 1:\r
+ // remove\r
+ ch = new ClientHandle(args[argc], args[argc + 1]);\r
+ ch.setClientUrn(args[argc + 2]);\r
+ argc += 3;\r
+ cfhand.removeClient(ch, null);\r
+ System.out.println("Client removed (apparently)");\r
+ break;\r
+ case 2:\r
+ // list\r
+ ClientHandle[] chlist = cfhand.retrieveClientList();\r
+ if (chlist != null) {\r
+ for (int chi = 0, che = chlist.length; chi < che; chi++) {\r
+ System.out.println("Client " + chi + " ("\r
+ + chlist[chi].getClientName() + " " + chlist[chi].getVersion()\r
+ + " " + chlist[chi].getClientUrn() + ")");\r
+ }\r
+ } else {\r
+ System.out.println("Client list is empty.");\r
+ }\r
+ break;\r
+ case 3:\r
+ // clear\r
+ // cfhand.get = null;\r
+ // cf.delete();\r
+ try {\r
+\r
+ cfhand.clearList();\r
+\r
+ } catch (Exception e) {\r
+ System.err.println("Failed on new empty clientfile creation!");\r
+ e.printStackTrace(System.err);\r
+ }\r
+ break;\r
+ case 4:\r
+ // watch\r
+ FileWatcher w = new FileWatcher(cf);\r
+ while (cf.exists()) {\r
+ // get watcher's lock to ensure state change is fixed for retrieval\r
+ Lock chlock = w.getChangedState();\r
+ if (chlock != null) {\r
+ ClientHandle[] cl = cfhand.retrieveClientList(chlock);\r
+ System.out.println("-- Watching " + cf.getName());\r
+ // while (w.hasChanged())\r
+ // ;\r
+ if (cl != null) {\r
+ for (int chi = 0, che = cl.length; chi < che; chi++) {\r
+ System.out.println("Client " + chi + " ("\r
+ + cl[chi].getClientName() + " " + cl[chi].getVersion()\r
+ + " " + cl[chi].getClientUrn() + ")");\r
+ }\r
+ } else {\r
+ System.out.println("Client list is empty.");\r
+ }\r
+ }\r
+\r
+ }\r
+ break;\r
+ case 5:\r
+ // monitor\r
+ int clpos = cfhand.addClient(ch = new ClientHandle(args[argc],\r
+ args[argc + 1]));\r
+ argc += 2;\r
+ if (clpos != 0)\r
+ System.out.println("Monitor Client added at " + clpos + " as urn:"\r
+ + ch.getClientUrn());\r
+ else {\r
+ System.err.println("Monitor Client was not added.");\r
+ break;\r
+ }\r
+ FileWatcher mon = new FileWatcher(cf);\r
+ while (cf.exists()) {\r
+ // get watcher's lock to ensure state change is fixed for retrieval\r
+ Lock chlock = mon.getChangedState();\r
+ if (chlock != null) {\r
+ ClientHandle[] cl = cfhand.retrieveClientList(chlock);\r
+ System.out.println("-- Monitor " + cf.getName());\r
+ // while (w.hasChanged())\r
+ // ;\r
+ int newpos = -1;\r
+ if (cl != null) {\r
+ for (int chi = 0, che = cl.length; chi < che; chi++) {\r
+ if (ch.equals(cl[chi]))\r
+ newpos = chi + 1;\r
+ }\r
+ }\r
+ if (newpos == -1) {\r
+ // add self again to cleared list.\r
+ newpos = cfhand.addClient(ch);\r
+ mon.setState();\r
+ if (newpos == 0) {\r
+ System.err\r
+ .println("Monitor client could not be re-added to list.");\r
+ break;\r
+ }\r
+ }\r
+ if (newpos != clpos) {\r
+ System.out.println("Monitor client moved from " + clpos + " to "\r
+ + newpos);\r
+ clpos = newpos;\r
+ }\r
+ }\r
+ }\r
+ break;\r
+ default:\r
+ if (com == -1) {\r
+ System.err\r
+ .println("Unknown command : " + args[argc++] + "*Ignored!*");\r
+ } else\r
+ System.err.println("Command " + args[argc++]\r
+ + " *Ignored!* - its not implemented.");\r
+ }\r
+\r
+ for (int j = 0; j < 900000; j++) {\r
+ Integer i = Integer.getInteger("1");\r
+ Integer q = i;\r
+ }\r
+ }\r
+\r
+ }\r
+\r
+ /*\r
+ * Iterator coms = commands.iterator(); int com=-1; while ((coms!=null) &&\r
+ * coms.hasNext()) { com++; if (args[argc].toLowerCase().equals((String)\r
+ * coms.next())) { System.out.println("Doing "+args[argc]); ClientHandle ch;\r
+ * argc++; switch (com) { case 0: // Add\r
+ * ClientsFileTest.complainArgs(args.length, argc, "add", 2,\r
+ * "for the Client's 'Name' and 'Version'"); int pos = cfhand.addClient(ch=new\r
+ * ClientHandle(args[argc],args[argc+1])); argc+=2; if (pos!=0)\r
+ * System.out.println("Client added at "+pos+" as urn:"+ch.getClientUrn());\r
+ * else System.out.println("Client was not added."); break; case 1: // remove\r
+ * ClientsFileTest.complainArgs(args.length, argc, "remove", 3,\r
+ * "for the Client's 'Name', Version and URN"); ch=new\r
+ * ClientHandle(args[argc], args[argc+1]); ch.setClientUrn(args[argc+2]);\r
+ * argc+=3; cfhand.removeClient(ch, null);\r
+ * System.out.println("Client removed (apparently)"); break; case 2: // list\r
+ * ClientHandle[] chlist = cfhand.retrieveClientList(); if (chlist!=null) {\r
+ * for (int chi=0,che=chlist.length; chi<che; chi++) {\r
+ * System.out.println("Client "\r
+ * +chi+" ("+chlist[chi].getClientName()+" "+chlist\r
+ * [chi].getVersion()+" "+chlist[chi].getClientUrn()+")"); } } else {\r
+ * System.out.println("Client list is empty."); } break; case 3: // clear\r
+ * cfhand = null; cf.delete(); try { cf.createNewFile(); cfhand = new\r
+ * ClientsFile(cf); } catch (Exception e) {\r
+ * System.err.println("Failed on new empty clientfile creation!");\r
+ * e.printStackTrace(System.err); } break; case 4: // watch FileWatcher w=new\r
+ * FileWatcher(cf); while (cf.exists()) { // get watcher's lock to ensure\r
+ * state change is fixed for retrieval Lock chlock=w.getChangedState(); if\r
+ * (chlock!=null) { ClientHandle[] cl = cfhand.retrieveClientList(chlock);\r
+ * System.out.println("-- Watching "+cf.getName()); //while (w.hasChanged())\r
+ * // ; if (cl!=null) { for (int chi=0,che=cl.length; chi<che; chi++) {\r
+ * System.\r
+ * out.println("Client "+chi+" ("+cl[chi].getClientName()+" "+cl[chi].getVersion\r
+ * ()+" "+cl[chi].getClientUrn()+")"); } } else {\r
+ * System.out.println("Client list is empty."); } }\r
+ * \r
+ * } break; case 5: // monitor ClientsFileTest.complainArgs(args.length, argc,\r
+ * "monitor", 2, "for the Client's 'Name' and 'Version'"); int clpos =\r
+ * cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1])); argc+=2; if\r
+ * (clpos!=0)\r
+ * System.out.println("Monitor Client added at "+clpos+" as urn:"+ch\r
+ * .getClientUrn()); else {\r
+ * System.err.println("Monitor Client was not added."); break; } FileWatcher\r
+ * mon=new FileWatcher(cf); while (cf.exists()) { // get watcher's lock to\r
+ * ensure state change is fixed for retrieval Lock\r
+ * chlock=mon.getChangedState(); if (chlock!=null) { ClientHandle[] cl =\r
+ * cfhand.retrieveClientList(chlock);\r
+ * System.out.println("-- Monitor "+cf.getName()); //while (w.hasChanged()) //\r
+ * ; int newpos=-1; if (cl!=null) { for (int chi=0,che=cl.length; chi<che;\r
+ * chi++) { if (ch.equals(cl[chi])) newpos=chi+1; } } if (newpos==-1) { // add\r
+ * self again to cleared list. newpos=cfhand.addClient(ch); mon.setState(); if\r
+ * (newpos==0) {\r
+ * System.err.println("Monitor client could not be re-added to list."); break;\r
+ * } } if (newpos!=clpos) {\r
+ * System.out.println("Monitor client moved from "+clpos+" to "+newpos);\r
+ * clpos=newpos; } } }\r
+ * \r
+ * } coms = null; } }\r
+ */\r
+\r
+}\r