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 / CommitPart.java
1
2
3 package iotcloud;
4
5 import java.nio.ByteBuffer;
6
7 class CommitPart extends Entry{
8
9     // Max size of the part excluding the fixed size header
10     public static final int MAX_NON_HEADER_SIZE = 512;
11
12
13     // Sequence number of the transaction this commit is for, -1 if not a cloud transaction
14     private long machineId = -1; // Machine Id of the device that made the commit
15     private long sequenceNumber = -1; // commit sequence number for this arbitrator
16     private long transactionSequenceNumber = -1;
17     private int partNumber = -1; // Parts position in the
18     private Boolean isLastPart = false;
19     private byte[] data = null;
20
21     private Pair<Long, Integer> partId = null;
22     private Pair<Long, Long> commitId = null;
23
24
25     public CommitPart(Slot s, long _machineId, long _sequenceNumber, long _transactionSequenceNumber, int _partNumber, byte[] _data, Boolean _isLastPart) {
26         super(s);
27         machineId = _machineId;
28         sequenceNumber = _sequenceNumber;
29         transactionSequenceNumber = _transactionSequenceNumber;
30         partNumber = _partNumber;
31         isLastPart = _isLastPart;
32         data = _data;
33
34         partId = new Pair<Long, Integer>(sequenceNumber, partNumber);
35         commitId = new Pair<Long, Long>(machineId, sequenceNumber);
36     }
37
38     public int getSize() {
39         if (data == null) {
40             //return (3 * Long.BYTES) + (2 * Integer.BYTES) + (2 * Byte.BYTES);
41             return (3 * Long.SIZE/8) + (2 * Integer.SIZE/8) + (2 * Byte.SIZE/8);
42         }
43         //return (3 * Long.BYTES) + (2 * Integer.BYTES) + (2 * Byte.BYTES) + data.length;
44         return (3 * Long.SIZE/8) + (2 * Integer.SIZE/8) + (2 * Byte.SIZE/8) + data.length;
45     }
46
47     public void setSlot(Slot s) {
48         parentslot = s;
49     }
50
51     public int getPartNumber() {
52         return partNumber;
53     }
54
55     public int getDataSize() {
56         return data.length;
57     }
58
59     public byte[] getData() {
60         return data;
61     }
62
63     public Pair<Long, Integer> getPartId() {
64         return partId;
65     }
66
67     public Pair<Long, Long> getCommitId() {
68         return commitId;
69     }
70
71     public Boolean isLastPart() {
72         return isLastPart;
73     }
74
75     public long getMachineId() {
76         return machineId;
77     }
78
79     public long getTransactionSequenceNumber() {
80         return transactionSequenceNumber;
81     }
82
83     public long getSequenceNumber() {
84         return sequenceNumber;
85     }
86
87     static Entry decode(Slot s, ByteBuffer bb) {
88         long machineId = bb.getLong();
89         long sequenceNumber = bb.getLong();
90         long transactionSequenceNumber = bb.getLong();
91         int partNumber = bb.getInt();
92         int dataSize = bb.getInt();
93         Boolean isLastPart = bb.get() == 1;
94
95         // Get the data
96         byte[] data = new byte[dataSize];
97         bb.get(data);
98
99         return new CommitPart(s, machineId, sequenceNumber, transactionSequenceNumber, partNumber, data, isLastPart);
100     }
101
102     public void encode(ByteBuffer bb) {
103         bb.put(Entry.TypeCommitPart);
104         bb.putLong(machineId);
105         bb.putLong(sequenceNumber);
106         bb.putLong(transactionSequenceNumber);
107         bb.putInt(partNumber);
108         bb.putInt(data.length);
109
110         if (isLastPart) {
111             bb.put((byte)1);
112         } else {
113             bb.put((byte)0);
114         }
115
116         bb.put(data);
117     }
118
119     public byte getType() {
120         return Entry.TypeCommitPart;
121     }
122
123     public Entry getCopy(Slot s) {
124         return new CommitPart(s, machineId, sequenceNumber, transactionSequenceNumber, partNumber, data, isLastPart);
125     }
126 }