-
Notifications
You must be signed in to change notification settings - Fork 19
/
Stabilize.java
71 lines (54 loc) · 1.66 KB
/
Stabilize.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
import java.net.InetSocketAddress;
/**
* Stabilize thread that periodically asks successor for its predecessor
* and determine if current node should update or delete its successor.
* @author Chuan Xia
*
*/
public class Stabilize extends Thread {
private Node local;
private boolean alive;
public Stabilize(Node _local) {
local = _local;
alive = true;
}
@Override
public void run() {
while (alive) {
InetSocketAddress successor = local.getSuccessor();
if (successor == null || successor.equals(local.getAddress())) {
local.updateFingers(-3, null); //fill
}
successor = local.getSuccessor();
if (successor != null && !successor.equals(local.getAddress())) {
// try to get my successor's predecessor
InetSocketAddress x = Helper.requestAddress(successor, "YOURPRE");
// if bad connection with successor! delete successor
if (x == null) {
local.updateFingers(-1, null);
}
// else if successor's predecessor is not itself
else if (!x.equals(successor)) {
long local_id = Helper.hashSocketAddress(local.getAddress());
long successor_relative_id = Helper.computeRelativeId(Helper.hashSocketAddress(successor), local_id);
long x_relative_id = Helper.computeRelativeId(Helper.hashSocketAddress(x),local_id);
if (x_relative_id>0 && x_relative_id < successor_relative_id) {
local.updateFingers(1,x);
}
}
// successor's predecessor is successor itself, then notify successor
else {
local.notify(successor);
}
}
try {
Thread.sleep(60);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void toDie() {
alive = false;
}
}