X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iotcloud.git;a=blobdiff_plain;f=src%2Fjava%2Fiotcloud%2FSlot.java;h=9de69db52d06dd09f786f15a2fc0e9cfce917c52;hp=848edb31f936d05a54a7dcaa6dd171dc85daa8a1;hb=57f0a878de30fb3afc7e84fc085ce149eeb6af41;hpb=632737b8234665649bd24c647037e4f0425bc0c9 diff --git a/src/java/iotcloud/Slot.java b/src/java/iotcloud/Slot.java index 848edb3..9de69db 100644 --- a/src/java/iotcloud/Slot.java +++ b/src/java/iotcloud/Slot.java @@ -4,26 +4,37 @@ 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; 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(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() { @@ -34,10 +45,26 @@ class Slot { 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) { mac.update(array, HMAC_SIZE, array.length-HMAC_SIZE); byte[] realmac=mac.doFinal(); @@ -53,18 +80,19 @@ class Slot { 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; } @@ -87,12 +132,21 @@ 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())+">"; + return "<"+getSequenceNumber()+">"; } }