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);
}
void setDead() {
islive = false;
+ parentslot.decrementLiveCount();
}
abstract void encode(ByteBuffer bb);
private IoTString key;
private IoTString value;
- KeyValue(IoTString _key, IoTString _value) {
+ KeyValue(Slot slot, IoTString _key, IoTString _value) {
+ super(slot);
key=_key;
value=_value;
}
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) {
private long machineid;
private long seqnum;
- LastMessage(long _machineid, long _seqnum) {
+ LastMessage(Slot slot, long _machineid, long _seqnum) {
+ super(slot);
machineid=_machineid;
seqnum=_seqnum;
}
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) {
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) {
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<Entry> entries;
-
- Slot(long _seqnum, long _machineid, byte[] _prevhmac, byte[] _hmac, Vector<Entry> _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<Entry>();
+ livecount=1;
}
Slot(long _seqnum, byte[] _bytes) {
return prevhmac;
}
+ void addEntry(Entry e) {
+ entries.add(e);
+ livecount++;
+ }
+
Vector<Entry> getEntries() {
return entries;
}
long seqnum=bb.getLong();
long machineid=bb.getLong();
int numentries=bb.getInt();
- Vector<Entry> entries=new Vector<Entry>();
+ Slot slot=new Slot(seqnum, machineid, prevhmac, hmac);
+
for(int i=0;i<numentries;i++) {
- entries.add(Entry.decode(bb));
+ slot.addEntry(Entry.decode(slot, bb));
}
- return new Slot(seqnum, machineid, prevhmac, hmac, entries);
+ return slot;
}
byte[] encode(Mac mac) {
byte[] getBytes() {
return null;
}
+
+ void decrementLiveCount() {
+ livecount--;
+ }
+ boolean isLive() {
+ return livecount > 0;
+ }
+
public String toString() {
return "<"+getSequenceNumber()+", "+new String(getBytes())+">";
}
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) {