Phone app (based on Ali's Control for iotcloud benchmark) to control alarm in the...
[iot2.git] / benchmarks / other / PhoneInterface / Control / app / src / main / java / iotcloud / Entry.java
diff --git a/benchmarks/other/PhoneInterface/Control/app/src/main/java/iotcloud/Entry.java b/benchmarks/other/PhoneInterface/Control/app/src/main/java/iotcloud/Entry.java
new file mode 100644 (file)
index 0000000..dd9e75b
--- /dev/null
@@ -0,0 +1,116 @@
+package iotcloud;
+import java.nio.ByteBuffer;
+
+/**
+ * Generic class that wraps all the different types of information
+ * that can be stored in a Slot.
+ * @author Brian Demsky <bdemsky@uci.edu>
+ * @version 1.0
+ */
+
+abstract class Entry implements Liveness {
+
+       static final byte TypeCommitPart = 1;
+       static final byte TypeAbort = 2;
+       static final byte TypeTransactionPart = 3;
+       static final byte TypeNewKey = 4;
+       static final byte TypeLastMessage = 5;
+       static final byte TypeRejectedMessage = 6;
+       static final byte TypeTableStatus = 7;
+
+
+
+       /* Records whether the information is still live or has been
+                superceded by a newer update.  */
+
+       private boolean islive = true;
+       protected Slot parentslot;
+
+       public Entry(Slot _parentslot) {
+               parentslot = _parentslot;
+       }
+
+       /**
+        * Static method for decoding byte array into Entry objects.  First
+        * byte tells the type of entry.
+        */
+       static Entry decode(Slot slot, ByteBuffer bb) {
+               byte type = bb.get();
+               switch (type) {
+
+               case TypeCommitPart:
+                       return CommitPart.decode(slot, bb);
+
+               case TypeAbort:
+                       return Abort.decode(slot, bb);
+
+               case TypeTransactionPart:
+                       return TransactionPart.decode(slot, bb);
+
+               case TypeNewKey:
+                       return NewKey.decode(slot, bb);
+
+               case TypeLastMessage:
+                       return LastMessage.decode(slot, bb);
+
+               case TypeRejectedMessage:
+                       return RejectedMessage.decode(slot, bb);
+
+               case TypeTableStatus:
+                       return TableStatus.decode(slot, bb);
+
+               default:
+                       throw new Error("Unrecognized Entry Type: " + type);
+               }
+       }
+
+       /**
+        * Returns true if the Entry object is still live.
+        */
+       public boolean isLive() {
+               return islive;
+       }
+
+
+       /**
+        * Flags the entry object as dead.  Also decrements the live count
+        * of the parent slot.
+        */
+       public void setDead() {
+
+               if (!islive ) {
+                       return; // already dead
+               }
+
+               islive = false;
+
+               if (parentslot != null) {
+                       parentslot.decrementLiveCount();
+               }
+       }
+
+
+       /**
+        * Serializes the Entry object into the byte buffer.
+        */
+       abstract void encode(ByteBuffer bb);
+
+
+       /**
+        * Returns the size in bytes the entry object will take in the byte
+        * array.
+        */
+       abstract int getSize();
+
+
+       /**
+        * Returns a byte encoding the type of the entry object.
+        */
+       abstract byte getType();
+
+
+       /**
+        * Returns a copy of the Entry that can be added to a different slot.
+        */
+       abstract Entry getCopy(Slot s);
+}