-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultiServerToServerThread2.java
299 lines (246 loc) · 11.9 KB
/
MultiServerToServerThread2.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* Created by andri on 02/07/2018.
*/
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.Map.Entry;
public class MultiServerToServerThread2 extends MultiServer2 implements Runnable {
final private Socket cSocket;
final private String failure_message = "failed to connect to server";
Scanner sockReader;
PrintWriter sockWriter;
MultiServerToServerThread2(Socket cSocket){
this.cSocket = cSocket;
}
public void run() {
String request;
String[] request_parts;
String final_response;
try {
char action;
int cId1, amount, cId2;
int oBalance1;
int forwarding;
sockReader = new Scanner(cSocket.getInputStream());
request = sockReader.nextLine();
//System.out.println("[Thread:" + Thread.currentThread().getId() + "]Server1 to Server3 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]);
forwarding = Integer.parseInt(request_parts[4]);
//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());
}
//send messages and receive response from the third server before proceeding
String response;
final_response = "true";
switch (action) {
case '+':
synchronized (account_lock.get(cId1)) {
oBalance1 = accounts.get(cId1);
//update other servers as well if forwarding = 2
if (forwarding == 2)
forward_to_server_and_get_response(request_parts, forwarding-1);
//already in majority. update yourself
accounts.put(cId1, oBalance1 + amount);
}
break;
case '-':
synchronized (account_lock.get(cId1)) {
oBalance1 = accounts.get(cId1);
if ((oBalance1 - amount) < 0){
final_response = "false";
break;
}
if (forwarding == 1){
accounts.put(cId1, oBalance1 - amount);
break;
}
//update other servers as well if forwarding = 2
response = forward_to_server_and_get_response(request_parts, forwarding-1);
if (response.equals("true"))
accounts.put(cId1, oBalance1 - amount);
}
break;
case '>':
if (!accounts.containsKey(cId2)){
accounts.put(cId2, 0);
account_lock.put(cId2, new Object());
}
if (cId1 > cId2) {
synchronized (account_lock.get(cId1)) {
synchronized (account_lock.get(cId2)) {
oBalance1 = accounts.get(cId1);
if ((oBalance1 - amount) < 0){
final_response = "false";
break;
}
if (forwarding == 1){
accounts.put(cId1, oBalance1 - amount);
accounts.put(cId2, accounts.get(cId2) + amount);
break;
}
//update other servers as well if forwarding = 2
response = forward_to_server_and_get_response(request_parts, forwarding-1);
if (response.equals("true")){
accounts.put(cId1, oBalance1 - amount);
accounts.put(cId2, accounts.get(cId2) + amount);
}
}
}
}
else{
synchronized (account_lock.get(cId2)) {
synchronized (account_lock.get(cId1)) {
oBalance1 = accounts.get(cId1);
if ((oBalance1 - amount) < 0){
final_response = "false";
break;
}
if (forwarding == 1){
accounts.put(cId1, oBalance1 - amount);
accounts.put(cId2, accounts.get(cId2) + amount);
break;
}
//update other servers as well if forwarding = 2
response = forward_to_server_and_get_response(request_parts, forwarding-1);
if (response.equals("true")){
accounts.put(cId1, oBalance1 - amount);
accounts.put(cId2, accounts.get(cId2) + amount);
}
}
}
}
break;
case 'i':
Thread.currentThread().setPriority(10);
//iterator over the hashmap
Iterator<Entry<Integer, Object>> it = account_lock.entrySet().iterator();
recursive_locking_and_update(it, forwarding, request_parts);
break;
}
if (action != 'i'){
sockWriter = new PrintWriter(cSocket.getOutputStream());
sockWriter.println(final_response);
//System.out.println("Server3 response to Server2 request: " + final_response);
sockWriter.flush();
}
System.out.println(accounts.size());
System.out.println(Arrays.asList(accounts));
}
catch(Exception e){
e.printStackTrace();
}
}
private String forward_to_server_and_get_response(String[] request_parts, int forwarding){
Socket cSocketSS;
PrintWriter sockWriterSS;
Scanner sockReaderSS;
try {
cSocketSS = new Socket("localhost", servers_ports[0]);
sockWriterSS = new PrintWriter(cSocketSS.getOutputStream());
sockWriterSS.println(request_parts[0] + "," + request_parts[1] + "," + request_parts[2] + "," + request_parts[3] + ",1");
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);
}
}
private void recursive_locking_and_update(Iterator<Entry<Integer, Object>> it, int forwarding, String[] request_parts){
Thread.currentThread().setPriority(10);
if (it.hasNext()) {
synchronized (account_lock.get(it.next().getKey())){
System.out.println("I am here, in recursion!");
recursive_locking_and_update(it, forwarding, request_parts);
}
}
else{
System.out.println("I have locked everything!");
//you got the locks over all the accounts. do your thing
Socket cSocketSS;
PrintWriter sockWriterSS;
Scanner sockReaderSS;
try{
if (forwarding == 1){
//say that you locked and wait until initialization is completed
sockWriter = new PrintWriter(cSocket.getOutputStream());
sockWriter.println("locked");
sockWriter.flush();
//System.out.println("Waiting for initialization to be completed...");
sockReader = new Scanner(cSocket.getInputStream());
sockReader.nextLine();
//System.out.println("Initialization completed...");
}
if (forwarding == 2){
try {
cSocketSS = new Socket("localhost", servers_ports[0]);
sockWriterSS = new PrintWriter(cSocketSS.getOutputStream());
sockWriterSS.println(request_parts[0] + "," + request_parts[1] + "," + request_parts[2] + "," + request_parts[3] + "," + Integer.toString(forwarding - 1));
sockWriterSS.flush();
sockReaderSS = new Scanner(cSocketSS.getInputStream());
//wait for response... means that the other server locked its map as well
sockReaderSS.nextLine();
//send info to server-client
sockReader = new Scanner(cSocket.getInputStream());
sockWriter = new PrintWriter(cSocket.getOutputStream());
for (Map.Entry<Integer, Integer> entry : accounts.entrySet()) {
sockWriter.println(entry.getKey() + "," + entry.getValue());
sockWriter.flush();
//wait for response, make sure he got it
sockReader.nextLine();
}
//inform that it is all sent
sockWriter.println("Done");
sockWriter.flush();
//inform the successor server so as to stop locking
sockWriterSS.println("Done");
sockWriterSS.flush();
cSocketSS.close();
}
catch (Exception e){
//send info to server-client
sockReader = new Scanner(cSocket.getInputStream());
sockWriter = new PrintWriter(cSocket.getOutputStream());
for (Map.Entry<Integer, Integer> entry : accounts.entrySet()) {
sockWriter.println(entry.getKey() + "," + entry.getValue());
sockWriter.flush();
//wait for response, be sure he got it
sockReader.nextLine();
}
//inform that it is all sent
sockWriter.println("Done");
sockWriter.flush();
}
}
if (forwarding == 0){
for (Map.Entry<Integer, Integer> entry : accounts.entrySet()) {
sockWriter = new PrintWriter(cSocket.getOutputStream());
sockWriter.println(entry.getKey() + "," + entry.getValue());
sockWriter.flush();
//wait for response, be sure he got it
sockReader = new Scanner(cSocket.getInputStream());
sockReader.nextLine();
}
//inform that it is all sent
sockWriter = new PrintWriter(cSocket.getOutputStream());
sockWriter.println("Done");
sockWriter.flush();
}
System.out.println("Now I am exiting recursively.. that's style!");
}
catch(Exception e){
e.printStackTrace();
}
}
}
}