Added Android App
[iotcloud.git] / version2 / src / Control / app / src / main / java / iotcloud / CommitPart.java
diff --git a/version2/src/Control/app/src/main/java/iotcloud/CommitPart.java b/version2/src/Control/app/src/main/java/iotcloud/CommitPart.java
new file mode 100644 (file)
index 0000000..53f164c
--- /dev/null
@@ -0,0 +1,124 @@
+
+
+package iotcloud;
+
+import java.nio.ByteBuffer;
+
+class CommitPart extends Entry{
+
+    // Max size of the part excluding the fixed size header
+    public static final int MAX_NON_HEADER_SIZE = 512;
+
+
+    // Sequence number of the transaction this commit is for, -1 if not a cloud transaction
+    private long machineId = -1; // Machine Id of the device that made the commit
+    private long sequenceNumber = -1; // commit sequence number for this arbitrator
+    private long transactionSequenceNumber = -1;
+    private int partNumber = -1; // Parts position in the
+    private Boolean isLastPart = false;
+    private byte[] data = null;
+
+    private Pair<Long, Integer> partId = null;
+    private Pair<Long, Long> commitId = null;
+
+
+    public CommitPart(Slot s, long _machineId, long _sequenceNumber, long _transactionSequenceNumber, int _partNumber, byte[] _data, Boolean _isLastPart) {
+        super(s);
+        machineId = _machineId;
+        sequenceNumber = _sequenceNumber;
+        transactionSequenceNumber = _transactionSequenceNumber;
+        partNumber = _partNumber;
+        isLastPart = _isLastPart;
+        data = _data;
+
+        partId = new Pair<Long, Integer>(sequenceNumber, partNumber);
+        commitId = new Pair<Long, Long>(machineId, sequenceNumber);
+    }
+
+    public int getSize() {
+        if (data == null) {
+            return (3 * Long.BYTES) + (2 * Integer.BYTES) + (2 * Byte.BYTES);
+        }
+        return (3 * Long.BYTES) + (2 * Integer.BYTES) + (2 * Byte.BYTES) + data.length;
+    }
+
+    public void setSlot(Slot s) {
+        parentslot = s;
+    }
+
+    public int getPartNumber() {
+        return partNumber;
+    }
+
+    public int getDataSize() {
+        return data.length;
+    }
+
+    public byte[] getData() {
+        return data;
+    }
+
+    public Pair<Long, Integer> getPartId() {
+        return partId;
+    }
+
+    public Pair<Long, Long> getCommitId() {
+        return commitId;
+    }
+
+    public Boolean isLastPart() {
+        return isLastPart;
+    }
+
+    public long getMachineId() {
+        return machineId;
+    }
+
+    public long getTransactionSequenceNumber() {
+        return transactionSequenceNumber;
+    }
+
+    public long getSequenceNumber() {
+        return sequenceNumber;
+    }
+
+    static Entry decode(Slot s, ByteBuffer bb) {
+        long machineId = bb.getLong();
+        long sequenceNumber = bb.getLong();
+        long transactionSequenceNumber = bb.getLong();
+        int partNumber = bb.getInt();
+        int dataSize = bb.getInt();
+        Boolean isLastPart = bb.get() == 1;
+
+        // Get the data
+        byte[] data = new byte[dataSize];
+        bb.get(data);
+
+        return new CommitPart(s, machineId, sequenceNumber, transactionSequenceNumber, partNumber, data, isLastPart);
+    }
+
+    public void encode(ByteBuffer bb) {
+        bb.put(Entry.TypeCommitPart);
+        bb.putLong(machineId);
+        bb.putLong(sequenceNumber);
+        bb.putLong(transactionSequenceNumber);
+        bb.putInt(partNumber);
+        bb.putInt(data.length);
+
+        if (isLastPart) {
+            bb.put((byte)1);
+        } else {
+            bb.put((byte)0);
+        }
+
+        bb.put(data);
+    }
+
+    public byte getType() {
+        return Entry.TypeCommitPart;
+    }
+
+    public Entry getCopy(Slot s) {
+        return new CommitPart(s, machineId, sequenceNumber, transactionSequenceNumber, partNumber, data, isLastPart);
+    }
+}
\ No newline at end of file