-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccessControl.java
185 lines (156 loc) · 4.49 KB
/
AccessControl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import org.deuce.Atomic;
import java.util.HashMap;
public interface AccessControl {
public void setSendPerm(int address, boolean perm);
public void setAcceptPerm(int address, int addressBegin, int addressEnd, boolean perm);
public boolean getSendPerm(int address);
public boolean getAcceptPerm(int address, int checkAdd);
}
@SuppressWarnings("unchecked")
class SerialAccessControl implements AccessControl {
private final int total;
private Boolean[] PNG;
public RangeSkipList[] R;
public SerialAccessControl(int numAddressesLog) {
total = (1 << numAddressesLog);
PNG = new Boolean[total];
R = new RangeSkipList[total];
for (int i = 0; i < total; i++) {
PNG[i] = true;
R[i] = new RangeSkipList();
}
}
public void setSendPerm(int address, boolean perm) {
PNG[address] = perm;
}
public void setAcceptPerm(int address, int addressBegin, int addressEnd, boolean perm) {
if (!perm)
R[address].put(addressBegin, addressEnd);
else
R[address].remove(addressBegin, addressEnd);
}
public boolean getSendPerm(int address) {
boolean val = PNG[address];
return val;
}
public boolean getAcceptPerm(int address, int checkAdd) {
boolean val = false;
val = !R[address].contains(checkAdd);
return val;
}
public void printSendPerms() {
System.out.println("--- PNG ---");
for (int i = 0; i < total; i++) {
System.out.println("PNG " + i + ": " + PNG[i]);
}
}
}
@SuppressWarnings("unchecked")
class ParallelSTMAccessControl implements AccessControl {
private final int total;
private Boolean[] PNG;
private RangeSkipList[] R;
public ParallelSTMAccessControl(int numAddressesLog) {
total = (1 << numAddressesLog);
PNG = new Boolean[total];
R = new RangeSkipList[total];
for (int i = 0; i < total; i++) {
PNG[i] = true;
R[i] = new RangeSkipList();
}
}
@Atomic
public void setSendPerm(int address, boolean perm) {
PNG[address] = perm;
}
@Atomic
public void setAcceptPerm(int address, int addressBegin, int addressEnd, boolean perm) {
if (!perm)
R[address].put(addressBegin, addressEnd);
else
R[address].remove(addressBegin, addressEnd);
}
@Atomic
public boolean getSendPerm(int address) {
return PNG[address];
}
@Atomic
public boolean getAcceptPerm(int address, int checkAdd) {
return !R[address].contains(checkAdd);
}
public void printSendPerms() {
System.out.println("--- PNG ---");
for (int i = 0; i < total; i++) {
System.out.println("PNG " + i + ": " + PNG[i]);
}
}
}
class ParallelAccessControl implements AccessControl {
private final int total;
private Boolean[] PNG;
private Cache cache;
public RangeSkipList[] R;
private MultiLock multiLock;
public ParallelAccessControl(int numAddressesLog) {
total = (1 << numAddressesLog);
PNG = new Boolean[total];
R = new RangeSkipList[total];
cache = new Cache(numAddressesLog);
for (int i = 0; i < total; i++) {
PNG[i] = true;
R[i] = new RangeSkipList();
}
multiLock = new MultiLock(total);
}
public void setSendPerm(int address, boolean perm) {
multiLock.acquireWrite(address);
PNG[address] = perm;
multiLock.releaseWrite(address);
}
public void setAcceptPermInit(int address, int addressBegin, int addressEnd, boolean perm) {
multiLock.acquireWrite(address);
if (!perm)
R[address].put(addressBegin, addressEnd);
else
R[address].remove(addressBegin, addressEnd);
multiLock.releaseWrite(address);
}
public void setAcceptPerm(int address, int addressBegin, int addressEnd, boolean perm) {
multiLock.acquireWrite(address);
if (!perm)
R[address].put(addressBegin, addressEnd);
else
R[address].remove(addressBegin, addressEnd);
for (int i = addressBegin; i < addressEnd; i++) {
cache.remove(address + "-" + i);
}
multiLock.releaseWrite(address);
}
public boolean getSendPerm(int address) {
multiLock.acquireRead(address);
boolean res = PNG[address];
multiLock.releaseRead(address);
return res;
}
public boolean getAcceptPerm(int address, int checkAdd) {
String key = address + "-" + checkAdd;
multiLock.acquireRead(address);
Boolean val = cache.get(key);
if (val == null) {
val = !R[address].contains(Integer.valueOf(checkAdd));
cache.put(key, val);
}
multiLock.releaseRead(address);
return val;
}
public boolean isAllowed(Header header) {
boolean res = !getSendPerm(header.source) && getAcceptPerm(header.dest, header.source);
return res;
}
public void printSendPerms() {
System.out.println("--- PNG ---");
for (int i = 0; i < total; i++) {
System.out.println("PNG " + i + ": " + PNG[i]);
}
}
}