Adding Fidelius manual.
[iotcloud.git] / version2 / src / java / iotcloud / Entry.java
1 package iotcloud;
2 import java.nio.ByteBuffer;
3
4 /**
5  * Generic class that wraps all the different types of information
6  * that can be stored in a Slot.
7  * @author Brian Demsky <bdemsky@uci.edu>
8  * @version 1.0
9  */
10
11 abstract class Entry implements Liveness {
12
13         static final byte TypeCommitPart = 1;
14         static final byte TypeAbort = 2;
15         static final byte TypeTransactionPart = 3;
16         static final byte TypeNewKey = 4;
17         static final byte TypeLastMessage = 5;
18         static final byte TypeRejectedMessage = 6;
19         static final byte TypeTableStatus = 7;
20
21
22
23         /* Records whether the information is still live or has been
24                  superceded by a newer update.  */
25
26         private boolean islive = true;
27         protected Slot parentslot;
28
29         public Entry(Slot _parentslot) {
30                 parentslot = _parentslot;
31         }
32
33         /**
34          * Static method for decoding byte array into Entry objects.  First
35          * byte tells the type of entry.
36          */
37         static Entry decode(Slot slot, ByteBuffer bb) {
38                 byte type = bb.get();
39                 switch (type) {
40
41                 case TypeCommitPart:
42                         return CommitPart.decode(slot, bb);
43
44                 case TypeAbort:
45                         return Abort.decode(slot, bb);
46
47                 case TypeTransactionPart:
48                         return TransactionPart.decode(slot, bb);
49
50                 case TypeNewKey:
51                         return NewKey.decode(slot, bb);
52
53                 case TypeLastMessage:
54                         return LastMessage.decode(slot, bb);
55
56                 case TypeRejectedMessage:
57                         return RejectedMessage.decode(slot, bb);
58
59                 case TypeTableStatus:
60                         return TableStatus.decode(slot, bb);
61
62                 default:
63                         throw new Error("Unrecognized Entry Type: " + type);
64                 }
65         }
66
67         /**
68          * Returns true if the Entry object is still live.
69          */
70         public boolean isLive() {
71                 return islive;
72         }
73
74
75         /**
76          * Flags the entry object as dead.  Also decrements the live count
77          * of the parent slot.
78          */
79         public void setDead() {
80
81                 if (!islive ) {
82                         return; // already dead
83                 }
84
85                 islive = false;
86
87                 if (parentslot != null) {
88                         parentslot.decrementLiveCount();
89                 }
90         }
91
92
93         /**
94          * Serializes the Entry object into the byte buffer.
95          */
96         abstract void encode(ByteBuffer bb);
97
98
99         /**
100          * Returns the size in bytes the entry object will take in the byte
101          * array.
102          */
103         abstract int getSize();
104
105
106         /**
107          * Returns a byte encoding the type of the entry object.
108          */
109         abstract byte getType();
110
111
112         /**
113          * Returns a copy of the Entry that can be added to a different slot.
114          */
115         abstract Entry getCopy(Slot s);
116 }