more updates
[iotcloud.git] / src / java / iotcloud / Slot.java
index ff0e901081d5850817c36306d7c72807f2995599..115c76120f9382dc2c0f47b6a82851b4f458b68b 100644 (file)
@@ -6,6 +6,7 @@ import java.util.Arrays;
 
 class Slot implements Liveness {
        static final int SLOT_SIZE=2048;
+       static final int RESERVED_SPACE=64;
        static final int HMAC_SIZE=32;
 
        private long seqnum;
@@ -15,6 +16,7 @@ class Slot implements Liveness {
        private Vector<Entry> entries;
        private int livecount;
        private boolean seqnumlive;
+       private int freespace;
        
        Slot(long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac) {
                seqnum=_seqnum;
@@ -24,12 +26,17 @@ class Slot implements Liveness {
                entries=new Vector<Entry>();
                livecount=1;
                seqnumlive=true;
+               freespace = SLOT_SIZE - getBaseSize();
        }
 
        Slot(long _seqnum, long _machineid, byte[] _prevhmac) {
                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;
        }
@@ -41,8 +48,19 @@ class Slot implements Liveness {
        void addEntry(Entry e) {
                entries.add(e);
                livecount++;
+               freespace -= e.getSize();
        }
 
+       boolean hasSpace(Entry e) {
+               int newfreespace = freespace - e.getSize();
+               return newfreespace > RESERVED_SPACE;
+       }
+
+       boolean canFit(Entry e) {
+               int newfreespace = freespace - e.getSize();
+               return newfreespace >= 0;
+       }
+       
        Vector<Entry> getEntries() {
                return entries;
        }
@@ -90,6 +108,22 @@ class Slot implements Liveness {
                return array;
        }
 
+       int getBaseSize() {
+               return 2*HMAC_SIZE+2*Long.BYTES+Integer.BYTES;
+       }
+       
+       Vector<Entry> getLiveEntries() {
+               Vector<Entry> liveEntries=new Vector<Entry>();
+               for(Entry entry: entries)
+                       if (entry.isLive())
+                               liveEntries.add(entry);
+
+               if (seqnumlive)
+                       liveEntries.add(new LastMessage(this, machineid, seqnum));
+               
+               return liveEntries;
+       }
+       
        long getSequenceNumber() {
                return seqnum;
        }