-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFollower.java
More file actions
executable file
·210 lines (114 loc) · 4.28 KB
/
Follower.java
File metadata and controls
executable file
·210 lines (114 loc) · 4.28 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
import java.io.*;
import java.net.Socket;
public class Follower {
Socket socket;
BufferedReader reader;
PrintWriter writer;
String SERVER_ADDRESS;
int SERVER_PORT;
public Follower(String SERVER_ADDRESS, int SERVER_PORT){
this.SERVER_ADDRESS = SERVER_ADDRESS;
this.SERVER_PORT = SERVER_PORT;
}
public void initialize_connection() {
boolean is_init = false;
do {
try {
socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream());
is_init = true;
} catch (IOException e) {
System.out.println("Follower : Creation error.");
}
if (!is_init) {
try {
Thread.sleep(5*1_000);
} catch (InterruptedException e) {
System.out.println("Follower : Suspend error.");
}
}
} while(!is_init);
}
public void send_command(String cmd) {
writer.println(cmd);
writer.flush();
}
public String get_response() {
String response = "";
try {
response = reader.readLine();
} catch (IOException e) {
System.out.println("Follower : Response error.");
}
return response;
}
public void terminate_connection() {
try {
if (socket!= null) {socket.close();}
if (reader != null) {reader.close();}
if (writer != null) {writer.close();}
} catch (IOException e) {
System.out.println("Follower : Termination error.");
}
}
}
class Master_Connecter implements Runnable{
Follower follower;
int timeout_ms;
public Master_Connecter(Follower follower, int timeout) {
this.follower = follower;
this.timeout_ms = timeout * 1_000;
}
@Override
public void run() {
while (true) {
if (Resources.is_changed()) {
// send files only if changes are present
System.out.println("Follower : changes are present.");
String str="";
do {
//client sends ClientSend command, and the details of transaction
String command = "ClientSend";
follower.send_command(command);
follower.send_command(Resources.get_changes_names());
follower.send_command(Resources.get_changes_sizes());
//send changes until success
Resources.send_files(follower.socket, Resources.get_changes_files());
str =follower.get_response();
} while (!str.equals("MasterReceived"));
System.out.println("reach");
System.out.flush();
} else {
System.out.println("Follower : changes are not present.");
}
// receive data for changes in master
boolean success;
do {
//client sends ClientReceive command, and gets the details of transaction
String command = "ClientReceive";
follower.send_command(command);
String filesToReceive = follower.get_response();
String size_filesToReceive = follower.get_response();
//ask for changes until success
success = Resources.receive_files(
follower.socket,
filesToReceive,
size_filesToReceive);
} while (!success);
// end connection
try {
// after exchange, create a metafile
Resources.create_metafile();
} catch (IOException e) {
System.out.println("Follower : could not create metafile");
}
//follower.send_command("ClientDone");
try {
Thread.sleep(timeout_ms);
} catch (InterruptedException e) {
System.out.println("Master_Connector thread interrupted");
}
}
}
}