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;
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) {
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) {
}
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));
}
}
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);
* 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;
}
/**
*/
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));
*/
void setDead() {
- seqnumlive=false;
+ seqnumlive = false;
decrementLiveCount();
}
void decrementLiveCount() {
livecount--;
- if (livecount==0)
+ if (livecount == 0) {
+ // System.out.println("Slot Set Dead"); // TODO: remove
table.decrementLiveCount();
+ }
}
/**
}
public String toString() {
- return "<"+getSequenceNumber()+">";
+ return "<" + getSequenceNumber() + ">";
}
}