bug fixes
[iotcloud.git] / src / java / iotcloud / Slot.java
index 8815a5bcbf42784fe1075bf575a86a19858d2779..d23e5c1b86dd76cb0c215b84d1b954ba4ac8fa15 100644 (file)
@@ -4,26 +4,56 @@ import java.nio.ByteBuffer;
 import javax.crypto.Mac;
 import java.util.Arrays;
 
-class Slot {
-       public static final int SLOT_SIZE=2048;
-       public static final int HMAC_SIZE=32;
+/**
+ * Data structuring for holding Slot information.
+ * @author Brian Demsky
+ * @version 1.0
+ */
 
-       private long seqnum;
+class Slot implements Liveness {
+       /** Sets the slot size. */
+       static final int SLOT_SIZE=2048;
+       /** Sets the size for the HMAC. */
+       static final int HMAC_SIZE=32;
+
+       /** Sequence number of the slot. */
+       private long seqnum;//
+       /** HMAC of previous slot. */
        private byte[] prevhmac;
+       /** HMAC of this slot. */
        private byte[] hmac;
+       /** Machine that sent this slot. */
        private long machineid;
+       /** Vector of entries in this slot. */
        private Vector<Entry> entries;
+       /** Pieces of information that are live. */
+       private int livecount;
+       /** Flag that indicates whether this slot is still live for
+        * recording the machine that sent it. */
+       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, Vector<Entry> _entries) {
+       Slot(Table _table, long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac) {
                seqnum=_seqnum;
                machineid=_machineid;
                prevhmac=_prevhmac;
                hmac=_hmac;
-               entries=_entries;
+               entries=new Vector<Entry>();
+               livecount=1;
+               seqnumlive=true;
+               freespace = SLOT_SIZE - getBaseSize();
+               table=_table;
        }
-       
-       Slot(long _seqnum, byte[] _bytes) {
-               seqnum=_seqnum;
+
+       Slot(Table _table, long _seqnum, long _machineid, byte[] _prevhmac) {
+               this(_table, _seqnum, _machineid, _prevhmac, null);
+       }
+
+       Slot(Table _table, long _seqnum, long _machineid) {
+               this(_table, _seqnum, _machineid, new byte[HMAC_SIZE], null);
        }
 
        byte[] getHMAC() {
@@ -34,11 +64,33 @@ class Slot {
                return prevhmac;
        }
 
+       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();
+       }
+
+       /**
+        * Returns true if the slot has free space to hold the entry without
+        * using its reserved space. */
+
+       boolean hasSpace(Entry e) {
+               int newfreespace = freespace - e.getSize();
+               return newfreespace >= 0;
+       }
+
        Vector<Entry> getEntries() {
                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();
 
@@ -53,18 +105,20 @@ class Slot {
                long seqnum=bb.getLong();
                long machineid=bb.getLong();
                int numentries=bb.getInt();
-               Vector<Entry> entries=new Vector<Entry>();
-               for(int i=0;i<numentries;i++) {
-                       entries.add(Entry.decode(bb));
+               Slot slot=new Slot(table, seqnum, machineid, prevhmac, hmac);
+
+               for(int i=0; i<numentries; i++) {
+                       slot.addShallowEntry(Entry.decode(slot, bb));
                }
-               
-               return new Slot(seqnum, machineid, prevhmac, hmac, entries);
+
+               return slot;
        }
 
        byte[] encode(Mac mac) {
                byte[] array=new byte[SLOT_SIZE];
                ByteBuffer bb=ByteBuffer.wrap(array);
-               bb.position(HMAC_SIZE); //Leave space for the HMACs
+               /* Leave space for the slot HMAC.  */
+               bb.position(HMAC_SIZE);
                bb.put(prevhmac);
                bb.putLong(seqnum);
                bb.putLong(machineid);
@@ -72,23 +126,89 @@ class Slot {
                for(Entry entry:entries) {
                        entry.encode(bb);
                }
-               //Compute our HMAC
+               /* Compute our HMAC */
                mac.update(array, HMAC_SIZE, array.length-HMAC_SIZE);
                byte[] realmac=mac.doFinal();
+               hmac = realmac;
                bb.position(0);
                bb.put(realmac);
                return array;
        }
-       
+
+       /**
+        * Returns the empty size of a Slot. Includes 2 HMACs, the machine
+        * identifier, the sequence number, and the number of entries.
+        */
+       int getBaseSize() {
+               return 2*HMAC_SIZE+2*Long.BYTES+Integer.BYTES;
+       }
+
+       /**
+        * Returns the live set of entries for this Slot.  Generates a fake
+        * LastMessage entry to represent the information stored by the slot
+        * itself.
+        */
+
+       Vector<Entry> getLiveEntries(boolean resize) {
+               Vector<Entry> liveEntries=new Vector<Entry>();
+               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;
+       }
+
+       /**
+        * Returns the sequence number of the slot.
+        */
+
        long getSequenceNumber() {
                return seqnum;
        }
 
-       byte[] getBytes() {
-               return null;
+       /**
+        * Returns the machine that sent this slot.
+        */
+
+       long getMachineID() {
+               return machineid;
        }
-       
+
+       /**
+        * Records that a newer slot records the fact that this slot was
+        * sent by the relevant machine.
+        */
+
+       void setDead() {
+               seqnumlive=false;
+               decrementLiveCount();
+       }
+
+       /**
+        * Update the count of live entries.
+        */
+
+       void decrementLiveCount() {
+               livecount--;
+               if (livecount==0)
+                       table.decrementLiveCount();
+       }
+
+       /**
+        * Returns whether the slot stores any live information.
+        */
+
+       boolean isLive() {
+               return livecount > 0;
+       }
+
        public String toString() {
-               return "<"+getSequenceNumber()+", "+new String(getBytes())+">";
+               return "<"+getSequenceNumber()+">";
        }
 }