Deleted Redundent Files
[iotcloud.git] / src2 / java / iotcloud / RejectedMessage.java
1 package iotcloud;
2 import java.nio.ByteBuffer;
3 import java.util.HashSet;
4
5 /**
6  * Entry for tracking messages that the server rejected.  We have to
7  * make sure that all clients know that this message was rejected to
8  * prevent the server from reusing these messages in an attack.
9  * @author Brian Demsky
10  * @version 1.0
11  */
12
13
14 class RejectedMessage extends Entry {
15         /* Machine identifier */
16         private long machineid;
17         /* Oldest sequence number in range */
18         private long oldseqnum;
19         /* Newest sequence number in range */
20         private long newseqnum;
21         /* Is the machine identifier of the relevant slots equal to (or not
22          * equal to) the specified machine identifier. */
23         private boolean equalto;
24         /* Set of machines that have not received notification. */
25         private HashSet<Long> watchset;
26
27         RejectedMessage(Slot slot, long _machineid, long _oldseqnum, long _newseqnum, boolean _equalto) {
28                 super(slot);
29                 machineid=_machineid;
30                 oldseqnum=_oldseqnum;
31                 newseqnum=_newseqnum;
32                 equalto=_equalto;
33         }
34
35         long getOldSeqNum() {
36                 return oldseqnum;
37         }
38
39         long getNewSeqNum() {
40                 return newseqnum;
41         }
42
43         boolean getEqual() {
44                 return equalto;
45         }
46
47         long getMachineID() {
48                 return machineid;
49         }
50
51         static Entry decode(Slot slot, ByteBuffer bb) {
52                 long machineid=bb.getLong();
53                 long oldseqnum=bb.getLong();
54                 long newseqnum=bb.getLong();
55                 byte equalto=bb.get();
56                 return new RejectedMessage(slot, machineid, oldseqnum, newseqnum, equalto==1);
57         }
58
59         void setWatchSet(HashSet<Long> _watchset) {
60                 watchset=_watchset;
61         }
62
63         void removeWatcher(long machineid) {
64                 if (watchset.remove(machineid))
65                         if (watchset.isEmpty())
66                                 setDead();
67         }
68
69         void encode(ByteBuffer bb) {
70                 bb.put(Entry.TypeRejectedMessage);
71                 bb.putLong(machineid);
72                 bb.putLong(oldseqnum);
73                 bb.putLong(newseqnum);
74                 bb.put(equalto?(byte)1:(byte)0);
75         }
76
77         int getSize() {
78                 return 3*Long.BYTES + 2*Byte.BYTES;
79         }
80
81         byte getType() {
82                 return Entry.TypeRejectedMessage;
83         }
84         
85         Entry getCopy(Slot s) {
86                 return new RejectedMessage(s, machineid, oldseqnum, newseqnum, equalto);
87         }
88 }