bug fixes
[iotcloud.git] / src / java / iotcloud / Slot.java
index 6604894e0e364e3cf556f254ac828ba4a74394d6..d23e5c1b86dd76cb0c215b84d1b954ba4ac8fa15 100644 (file)
@@ -13,13 +13,11 @@ import java.util.Arrays;
 class Slot implements Liveness {
        /** Sets the slot size. */
        static final int SLOT_SIZE=2048;
-       /** Sets how many bytes we reserve. */
-       static final int RESERVED_SPACE=64;
        /** Sets the size for the HMAC. */
        static final int HMAC_SIZE=32;
 
        /** Sequence number of the slot. */
-       private long seqnum;
+       private long seqnum;//
        /** HMAC of previous slot. */
        private byte[] prevhmac;
        /** HMAC of this slot. */
@@ -35,8 +33,10 @@ class Slot implements Liveness {
        private boolean seqnumlive;
        /** Number of bytes of free space. */
        private int freespace;
+       /** Reference to Table */
+       private Table table;
 
-       Slot(long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac) {
+       Slot(Table _table, long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac) {
                seqnum=_seqnum;
                machineid=_machineid;
                prevhmac=_prevhmac;
@@ -45,14 +45,15 @@ class Slot implements Liveness {
                livecount=1;
                seqnumlive=true;
                freespace = SLOT_SIZE - getBaseSize();
+               table=_table;
        }
 
-       Slot(long _seqnum, long _machineid, byte[] _prevhmac) {
-               this(_seqnum, _machineid, _prevhmac, null);
+       Slot(Table _table, long _seqnum, long _machineid, byte[] _prevhmac) {
+               this(_table, _seqnum, _machineid, _prevhmac, null);
        }
 
-       Slot(long _seqnum, long _machineid) {
-               this(_seqnum, _machineid, new byte[HMAC_SIZE], null);
+       Slot(Table _table, long _seqnum, long _machineid) {
+               this(_table, _seqnum, _machineid, new byte[HMAC_SIZE], null);
        }
 
        byte[] getHMAC() {
@@ -64,6 +65,13 @@ class Slot implements Liveness {
        }
 
        void addEntry(Entry e) {
+               e=e.getCopy(this);
+               entries.add(e);
+               livecount++;
+               freespace -= e.getSize();
+       }
+
+       private void addShallowEntry(Entry e) {
                entries.add(e);
                livecount++;
                freespace -= e.getSize();
@@ -74,15 +82,6 @@ class Slot implements Liveness {
         * using its reserved space. */
 
        boolean hasSpace(Entry e) {
-               int newfreespace = freespace - e.getSize();
-               return newfreespace > RESERVED_SPACE;
-       }
-
-       /**
-        * Returns true if the slot can fit the entry potentially using the
-        * reserved space. */
-
-       boolean canFit(Entry e) {
                int newfreespace = freespace - e.getSize();
                return newfreespace >= 0;
        }
@@ -91,7 +90,7 @@ class Slot implements Liveness {
                return entries;
        }
 
-       static Slot decode(byte[] array, Mac mac) {
+       static Slot decode(Table table, byte[] array, Mac mac) {
                mac.update(array, HMAC_SIZE, array.length-HMAC_SIZE);
                byte[] realmac=mac.doFinal();
 
@@ -106,10 +105,10 @@ class Slot implements Liveness {
                long seqnum=bb.getLong();
                long machineid=bb.getLong();
                int numentries=bb.getInt();
-               Slot slot=new Slot(seqnum, machineid, prevhmac, hmac);
+               Slot slot=new Slot(table, seqnum, machineid, prevhmac, hmac);
 
                for(int i=0; i<numentries; i++) {
-                       slot.addEntry(Entry.decode(slot, bb));
+                       slot.addShallowEntry(Entry.decode(slot, bb));
                }
 
                return slot;
@@ -150,13 +149,16 @@ class Slot implements Liveness {
         * itself.
         */
 
-       Vector<Entry> getLiveEntries() {
+       Vector<Entry> getLiveEntries(boolean resize) {
                Vector<Entry> liveEntries=new Vector<Entry>();
-               for(Entry entry: entries)
-                       if (entry.isLive())
-                               liveEntries.add(entry);
-
-               if (seqnumlive)
+               for(Entry entry: entries) {
+                       if (entry.isLive()) {
+                               if (!resize || entry.getType() != Entry.TypeTableStatus)
+                                       liveEntries.add(entry);
+                       }
+               }
+                       
+               if (seqnumlive && !resize)
                        liveEntries.add(new LastMessage(this, machineid, seqnum));
 
                return liveEntries;
@@ -184,8 +186,8 @@ class Slot implements Liveness {
         */
 
        void setDead() {
-               decrementLiveCount();
                seqnumlive=false;
+               decrementLiveCount();
        }
 
        /**
@@ -194,6 +196,8 @@ class Slot implements Liveness {
 
        void decrementLiveCount() {
                livecount--;
+               if (livecount==0)
+                       table.decrementLiveCount();
        }
 
        /**