introducing Lock File based locking (for portability) - part implemented.
[vamsas.git] / src / org / vamsas / client / simpleclient / ClientsFile.java
index 598bfdd..cab03cd 100644 (file)
@@ -43,8 +43,8 @@ public class ClientsFile extends ListFile {
       try {
         ClientHandle[] clients=null;
         if (this.fileLock.rafile.length()>0) {
-          ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(
-              new java.io.FileInputStream(sessionFile)));
+          
+          ObjectInputStream is = new ObjectInputStream(fileLock.getBufferedInputStream(true));
           Object o;
           o=is.readObject();
           if (o!=null) {
@@ -179,6 +179,9 @@ public class ClientsFile extends ListFile {
    */
   protected int addClient(ClientHandle me, boolean disambiguate) {
     int newclient = 0;
+    int tries=5;
+    while (tries-->0 && !lockFile())
+      try { Thread.sleep(1); } catch (Exception e){};
     if (lockFile()) {
       ClientHandle[] clients = retrieveClientHandles();
       if (me.getClientUrn()==null) {
@@ -231,11 +234,14 @@ public class ClientsFile extends ListFile {
       File templist = backupSessionFile();
       if (templist != null) {
         try {
-          fileLock.rafile.setLength(0);
-          ObjectOutputStream os = new ObjectOutputStream(
-              new BufferedOutputStream(new FileOutputStream(this.sessionFile)));
+          // fileLock.rafile.setLength(0);
+          ObjectOutputStream os = 
+            new ObjectOutputStream(fileLock.getBufferedOutputStream(true));
+          
+//              new BufferedOutputStream(new FileOutputStream(this.sessionFile)));
+          //    new BufferedOutputStream(new FileOutputStream(this.fileLock.rafile.getFD())));
           os.writeObject(clients);
-          os.close();
+          os.close(); // close destroys lock!
           // All done - remove the backup.
           templist.delete();
           templist = null;