Changes
[iotcloud.git] / version2 / src / java / iotcloud / Slot.java
index 5828fd325eff8e8663f50f2fe9cedc5ed78cce22..ab0435963101341775976166c57a140363648c1d 100644 (file)
@@ -12,9 +12,9 @@ import java.util.Arrays;
 
 class Slot implements Liveness {
        /** Sets the slot size. */
-       static final int SLOT_SIZE=2048;
+       static final int SLOT_SIZE = 2048;
        /** Sets the size for the HMAC. */
-       static final int HMAC_SIZE=32;
+       static final int HMAC_SIZE = 32;
 
        /** Sequence number of the slot. */
        private long seqnum;
@@ -37,15 +37,15 @@ class Slot implements Liveness {
        private Table table;
 
        Slot(Table _table, long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac) {
-               seqnum=_seqnum;
-               machineid=_machineid;
-               prevhmac=_prevhmac;
-               hmac=_hmac;
-               entries=new Vector<Entry>();
-               livecount=1;
-               seqnumlive=true;
+               seqnum = _seqnum;
+               machineid = _machineid;
+               prevhmac = _prevhmac;
+               hmac = _hmac;
+               entries = new Vector<Entry>();
+               livecount = 1;
+               seqnumlive = true;
                freespace = SLOT_SIZE - getBaseSize();
-               table=_table;
+               table = _table;
        }
 
        Slot(Table _table, long _seqnum, long _machineid, byte[] _prevhmac) {
@@ -64,11 +64,18 @@ class Slot implements Liveness {
                return prevhmac;
        }
 
-       void addEntry(Entry e) {
-               e=e.getCopy(this);
+       Entry addEntry(Entry e) {
+               e = e.getCopy(this);
                entries.add(e);
                livecount++;
                freespace -= e.getSize();
+               return e;
+       }
+
+       void removeEntry(Entry e) {
+               entries.remove(e);
+               livecount--;
+               freespace += e.getSize();
        }
 
        private void addShallowEntry(Entry e) {
@@ -91,23 +98,23 @@ class Slot implements Liveness {
        }
 
        static Slot decode(Table table, byte[] array, Mac mac) {
-               mac.update(array, HMAC_SIZE, array.length-HMAC_SIZE);
-               byte[] realmac=mac.doFinal();
+               mac.update(array, HMAC_SIZE, array.length - HMAC_SIZE);
+               byte[] realmac = mac.doFinal();
 
-               ByteBuffer bb=ByteBuffer.wrap(array);
-               byte[] hmac=new byte[HMAC_SIZE];
-               byte[] prevhmac=new byte[HMAC_SIZE];
+               ByteBuffer bb = ByteBuffer.wrap(array);
+               byte[] hmac = new byte[HMAC_SIZE];
+               byte[] prevhmac = new byte[HMAC_SIZE];
                bb.get(hmac);
                bb.get(prevhmac);
                if (!Arrays.equals(realmac, hmac))
                        throw new Error("Server Error: Invalid HMAC!  Potential Attack!");
 
-               long seqnum=bb.getLong();
-               long machineid=bb.getLong();
-               int numentries=bb.getInt();
-               Slot slot=new Slot(table, seqnum, machineid, prevhmac, hmac);
+               long seqnum = bb.getLong();
+               long machineid = bb.getLong();
+               int numentries = bb.getInt();
+               Slot slot = new Slot(table, seqnum, machineid, prevhmac, hmac);
 
-               for(int i=0; i<numentries; i++) {
+               for (int i = 0; i < numentries; i++) {
                        slot.addShallowEntry(Entry.decode(slot, bb));
                }
 
@@ -115,20 +122,20 @@ class Slot implements Liveness {
        }
 
        byte[] encode(Mac mac) {
-               byte[] array=new byte[SLOT_SIZE];
-               ByteBuffer bb=ByteBuffer.wrap(array);
+               byte[] array = new byte[SLOT_SIZE];
+               ByteBuffer bb = ByteBuffer.wrap(array);
                /* Leave space for the slot HMAC.  */
                bb.position(HMAC_SIZE);
                bb.put(prevhmac);
                bb.putLong(seqnum);
                bb.putLong(machineid);
                bb.putInt(entries.size());
-               for(Entry entry:entries) {
+               for (Entry entry : entries) {
                        entry.encode(bb);
                }
                /* Compute our HMAC */
-               mac.update(array, HMAC_SIZE, array.length-HMAC_SIZE);
-               byte[] realmac=mac.doFinal();
+               mac.update(array, HMAC_SIZE, array.length - HMAC_SIZE);
+               byte[] realmac = mac.doFinal();
                hmac = realmac;
                bb.position(0);
                bb.put(realmac);
@@ -140,7 +147,7 @@ class Slot implements Liveness {
         * identifier, the sequence number, and the number of entries.
         */
        int getBaseSize() {
-               return 2*HMAC_SIZE+2*Long.BYTES+Integer.BYTES;
+               return 2 * HMAC_SIZE + 2 * Long.BYTES + Integer.BYTES;
        }
 
        /**
@@ -150,14 +157,14 @@ class Slot implements Liveness {
         */
 
        Vector<Entry> getLiveEntries(boolean resize) {
-               Vector<Entry> liveEntries=new Vector<Entry>();
-               for(Entry entry: entries) {
+               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));
 
@@ -186,7 +193,7 @@ class Slot implements Liveness {
         */
 
        void setDead() {
-               seqnumlive=false;
+               seqnumlive = false;
                decrementLiveCount();
        }
 
@@ -196,8 +203,9 @@ class Slot implements Liveness {
 
        void decrementLiveCount() {
                livecount--;
-               if (livecount==0)
+               if (livecount == 0) {
                        table.decrementLiveCount();
+               }
        }
 
        /**
@@ -209,6 +217,6 @@ class Slot implements Liveness {
        }
 
        public String toString() {
-               return "<"+getSequenceNumber()+">";
+               return "<" + getSequenceNumber() + ">";
        }
 }