refactored IClientAppdata and implementation has been minimally tested and bugfixed...
[vamsas.git] / src / uk / ac / vamsas / test / ExampleApplication.java
index bfb1425..3bebe6d 100644 (file)
@@ -10,8 +10,15 @@ import uk.ac.vamsas.test.objects.Core;
 import java.awt.Event;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
 import java.util.Vector;
 
 /**
@@ -69,6 +76,9 @@ public class ExampleApplication
   private int totalUpdates = 9;
   private uk.ac.vamsas.client.VorbaId recover = null;
   private int calls=0;
+  private long mdatahash = 0;
+  private long muserdatahash = 0;
+  
   private void processVamsasDocument(IClientDocument doc)
   {
     if (doc.getVamsasRoots().length<4) {
@@ -90,15 +100,110 @@ public class ExampleApplication
         System.out.println("Modified Sequence:\n"+alsq.getSequence());
         doc.setVamsasRoots(doc.getVamsasRoots());
       } catch (Exception ee) {
-      
+        
       }
     }
-    vorbaclient.updateDocument(doc);
-    // merge vamsasObjects with vamsas objects in document
     // get this apps 'mydata' if it hasn't got it already.
+    System.out.println("Trying to get appdata and modify it.....");
+    try {
+      processAppData(doc);
+      System.out.println(".....Finished.");
+    } catch (Exception e)
+    {
+      System.err.println("Failed to process appdata for our application.");
+      e.printStackTrace(System.err);
+    }
+    
+    
     // .. access this application's 'public' mydata' if there is any.
+    vorbaclient.updateDocument(doc);
+    // merge vamsasObjects with vamsas objects in document
+    
+  }
+  private int appdatareads=0;
+  private void processAppData(IClientDocument doc) throws Exception {
+    appdatareads++;
+    boolean writtenonce=false;
+    if (doc!=null)
+    {
+      uk.ac.vamsas.client.IClientAppdata appd = doc.getClientAppdata();
+      if (appd.hasClientAppdata() && !(appdatareads % 2==0))
+      {
+        //byte[] cappd = appd.getClientAppdata();
+        //if (cappd!=null)
+        //  System.out.println("Client appdata\n"+cappd.toString()+"\nEnd of Appdata\n");
+        System.out.println("Testing read from inputstream");
+        String cappds = readData(appd.getClientInputStream());
+        System.out.println("Client appdata\n"+cappds+"\nEnd of Appdata\n");
+      } else {
+        if (!writtenonce)
+        {  
+          writtenonce=true;
+          // appd.setClientAppdata(makeappData("Client Appdata for "+user.toString()+" written"));
+          writeData(appd.getClientOutputStream(), "Client Appdata for all users written on "+appdatareads+" read by "+vorbaclient.getUserHandle());
+          System.out.println("Written to ClientAppdata stream.");
+        }
+      }
+      if (appd.hasUserAppdata() && !(appdatareads % 2==0))
+      {
+        byte[] cappd = appd.getUserAppdata();
+        if (cappd!=null)
+          System.out.println("User appdata\n"+new String(cappd)+"\nEnd of Users' Appdata\n");
+        else
+        {
+          System.out.println("No user appdata.");
+          appd.setUserAppdata(("no default - overwritten null byte set on "+appdatareads+" read by "+vorbaclient.getUserHandle()+"").getBytes());
+        }
+      } else
+        if (!writtenonce)
+      {  
+        writtenonce=true;
+        byte[] bts = makeappData("User Appdata for "+user+" written on "+appdatareads+" read at ");
+        System.out.println("Setting appData bytes to\n"+new String(bts)+"\nEnd.");
+        appd.setUserAppdata(bts);
+        System.out.println("Written to UserAppdata stream.");
+      }
+    }
+  }
+  private byte[] makeappData(String message)
+  {
+    StringBuffer sb = new StringBuffer();
+    sb.append(message);
+    sb.append("on "+new java.util.Date());
+    return sb.toString().getBytes();
+  }
+  private boolean writeData(AppDataOutputStream os,  String message)
+  {
+    StringBuffer sb = new StringBuffer();
+    sb.append(message);
+    sb.append("on "+new java.util.Date());
+    try {
+      ObjectOutputStream oos = new ObjectOutputStream(os);
+      oos.writeObject(sb.toString());
+      oos.flush();
+      oos.close();
+    }
+    catch (Exception e) {
+      System.err.println("Problem serialising this message:\n"+sb);
+      e.printStackTrace(System.err);
+      return false;
+    }
+    return true;
+  }
+  private String readData(AppDataInputStream is)
+  {
+    try {
+      ObjectInputStream ois = new ObjectInputStream(is);
+      String rs = (String) ois.readObject();
+      return rs;
+    }
+    catch (Exception e)
+    {
+      System.err.println("Failed to read a string from input stream!");
+      e.printStackTrace(System.err);
+    }
+    return "";
   }
-
   private void addHandlers(IClient avorbaclient)
   {
     // make a non-volatile reference to the client instance.