updates
authorBrian Demsky <bdemsky@plrg.eecs.uci.edu>
Sat, 23 Jul 2016 21:17:29 +0000 (14:17 -0700)
committerBrian Demsky <bdemsky@plrg.eecs.uci.edu>
Sat, 23 Jul 2016 21:17:29 +0000 (14:17 -0700)
src/java/iotcloud/IoTString.java
src/java/iotcloud/KeyValue.java
src/java/iotcloud/LastMessage.java
src/java/iotcloud/Pair.java [new file with mode: 0644]
src/java/iotcloud/Slot.java
src/java/iotcloud/Table.java

index 37fba1b..11e86e2 100644 (file)
@@ -6,6 +6,9 @@ public class IoTString {
        byte[] array;
        int hashcode;
 
+       private IoTString() {
+       }
+       
        public IoTString(byte[] _array) {
                array=(byte[]) _array.clone();
                hashcode=Arrays.hashCode(array);
@@ -16,6 +19,17 @@ public class IoTString {
                hashcode=Arrays.hashCode(array);
        }
 
+       static IoTString shallow(byte[] _array) {
+               IoTString i=new IoTString();
+               i.array = _array;
+               i.hashcode = Arrays.hashCode(_array);
+               return i;
+       }
+
+       byte[] internalBytes() {
+               return array;
+       }
+       
        public int hashCode() {
                return hashcode;
        }
@@ -35,4 +49,8 @@ public class IoTString {
                }
                return false;
        }
+
+       public int length() {
+               return array.length;
+       }
 }
index 36a56dc..6f620fa 100644 (file)
@@ -2,12 +2,21 @@ package iotcloud;
 import java.nio.ByteBuffer;
 
 class KeyValue extends Entry {
-       byte[] key;
-       byte[] value;
-       KeyValue(byte[] _key, byte[] _value) {
+       private IoTString key;
+       private IoTString value;
+       
+       KeyValue(IoTString _key, IoTString _value) {
                key=_key;
                value=_value;
        }
+
+       IoTString getKey() {
+               return key;
+       }
+
+       IoTString getValue() {
+               return value;
+       }
        
        static Entry decode(ByteBuffer bb) {
                int keylength=bb.getInt();
@@ -16,19 +25,19 @@ class KeyValue extends Entry {
                byte[] value=new byte[valuelength];
                bb.get(key);
                bb.get(value);
-               return new KeyValue(key, value);
+               return new KeyValue(IoTString.shallow(key), IoTString.shallow(value));
        }
 
        void encode(ByteBuffer bb) {
                bb.put(Entry.TypeKeyValue);
-               bb.putInt(key.length);
-               bb.putInt(value.length);
-               bb.put(key);
-               bb.put(value);
+               bb.putInt(key.length());
+               bb.putInt(value.length());
+               bb.put(key.internalBytes());
+               bb.put(value.internalBytes());
        }
 
        int getSize() {
-               return 2*Integer.BYTES+key.length+value.length+Byte.BYTES;
+               return 2*Integer.BYTES+key.length()+value.length()+Byte.BYTES;
        }
 
        byte getType() {
index 23e6bcd..9ea9bcc 100644 (file)
@@ -11,6 +11,14 @@ class LastMessage extends Entry {
                seqnum=_seqnum;
        }
 
+       long getMachineID() {
+               return machineid;
+       }
+
+       long getSequenceNumber() {
+               return seqnum;
+       }
+       
        static Entry decode(ByteBuffer bb) {
                long machineid=bb.getLong();
                long seqnum=bb.getLong();
diff --git a/src/java/iotcloud/Pair.java b/src/java/iotcloud/Pair.java
new file mode 100644 (file)
index 0000000..1c81444
--- /dev/null
@@ -0,0 +1,23 @@
+package iotcloud;
+
+public class Pair<A,B> {
+  private A a;
+  private B b;
+
+  public Pair(A a, B b) {
+    this.a=a;
+    this.b=b;
+  }
+
+  public A getFirst() {
+    return a;
+  }
+
+  public B getSecond() {
+    return b;
+  }
+
+  public String toString() {
+    return "<"+a+","+b+">";
+  }
+}
index 8815a5b..848edb3 100644 (file)
@@ -84,6 +84,10 @@ class Slot {
                return seqnum;
        }
 
+       long getMachineID() {
+               return machineid;
+       }
+       
        byte[] getBytes() {
                return null;
        }
index 679421e..ae4ed16 100644 (file)
@@ -6,7 +6,7 @@ import javax.crypto.*;
 
 public class Table {
        int numslots;
-       HashMap<IoTString, IoTString> table=new HashMap<IoTString, IoTString>();
+       HashMap<IoTString, KeyValue> table=new HashMap<IoTString, KeyValue>();
        HashMap<Long, Long> lastmessage=new HashMap<Long, Long>();
        SlotBuffer buffer;
        CloudComm cloud;
@@ -71,22 +71,33 @@ public class Table {
        }
 
        void processEntry(KeyValue entry, SlotIndexer indexer, Slot slot) {
-
+               IoTString key=entry.getKey();
+               KeyValue oldvalue=table.get(key);
+               if (oldvalue != null) {
+                       oldvalue.setDead();
+               }
+               table.put(key, entry);
        }
 
        void processEntry(LastMessage entry, SlotIndexer indexer, Slot slot) {
-
+               updateLastMessage(entry.getMachineID(), entry.getSequenceNumber(), null, entry);
        }
 
        void processEntry(RejectedMessage entry, SlotIndexer indexer, Slot slot) {
-
+               
        }
 
        void processEntry(TableStatus entry, SlotIndexer indexer, Slot slot) {
 
+       }
+
+       void updateLastMessage(long machineid, long seqnum, Slot slot, LastMessage entry) {
+               
        }
        
        void processSlot(SlotIndexer indexer, Slot slot) {
+               updateLastMessage(slot.getMachineID(), slot.getSequenceNumber(), slot, null);
+
                for(Entry entry : slot.getEntries()) {
                        switch(entry.getType()) {
                        case Entry.TypeKeyValue: