X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iotcloud.git;a=blobdiff_plain;f=src%2Fjava%2Fiotcloud%2FSlot.java;h=9de69db52d06dd09f786f15a2fc0e9cfce917c52;hp=8d12fc27cfa3d4c2c24397d9b61b0c810f695d4a;hb=57f0a878de30fb3afc7e84fc085ce149eeb6af41;hpb=021c91a082ea8c5f22b368ddca3926d97101279c diff --git a/src/java/iotcloud/Slot.java b/src/java/iotcloud/Slot.java index 8d12fc2..9de69db 100644 --- a/src/java/iotcloud/Slot.java +++ b/src/java/iotcloud/Slot.java @@ -4,30 +4,65 @@ 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 RESERVED_SPACE=64; + static final int HMAC_SIZE=32; - long seqnum; - byte[] prevhmac; - byte[] hmac; - long machineid; - Vector entries; + private long seqnum; + private byte[] prevhmac; + private byte[] hmac; + private long machineid; + private Vector entries; + private int livecount; + private boolean seqnumlive; + private int freespace; - Slot(Vector _entries) { - entries=_entries; - } - - Slot(long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac, Vector _entries) { + Slot(long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac) { seqnum=_seqnum; machineid=_machineid; prevhmac=_prevhmac; hmac=_hmac; - entries=_entries; + entries=new Vector(); + livecount=1; + seqnumlive=true; + freespace = SLOT_SIZE - getBaseSize(); } - - Slot(long _seqnum, byte[] _bytes) { - seqnum=_seqnum; + + Slot(long _seqnum, long _machineid, byte[] _prevhmac) { + this(_seqnum, _machineid, _prevhmac, null); + } + + Slot(long _seqnum, long _machineid) { + this(_seqnum, _machineid, new byte[HMAC_SIZE], null); + } + + byte[] getHMAC() { + return hmac; + } + + byte[] getPrevHMAC() { + return prevhmac; + } + + 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 getEntries() { + return entries; } static Slot decode(byte[] array, Mac mac) { @@ -40,23 +75,24 @@ class Slot { bb.get(hmac); bb.get(prevhmac); if (!Arrays.equals(realmac, hmac)) - throw new Error("Invalid HMAC! Potential Attack!"); + throw new Error("Server Error: Invalid HMAC! Potential Attack!"); long seqnum=bb.getLong(); long machineid=bb.getLong(); int numentries=bb.getInt(); - Vector entries=new Vector(); - for(int i=0;i getLiveEntries() { + Vector liveEntries=new Vector(); + 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; } - byte[] getBytes() { - return null; + long getMachineID() { + return machineid; + } + + void setDead() { + decrementLiveCount(); + seqnumlive=false; + } + + void decrementLiveCount() { + livecount--; + } + + boolean isLive() { + return livecount > 0; } - + public String toString() { - return "<"+getSequenceNumber()+", "+new String(getBytes())+">"; + return "<"+getSequenceNumber()+">"; } }