-
Notifications
You must be signed in to change notification settings - Fork 1
/
InitializeThread1.java
84 lines (63 loc) · 2.45 KB
/
InitializeThread1.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
/**
* Created by andri on 2/7/2018.
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class InitializeThread1 extends MultiServer1 implements Runnable {
String failure_message = "failed to connect to server";
InitializeThread1(){ }
public void run() {
System.out.println("Initialization Thread 1 starting..");
/* server to server request = "Action,Client1,Amount,Client2, forwarding_value"; */
String response1, response2;
int next_server_to_send = 0;
String request = "i,-1,-1,-1";
response1 = send_request_and_initialize_map(next_server_to_send,request + ",2");
if (response1.equals(failure_message)) {
next_server_to_send = 1;
response2 = send_request_and_initialize_map(next_server_to_send, request + ",0");
if (response2.equals(failure_message)) {
System.out.println("No majority. Request aborted.");
}
}
System.out.println("Initialization Thread 1 exiting..");
}
private String send_request_and_initialize_map(int index, String request){
Socket cSocket;
PrintWriter sockWriter;
Scanner sockReader;
String response ;
String[] response_parts;
Integer key, value;
//send request
try{
cSocket = new Socket("localhost", servers_ports[index]);
sockWriter = new PrintWriter(cSocket.getOutputStream());
sockWriter.println(request);
sockWriter.flush();
//wait for the server to response before running the next request
sockReader = new Scanner(cSocket.getInputStream());
response = sockReader.nextLine();
while (!response.equals("Done")){
response_parts = response.split(",");
key = Integer.parseInt(response_parts[0]);
value = Integer.parseInt(response_parts[1]);
accounts.put(key, value);
account_lock.put(key, new Object());
//inform that you got it
sockWriter.println("ok");
sockWriter.flush();
response = sockReader.nextLine();
}
//inform that you got it
//sockWriter.println("ok");
//sockWriter.flush();
cSocket.close();
return response;
}
catch (Exception e){
return failure_message;
}
}
}