bug fixes
[iotcloud.git] / src / java / iotcloud / Table.java
index baf171d7725c8c28a2879feee44ffc7816c09ed1..26fc7a1802c6db014f63979fd24c9746f0e45a24 100644 (file)
@@ -75,6 +75,10 @@ final public class Table {
                }
        }
 
+       public String toString() {
+               return table.toString();
+       }
+       
        public IoTString put(IoTString key, IoTString value) {
                while(true) {
                        KeyValue oldvalue=table.get(key);
@@ -92,26 +96,20 @@ final public class Table {
                long seqn = buffer.getOldestSeqNum();
 
                if (forcedresize) {
-                       System.out.println("A");
                        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 */
-                       System.out.println("B");
                        long fullfirstseqn = buffer.getNewestSeqNum() + 1 - numslots;
                        seqn = fullfirstseqn < 1?1:fullfirstseqn;
                        for(int i=0; i < FREE_SLOTS; i++, seqn++) {
                                Slot prevslot=buffer.getSlot(seqn);
-                               System.out.println(i);
                                if (!prevslot.isLive())
                                        continue;
-                               System.out.println("islive");
                                Vector<Entry> liveentries = prevslot.getLiveEntries();
                                for(Entry liveentry:liveentries) {
-                                       if (redundant(liveentry))
-                                               continue;
                                        if (s.hasSpace(liveentry))
                                                s.addEntry(liveentry);
                                        else if (i==0) {
@@ -139,8 +137,6 @@ search:
                                continue;
                        Vector<Entry> liveentries = prevslot.getLiveEntries();
                        for(Entry liveentry:liveentries) {
-                               if (redundant(liveentry))
-                                       continue;
                                if (s.hasSpace(liveentry))
                                        s.addEntry(liveentry);
                                else
@@ -163,15 +159,6 @@ search:
                return insertedkv;
        }
 
-       boolean redundant(Entry liveentry) {
-               if (liveentry.getType()==Entry.TypeLastMessage) {
-                       LastMessage lastmsg=(LastMessage) liveentry;
-                       return lastmsg.getMachineID() == localmachineid;
-               }
-               return false;
-       }
-
-
        private void validateandupdate(Slot[] newslots, boolean acceptupdatestolocal) {
                /* The cloud communication layer has checked slot HMACs already
                         before decoding */
@@ -317,21 +304,35 @@ search:
                                }
                        }
                }
-
+               
+               if (machineid == localmachineid) {
+                       /* Our own messages are immediately dead. */
+                       if (liveness instanceof LastMessage) {
+                               ((LastMessage)liveness).setDead();
+                       } else if (liveness instanceof Slot) {
+                               ((Slot)liveness).setDead();
+                       } else {
+                               throw new Error("Unrecognized type");
+                       }
+               }
+               
+               
                Pair<Long, Liveness> lastmsgentry = lastmessagetable.put(machineid, new Pair<Long, Liveness>(seqnum, liveness));
                if (lastmsgentry == null)
                        return;
 
                long lastmsgseqnum = lastmsgentry.getFirst();
                Liveness lastentry = lastmsgentry.getSecond();
-               if (lastentry instanceof LastMessage) {
-                       ((LastMessage)lastentry).setDead();
-               } else if (lastentry instanceof Slot) {
-                       ((Slot)lastentry).setDead();
-               } else {
-                       throw new Error("Unrecognized type");
+               if (machineid != localmachineid) {
+                       if (lastentry instanceof LastMessage) {
+                               ((LastMessage)lastentry).setDead();
+                       } else if (lastentry instanceof Slot) {
+                               ((Slot)lastentry).setDead();
+                       } else {
+                               throw new Error("Unrecognized type");
+                       }
                }
-
+               
                if (machineid == localmachineid) {
                        if (lastmsgseqnum != seqnum && !acceptupdatestolocal)
                                throw new Error("Server Error: Mismatch on local machine sequence number");
@@ -343,7 +344,6 @@ search:
 
        private void processSlot(SlotIndexer indexer, Slot slot, boolean acceptupdatestolocal, HashSet<Long> machineSet) {
                updateLastMessage(slot.getMachineID(), slot.getSequenceNumber(), slot, acceptupdatestolocal, machineSet);
-
                for(Entry entry : slot.getEntries()) {
                        switch(entry.getType()) {
                        case Entry.TypeKeyValue: