-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMultiServerToClientThread2.java
More file actions
259 lines (209 loc) · 10.6 KB
/
Copy pathMultiServerToClientThread2.java
File metadata and controls
259 lines (209 loc) · 10.6 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* Created by andri on 2/7/2018.
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class MultiServerToClientThread2 extends MultiServer2 implements Runnable {
final private Socket cSocket;
final private String failure_message = "failed to connect to server";
private String request;
MultiServerToClientThread2(Socket cSocket) {
this.cSocket = cSocket;
}
public void run() {
//System.out.println("[Thread:" + Thread.currentThread().getId() + "]New client connection established");
Scanner sockReader;
PrintWriter sockWriter;
String[] request_parts;
boolean is_minority = false;
int account_just_created = 0;
//default 0. if the successor server is down change it to forward to the one before
int next_server_to_send = 0;
try {
char action;
int cId1, amount, cId2;
int oBalance1;
int nBalance1;
final Random rand = new Random();
Thread.sleep(rand.nextInt(10));
sockReader = new Scanner(cSocket.getInputStream());
request = sockReader.nextLine();
//System.out.println("[Thread:" + Thread.currentThread().getId() + "]Server 1 " + ", Client Request: " + request);
request_parts = request.split(",");
action = request_parts[0].charAt(0);
cId1 = Integer.parseInt(request_parts[1]);
amount = Integer.parseInt(request_parts[2]);
cId2 = Integer.parseInt(request_parts[3]);
String response, response1, response2 = "false";
//dummy initialization
nBalance1 = 0;
switch (action) {
case '+':
//create account if this is the first request ever
if (cId1 >=0 && !accounts.containsKey(cId1)){
accounts.put(cId1, 0);
account_lock.put(cId1, new Object());
account_just_created = cId1;
}
synchronized (account_lock.get(cId1)) {
oBalance1 = accounts.get(cId1);
response1 = send_to_server_and_get_response(next_server_to_send,request + ",2");
if (response1.equals(failure_message)) {
next_server_to_send++;
response2 = send_to_server_and_get_response(next_server_to_send, request + ",1");
if (response2.equals(failure_message)) {
is_minority = true;
System.out.println("No majority. Request aborted.");
break;
}
}
if (response1.equals("true") || response2.equals("true")){
nBalance1 = oBalance1 + amount;
accounts.put(cId1, nBalance1);
}
//System.out.println("[Thread" + Thread.currentThread().getId() + "] Client " + cId1 + ", Balance: " + (nBalance1 - amount) + " -> " + nBalance1);
}
break;
case '-':
if (!accounts.containsKey(cId1)){
System.out.println("transaction aborted. No such account.");
break;
}
synchronized (account_lock.get(cId1)) {
oBalance1 = accounts.get(cId1);
nBalance1 = oBalance1;
if ((oBalance1 - amount) < 0){
System.out.println("Transaction Aborted. Invalid amount");
break;
}
response1 = send_to_server_and_get_response(next_server_to_send,request + ",2");
if (response1.equals(failure_message)) {
next_server_to_send++;
response2 = send_to_server_and_get_response(next_server_to_send, request + ",1");
if (response2.equals(failure_message)) {
is_minority = true;
System.out.println("All other servers are down. No majority. Request aborted.");
break;
}
}
if (response1.equals("true") || response2.equals("true")){
nBalance1 = oBalance1 - amount;
accounts.put(cId1, nBalance1);
}
//System.out.println("[Thread" + Thread.currentThread().getId() + "] Client " + cId1 + ", Balance: " + (nBalance1 - amount) + " -> " + nBalance1);
}
break;
case '>':
if (!accounts.containsKey(cId1)){
System.out.println("transaction aborted. No such account.");
break;
}
if (!accounts.containsKey(cId2)){
accounts.put(cId2, 0);
account_lock.put(cId2, new Object());
account_just_created = cId2;
}
//avoid deadlock
if (cId1 > cId2){
synchronized (account_lock.get(cId1)) {
synchronized (account_lock.get(cId2)) {
oBalance1 = accounts.get(cId1);
//if anything goes wrong, new balance is equal to the old balance
nBalance1 = oBalance1;
if ((oBalance1 - amount) < 0){
System.out.println("Transaction Aborted. Invalid amount");
break;
}
response1 = send_to_server_and_get_response(next_server_to_send,request + ",2");
if (response1.equals(failure_message)) {
next_server_to_send++;
response2 = send_to_server_and_get_response(next_server_to_send, request + ",1");
if (response2.equals(failure_message)) {
is_minority = true;
System.out.println("All other servers are down. No majority. Request aborted.");
break;
}
}
if (response1.equals("true") || response2.equals("true")){
nBalance1 = oBalance1 - amount;
accounts.put(cId1, nBalance1);
accounts.put(cId2, accounts.get(cId2) + amount);
}
}
}
}
else {
synchronized (account_lock.get(cId2)) {
synchronized (account_lock.get(cId1)) {
oBalance1 = accounts.get(cId1);
nBalance1 = oBalance1;
//first check the validity of the transaction yourself
if ((oBalance1 - amount) < 0){
//System.out.println("Transaction Aborted. Invalid amount");
break;
}
response1 = send_to_server_and_get_response(next_server_to_send,request + ",2");
if (response1.equals(failure_message)) {
next_server_to_send++;
response2 = send_to_server_and_get_response(next_server_to_send, request + ",1");
if (response2.equals(failure_message)) {
is_minority = true;
System.out.println("All other servers are down. No majority. Request aborted.");
break;
}
}
if (response1.equals("true") || response2.equals("true")){
nBalance1 = oBalance1 - amount;
accounts.put(cId1, nBalance1);
accounts.put(cId2, accounts.get(cId2) + amount);
}
}
}
}
break;
case '?':
if (!accounts.containsKey(cId1)){
System.out.println("transaction aborted. No such account.");
break;
}
synchronized (account_lock.get(cId1)){
nBalance1 = accounts.get(cId1);
}
}
sockWriter = new PrintWriter(cSocket.getOutputStream());
response = "Your new balance is: " + nBalance1;
sockWriter.println(response);
sockWriter.flush();
if (is_minority && (account_just_created >= 0)){
accounts.remove(account_just_created);
account_lock.remove(account_just_created);
}
System.out.println(accounts.size());
System.out.println(Arrays.asList(accounts));
}
catch(Exception e){
e.printStackTrace();
}
}
private String send_to_server_and_get_response(int index, String request){
Socket cSocketSS;
PrintWriter sockWriterSS;
Scanner sockReaderSS;
try {
//send message and receive response
cSocketSS = new Socket("localhost", servers_ports[index]);
sockWriterSS = new PrintWriter(cSocketSS.getOutputStream());
sockWriterSS.println(request);
sockWriterSS.flush();
sockReaderSS = new Scanner(cSocketSS.getInputStream());
//wait for response... indicates if other servers updated their map
String response = sockReaderSS.nextLine();
cSocketSS.close();
return response;
}
catch (Exception e){
return(failure_message);
}
}
}