more changes
[iotcloud.git] / src / java / iotcloud / Slot.java
index 848edb31f936d05a54a7dcaa6dd171dc85daa8a1..ff0e901081d5850817c36306d7c72807f2995599 100644 (file)
@@ -4,26 +4,30 @@ 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;
+class Slot implements Liveness {
+       static final int SLOT_SIZE=2048;
+       static final int HMAC_SIZE=32;
 
        private long seqnum;
        private byte[] prevhmac;
        private byte[] hmac;
        private long machineid;
        private Vector<Entry> entries;
-
-       Slot(long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac, Vector<Entry> _entries) {
+       private int livecount;
+       private boolean seqnumlive;
+       
+       Slot(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;
        }
-       
-       Slot(long _seqnum, byte[] _bytes) {
-               seqnum=_seqnum;
+
+       Slot(long _seqnum, long _machineid, byte[] _prevhmac) {
+               this(_seqnum, _machineid, _prevhmac, new byte[HMAC_SIZE]);
        }
 
        byte[] getHMAC() {
@@ -34,10 +38,15 @@ class Slot {
                return prevhmac;
        }
 
+       void addEntry(Entry e) {
+               entries.add(e);
+               livecount++;
+       }
+
        Vector<Entry> getEntries() {
                return entries;
        }
-       
+
        static Slot decode(byte[] array, Mac mac) {
                mac.update(array, HMAC_SIZE, array.length-HMAC_SIZE);
                byte[] realmac=mac.doFinal();
@@ -53,18 +62,19 @@ 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(seqnum, machineid, prevhmac, hmac);
+
+               for(int i=0; i<numentries; i++) {
+                       slot.addEntry(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
+               bb.position(HMAC_SIZE);                                                                                                                                                                                                                                                 //Leave space for the HMACs
                bb.put(prevhmac);
                bb.putLong(seqnum);
                bb.putLong(machineid);
@@ -79,7 +89,7 @@ class Slot {
                bb.put(realmac);
                return array;
        }
-       
+
        long getSequenceNumber() {
                return seqnum;
        }
@@ -87,11 +97,24 @@ class Slot {
        long getMachineID() {
                return machineid;
        }
-       
+
        byte[] getBytes() {
                return null;
        }
+
+       void setDead() {
+               decrementLiveCount();
+               seqnumlive=false;
+       }
        
+       void decrementLiveCount() {
+               livecount--;
+       }
+
+       boolean isLive() {
+               return livecount > 0;
+       }
+
        public String toString() {
                return "<"+getSequenceNumber()+", "+new String(getBytes())+">";
        }