1 package org.vamsas.client.simpleclient;
3 import org.vamsas.client.*;
5 import java.io.BufferedInputStream;
6 import java.io.BufferedOutputStream;
8 import java.io.FileInputStream;
9 import java.io.FileNotFoundException;
10 import java.io.FileOutputStream;
11 import java.io.IOException;
12 import java.io.InputStreamReader;
13 import java.io.ObjectInputStream;
14 import java.io.ObjectOutput;
15 import java.io.ObjectOutputStream;
16 import java.io.OutputStream;
17 import java.util.Vector;
20 * Handler for the clientsFile within a vamsas session thread.
23 public class ClientsFile extends SessionFile {
25 * number of my client in list - passed back when a client
26 * is added to list, and used (if valid) for quickly
27 * looking up presence of client handle in the list.
29 private int syncnum = 1;
31 public ClientsFile(File filelist) throws IOException {
33 if (!this.sessionFile.exists())
34 this.sessionFile.createNewFile();
38 * internal method for getting clientList - ensures a lock has been made but
39 * does not release it.
41 * @return list of clients
43 private ClientHandle[] retrieveClientHandles() {
46 ClientHandle[] clients=null;
47 if (this.fileLock.rafile.length()>0) {
48 ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(
49 new java.io.FileInputStream(sessionFile)));
54 clients = (ClientHandle[]) o;
57 System.err.println("Garbage in the clientHandle list "+this.sessionFile);
62 } catch (FileNotFoundException e) {
63 // TODO Auto-generated catch block
64 e.printStackTrace(System.err);
65 } catch (Exception e) {
66 e.printStackTrace(System.err);
72 * get the clientList from the file. May return null if lock failed!
75 public ClientHandle[] retrieveClientList() {
77 ClientHandle[] clients = retrieveClientHandles();
84 * get list from the locked ClientList.
86 * @return clientList or null if lock failed (or file was empty)
88 public ClientHandle[] retrieveClientList(Lock extantlock) {
89 if (lockFile(extantlock)) {
90 ClientHandle[] clients = retrieveClientHandles();
97 * adds clientHandle me to the clientList under an existing lock extantLock.
100 * @return client index in list or 0 if lock was invalid or addClient operation failed.
102 public int addClient(ClientHandle me, Lock extantLock) {
103 return addClient(me, true, extantLock);
107 * adds clientHandle me to the clientList under an existing lock.
108 * @param me - clientHandle
109 * @param disambig - if true then add will fail if an identical clientHandle already exists
110 * @param extantLock - existing lock
111 * @return client index in list or 0 if addClient (or the lock) failed.
114 public int addClient(ClientHandle me, boolean disambig, Lock extantLock) {
115 if (lockFile(extantLock)) {
116 syncnum = addClient(me, disambig);
124 * adds the ClientHandle to the list - if it is not unique, then the
125 * ClientHandle object is modified to make it unique in the list. returns the
126 * clientNumber for the client in the session.
132 public int addClient(ClientHandle me) {
133 syncnum = addClient(me, true);
139 * removes 'me' from the session ClientList without complaint if 'me' isn't in the clientList already.
140 * @param me client handle to be removed
141 * @param clientlock existing lock passed from watcher.
143 public void removeClient(ClientHandle me, Lock clientlock) {
145 if (lockFile(clientlock)) {
146 ClientHandle[] clients = retrieveClientHandles();
147 if (clients != null) {
148 if ((syncnum<=0 || syncnum>clients.length) || clients[syncnum-1]!=me) {
149 for (int i = 0, j = clients.length; i < j; i++)
150 if (clients[i].equals(me)) {
158 ClientHandle[] newlist = new ClientHandle[clients.length - 1];
159 for (int k=0,i = 0, j = clients.length; i < j; i++)
161 newlist[k++] = clients[i];
162 if (!putClientList(newlist))
163 throw new Error("Failed to write new clientList!"); // failed to put the clientList to disk.
168 throw new Error("Couldn't get lock for "+((sessionFile==null) ? "Unitialised sessionFile in ClientsFile" : sessionFile.getAbsolutePath()));
172 * Adds a ClientHandle to the ClientList file - optionally disambiguating
173 * the ClientHandle (modifes the URN).
174 * Note: Caller is left to release the lock on the ClientList.
176 * @param disambiguate -
177 * flag indicating if the URN for me should be disambiguated to
178 * differentiate between sessions.
179 * @return index of clientHandle in new list, or -1-position of existing
180 * clientHandle (if disambiguate is true)
182 protected int addClient(ClientHandle me, boolean disambiguate) {
185 ClientHandle[] clients = retrieveClientHandles();
186 if (me.getClientUrn()==null) {
187 // TODO: move this into ClientUrn as a standard form method.
188 me.setClientUrn("vamsas://"+me.getClientName()+":"+me.getVersion()+"/");
190 if (clients == null) {
191 clients = new ClientHandle[1];
196 for (int i = 0, j = clients.length; i < j; i++) {
197 if (clients[i].equals(me)) {
199 while (clients[i].equals(me)) {
200 me.setClientUrn(me.getClientUrn() + k++); // TODO: make a better
205 // will not write the ambiguous clientHandle to disk, just return
212 ClientHandle[] newlist = new ClientHandle[clients.length + 1];
213 for (i = 0, j = clients.length; i < j; i++)
214 newlist[i] = clients[i];
219 if (!putClientList(clients))
220 return 0; // failed to put the clientList to disk.
226 * safely writes clients array to the file referred to by sessionFile.
229 * @return true if successful write. Throws Errors otherwise.
231 protected boolean putClientList(ClientHandle[] clients) {
233 File templist = backupSessionFile();
234 if (templist != null) {
236 fileLock.rafile.setLength(0);
237 ObjectOutputStream os = new ObjectOutputStream(
238 new BufferedOutputStream(new FileOutputStream(this.sessionFile)));
239 os.writeObject(clients);
241 // All done - remove the backup.
244 } catch (Exception e) {
245 if (templist != null) {
247 .println("Serious - problems writing to sessionFile. Backup in "
248 + templist.getAbsolutePath());
249 e.printStackTrace(System.err);
254 "Couldn't create backup of the clientList before writing to it!");
257 throw new Error("Could not lock the clientList: "
258 + ((sessionFile == null) ? "Unitialized ClientsFile"
259 : " failed to get lock on " + sessionFile.getAbsolutePath()));