-
Notifications
You must be signed in to change notification settings - Fork 1
/
RowInFingerTable.java
52 lines (50 loc) · 1.44 KB
/
RowInFingerTable.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
// author: Abhishek Agrawal
// dateCreated: 06/03/2018
import java.io.*;
import java.net.*;
import java.util.*;
public class RowInFingerTable {
public int startInterval, endInterval, port, key;
public String IPAddress;
public RowInFingerTable(int startInterval, int endInterval, int port, String IPAddress) throws Exception {
this.startInterval = startInterval;
this.endInterval = endInterval;
this.port = port;
this.IPAddress = IPAddress;
this.key = ObtainSHA.SHA1(IPAddress + ":" + port);
}
public static int clockwiseClosest(int from, int first, int second) {
boolean f = false, s = false;
if(from <= first && first < Math.round(Math.pow(2, Peer.m)))
f = true;
if(from <= second && second < Math.round(Math.pow(2, Peer.m)))
s = true;
if(f && s)
return Math.min(first, second);
else if(!f && !s)
return Math.min(first, second);
else
return Math.max(first, second);
}
public static boolean insideInterval(int key, int closedStart, int openEnd) {
if(closedStart < openEnd && closedStart <= key && key < openEnd)
return true;
else if(closedStart < openEnd)
return false;
else {
int i = closedStart;
while(i < Math.round(Math.pow(2, Peer.m))) {
if(key == i)
return true;
i++;
}
i = 0;
while(i < openEnd) {
if(key == i)
return true;
i++;
}
return false;
}
}
}