-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseIncorrect.java
59 lines (53 loc) · 1.67 KB
/
DatabaseIncorrect.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
import java.util.concurrent.*;
public class DatabaseIncorrect extends Database {
public DatabaseIncorrect(int id, Simulator simulator) {
super(id, simulator);
}
public void run() {
exit = false;
messages = new LinkedBlockingQueue<TransactionMessage>();
try {
while (!exit) {
TransactionMessage msg = messages.take();
switch (msg.type) {
case DB_CHECKUSER:
authenticate(msg.senderID, msg.recordID);
break;
case DB_CHANGEBALANCE:
withdraw(msg.senderID, msg.recordID, msg.value);
break;
case DB_CHECKBALANCE:
checkBalance(msg.senderID, msg.recordID);
break;
case DB_UPDATE_AMOUNT:
updateAmount(msg.recordID, msg.value);
break;
case EXIT:
exit = true;
break;
default:
break;
}
}
} catch (InterruptedException e) {
System.out.println("Caught InterruptedException: " + e);
}
// System.out.println("Database " + id + " ending.");
}
public boolean withdraw(int senderID, int recordID, int amount) throws InterruptedException {
int balance = records.get(recordID);
CloudProcessor cpu = sim.getCloudProcessorByID(senderID);
if (amount > balance) {
sendMessageTo(cpu, new TransactionMessage(id, TransactionMessage.Type.DB_CHANGEBALANCE_FAIL, recordID));
return false;
} else {
//records.put(recordID, balance - amount);
pushMessage(new TransactionMessage(id, TransactionMessage.Type.DB_UPDATE_AMOUNT, recordID, balance-amount));
sendMessageTo(cpu, new TransactionMessage(id, TransactionMessage.Type.DB_CHANGEBALANCE_SUCCESS, recordID));
return true;
}
}
private void updateAmount(int recordID, int amount) {
records.put(recordID, amount);
}
}