From 9623ac4706e157eb7001dab1792ddb50f6a1b345 Mon Sep 17 00:00:00 2001 From: Brian Demsky Date: Sat, 23 Jul 2016 15:42:40 -0700 Subject: [PATCH] add makefile --- src/java/iotcloud/Entry.java | 4 +- src/java/iotcloud/Liveness.java | 4 ++ src/java/iotcloud/Makefile | 11 ++++ src/java/iotcloud/RejectedMessage.java | 22 ++++++- src/java/iotcloud/Slot.java | 2 +- src/java/iotcloud/Table.java | 79 ++++++++++++++++++++------ 6 files changed, 99 insertions(+), 23 deletions(-) create mode 100644 src/java/iotcloud/Liveness.java create mode 100644 src/java/iotcloud/Makefile diff --git a/src/java/iotcloud/Entry.java b/src/java/iotcloud/Entry.java index 157a09b..881e5ce 100644 --- a/src/java/iotcloud/Entry.java +++ b/src/java/iotcloud/Entry.java @@ -1,7 +1,7 @@ package iotcloud; import java.nio.ByteBuffer; -abstract class Entry { +abstract class Entry implements Liveness { static final byte TypeKeyValue = 1; static final byte TypeLastMessage = 2; static final byte TypeRejectedMessage = 3; @@ -33,7 +33,7 @@ abstract class Entry { return islive; } - void setDead() { + void decrementLiveCount() { islive = false; parentslot.decrementLiveCount(); } diff --git a/src/java/iotcloud/Liveness.java b/src/java/iotcloud/Liveness.java new file mode 100644 index 0000000..1f241a6 --- /dev/null +++ b/src/java/iotcloud/Liveness.java @@ -0,0 +1,4 @@ +package iotcloud; + +interface Liveness { +} diff --git a/src/java/iotcloud/Makefile b/src/java/iotcloud/Makefile new file mode 100644 index 0000000..aab86c5 --- /dev/null +++ b/src/java/iotcloud/Makefile @@ -0,0 +1,11 @@ +all: server + +JAVAC = javac +BIN_DIR = bin + +server: + $(JAVAC) -d $(BIN_DIR) *.java + +clean: + rm -r bin/* + rm *~ diff --git a/src/java/iotcloud/RejectedMessage.java b/src/java/iotcloud/RejectedMessage.java index cd58cdf..44059bf 100644 --- a/src/java/iotcloud/RejectedMessage.java +++ b/src/java/iotcloud/RejectedMessage.java @@ -3,9 +3,9 @@ import java.nio.ByteBuffer; class RejectedMessage extends Entry { private long machineid; - private long oldseqnum; - private long newseqnum; - private boolean equalto; + private long oldseqnum;//Oldest seqnum in range + private long newseqnum;//Newest seqnum in range (inclusive) + private boolean equalto;//Is message sent or not sent by machineid RejectedMessage(Slot slot, long _machineid, long _oldseqnum, long _newseqnum, boolean _equalto) { super(slot); @@ -14,6 +14,22 @@ class RejectedMessage extends Entry { newseqnum=_newseqnum; equalto=_equalto; } + + long getOldSeqNum() { + return oldseqnum; + } + + long getNewSeqNum() { + return newseqnum; + } + + boolean getEqual() { + return equalto; + } + + long getMachineID() { + return machineid; + } static Entry decode(Slot slot, ByteBuffer bb) { long machineid=bb.getLong(); diff --git a/src/java/iotcloud/Slot.java b/src/java/iotcloud/Slot.java index e111f22..0452590 100644 --- a/src/java/iotcloud/Slot.java +++ b/src/java/iotcloud/Slot.java @@ -4,7 +4,7 @@ import java.nio.ByteBuffer; import javax.crypto.Mac; import java.util.Arrays; -class Slot { +class Slot implements Liveness { static final int SLOT_SIZE=2048; static final int HMAC_SIZE=32; diff --git a/src/java/iotcloud/Table.java b/src/java/iotcloud/Table.java index ae4ed16..6cd2b32 100644 --- a/src/java/iotcloud/Table.java +++ b/src/java/iotcloud/Table.java @@ -7,15 +7,15 @@ import javax.crypto.*; public class Table { int numslots; HashMap table=new HashMap(); - HashMap lastmessage=new HashMap(); + HashMap> lastmessagetable=new HashMap>(); SlotBuffer buffer; CloudComm cloud; private Mac hmac; long sequencenumber; - long machineid; + long localmachineid; - public Table(String baseurl, String password, long _machineid) { - machineid=_machineid; + public Table(String baseurl, String password, long _localmachineid) { + localmachineid=_localmachineid; buffer = new SlotBuffer(); sequencenumber = 1; initCloud(baseurl, password); @@ -52,6 +52,18 @@ public class Table { validateandupdate(newslots); } + public IoTString get(IoTString key) { + KeyValue kv=table.get(key); + if (kv != null) + return kv.getValue(); + else + return null; + } + + public IoTString put(IoTString key, IoTString value) { + return null; + } + void validateandupdate(Slot[] newslots) { //The cloud communication layer has checked slot HMACs already //before decoding @@ -70,44 +82,77 @@ public class Table { } - void processEntry(KeyValue entry, SlotIndexer indexer, Slot slot) { + void processEntry(KeyValue entry, SlotIndexer indexer) { IoTString key=entry.getKey(); KeyValue oldvalue=table.get(key); if (oldvalue != null) { - oldvalue.setDead(); + oldvalue.decrementLiveCount(); } table.put(key, entry); } - void processEntry(LastMessage entry, SlotIndexer indexer, Slot slot) { - updateLastMessage(entry.getMachineID(), entry.getSequenceNumber(), null, entry); + void processEntry(LastMessage entry, SlotIndexer indexer) { + updateLastMessage(entry.getMachineID(), entry.getSequenceNumber(), entry); } - void processEntry(RejectedMessage entry, SlotIndexer indexer, Slot slot) { - + void processEntry(RejectedMessage entry, SlotIndexer indexer) { + long oldseqnum=entry.getOldSeqNum(); + long newseqnum=entry.getNewSeqNum(); + boolean isequal=entry.getEqual(); + long machineid=entry.getMachineID(); + for(long seqnum=oldseqnum;seqnum<=newseqnum;seqnum++) { + Slot slot=indexer.getSlot(seqnum); + if (slot != null) { + long slotmachineid=slot.getMachineID(); + if (isequal!=(slotmachineid==machineid)) { + throw new Error("Server Error: Trying to insert rejected message for slot "+seqnum); + } + } + } } - + void processEntry(TableStatus entry, SlotIndexer indexer, Slot slot) { - + } - void updateLastMessage(long machineid, long seqnum, Slot slot, LastMessage entry) { + void updateLastMessage(long machineid, long seqnum, Liveness liveness) { + Pair lastmsgentry = lastmessagetable.put(machineid, new Pair(seqnum, liveness)); + if (lastmsgentry == null) + return; + + long lastmsgseqnum = lastmsgentry.getFirst(); + Liveness lastentry = lastmsgentry.getSecond(); + if (lastentry instanceof LastMessage) { + ((LastMessage)lastentry).decrementLiveCount(); + } else if (lastentry instanceof Slot) { + ((Slot)lastentry).decrementLiveCount(); + } else { + throw new Error("Unrecognized type"); + } + //Check that nothing funny happened + if (machineid == localmachineid) { + if (lastmsgseqnum != seqnum) + throw new Error("Server Error: Mismatch on local machine sequence number"); + } else { + if (lastmsgseqnum > seqnum) + throw new Error("Server Error: Rolback on remote machine sequence number"); + } } void processSlot(SlotIndexer indexer, Slot slot) { - updateLastMessage(slot.getMachineID(), slot.getSequenceNumber(), slot, null); + updateLastMessage(slot.getMachineID(), slot.getSequenceNumber(), slot); for(Entry entry : slot.getEntries()) { switch(entry.getType()) { case Entry.TypeKeyValue: - processEntry((KeyValue)entry, indexer, slot); + processEntry((KeyValue)entry, indexer); break; case Entry.TypeLastMessage: - processEntry((LastMessage)entry, indexer, slot); + processEntry((LastMessage)entry, indexer); break; case Entry.TypeRejectedMessage: - processEntry((RejectedMessage)entry, indexer, slot); + processEntry((RejectedMessage)entry, indexer); break; case Entry.TypeTableStatus: processEntry((TableStatus)entry, indexer, slot); -- 2.34.1