applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / test / simpleclient / ClientsFileTest.java
1 /*\r
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
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \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
13  *  \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
18  * \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
21  */\r
22 package uk.ac.vamsas.test.simpleclient;\r
23 \r
24 import java.io.File;\r
25 import java.util.Iterator;\r
26 import java.util.Vector;\r
27 \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
32 \r
33 public class ClientsFileTest {\r
34   private static CommandProcessor cproc;\r
35 \r
36   private static Vector commands;\r
37   static {\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
52   }\r
53 \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
58   }\r
59 \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
63     ClientsFile cfhand;\r
64     try {\r
65       cfhand = new ClientsFile(cf);\r
66     } catch (Exception e) {\r
67       e.printStackTrace(System.err);\r
68       return;\r
69     }\r
70     int argc = 1;\r
71     while (argc < args.length) {\r
72       // vars needed for operations\r
73       ClientHandle ch;\r
74       int com = cproc.getCommand(args, argc);\r
75       argc++;\r
76       switch (com) {\r
77       case 0:\r
78         // Add\r
79         int pos = cfhand.addClient(ch = new ClientHandle(args[argc],\r
80             args[argc + 1]));\r
81         argc += 2;\r
82         if (pos != 0)\r
83           System.out.println("Client added at " + pos + " as urn:"\r
84               + ch.getClientUrn());\r
85         else\r
86           System.out.println("Client was not added.");\r
87         break;\r
88       case 1:\r
89         // remove\r
90         ch = new ClientHandle(args[argc], args[argc + 1]);\r
91         ch.setClientUrn(args[argc + 2]);\r
92         argc += 3;\r
93         cfhand.removeClient(ch, null);\r
94         System.out.println("Client removed (apparently)");\r
95         break;\r
96       case 2:\r
97         // list\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
104           }\r
105         } else {\r
106           System.out.println("Client list is empty.");\r
107         }\r
108         break;\r
109       case 3:\r
110         // clear\r
111         // cfhand.get = null;\r
112         // cf.delete();\r
113         try {\r
114 \r
115           cfhand.clearList();\r
116 \r
117         } catch (Exception e) {\r
118           System.err.println("Failed on new empty clientfile creation!");\r
119           e.printStackTrace(System.err);\r
120         }\r
121         break;\r
122       case 4:\r
123         // watch\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
132             // ;\r
133             if (cl != null) {\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
138               }\r
139             } else {\r
140               System.out.println("Client list is empty.");\r
141             }\r
142           }\r
143 \r
144         }\r
145         break;\r
146       case 5:\r
147         // monitor\r
148         int clpos = cfhand.addClient(ch = new ClientHandle(args[argc],\r
149             args[argc + 1]));\r
150         argc += 2;\r
151         if (clpos != 0)\r
152           System.out.println("Monitor Client added at " + clpos + " as urn:"\r
153               + ch.getClientUrn());\r
154         else {\r
155           System.err.println("Monitor Client was not added.");\r
156           break;\r
157         }\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
166             // ;\r
167             int newpos = -1;\r
168             if (cl != null) {\r
169               for (int chi = 0, che = cl.length; chi < che; chi++) {\r
170                 if (ch.equals(cl[chi]))\r
171                   newpos = chi + 1;\r
172               }\r
173             }\r
174             if (newpos == -1) {\r
175               // add self again to cleared list.\r
176               newpos = cfhand.addClient(ch);\r
177               mon.setState();\r
178               if (newpos == 0) {\r
179                 System.err\r
180                     .println("Monitor client could not be re-added to list.");\r
181                 break;\r
182               }\r
183             }\r
184             if (newpos != clpos) {\r
185               System.out.println("Monitor client moved from " + clpos + " to "\r
186                   + newpos);\r
187               clpos = newpos;\r
188             }\r
189           }\r
190         }\r
191         break;\r
192       default:\r
193         if (com == -1) {\r
194           System.err\r
195               .println("Unknown command : " + args[argc++] + "*Ignored!*");\r
196         } else\r
197           System.err.println("Command " + args[argc++]\r
198               + " *Ignored!* - its not implemented.");\r
199       }\r
200 \r
201       for (int j = 0; j < 900000; j++) {\r
202         Integer i = Integer.getInteger("1");\r
203         Integer q = i;\r
204       }\r
205     }\r
206 \r
207   }\r
208 \r
209   /*\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
239    * System.\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
243    * \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
247    * (clpos!=0)\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
259    * (newpos==0) {\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
264    * \r
265    * } coms = null; } }\r
266    */\r
267 \r
268 }\r