-
Notifications
You must be signed in to change notification settings - Fork 15
/
BullyAlgo.java
404 lines (316 loc) · 14.6 KB
/
BullyAlgo.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Bully.java
*
* Version: 1.1
*
*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
/**
* Implementation of the Bully Election Algorithm
* @author Shweta Yakkali
*/
public class BullyAlgo implements Runnable{
static int leader_id=-1;
static int self_id=-1;
static int server_Port = 5511 ;
String operation;
String reqtype;
static int source_id=-1;
static HashMap<Integer,String> processes= new HashMap<Integer,String>();
static boolean received=false;
static long start_time=-1;
static boolean leader_flag=false;
static boolean election_req=false;
static int higher;
static int ok_ctr=0;
static long start_time_ok=-1;
public BullyAlgo(String operation) {
this.operation = operation;
}
public BullyAlgo(String operation, String reqtype) {
this.operation = operation;
this.reqtype=reqtype;
}
/**
* The main() method starts two threads, RECEIVER and HEARTBEAT.
*
*/
public static void main(String args[]) throws UnknownHostException, IOException, InterruptedException{
initialize();
Runnable receiver = new BullyAlgo("receiver");
new Thread(receiver).start();
// Thread.sleep(10);
Runnable heartbeat = new BullyAlgo("heartbeat");
new Thread(heartbeat).start();
while(true){}
}
/**
* The run() method has the required logic for handling the receiver, sender, timer and heartbeat thread.
* The timer thread waits for 7 seconds to receive a response. If it receives an OK but doesn't receive a leader
* then it starts an election process again. The receiver thread accepts the various incoming requests.
*
*/
public void run(){
if(operation.equals("timer")){
System.out.println("Inside timer thread");
try{
Thread.sleep(7000);
//System.out.println("Timer thread Awake");
if(!received){
leader_id=self_id;
election_req=false; //to allow another election req to come in
leader_flag=true;
System.out.println("******I am the selected LEADER ! "+leader_id);
Runnable sender = new BullyAlgo("sender","cood");
new Thread(sender).start();
}
if(received && !leader_flag){
System.out.println("Received OK but LEADER HAS FAILED, leaderflag= "+leader_flag);
election_req=false;
received=false;
System.out.println("election_req= "+election_req+" received = "+received);
Runnable sender = new BullyAlgo("sender","elecreq");
new Thread(sender).start();
}
}
catch(Exception e){
System.out.println("Interrupted in Timer Thread");
}
}
else if(operation.equals("timerok")){
System.out.println("Inside timerOK thread");
while(true){
if((!leader_flag) && System.currentTimeMillis()-start_time_ok>(5000+ (5000*(5-self_id)))){
ok_ctr=0;
System.out.println("Higher Process Sent OK but Failed, so Start a new Election process");
Runnable sender = new BullyAlgo("sender","elecreq");
new Thread(sender).start();
break;
}
}
}
else if(operation.equals("receiver")){
ServerSocket serverSocket = null;
try{
serverSocket = new ServerSocket(server_Port);
while(true){
Socket socket = serverSocket.accept();
//System.out.println("Connection established.....");
DataInputStream in = new DataInputStream(socket.getInputStream());
String option=in.readUTF();
if(option.equals("elecreq")){
source_id=Integer.parseInt(in.readUTF());
System.out.println("received election request from "+source_id);
if(self_id>source_id){
Runnable sender = new BullyAlgo("sender","ok");
new Thread(sender).start();
}
if(!election_req){
Runnable sender = new BullyAlgo("sender","elecreq");
new Thread(sender).start();
start_time=System.currentTimeMillis();
election_req=true;
Runnable timer = new BullyAlgo("timer");
new Thread(timer).start();
System.out.println("Start time here is :"+start_time);
}
}
else if(option.equals("ok")){
received=true;
int sender=Integer.parseInt(in.readUTF());
System.out.println("Received OK from "+processes.get(sender));
}
else if(option.equals("cood")){
leader_id=Integer.parseInt(in.readUTF());
leader_flag=true;
election_req=false;
received=false;
System.out.println("******LEADER selected is :"+leader_id+" : "+processes.get(leader_id)+ " Leader Flag= "+leader_flag);
System.out.println("Election Request value= "+election_req+" Value of received= "+received);
}
else if(option.equals("heartbeat")){
int sender=Integer.parseInt(in.readUTF());
System.out.println("HEARTBEAT received from "+processes.get(sender));
}
socket.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}
else if(operation.equals("heartbeat")){
while(true){
try{
System.out.print("");
if(leader_flag && (self_id!=leader_id)){
Thread.sleep(1500);
String destination_server=processes.get(leader_id);
Socket socket = new Socket(destination_server + ".cs.rit.edu", server_Port);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeUTF("heartbeat");
out.writeUTF(self_id+"");
System.out.println("Sent HEARTBEAT to : "+destination_server);
}
}
catch(Exception e){
leader_flag=false;
System.out.println("Leader has FAILED!");
System.out.println("election_req= "+election_req+" received = "+received);
//send election request
Runnable sender = new BullyAlgo("sender","elecreq");
new Thread(sender).start();
}
}
}
else if(operation.equals("sender")){
if(reqtype.equals("elecreq")){
try{
sendElectionRequest();
}
catch(Exception e){
System.out.println("Process has FAILED, Election Request cannot be processed !");
}
}
else if(reqtype.equals("ok")){
try{
sendOK();
}
catch(Exception e){
e.printStackTrace();
}
}
else if(reqtype.equals("cood")){
try{
sendCoordinatorMsg();
}
catch(Exception e){
System.out.println("Process has FAILED, Won't get the new leader !");
}
}
}
}
/**
* The sendCoordinatorMsg() method broadcasts the leader to all the process. If the process has failed then a message is displayed
* to indicate that the process has .failed.
*
*/
public static void sendCoordinatorMsg() throws IOException{
for (int key : processes.keySet()) {
if(key!=self_id){
String destination_server=processes.get(key);
try{
Socket socket = new Socket(destination_server + ".cs.rit.edu", server_Port);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeUTF("cood");
out.writeUTF(leader_id+"");
System.out.println("Sent Leader ID to : "+destination_server);
}
catch(Exception e){
System.out.println("The process "+destination_server+" has failed, won't get the new leader !");
}
}
}
}
/**
* The sendOK() method sends OK message to the incoming process which has requested an election request.
*
*/
public static void sendOK() throws IOException{
try{
String destination_server=processes.get(source_id);
Socket socket = new Socket(destination_server + ".cs.rit.edu", server_Port);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeUTF("ok");
out.writeUTF(self_id+"");
System.out.println("Sent OK to : "+destination_server);
}
catch(Exception e){
System.out.println("Process "+processes.get(source_id)+" has FAILED. OK Message cannot be sent !");
}
}
/**
* The sendElectionRequest() method sends Election Request to all the higher processes.
*
*/
public static void sendElectionRequest() throws IOException{
System.out.println("Election Initiated..");
int failure=0;
for (int key : processes.keySet()) {
if(key>self_id){
String destination_server=processes.get(key);
try{
Socket socket = new Socket(destination_server + ".cs.rit.edu", server_Port);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeUTF("elecreq");
out.writeUTF(self_id+"");
System.out.println("Sent Election Request to : "+destination_server);
}
catch(Exception e){
System.out.println("The process :"+destination_server+" has FAILED, cannot send Election Request !");
failure++;
}
}
}
if(failure==higher){
if(!election_req){
start_time=System.currentTimeMillis();
System.out.println("Inside if of sendElectionRequest, start_time= "+start_time);
election_req=true;
received=false;
Runnable timer = new BullyAlgo("timer");
new Thread(timer).start();
}
}
}
/**
* The initialize() method makes all the initializations and one of the processes starts the election algorithm.
*
*/
public static void initialize() throws UnknownHostException, IOException{
processes.put(5,"yes");
processes.put(4,"comet");
processes.put(3,"doors");
processes.put(2,"gorgon");
processes.put(1,"queeg");
InetAddress iAddress = InetAddress.getLocalHost();
String server_name = iAddress.getHostName();
if(server_name.equals("yes")){
self_id=5;
System.out.println("Server id here is= "+self_id);
higher=-1;
}
else if(server_name.equals("comet")){
self_id=4;
System.out.println("Server id here is= "+self_id);
higher=1;
}
else if(server_name.equals("doors")){
self_id=3;
System.out.println("Server id here is= "+self_id);
higher=2;
//Runnable sender = new BullyAlgo("sender","elecreq");
//new Thread(sender).start();
}
else if(server_name.equals("gorgon")){
self_id=2;
higher=3;
System.out.println("Server id here is= "+self_id);
//Election Initiator
Runnable sender = new BullyAlgo("sender","elecreq");
new Thread(sender).start();
}
else if(server_name.equals("queeg")){
self_id=1;
higher=4;
System.out.println("Server id here is= "+self_id);
}
}
}