-
Notifications
You must be signed in to change notification settings - Fork 1
/
MemberInfo.java
69 lines (58 loc) · 1.88 KB
/
MemberInfo.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
/* class containing information that will need to be sent with each message */
import java.io.Serializable;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
//import java.security.MessageDigest;
//import java.security.NoSuchAlgorithmException;
//import java.nio.ByteBuffer;
public class MemberInfo implements Serializable {
public static final int chordIDLength = 4; // m in chord paper
public InetAddress IP;
public int receivePort;
public int sendPort;
public int chordID;
//Info about another machine
public MemberInfo(InetAddress IP, int receivePort){
this.receivePort = receivePort;
this.sendPort = 4000;
this.IP = IP;
this.chordID = generateChordID(this.IP.toString());
}
//Info about myself
public MemberInfo(int receivePort){
this.receivePort = receivePort;
this.sendPort = 4000;
getLocalIP();
this.chordID = generateChordID(this.IP.toString());
}
public boolean equals(MemberInfo other){
return this.IP.toString().equals(other.IP.toString());
}
//Publicly available function to generate Chord ID for any value
public int generateChordID(String value){
return IDGenerator.generateChordID(value, chordIDLength);
}
public static InetAddress parseIP(String address){
try{
return InetAddress.getByName(address);
}
catch(Exception e){
System.out.println("Error parsing IP from string: " + address);
return null;
}
}
//Connect to a public IP lookup service and read what my public IP is
private void getLocalIP(){
try{
URL whatismyip = new URL("http://checkip.amazonaws.com");
BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
this.IP = InetAddress.getByName(in.readLine());
}
catch(Exception e){
System.out.println("Error retrieving local IP");
e.printStackTrace();
}
}
}