-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelloMessage.java
105 lines (83 loc) · 3.15 KB
/
HelloMessage.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
/* Le Hénaff Pablo ; Basudan Hossam*/
import java.util.Vector;
public class HelloMessage {
private String senderID;
private int sequenceNo;
private int helloInterval;
private int numPeers;
private Vector<String> peer;
public HelloMessage(String s) throws Exception {
s = s.replaceAll("[\\t\\n\\r]+", "");
String [] tmp = s.split(";");
//ERROR HANDLING
if(!tmp[0].equals("HELLO")) throw new NotRightTypeException("Not a HELLO message");
if (tmp.length<5)
throw new Exception("incorrect format of HelloMessage less than 5 elements");
if(tmp[1].length() > 16)
throw new Exception("incorrect format of HelloMessage id plus than 16 char");
if(Integer.parseInt(tmp[3]) < 0 || Integer.parseInt(tmp[3])>255)
throw new Exception("incorrect format of HelloMessage sequence number bounders");
if(Integer.parseInt(tmp[4]) < 0 || Integer.parseInt(tmp[4])>255)
throw new Exception("Number of peers exceeded 255");
if(Integer.parseInt(tmp[4]) != tmp.length - 5)
throw new Exception("Wrong number of peers");
this.senderID = tmp[1];
this.sequenceNo = Integer.parseInt(tmp[2]);
this.helloInterval = Integer.parseInt(tmp[3]);
this.numPeers = Integer.parseInt(tmp[4]);
this.peer = new Vector<String>();
System.out.println(s);
if(tmp.length >= 5) for (int i = 5 ; i < this.numPeers+5 ; i++){
this.peer.add(tmp[i]);
}
}
public HelloMessage (String senderID, int sequenceNo, int HelloInterval) {
if(senderID.length() > 16)
System.err.println("incorrect format of HelloMessage ");
if(HelloInterval < 0 || HelloInterval>255)
System.err.println("incorrect format of HelloMessage ");
this.senderID=senderID;
this.sequenceNo= sequenceNo;
this.helloInterval=HelloInterval;
this.numPeers=0;
this.peer = new Vector<String>();
}
public void addPeer(String peerID){
if (this.numPeers<255){
this.numPeers++;
this.peer.add(peerID);
}
else
System.err.println("Number of peers exceeded 255");
}
public String getHelloMessageAsEncodedString(){
String s= "HELLO;" + this.senderID + ";" + this.sequenceNo + ";" +
this.helloInterval + ";" + this.numPeers;
if(this.numPeers>0){
for (int i= 0 ; i<this.peer.size(); i++){
s+= ";" + this.peer.get(i);
}
}
return s;
}
@Override
public String toString() {
return "HelloMessage{" +
"senderID='" + senderID + '\'' +
", sequenceNo=" + sequenceNo +
", helloInterval=" + helloInterval +
", numPeers=" + numPeers +
", peer=" + peer +
'}';
}
public String getSenderID() {
return senderID;
}
public int getSequenceNo() {
return sequenceNo;
}
public int getHelloInterval() { return helloInterval; }
public Vector<String> getPeer() {
return peer;
}
}