-
Notifications
You must be signed in to change notification settings - Fork 0
/
SynMessage.java
65 lines (57 loc) · 2.39 KB
/
SynMessage.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
/* Le Hénaff Pablo ; Basudan Hossam*/
public class SynMessage {
private String senderID;
private String peerID;
private int sequenceNo;
//SynMessage PARSER
public SynMessage(String s) throws Exception {
s = s.replaceAll("[\\t\\n\\r]+", "");
String [] tmp = s.split(";");
//ERROR HANDLING
if(!tmp[0].equals("SYN"))
throw new NotRightTypeException("SynMessage PARSER : not a SYN message");
if (tmp.length<4)
throw new Exception("SynMessage PARSER : incorrect format of SynMessage less than 4 elements");
if(tmp[1].length() > 16)
throw new Exception("SynMessage PARSER : incorrect format of SynMessage senderID more than 16 char");
if(tmp[2].length() > 16)
throw new Exception("SynMessage PARSER : incorrect format of SynMessage peerID more than 16 char");
if(Integer.parseInt(tmp[3]) < 0 || Integer.parseInt(tmp[3])>255)
throw new Exception("SynMessage PARSER : incorrect format of SynMessage sequence number bounders");
this.senderID = tmp[1];
this.peerID= tmp[2];
this.sequenceNo = Integer.parseInt(tmp[3]);
}
//SynMessage GENERATOR
public SynMessage(String senderID, String peerID, int sequenceNo) throws Exception{
if(senderID.length() > 16)
throw new Exception("SynMessage GENERATOR : incorrect format of SynMessage senderID more than 16 char");
if(peerID.length() > 16)
throw new Exception("SynMessage GENERATOR : incorrect format of SynMessage peerID more than 16 char");
if(sequenceNo < 0 || sequenceNo>255)
throw new Exception("SynMessage GENERATOR : incorrect format of SynMessage sequence number bounders");
this.senderID=senderID;
this.peerID=peerID;
this.sequenceNo= sequenceNo;
}
public String getSynMessageAsEncodedString(){
return "SYN;" + this.senderID + ";" + this.peerID + ";" + this.sequenceNo + ";";
}
public String getSenderID() {
return senderID;
}
public String getPeerId() {
return peerID;
}
public int getSequenceN() {
return sequenceNo;
}
@Override
public String toString() {
return "SynMessage{" +
"senderID='" + senderID + '\'' +
", peerID='" + peerID + '\'' +
", sequenceNo=" + sequenceNo +
'}';
}
}