-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathATM.java
174 lines (148 loc) · 4.82 KB
/
ATM.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
import java.util.*;
import java.util.concurrent.*;
public class ATM implements Runnable {
public static class Action {
public enum Type { WITHDRAW, CHECKBALANCE }
Type type;
int recordID;
int amount;
Action(Type type, int recordID, int amount) {
this.type = type;
this.recordID = recordID;
this.amount = amount;
}
}
public ATM(int id, Simulator simulator) {
this.id = id;
sim = simulator;
actions = new LinkedList<Action>();
}
public void run() {
exit = false;
messages = new LinkedBlockingQueue<TransactionMessage>();
try {
Iterator<Action> iter = actions.iterator();
while (!exit && iter.hasNext()) {
Action action = iter.next();
curRecordID = action.recordID;
cpu = sim.getCloudProcessor();
if (authenticate()) {
switch (action.type) {
case WITHDRAW:
withdraw(action.amount);
break;
case CHECKBALANCE:
checkBalance();
break;
}
} else {
//TODO: go back to original state
}
}
sim.notifyThreadCompleted(id);
while (!exit) {
// Wait for the stop or exit command
}
} catch (InterruptedException e) {
System.out.println("Caught InterruptedException: " + e);
}
// System.out.println("ATM " + id + " ending.");
}
public boolean authenticate() throws InterruptedException {
System.out.println("ATM " + id + " authenticating for Record " + curRecordID);
TransactionMessage msg;
do
{
sendMessageTo(cpu, new TransactionMessage(id, TransactionMessage.Type.AUTHEN, curRecordID));
msg = messages.poll(TIMEOUT_INTERVAL, TimeUnit.MILLISECONDS);
if (msg == null) {
System.out.println("ATM " + id + " authenticate timeout");
return false;
} else if (msg.type == TransactionMessage.Type.AUTHEN_FAIL) {
System.out.println("ATM " + id + " authenticate failed");
return false;
} else if (msg.type == TransactionMessage.Type.AUTHEN_SUCCESS) {
return true;
}
} while (msg.type == TransactionMessage.Type.SEND_AUTHEN_FAIL);
return false;
}
public boolean withdraw(int amount) throws InterruptedException {
System.out.println("ATM " + id + " attempting to withdraw " + amount + "cents from Record " + curRecordID);
TransactionMessage msg;
do
{
sendMessageTo(cpu, new TransactionMessage(id, TransactionMessage.Type.WITHDRAW_AMOUNT, curRecordID, amount));
msg = messages.poll(TIMEOUT_INTERVAL, TimeUnit.MILLISECONDS);
if (msg == null) {
System.out.println("ATM " + id + " withdraw - Timeout");
return false;
} else if (msg.type == TransactionMessage.Type.WITHDRAW_FAIL) {
System.out.println("ATM " + id + " withdraw - Failed");
return false;
} else if (msg.type == TransactionMessage.Type.WITHDRAW_SUCCESS) {
System.out.println("ATM " + id + " withdraw - Success");
return true;
}
} while (msg.type == TransactionMessage.Type.SEND_AMOUNT_FAIL);
return false;
}
public boolean checkBalance() throws InterruptedException {
System.out.println("ATM " + id + " checking balance for Record " + curRecordID);
TransactionMessage msg;
do
{
sendMessageTo(cpu, new TransactionMessage(id, TransactionMessage.Type.CHECKBALANCE, curRecordID));
msg = messages.poll(TIMEOUT_INTERVAL, TimeUnit.MILLISECONDS);
if (msg == null) {
System.out.println("ATM " + id + " checking balance - Timeout");
return false;
} else if (msg.type == TransactionMessage.Type.GET_CB_RESPOND) {
System.out.println("ATM " + id + ": Record " + curRecordID + " Balance: " + msg.value);
return true;
}
} while (msg.type == TransactionMessage.Type.SEND_CHECKBALANCE_FAIL);
return false;
}
public void stop() {
exit = true;
messages.add(new TransactionMessage(0, TransactionMessage.Type.EXIT, 0)); //HACK: get out of infinite loop
}
public void setActions(List<Action> newActions) {
if (newActions != null)
actions = newActions;
}
synchronized public void pushMessage(TransactionMessage msg) {
messages.add(msg);
}
public void sendMessageTo(CloudProcessor cpu, TransactionMessage msg) {
// Simulate unreliability
if (rand.nextInt(10) < 1) {
// Sending failed, so we tell ourselves it failed
TransactionMessage.Type responseType = TransactionMessage.Type.SEND_AUTHEN_FAIL;
switch (msg.type) {
case AUTHEN:
responseType = TransactionMessage.Type.SEND_AUTHEN_FAIL;
break;
case WITHDRAW_AMOUNT:
responseType = TransactionMessage.Type.SEND_AMOUNT_FAIL;
break;
case CHECKBALANCE:
responseType = TransactionMessage.Type.SEND_CHECKBALANCE_FAIL;
break;
}
pushMessage(new TransactionMessage(id, responseType, curRecordID));
} else {
cpu.pushMessage(msg);
}
}
int id;
Simulator sim;
CloudProcessor cpu;
boolean exit;
BlockingQueue<TransactionMessage> messages;
List<Action> actions;
int curRecordID;
Random rand = new Random();
public static int TIMEOUT_INTERVAL = 5000; //TIMEOUT in milliseconds
}