more updates
authorbdemsky <bdemsky@uci.edu>
Sun, 24 Jul 2016 04:32:06 +0000 (21:32 -0700)
committerbdemsky <bdemsky@uci.edu>
Sun, 24 Jul 2016 04:32:06 +0000 (21:32 -0700)
src/java/iotcloud/CloudComm.java
src/java/iotcloud/Slot.java
src/java/iotcloud/SlotBuffer.java
src/java/iotcloud/Table.java
src/java/iotcloud/Test.java [new file with mode: 0644]
src/java/iotcloud/TestCloudComm.java [new file with mode: 0644]

index 1993a8db1e9b933ca1aa890ccf32785a9413569c..6d421b5513630ebc1c9929fdafefc95d00eba0ac 100644 (file)
@@ -10,6 +10,9 @@ class CloudComm {
        Cipher decryptcipher;
        Mac mac;
 
+       CloudComm() {
+       }
+       
        CloudComm(String _baseurl, Cipher _encrypt, Cipher _decrypt, Mac _mac) {
                this.baseurl=_baseurl;
                this.encryptcipher = _encrypt;
index dc5389ee7268f6a8f60d0636ccf2638f05f3e643..115c76120f9382dc2c0f47b6a82851b4f458b68b 100644 (file)
@@ -33,6 +33,10 @@ class Slot implements Liveness {
                this(_seqnum, _machineid, _prevhmac, new byte[HMAC_SIZE]);
        }
 
+       Slot(long _seqnum, long _machineid) {
+               this(_seqnum, _machineid, new byte[HMAC_SIZE], new byte[HMAC_SIZE]);
+       }
+
        byte[] getHMAC() {
                return hmac;
        }
index 8a8662b85f42d67b810046e4d5ad0f0569c5462d..e3ababf7666a3e3d29b315f1bb5e6ba27844d1f2 100644 (file)
@@ -10,6 +10,8 @@ class SlotBuffer {
 
        SlotBuffer() {
                array=new Slot[DEFAULT_SIZE+1];
+               head=tail=0;
+               oldestseqn=0;
        }
 
        int size() {
@@ -18,6 +20,10 @@ class SlotBuffer {
                return (array.length + head) - tail;
        }
 
+       int capacity() {
+               return array.length - 1;
+       }
+       
        void resize(int newsize) {
                if (newsize == (array.length-1))
                        return;
index c2adbc93fe9a67e84b6f669e52177760ff9d760a..c31aed86851d4e273eb950b0656f3223a54a7698 100644 (file)
@@ -21,10 +21,18 @@ final public class Table {
        public Table(String baseurl, String password, long _localmachineid) {
                localmachineid=_localmachineid;
                buffer = new SlotBuffer();
+               numslots = buffer.capacity();
                sequencenumber = 1;
                initCloud(baseurl, password);
        }
 
+       public Table(CloudComm _cloud, long _localmachineid) {
+               localmachineid=_localmachineid;
+               buffer = new SlotBuffer();
+               sequencenumber = 1;
+               cloud=_cloud;
+       }
+       
        private void initCloud(String baseurl, String password) {
                try {
                        SecretKeySpec secret=getKey(password);
@@ -64,21 +72,37 @@ final public class Table {
                        return null;
        }
 
+       public void initTable() {
+               Slot s=new Slot(1, localmachineid);
+               TableStatus status=new TableStatus(s, numslots);
+               s.addEntry(status);
+    Slot[] array=cloud.putSlot(s, numslots);
+    if (array == null) {
+      array = new Slot[] {s};
+                       validateandupdate(array); // update data structure
+               } else {
+                       throw new Error("Error on initialization");
+               }
+       }
+       
        public IoTString put(IoTString key, IoTString value) {
     while(true) {
       KeyValue oldvalue=table.get(key);
-      if (tryput(key, value)) {
+      if (tryput(key, value, false)) {
         return oldvalue.getValue();
       }
     }
   }
 
-  private boolean tryput(IoTString key, IoTString value) {
-    Slot s=new Slot(sequencenumber+1, localmachineid, buffer.getSlot(sequencenumber).getHMAC());
-    boolean forcedresize = false;
-
+  private boolean tryput(IoTString key, IoTString value, boolean forcedresize) {
+               Slot s=new Slot(sequencenumber+1, localmachineid, buffer.getSlot(sequencenumber).getHMAC());
     long seqn = buffer.getOldestSeqNum();
-    
+
+               if (forcedresize) {
+                       TableStatus status=new TableStatus(s, FORCED_RESIZE_INCREMENT + numslots);
+                       s.addEntry(status);
+               }
+               
     if ((numslots - buffer.size()) < FREE_SLOTS) {
       //have to check whether we have enough free slots
       seqn = buffer.getNewestSeqNum() + 1 - numslots;
@@ -93,9 +117,10 @@ final public class Table {
           else if (i==0) {
             if (s.canFit(liveentry))
               s.addEntry(liveentry);
-            else
-              forcedresize = true;
-          }
+            else if (!forcedresize) {
+              return tryput(key, value, true);
+                                               }
+                                       }
         }
       }
     }
diff --git a/src/java/iotcloud/Test.java b/src/java/iotcloud/Test.java
new file mode 100644 (file)
index 0000000..5ecd9d2
--- /dev/null
@@ -0,0 +1,21 @@
+package iotcloud;
+
+public class Test {
+       public static void main(String[] args) {
+               TestCloudComm cc=new TestCloudComm();
+               Table t1=new Table(cc, 6513);
+               t1.initTable();
+               Table t2=new Table(cc, 6512);
+               t2.update();
+               for(int i=0;i<100;i++) {
+                       String a="STR"+i;
+                       String b="ABR"+i;
+                       IoTString ia=new IoTString(a);
+                       IoTString ib=new IoTString(b);
+                       t1.put(ia, ia);
+                       t2.put(ib, ib);
+                       System.out.println(ib+"->"+t1.get(ib));
+                       System.out.println(ia+"->"+t2.get(ia));
+               }
+       }
+}
diff --git a/src/java/iotcloud/TestCloudComm.java b/src/java/iotcloud/TestCloudComm.java
new file mode 100644 (file)
index 0000000..ce5b9ba
--- /dev/null
@@ -0,0 +1,35 @@
+package iotcloud;
+import java.io.*;
+import java.net.*;
+import java.util.Arrays;
+import javax.crypto.*;
+
+class TestCloudComm extends CloudComm {
+       SlotBuffer buffer;
+
+       TestCloudComm() {
+               buffer = new SlotBuffer();
+       }
+
+       public synchronized Slot[] putSlot(Slot slot, int max) {
+               if (buffer.getNewestSeqNum()+1 == slot.getSequenceNumber()) {
+                       if (max!=0)
+                               buffer.resize(max);
+                       buffer.putSlot(slot);
+                       return null;
+               } else
+                       return getSlots(slot.getSequenceNumber());
+       }
+
+       public synchronized Slot[] getSlots(long sequencenumber) {
+               long newestseqnum=buffer.getNewestSeqNum();
+               long oldestseqnum=buffer.getOldestSeqNum();
+               if (sequencenumber < oldestseqnum)
+                       sequencenumber=oldestseqnum;
+               int numslots=(int)((newestseqnum - sequencenumber)+1);
+               Slot[] slots=new Slot[numslots];
+               for(int i=0;i<numslots;i++,sequencenumber++)
+                       slots[i]=buffer.getSlot(sequencenumber);
+               return slots;
+       }
+}