-
Notifications
You must be signed in to change notification settings - Fork 1
/
Node2.java
225 lines (195 loc) · 5.62 KB
/
Node2.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
package bully.algorithm;
import java.rmi.AccessException;
import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
/**
* @author Ekirapa
*/
public class Node2 implements BullyInterface {
ArrayList<String> nodes = new ArrayList<>();
boolean foundgreater = false;
static boolean electionInProgress = false;
private static String thisNode = "2";
private static String coordinator;
static BullyInterface stub2;
public Node2() {
}
public static void main(String[] args) {
Node2 obj = new Node2();
try {
stub2 = (BullyInterface) UnicastRemoteObject.exportObject(obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind(thisNode, stub2);
System.err.println("Node" + thisNode + " is ready");
stub2.startElection(thisNode);
} catch (RemoteException e) {
System.out.println("Couldnt bind node to registry\n");
e.printStackTrace();
} catch (AlreadyBoundException e) {
System.out.println("Node already bound to registry \n");
e.printStackTrace();
}
Runtime.getRuntime().addShutdownHook(new ShutDown());
repeat();
}
public static void repeat() {
Random rand = new Random();
int randomNum = rand.nextInt((6 - 1) + 1) + 1;
Timer timer = new Timer();
timer.schedule(new TimerCheck(), randomNum * 1000);
}
@Override
public String sayHello() throws RemoteException {
return "This is a server speaking";
}
@Override
public String startElection(String nodeId) throws RemoteException {
electionInProgress = true;
foundgreater = false;
if (nodeId.equals(thisNode)) {
System.out.println("You started the elections");
Registry reg = LocateRegistry.getRegistry();
for (String nodeName : reg.list()) {
if (!nodeName.equals(thisNode) && Integer.parseInt(nodeName) > 2) {
System.out.println(reg.list().length);
BullyInterface stub;
try {
stub = (BullyInterface) reg.lookup(nodeName);
System.out.println("Sending election challenge to " + nodeName);
stub.startElection(nodeId);
foundgreater = true;
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
if (!foundgreater) {
iWon(thisNode);
}
return null;
} else {
System.out.println("Received election request from " + nodeId);
sendOk(thisNode, nodeId);
return null;
}
}
@Override
public String sendOk(String where, String to) throws RemoteException {
if (!thisNode.equals(to)) {
try {
Registry reg = LocateRegistry.getRegistry();
BullyInterface stub = (BullyInterface) reg.lookup(to);
System.out.println("Sending OK to " + to);
stub.sendOk(where, to);
// start election after sending OK
startElection(thisNode);
} catch (NotBoundException e) {
e.printStackTrace();
}
} else {
// receive OK
System.out.println(where + " Replied with Ok..");
}
return null;
}
@Override
public String iWon(String node) throws RemoteException {
coordinator = node;
electionInProgress = false;
if (node.equals(thisNode)) {
// send win
System.out.println("You have won the election.");
System.out.println("Bragging about winning to other nodes.....");
Registry reg = LocateRegistry.getRegistry();
for (String nodeName : reg.list()) {
if (!nodeName.equals(thisNode)) {
BullyInterface stub;
try {
stub = (BullyInterface) reg.lookup(nodeName);
stub.iWon(node);
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println("Node " + node + " is the new Coodinator\n");
} else {
// receive win
System.out.println("Node " + node + " has won the election.");
System.out.println("Node " + node + " is the new Coodinator\n");
}
return null;
}
@Override
public String register(String id) throws RemoteException {
nodes.add(id);
return null;
}
static class TimerCheck extends TimerTask {
@Override
public void run() {
if (!thisNode.equals(coordinator) && !electionInProgress) {
try {
Registry reg = LocateRegistry.getRegistry();
BullyInterface stub;
stub = (BullyInterface) reg.lookup(coordinator);
stub.isalive();
} catch (RemoteException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
coordinatorCrashed();
} catch (NotBoundException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
coordinatorCrashed();
}
}
repeat();
}
}
private static void coordinatorCrashed() {
System.out.println("Coordinator has crushed. Iniating new election");
try {
stub2.startElection(thisNode);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean isalive() throws RemoteException {
// TODO Auto-generated method stub
return true;
}
static class ShutDown extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
System.out.println("Terminating node");
LocateRegistry.getRegistry().unbind(thisNode);
} catch (AccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}