From: Brian Demsky Date: Sat, 23 Jul 2016 21:41:24 +0000 (-0700) Subject: more code X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iotcloud.git;a=commitdiff_plain;h=89e8371e2ca27ce4f581596497b786eb0a209678 more code --- diff --git a/src/java/iotcloud/Entry.java b/src/java/iotcloud/Entry.java index 076b4fc..157a09b 100644 --- a/src/java/iotcloud/Entry.java +++ b/src/java/iotcloud/Entry.java @@ -6,19 +6,24 @@ abstract class Entry { static final byte TypeLastMessage = 2; static final byte TypeRejectedMessage = 3; static final byte TypeTableStatus = 4; - boolean islive = true; + private boolean islive = true; + private Slot parentslot; + + Entry(Slot _parentslot) { + parentslot = _parentslot; + } - static Entry decode(ByteBuffer bb) { + static Entry decode(Slot slot, ByteBuffer bb) { byte type=bb.get(); switch(type) { case TypeKeyValue: - return KeyValue.decode(bb); + return KeyValue.decode(slot, bb); case TypeLastMessage: - return LastMessage.decode(bb); + return LastMessage.decode(slot, bb); case TypeRejectedMessage: - return RejectedMessage.decode(bb); + return RejectedMessage.decode(slot, bb); case TypeTableStatus: - return TableStatus.decode(bb); + return TableStatus.decode(slot, bb); default: throw new Error("Unrecognized Entry Type: "+type); } @@ -30,6 +35,7 @@ abstract class Entry { void setDead() { islive = false; + parentslot.decrementLiveCount(); } abstract void encode(ByteBuffer bb); diff --git a/src/java/iotcloud/KeyValue.java b/src/java/iotcloud/KeyValue.java index 6f620fa..ac19759 100644 --- a/src/java/iotcloud/KeyValue.java +++ b/src/java/iotcloud/KeyValue.java @@ -5,7 +5,8 @@ class KeyValue extends Entry { private IoTString key; private IoTString value; - KeyValue(IoTString _key, IoTString _value) { + KeyValue(Slot slot, IoTString _key, IoTString _value) { + super(slot); key=_key; value=_value; } @@ -18,14 +19,14 @@ class KeyValue extends Entry { return value; } - static Entry decode(ByteBuffer bb) { + static Entry decode(Slot slot, ByteBuffer bb) { int keylength=bb.getInt(); int valuelength=bb.getInt(); byte[] key=new byte[keylength]; byte[] value=new byte[valuelength]; bb.get(key); bb.get(value); - return new KeyValue(IoTString.shallow(key), IoTString.shallow(value)); + return new KeyValue(slot, IoTString.shallow(key), IoTString.shallow(value)); } void encode(ByteBuffer bb) { diff --git a/src/java/iotcloud/LastMessage.java b/src/java/iotcloud/LastMessage.java index 9ea9bcc..d49ce65 100644 --- a/src/java/iotcloud/LastMessage.java +++ b/src/java/iotcloud/LastMessage.java @@ -6,7 +6,8 @@ class LastMessage extends Entry { private long machineid; private long seqnum; - LastMessage(long _machineid, long _seqnum) { + LastMessage(Slot slot, long _machineid, long _seqnum) { + super(slot); machineid=_machineid; seqnum=_seqnum; } @@ -19,10 +20,10 @@ class LastMessage extends Entry { return seqnum; } - static Entry decode(ByteBuffer bb) { + static Entry decode(Slot slot, ByteBuffer bb) { long machineid=bb.getLong(); long seqnum=bb.getLong(); - return new LastMessage(machineid, seqnum); + return new LastMessage(slot, machineid, seqnum); } void encode(ByteBuffer bb) { diff --git a/src/java/iotcloud/RejectedMessage.java b/src/java/iotcloud/RejectedMessage.java index f8af5ec..cd58cdf 100644 --- a/src/java/iotcloud/RejectedMessage.java +++ b/src/java/iotcloud/RejectedMessage.java @@ -7,19 +7,20 @@ class RejectedMessage extends Entry { private long newseqnum; private boolean equalto; - RejectedMessage(long _machineid, long _oldseqnum, long _newseqnum, boolean _equalto) { + RejectedMessage(Slot slot, long _machineid, long _oldseqnum, long _newseqnum, boolean _equalto) { + super(slot); machineid=_machineid; oldseqnum=_oldseqnum; newseqnum=_newseqnum; equalto=_equalto; } - static Entry decode(ByteBuffer bb) { + static Entry decode(Slot slot, ByteBuffer bb) { long machineid=bb.getLong(); long oldseqnum=bb.getLong(); long newseqnum=bb.getLong(); byte equalto=bb.get(); - return new RejectedMessage(machineid, oldseqnum, newseqnum, equalto==1); + return new RejectedMessage(slot, machineid, oldseqnum, newseqnum, equalto==1); } void encode(ByteBuffer bb) { diff --git a/src/java/iotcloud/Slot.java b/src/java/iotcloud/Slot.java index 848edb3..e111f22 100644 --- a/src/java/iotcloud/Slot.java +++ b/src/java/iotcloud/Slot.java @@ -5,21 +5,23 @@ import javax.crypto.Mac; import java.util.Arrays; class Slot { - public static final int SLOT_SIZE=2048; - public static final int HMAC_SIZE=32; + 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 entries; - - Slot(long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac, Vector _entries) { + private int livecount; + + Slot(long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac) { seqnum=_seqnum; machineid=_machineid; prevhmac=_prevhmac; hmac=_hmac; - entries=_entries; + entries=new Vector(); + livecount=1; } Slot(long _seqnum, byte[] _bytes) { @@ -34,6 +36,11 @@ class Slot { return prevhmac; } + void addEntry(Entry e) { + entries.add(e); + livecount++; + } + Vector getEntries() { return entries; } @@ -53,12 +60,13 @@ class Slot { long seqnum=bb.getLong(); long machineid=bb.getLong(); int numentries=bb.getInt(); - Vector entries=new Vector(); + Slot slot=new Slot(seqnum, machineid, prevhmac, hmac); + for(int i=0;i 0; + } + public String toString() { return "<"+getSequenceNumber()+", "+new String(getBytes())+">"; } diff --git a/src/java/iotcloud/TableStatus.java b/src/java/iotcloud/TableStatus.java index 970ce18..05e5896 100644 --- a/src/java/iotcloud/TableStatus.java +++ b/src/java/iotcloud/TableStatus.java @@ -4,13 +4,14 @@ import java.nio.ByteBuffer; class TableStatus extends Entry { int maxslots; - TableStatus(int _maxslots) { + TableStatus(Slot slot, int _maxslots) { + super(slot); maxslots=_maxslots; } - static Entry decode(ByteBuffer bb) { + static Entry decode(Slot slot, ByteBuffer bb) { int maxslots=bb.getInt(); - return new TableStatus(maxslots); + return new TableStatus(slot, maxslots); } void encode(ByteBuffer bb) {